private Standings CalculateStandingForTeams(List <Team> teams) { List <Game> games = this.dataAccess.LoadGamesOfSeason(this.season.ID, this.date, teams.Select(t => t.ID).ToList()); Dictionary <long, StandingsEntry> standingsEntries = new Dictionary <long, StandingsEntry>(); foreach (Game game in games) { ScoreCalculation scoreCalculation = new ScoreCalculation(game); Score score = scoreCalculation.CalculateGuestTeamScore(); StandingsCalculation.AddEntryValuesForTeam(standingsEntries, scoreCalculation.CalculateHomeTeamScore(), game.HomeTeam); StandingsCalculation.AddEntryValuesForTeam(standingsEntries, scoreCalculation.CalculateGuestTeamScore(), game.GuestTeam); } //make sure that there is an entry for each team foreach (Team team in teams) { GetStandingsEntryForTeam(standingsEntries, team); } List <StandingsEntry> sortedEntries = StandingsCalculation.SortStandingsEntries(standingsEntries.Values.ToList()); StandingsCalculation.SetPosition(sortedEntries); return(new Standings(this.date, this.season, sortedEntries)); }
public Standings CalculateStanding() { List <Game> games = this.dataAccess.LoadGamesOfSeason(this.season.ID, this.date, null); Dictionary <long, StandingsEntry> standingsEntries = new Dictionary <long, StandingsEntry>(); foreach (Game game in games) { ScoreCalculation scoreCalculation = new ScoreCalculation(game); Score score = scoreCalculation.CalculateGuestTeamScore(); StandingsCalculation.AddEntryValuesForTeam(standingsEntries, scoreCalculation.CalculateHomeTeamScore(), game.HomeTeam); StandingsCalculation.AddEntryValuesForTeam(standingsEntries, scoreCalculation.CalculateGuestTeamScore(), game.GuestTeam); } List <StandingsEntry> sortedEntries = new List <StandingsEntry>(); foreach (var entryGroup in standingsEntries.Values.GroupBy(se => se.SortString).OrderBy(eg => eg.Key)) { if (entryGroup.Count() == 1) { sortedEntries.Add(entryGroup.First()); } else { Standings groupStandings = this.CalculateStandingForTeams(entryGroup.Select(eg => eg.Team).ToList()); foreach (var groupStandingsEntryGroup in groupStandings.Entries.GroupBy(se => se.Position)) { if (groupStandingsEntryGroup.Count() == 1) { sortedEntries.Add(entryGroup.First(se => se.Team.ID == groupStandingsEntryGroup.First().Team.ID)); } else { List <StandingsEntry> entriesToInsert = new List <StandingsEntry>(); foreach (StandingsEntry entry in groupStandingsEntryGroup) { entriesToInsert.Add(entryGroup.First(se => se.Team.ID == entry.Team.ID)); } List <StandingsEntry> sortedGroupEntries = StandingsCalculation.SortStandingsEntries(entriesToInsert); foreach (StandingsEntry entryToInsert in sortedGroupEntries) { sortedEntries.Add(entryToInsert); } } } } } int position = 1; foreach (StandingsEntry entry in sortedEntries) { entry.Position = position; position++; } return(new Standings(this.date, this.season, sortedEntries)); }