Class: Tournament::Driver
- Inherits:
-
Object
- Object
- Tournament::Driver
- Defined in:
- lib/tournament/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 may result in undefined behaviour.
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(matches) ⇒ 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_score(team) ⇒ Number
Required to implement: Get a specific score for a team.
-
#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.
-
#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.
Instance Method Details
#build_match(home_team, away_team) ⇒ nil
Required to implement: Called when a match is created by a tournament system.
80 81 82 |
# File 'lib/tournament/driver.rb', line 80 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.
109 110 111 |
# File 'lib/tournament/driver.rb', line 109 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.
121 122 123 124 125 126 |
# File 'lib/tournament/driver.rb', line 121 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(matches) ⇒ nil
Create a bunch of matches. Used by tournament systems.
133 134 135 136 137 |
# File 'lib/tournament/driver.rb', line 133 def create_matches(matches) matches.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.
91 92 93 94 95 96 |
# File 'lib/tournament/driver.rb', line 91 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.
57 58 59 |
# File 'lib/tournament/driver.rb', line 57 def get_match_teams(match) raise 'Not Implemented' end |
#get_match_winner(match) ⇒ team?
Required to implement: Get the winning team of a match.
49 50 51 |
# File 'lib/tournament/driver.rb', line 49 def get_match_winner(match) raise 'Not Implemented' end |
#get_team_score(team) ⇒ Number
Required to implement: Get a specific score for a team.
65 66 67 |
# File 'lib/tournament/driver.rb', line 65 def get_team_score(team) raise 'Not Implemented' end |
#matches ⇒ Array<match>
Required to implement: Get all matches.
27 28 29 |
# File 'lib/tournament/driver.rb', line 27 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.
101 102 103 |
# File 'lib/tournament/driver.rb', line 101 def matches_hash @matches_hash ||= build_matches_hash end |
#ranked_teams ⇒ Array<team>
Required to implement: Get the teams ranked by their current position in the tournament.
41 42 43 |
# File 'lib/tournament/driver.rb', line 41 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.
142 143 144 145 |
# File 'lib/tournament/driver.rb', line 142 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.
34 35 36 |
# File 'lib/tournament/driver.rb', line 34 def seeded_teams raise 'Not Implemented' end |