public static void ApplyMatch(this TeamTableStanding tableStanding, Match match) { if (match.Team1Score == null || match.Team2Score == null) { throw new FlmModelException( "Can't apply match to team table standing. Match result wasn't provided." ); } if (!(match.Team1Id == tableStanding.TeamId || match.Team2Id == tableStanding.TeamId)) { throw new FlmModelException( "Can't apply match to team table standing. Team is not presented in provided match." ); } var isHomeMatch = match.Team1Id == tableStanding.TeamId; // - Add Points - if (match.IsDraw()) { // - Draw - tableStanding.Points += FootballConstants.PointsForDraw; tableStanding.MatchesDrawn++; } else { var isHomeWon = match.IsHomeTeamWon(); if ((isHomeMatch && isHomeWon) || (!isHomeMatch && !isHomeWon)) { // - Win - tableStanding.Points += FootballConstants.PointsForWin; tableStanding.MatchesWon++; } else { // - Lose - tableStanding.Points += FootballConstants.PointsForLose; tableStanding.MatchesLost++; } } tableStanding.MatchesPlayed++; // - Add Goals For/Against - tableStanding.GoalsFor += isHomeMatch ? match.Team1Score.Value : match.Team2Score.Value; tableStanding.GoalsAgainst += isHomeMatch ? match.Team2Score.Value : match.Team1Score.Value; }
private static int TablePositionComparer(TeamTableStanding team1, TeamTableStanding team2) { // compare by teams points if (team1.Points > team2.Points) { return(1); } if (team1.Points < team2.Points) { return(-1); } // if teams have equal points, compare by goals difference var team1GD = team1.GoalsDifference; var team2GD = team2.GoalsDifference; if (team1GD > team2GD) { return(1); } if (team1GD < team2GD) { return(-1); } // if teams have equal goals difference, compare by what team scores more if (team1.GoalsFor > team2.GoalsFor) { return(1); } if (team1.GoalsFor < team2.GoalsFor) { return(-1); } // TODO: provide additional comparison criteria for the case team1.GoalsFor == team2.GoalsFor return(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); }
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(); }