Exemplo n.º 1
0
        public async Task RecalculateLeagueStandingsAsync(int leagueId)
        {
            var league = await GetAndValidateItem(leagueId);

            var teams = LeagueRepository.GetTeamLeagueAssignments()
                        .Where(tla => tla.LeagueId == leagueId)
                        .Select(tla => tla.TeamId);

            var currentStandings = new List <TeamTableStanding>();

            foreach (var teamId in teams)
            {
                var playedMatches = MatchRepository.GetItems()
                                    .Where(m => m.LeagueId == league.Id &&
                                           (m.Team1Id == teamId || m.Team2Id == teamId) &&
                                           m.Date < DateTime.Now);

                var tableStanding = new TeamTableStanding()
                {
                    LeagueId = leagueId,
                    TeamId   = teamId
                };

                foreach (var match in playedMatches)
                {
                    tableStanding.ApplyMatch(match);
                }

                currentStandings.Add(tableStanding);
            }

            LeagueTableSorter.CalculateTeamPositions(currentStandings);

            var oldStandings = LeagueRepository.GetStandings()
                               .Where(tts => tts.LeagueId == leagueId);

            await LeagueRepository.RemoveTeamStandingsAsync(oldStandings);

            await LeagueRepository.AddTeamStandingsAsync(currentStandings);
        }
Exemplo n.º 2
0
        private void RecalculateLeagueTables()
        {
            var leagues = _context.Leagues.ToList();

            foreach (var league in leagues)
            {
                var teams = _context.TeamLeagueAssignments
                            .Where(tla => tla.LeagueId == league.Id).Select(tla => tla.TeamId);

                var tableStandings = new List <TeamTableStanding>();

                foreach (var teamId in teams)
                {
                    var playedMatches = _context.Matches
                                        .Where(m => m.LeagueId == league.Id &&
                                               (m.Team1Id == teamId || m.Team2Id == teamId) &&
                                               m.Date < DateTime.Now);

                    var tableStanding = new TeamTableStanding()
                    {
                        LeagueId = league.Id,
                        TeamId   = teamId
                    };

                    foreach (var match in playedMatches)
                    {
                        tableStanding.ApplyMatch(match);
                    }

                    tableStandings.Add(tableStanding);
                }

                LeagueTableSorter.CalculateTeamPositions(tableStandings);
                _context.TableStandings.AddRange(tableStandings);
            }

            _context.SaveChanges();
        }