public void CalculateFinalPosition(Competition competition) { var rank = 1; CompetitionRanking prevTeamRanking = null; foreach (var teamRanking in competition.Rankings .OrderByDescending(x => x.Points) .ThenByDescending(x => x.GoalDifference) .ThenByDescending(x => x.GoalsFor)) { teamRanking.Position = rank; if (teamRanking.Equals(prevTeamRanking)) { //find onderling result: only works if 2 teams end the same. Doesn't work for 3 teams with same points var match = competition.Matches.Find(x => x.MatchBetween(new[] { teamRanking.Team.TeamId, prevTeamRanking.Team.TeamId })); if (match.WinningTeam == teamRanking.Team) { //current team wins, so will swap with the other team teamRanking.Position = prevTeamRanking.Position; prevTeamRanking.Position = rank; } else if (match.WinningTeam == null) //a draw! { teamRanking.Position = prevTeamRanking.Position; //same final position } } rank++; prevTeamRanking = teamRanking; } }
public void Equals_WithNullRanking_ReturnsFalse() { var ranking1 = new CompetitionRanking { Played = 3, Points = 4, GoalsFor = 3, GoalsAgainst = 1 }; Assert.False(ranking1.Equals(null)); }
public void Equals_WithDifferentGoalDifference_ReturnsFalse() { var ranking1 = new CompetitionRanking { Played = 3, Points = 4, GoalsFor = 3, GoalsAgainst = 1 }; var ranking2 = new CompetitionRanking { Played = 3, Points = 4, GoalsFor = 3, GoalsAgainst = 2 }; Assert.False(ranking1.Equals(ranking2)); }
public void Equals_WithEqualRankings_ReturnsTrue() { var ranking1 = new CompetitionRanking { Played = 3, Points = 4, GoalsFor = 3, GoalsAgainst = 1 }; var ranking2 = new CompetitionRanking { Played = 3, Points = 4, GoalsFor = 3, GoalsAgainst = 1 }; Assert.True(ranking1.Equals(ranking2)); }