Class: TournamentSystem::Driver
- Inherits:
-
Object
- Object
- TournamentSystem::Driver
- Defined in:
- lib/tournament_system/driver.rb
Overview
An interface for external tournament data.
To use any tournament system implemented in this gem, simply subclass this class and implement the interface functions.
The interface is designed to be useable with arbitrary data, meaning that as long as your data is consistent it will work with this gem. Be it Ruby on Rails Models or simply integers.
Certain tournament systems will not make use of certain parts of this interface. You can for example leave out ‘#get_team_score` if you’re not using the Swiss tournament system.
This class caches certain calculations/objects, it is designed to be a one-time use with any one tournament system. Reusing an instance is undefined behaviour.
Direct Known Subclasses
Instance Method Summary collapse
-
#build_match(home_team, away_team) ⇒ nil
Required to implement: Called when a match is created by a tournament system.
-
#count_duplicate_matches(matches) ⇒ Integer
Count the number of times each pair of teams has played already.
-
#create_match(home_team, away_team) ⇒ nil
Create a match.
-
#create_matches(pairs) ⇒ nil
Create a bunch of matches.
-
#get_match_loser(match) ⇒ team?
Get the losing team of a specific match.
-
#get_match_teams(match) ⇒ Array(team, team)
Required to implement: Get the pair of teams playing for a match.
-
#get_match_winner(match) ⇒ team?
Required to implement: Get the winning team of a match.
-
#get_team_matches(team) ⇒ Array<match>
Required to implement: Get the matches a team has participated in.
-
#get_team_score(team) ⇒ Number
Required to implement: Get a specific score for a team.
-
#loss_count_hash ⇒ Hash{team => Number}
Get a hash of the number of losses of each team.
-
#match_bye?(match) ⇒ Boolean
Determine whether a specific match was a bye? By default uses #get_match_teams to determine a bye.
-
#matches ⇒ Array<match>
Required to implement: Get all matches.
-
#matches_hash ⇒ Hash{Set(team, team) => Integer}
Get a hash of unique team pairs and their number of occurences.
-
#non_bye_matches ⇒ Array<match>
Get a list of matches that weren’t byes.
-
#ranked_teams ⇒ Array<team>
Required to implement: Get the teams ranked by their current position in the tournament.
-
#scores_hash ⇒ Hash{team => Number}
Get a hash of the scores of all ranked teams.
-
#seeded_teams ⇒ Array<team>
Required to implement: Get the teams with their initial seedings.
-
#team_matches_hash ⇒ Hash{team => Array<match>}
Get a hash of the matches teams have played in.
Instance Method Details
#build_match(home_team, away_team) ⇒ nil
Required to implement: Called when a match is created by a tournament system.
144 145 146 |
# File 'lib/tournament_system/driver.rb', line 144 def build_match(home_team, away_team) raise 'Not Implemented' end |
#count_duplicate_matches(matches) ⇒ Integer
Count the number of times each pair of teams has played already. Used by tournament systems.
195 196 197 |
# File 'lib/tournament_system/driver.rb', line 195 def count_duplicate_matches(matches) matches.map { |match| matches_hash[Set.new match] }.reduce(0, :+) end |
#create_match(home_team, away_team) ⇒ nil
Create a match. Used by tournament systems.
Specially handles byes, swapping home/away if required.
207 208 209 210 211 212 |
# File 'lib/tournament_system/driver.rb', line 207 def create_match(home_team, away_team) home_team, away_team = away_team, home_team unless home_team raise 'Invalid match' unless home_team build_match(home_team, away_team) end |
#create_matches(pairs) ⇒ nil
Create a bunch of matches. Used by tournament systems.
219 220 221 222 223 |
# File 'lib/tournament_system/driver.rb', line 219 def create_matches(pairs) pairs.each do |home_team, away_team| create_match(home_team, away_team) end end |
#get_match_loser(match) ⇒ team?
Get the losing team of a specific match. By default uses #get_match_winner and #get_match_teams to determine which team lost. Override if you have better access to this information.
155 156 157 158 159 160 |
# File 'lib/tournament_system/driver.rb', line 155 def get_match_loser(match) winner = get_match_winner(match) return nil unless winner get_match_teams(match).reject { |team| team == winner }.first end |
#get_match_teams(match) ⇒ Array(team, team)
Required to implement: Get the pair of teams playing for a match.
113 114 115 |
# File 'lib/tournament_system/driver.rb', line 113 def get_match_teams(match) raise 'Not Implemented' end |
#get_match_winner(match) ⇒ team?
Required to implement: Get the winning team of a match.
105 106 107 |
# File 'lib/tournament_system/driver.rb', line 105 def get_match_winner(match) raise 'Not Implemented' end |
#get_team_matches(team) ⇒ Array<match>
Required to implement: Get the matches a team has participated in.
129 130 131 |
# File 'lib/tournament_system/driver.rb', line 129 def get_team_matches(team) raise 'Not Implemented' end |
#get_team_score(team) ⇒ Number
Required to implement: Get a specific score for a team.
121 122 123 |
# File 'lib/tournament_system/driver.rb', line 121 def get_team_score(team) raise 'Not Implemented' end |
#loss_count_hash ⇒ Hash{team => Number}
Get a hash of the number of losses of each team. Used by tournament systems.
235 236 237 |
# File 'lib/tournament_system/driver.rb', line 235 def loss_count_hash @loss_count_hash ||= matches.each_with_object(Hash.new(0)) { |match, hash| hash[get_match_loser(match)] += 1 } end |
#match_bye?(match) ⇒ Boolean
Determine whether a specific match was a bye? By default uses #get_match_teams to determine a bye. Override if you have better access to this information.
166 167 168 |
# File 'lib/tournament_system/driver.rb', line 166 def match_bye?(match) get_match_teams(match).include?(nil) end |
#matches ⇒ Array<match>
Required to implement: Get all matches.
83 84 85 |
# File 'lib/tournament_system/driver.rb', line 83 def matches raise 'Not Implemented' end |
#matches_hash ⇒ Hash{Set(team, team) => Integer}
Get a hash of unique team pairs and their number of occurences. Used by tournament systems.
180 181 182 |
# File 'lib/tournament_system/driver.rb', line 180 def matches_hash @matches_hash ||= build_matches_hash end |
#non_bye_matches ⇒ Array<match>
Get a list of matches that weren’t byes. Used by tournament systems.
173 174 175 |
# File 'lib/tournament_system/driver.rb', line 173 def non_bye_matches matches.reject { |match| match_bye?(match) } end |
#ranked_teams ⇒ Array<team>
Required to implement: Get the teams ranked by their current position in the tournament.
97 98 99 |
# File 'lib/tournament_system/driver.rb', line 97 def ranked_teams raise 'Not Implemented' end |
#scores_hash ⇒ Hash{team => Number}
Get a hash of the scores of all ranked teams. Used by tournament systems.
228 229 230 |
# File 'lib/tournament_system/driver.rb', line 228 def scores_hash @scores_hash ||= ranked_teams.map { |team| [team, get_team_score(team)] }.to_h end |
#seeded_teams ⇒ Array<team>
Required to implement: Get the teams with their initial seedings.
90 91 92 |
# File 'lib/tournament_system/driver.rb', line 90 def seeded_teams raise 'Not Implemented' end |
#team_matches_hash ⇒ Hash{team => Array<match>}
Get a hash of the matches teams have played in. Used by tournament systems.
187 188 189 |
# File 'lib/tournament_system/driver.rb', line 187 def team_matches_hash @team_matches_hash ||= build_team_matches_hash end |