module TournamentSystem::RoundRobin

Implements the round-robin tournament system.

Public Instance Methods

generate(driver, options = {}) click to toggle source

Generate matches with the given driver.

@param driver [Driver] @option options [Integer] round the round to generate @return [nil]

# File lib/tournament_system/round_robin.rb, line 14
def generate(driver, options = {})
  round = options[:round] || guess_round(driver)

  teams = Algorithm::Util.padd_teams_even(driver.seeded_teams)

  matches = Algorithm::RoundRobin.round_robin_pairing(teams, round)

  create_matches driver, matches, round
end
guess_round(driver) click to toggle source

Guess the next round number (starting at 0) from the state in driver.

@param driver [Driver] @return [Integer]

# File lib/tournament_system/round_robin.rb, line 37
def guess_round(driver)
  Algorithm::RoundRobin.guess_round(driver.seeded_teams.length,
                                    driver.matches.length)
end
total_rounds(driver) click to toggle source

The total number of rounds needed for a round robin tournament with the given driver.

@param driver [Driver] @return [Integer]

# File lib/tournament_system/round_robin.rb, line 29
def total_rounds(driver)
  Algorithm::RoundRobin.total_rounds(driver.seeded_teams.length)
end

Private Instance Methods

create_matches(driver, matches, round) click to toggle source
# File lib/tournament_system/round_robin.rb, line 44
def create_matches(driver, matches, round)
  matches.each do |match|
    # Alternate home/away
    match = match.reverse if round.odd? && match[0]

    driver.create_match(*match)
  end
end