Class: Tournament::Driver

Inherits:
Object
  • Object
show all
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

Instance Method Details

#build_match(home_team, away_team) ⇒ nil

Required to implement: Called when a match is created by a tournament system.

Examples:

rails

def build_match(home_team, away_team)
  Match.create!(home_team, away_team)
end

Parameters:

  • home_team (team)

    the home team for the match, never nil

  • away_team (team, nil)

    the away team for the match, may be nil for byes.

Returns:

  • (nil)


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.

Parameters:

  • matches (Array<match>)

Returns:

  • (Integer)

    the number of duplicate matches



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.

Parameters:

  • home_team (team, nil)
  • away_team (team, nil)

Returns:

  • (nil)

Raises:

  • when both teams are nil



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.

Parameters:

  • matches (Array<Array(team, team)>)

    a collection of pairs

Returns:

  • (nil)

See Also:



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.

Returns:

  • (team, nil)

    the lower of the match, if applicable



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.

Parameters:

  • match

    a match, eg. one returned by #matches

Returns:

  • (Array(team, team))

    the pair of teams playing in the 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.

Parameters:

  • match

    a match, eg. one returned by #matches

Returns:

  • (team, nil)

    the winner of the match if applicable



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.

Parameters:

Returns:

  • (Number)

    the score of the team



65
66
67
# File 'lib/tournament/driver.rb', line 65

def get_team_score(team)
  raise 'Not Implemented'
end

#matchesArray<match>

Required to implement: Get all matches.

Returns:

  • (Array<match>)


27
28
29
# File 'lib/tournament/driver.rb', line 27

def matches
  raise 'Not Implemented'
end

#matches_hashHash{Set(team, team) => Integer}

Get a hash of unique team pairs and their number of occurences. Used by tournament systems.

Returns:

  • (Hash{Set(team, team) => Integer})


101
102
103
# File 'lib/tournament/driver.rb', line 101

def matches_hash
  @matches_hash ||= build_matches_hash
end

#ranked_teamsArray<team>

Required to implement: Get the teams ranked by their current position in the tournament.

Returns:

  • (Array<team>)


41
42
43
# File 'lib/tournament/driver.rb', line 41

def ranked_teams
  raise 'Not Implemented'
end

#scores_hashHash{team => Number}

Get a hash of the scores of all ranked teams. Used by tournament systems.

Returns:

  • (Hash{team => Number})

    a mapping from teams to scores



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_teamsArray<team>

Required to implement: Get the teams with their initial seedings.

Returns:

  • (Array<team>)


34
35
36
# File 'lib/tournament/driver.rb', line 34

def seeded_teams
  raise 'Not Implemented'
end