public override bool Equals(object obj) { RRWinRecord record = obj as RRWinRecord; if (record != null) { return(this == record); } else { return(false); } }
public int CompareTo(object obj) { if (obj == null) { return(1); } RRWinRecord record = obj as RRWinRecord; if (record != null) { int comp = this.WinRecord.CompareTo(record.WinRecord); if (comp != 0) { return(comp); } comp = this.Wins.CompareTo(record.Wins); if (comp != 0) { return(comp); } comp = this.Losses.CompareTo(record.Losses); if (comp != 0) { return(-1 * comp); } if (record.OverallScore != null) { comp = this.OverallScore.CompareTo(record.OverallScore); if (comp != 0) { return(comp); } } return(0); } else { throw new ArgumentException("Object is not an RRWinRecord", "obj"); } }
public IEnumerable <TournamentRanking> GenerateRankings() { if (this.loadedPairings.Count > 0) { throw new InvalidTournamentStateException("The tournament is not in a state that allows ranking for the following reason: There is at least one pairing left to execute."); } Dictionary <TournamentTeam, RRWinRecord> records = new Dictionary <TournamentTeam, RRWinRecord>(); foreach (TournamentTeam team in this.loadedTeams) { records[team] = new RRWinRecord() { Wins = 0, Losses = 0, Draws = 0, OverallScore = null }; } foreach (TournamentRound round in loadedRounds) { foreach (TournamentPairing pairing in round.Pairings) { List <TournamentTeamScore> pair = new List <TournamentTeamScore>(pairing.TeamScores); if (pair.Count <= 1) { continue; } TournamentTeam teamA = pair[0].Team; var scoreA = pair[0].Score; TournamentTeam teamB = pair[1].Team; var scoreB = pair[1].Score; if (scoreA == null || scoreB == null) { //throw new InvalidTournamentStateException("The tournament is not in a state that allows ranking for the following reason: At least one pairing is missing a score."); continue; } records[teamA].OverallScore += scoreA; records[teamB].OverallScore += scoreB; if (scoreA == scoreB) { records[teamA].Draws += 1; records[teamB].Draws += 1; } else if (scoreA > scoreB) { records[teamA].Wins += 1; records[teamB].Losses += 1; } else { records[teamA].Losses += 1; records[teamB].Wins += 1; } } } int r = 1, lastRank = 1; RRWinRecord lastRecord = null; var ranks = from team in records.Keys let teamRecord = records[team] orderby teamRecord descending select new RRRank() { Team = team, Rank = r++, Record = teamRecord }; foreach (var rank in ranks) { if (rank.Record != null && lastRecord == rank.Record) { rank.Rank = lastRank; } lastRecord = rank.Record; lastRank = rank.Rank; string scoreDescription = String.Format("{0:F} ({1}-{2}-{3} with {4} overall).", rank.Record.WinRecord, rank.Record.Wins, rank.Record.Draws, rank.Record.Losses, rank.Record.OverallScore); yield return(new TournamentRanking(rank.Team, rank.Rank, scoreDescription)); } yield break; }
public IEnumerable<TournamentRanking> GenerateRankings() { if (this.loadedPairings.Count > 0) { throw new InvalidTournamentStateException("The tournament is not in a state that allows ranking for the following reason: There is at least one pairing left to execute."); } Dictionary<TournamentTeam, RRWinRecord> records = new Dictionary<TournamentTeam, RRWinRecord>(); foreach (TournamentTeam team in this.loadedTeams) { records[team] = new RRWinRecord() { Wins = 0, Losses = 0, Draws = 0, OverallScore = null }; } foreach (TournamentRound round in loadedRounds) { foreach (TournamentPairing pairing in round.Pairings) { List<TournamentTeamScore> pair = new List<TournamentTeamScore>(pairing.TeamScores); if (pair.Count <= 1) { continue; } TournamentTeam teamA = pair[0].Team; var scoreA = pair[0].Score; TournamentTeam teamB = pair[1].Team; var scoreB = pair[1].Score; if (scoreA == null || scoreB == null) { throw new InvalidTournamentStateException("The tournament is not in a state that allows ranking for the following reason: At least one pairing is missing a score."); //continue; } records[teamA].OverallScore += scoreA; records[teamB].OverallScore += scoreB; if (scoreA == scoreB) { records[teamA].Draws += 1; records[teamB].Draws += 1; } else if (scoreA > scoreB) { records[teamA].Wins += 1; records[teamB].Losses += 1; } else { records[teamA].Losses += 1; records[teamB].Wins += 1; } } } int r = 1, lastRank = 1; RRWinRecord lastRecord = null; var ranks = from team in records.Keys let teamRecord = records[team] orderby teamRecord descending select new RRRank() { Team = team, Rank = r++, Record = teamRecord }; foreach (var rank in ranks) { if (rank.Record != null && lastRecord == rank.Record) { rank.Rank = lastRank; } lastRecord = rank.Record; lastRank = rank.Rank; string scoreDescription = String.Format("{0:F} ({1}-{2}-{3} with {4} overall).", rank.Record.WinRecord, rank.Record.Wins, rank.Record.Draws, rank.Record.Losses, rank.Record.OverallScore); yield return new TournamentRanking(rank.Team, rank.Rank, scoreDescription); } yield break; }