protected IEnumerable<int> PersistGenericPredictions(IEnumerable<GenericPrediction> predictions)
    {
      var matchIDs = new List<int>();

      foreach (var prediction in predictions)
      {
        var teamA = this.fixtureRepository.GetTeamOrPlayerFromNameAndMaybeFirstName(prediction.TeamOrPlayerA, prediction.PlayerAFirstName);
        var teamB = this.fixtureRepository.GetTeamOrPlayerFromNameAndMaybeFirstName(prediction.TeamOrPlayerB, prediction.PlayerBFirstName);

        var match = this.fixtureRepository.GetMatchFromTeamSelections(teamA, teamB, prediction.MatchDate);
        if (match == null)
        {
          var tournamentName = Reg.Regex.Replace(prediction.TournamentName, @" 20\d{2}", "");

          var tournamentEvent = this.fixtureRepository.GetTournamentEventFromTournamentEventNameAndDate(prediction.MatchDate, tournamentName);
          match = this.fixtureRepository.CreateMatch(teamA, teamB, prediction.MatchDate, tournamentEvent);
        }
        matchIDs.Add(match.Id);

        var persistedOutcomes = this.predictionRepository.GetMatchOutcomeProbabilities(match.Id).ToList();

        foreach (var outcome in prediction.OutcomeProbabilities)
        {
          var persistedOutcome = persistedOutcomes.FirstOrDefault(p => p.MatchOutcomeID == (int)outcome.Key); 

          if (persistedOutcome == null)
          {
            var newOutcomeProb = new MatchOutcomeProbabilitiesInMatch
            {
              MatchID = match.Id,
              MatchOutcome = this.fixtureRepository.GetMatchOutcomeByID((int)outcome.Key),
              MatchOutcomeProbability = (decimal)outcome.Value
            };
            this.predictionRepository.AddMatchOutcomeProbabilitiesInMatch(newOutcomeProb);
          }
          else
          {
            persistedOutcome.MatchOutcomeProbability = (decimal)outcome.Value;
          }
        }

        var persistedScoreLines = this.predictionRepository.GetScoreOutcomeProbabilities(match.Id).ToList();

        foreach (var scoreLine in prediction.ScoreLineProbabilities)
        {
          var persistedScoreLine = persistedScoreLines.FirstOrDefault(s => string.Format("{0}-{1}", s.ScoreOutcome.TeamAScore, s.ScoreOutcome.TeamBScore) == scoreLine.Key);

          if (persistedScoreLine == null)
          {
            if (scoreLine.Value.HasValue)
            {
              var newScoreOutcomeProbabilty = new ScoreOutcomeProbabilitiesInMatch
              {
                MatchID = match.Id,
                ScoreOutcome = this.fixtureRepository.GetScoreOutcome(int.Parse(scoreLine.Key.Split('-')[0]), int.Parse(scoreLine.Key.Split('-')[1])),
                ScoreOutcomeProbability = (decimal)(scoreLine.Value ?? 0.0)
              };
              this.predictionRepository.AddScoreOutcomeProbabilities(newScoreOutcomeProbabilty);
            }
          }
          else
          {
            persistedScoreLine.ScoreOutcomeProbability = (decimal)(scoreLine.Value ?? 0.0); //should be nullable
          }
        }
      }
      this.fixtureRepository.SaveChanges();
      return matchIDs;
    }
Пример #2
0
 public void AddScoreOutcomeProbabilities(ScoreOutcomeProbabilitiesInMatch entity)
 {
   Add<ScoreOutcomeProbabilitiesInMatch>(entity);
 }