public async Task Update(Head2Head h2h)
 {
     _h2HRepository.Update(h2h);
     await _h2HRepository.SaveChanges();
 }
Exemple #2
0
        public async Task AddMatch(Match match)
        {
            match.MatchResult = GetMatchResult(match);

            Tournament tournament = await _tournamentRepository.Get(match.TournamentID);

            Team team1 = await _teamRepository.Get(match.Team1ID);

            Team team2 = await _teamRepository.Get(match.Team2ID);

            #region Update Team Stats
            var team1Stat = _teamStatRepository.GetOrCreateByTeam(match.Team1ID);
            var team2Stat = _teamStatRepository.GetOrCreateByTeam(match.Team2ID);

            CompleteTeamsStat(match, ref team1Stat, ref team2Stat);

            _teamStatRepository.Update(team1Stat);
            _teamStatRepository.Update(team2Stat);

            if (tournament.TournamentType.Format == TournamentFormat.WorldCup)
            {
                var team1StatWorldCup = _teamStatWorldCupRepository.GetOrCreateByTeam(match.Team1ID);
                var team2StatWorldCup = _teamStatWorldCupRepository.GetOrCreateByTeam(match.Team2ID);

                CompleteTeamsStatWorldCup(match, ref team1StatWorldCup, ref team2StatWorldCup);

                _teamStatWorldCupRepository.Update(team1StatWorldCup);
                _teamStatWorldCupRepository.Update(team2StatWorldCup);
            }
            #endregion

            #region Update Head 2 Head
            var h2hTeam1 = _h2hRepository.GetOrCreateByTeams(match.Team1ID, match.Team2ID);
            var h2hTeam2 = _h2hRepository.GetOrCreateByTeams(match.Team2ID, match.Team1ID);

            CompleteH2H(match, ref h2hTeam1, ref h2hTeam2);

            _h2hRepository.Update(h2hTeam1);
            _h2hRepository.Update(h2hTeam2);

            if (tournament.TournamentType.Format == TournamentFormat.WorldCup)
            {
                var h2hTeam1WorldCup = _h2hWorldCupRepository.GetOrCreateByTeams(match.Team1ID, match.Team2ID);
                var h2hTeam2WorldCup = _h2hWorldCupRepository.GetOrCreateByTeams(match.Team2ID, match.Team1ID);

                CompleteH2HWorldCup(match, ref h2hTeam1WorldCup, ref h2hTeam2WorldCup);

                _h2hWorldCupRepository.Update(h2hTeam1WorldCup);
                _h2hWorldCupRepository.Update(h2hTeam2WorldCup);
            }
            #endregion

            #region Update Rankings
            var regionalWeight = GetRegionalWeight(team1.Confederation.Weight, team2.Confederation.Weight);
            var team1Points    = GetTeamPoints(GetTeamResult(match, 1), regionalWeight, CalculateOposition(team2.ActualRank), tournament.TournamentType.MatchType.Weight);
            var team2Points    = GetTeamPoints(GetTeamResult(match, 2), regionalWeight, CalculateOposition(team1.ActualRank), tournament.TournamentType.MatchType.Weight);

            Domain.Ranking ranking1 = await _rankingRepository.GetActual(match.Team1ID);

            ranking1.Points += team1Points;
            _rankingRepository.Update(ranking1);

            Domain.Ranking ranking2 = await _rankingRepository.GetActual(match.Team2ID);

            ranking2.Points += team2Points;
            _rankingRepository.Update(ranking2);

            team1.TotalPoints += team1Points;
            _teamRepository.Update(team1);

            team2.TotalPoints += team2Points;
            _teamRepository.Update(team2);
            #endregion

            #region Update Goalscorers
            var matchGoalscorers = match.Team1Goalscorers.Union(match.Team2Goalscorers);
            foreach (Goalscorer matchGoalscorer in matchGoalscorers)
            {
                var goalscorer = await GetOrCreateGoalscorer(matchGoalscorer.PlayerID, match.TournamentID);

                goalscorer.PlayerID     = matchGoalscorer.PlayerID;
                goalscorer.TournamentID = match.TournamentID;
                goalscorer.Goals       += matchGoalscorer.Goals;

                if (goalscorer.Id == 0)
                {
                    await _goalscorerRepository.Add(goalscorer);
                }
                else
                {
                    _goalscorerRepository.Update(goalscorer);
                }

                var player = await _playerRepository.Get(matchGoalscorer.PlayerID);

                switch (tournament.TournamentType.Format)
                {
                case TournamentFormat.Qualification:
                    player.QualificationGoals += matchGoalscorer.Goals;
                    break;

                case TournamentFormat.WorldCup:
                    player.WorldCupGoals += matchGoalscorer.Goals;
                    break;

                case TournamentFormat.ConfederationsCup:
                    player.ConfederationsGoals += matchGoalscorer.Goals;
                    break;

                case TournamentFormat.ConfederationTournament:
                    player.ConfederationTournamentGoals += matchGoalscorer.Goals;
                    break;
                }

                _playerRepository.Update(player);
            }
            #endregion

            await _matchRepository.Add(match);

            await _teamRepository.SaveChanges();
        }