module TournamentSystem::DoubleElimination

Implements the double 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/double_elimination.rb, line 13
def generate(driver, _options = {})
  round = guess_round driver

  teams_padded = Algorithm::Util.padd_teams_pow2 driver.seeded_teams
  teams_seeded = Algorithm::DoubleBracket.seed teams_padded

  teams = if driver.matches.empty?
            teams_seeded
          else
            get_round_teams driver, round, teams_seeded
          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/double_elimination.rb, line 41
def guess_round(driver)
  Algorithm::DoubleBracket.guess_round(driver.seeded_teams.length,
                                       driver.non_bye_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/double_elimination.rb, line 33
def total_rounds(driver)
  Algorithm::DoubleBracket.total_rounds(driver.seeded_teams.length)
end

Private Instance Methods

get_round_teams(driver, round, teams_seeded) click to toggle source
# File lib/tournament_system/double_elimination.rb, line 48
def get_round_teams(driver, round, teams_seeded)
  loss_counts = driver.loss_count_hash

  winners = teams_seeded.select { |team| loss_counts[team].zero? }
  losers  = teams_seeded.select { |team| loss_counts[team] == 1 }

  if Algorithm::DoubleBracket.minor_round?(round)
    winners + losers
  elsif Algorithm::DoubleBracket.major_round?(round)
    losers
  else
    winners
  end
end