module TournamentSystem::PagePlayoff

Implements the page playoff 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 @option options [Boolean] bronze_match whether to generate a bronze match

on the final round.
# File lib/tournament_system/page_playoff.rb, line 15
def generate(driver, options = {})
  teams = driver.ranked_teams
  raise 'Page Playoffs only works with 4 teams' if teams.length != 4

  round = options[:round] || guess_round(driver)

  case round
  when 0 then semi_finals(driver, teams)
  when 1 then preliminary_finals(driver)
  when 2 then grand_finals(driver, options)
  else
    raise 'Invalid round number'
  end
end
guess_round(driver) click to toggle source

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

@param driver [Driver] @return [Integer]

# File lib/tournament_system/page_playoff.rb, line 47
def guess_round(driver)
  Algorithm::PagePlayoff.guess_round(driver.matches.length)
end
total_rounds(_ = nil) click to toggle source

The total number of rounds in a page playoff tournament

@param _ for keeping the same interface as other tournament systems. @return [Integer]

# File lib/tournament_system/page_playoff.rb, line 37
def total_rounds(_ = nil)
  Algorithm::PagePlayoff::TOTAL_ROUNDS
end

Private Instance Methods

bronze_finals(driver, matches) click to toggle source
# File lib/tournament_system/page_playoff.rb, line 75
def bronze_finals(driver, matches)
  prelim_loser = driver.get_match_loser matches[2]
  bottom_semi_loser = driver.get_match_loser matches[1]

  driver.create_match prelim_loser, bottom_semi_loser
end
grand_finals(driver, options) click to toggle source
# File lib/tournament_system/page_playoff.rb, line 65
def grand_finals(driver, options)
  matches = driver.matches
  top_winner = driver.get_match_winner matches[0]
  bottom_winner = driver.get_match_winner matches[2]

  driver.create_match top_winner, bottom_winner

  bronze_finals(driver, matches) if options[:bronze_match]
end
preliminary_finals(driver) click to toggle source
# File lib/tournament_system/page_playoff.rb, line 57
def preliminary_finals(driver)
  matches = driver.matches
  top_loser = driver.get_match_loser matches[0]
  bottom_winner = driver.get_match_winner matches[1]

  driver.create_match top_loser, bottom_winner
end
semi_finals(driver, teams) click to toggle source
# File lib/tournament_system/page_playoff.rb, line 53
def semi_finals(driver, teams)
  driver.create_matches Algorithm::GroupPairing.adjacent(teams)
end