module TournamentSystem::SingleElimination

Implements the single bracket elimination tournament system.

Public Instance Methods

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

Generate matches with the given driver

@param driver [Driver] @return [nil]

# File lib/tournament_system/single_elimination.rb, line 13
def generate(driver, _options = {})
  round = guess_round(driver)

  teams = if driver.matches.empty?
            padded = Algorithm::Util.padd_teams_pow2 driver.seeded_teams
            Algorithm::SingleBracket.seed padded
          else
            last_matches = previous_round_matches driver, round
            get_match_winners driver, last_matches
          end

  driver.create_matches Algorithm::GroupPairing.adjacent(teams)
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/single_elimination.rb, line 40
def guess_round(driver)
  Algorithm::SingleBracket.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 single elimination tournament with the given driver.

@param driver [Driver] @return [Integer]

# File lib/tournament_system/single_elimination.rb, line 32
def total_rounds(driver)
  Algorithm::SingleBracket.total_rounds(driver.seeded_teams.length)
end

Private Instance Methods

get_match_winners(driver, matches) click to toggle source
# File lib/tournament_system/single_elimination.rb, line 47
def get_match_winners(driver, matches)
  matches.map { |match| driver.get_match_winner(match) }
end
previous_round_matches(driver, round) click to toggle source
# File lib/tournament_system/single_elimination.rb, line 51
def previous_round_matches(driver, round)
  rounds_left = total_rounds(driver) - round
  previous_matches_count = 2**rounds_left

  driver.matches.last(previous_matches_count)
end