Module: TournamentSystem::Algorithm::SingleBracket

Extended by:
SingleBracket
Included in:
SingleBracket
Defined in:
lib/tournament_system/algorithm/single_bracket.rb

Overview

This module provides algorithms for dealing with single bracket elimination tournament systems.

Instance Method Summary collapse

Instance Method Details

#guess_round(teams_count, matches_count) ⇒ Integer

Guess the next round (starting at 0) for a single bracket tournament.

Parameters:

  • teams_count (Integer)

    the number of teams

  • matches_count (Integer)

    the number of existing matches

Returns:

  • (Integer)

    next round number

Raises:

  • (ArgumentError)

    when the number of matches does not add up



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tournament_system/algorithm/single_bracket.rb', line 33

def guess_round(teams_count, matches_count)
  rounds = total_rounds(teams_count)
  total_teams = max_teams(rounds)

  # Make sure we don't have too many matches
  raise ArgumentError, 'Too many matches' unless total_teams >= matches_count

  round = rounds - Math.log2(total_teams - matches_count)
  # Make sure we don't have some weird number of matches
  raise ArgumentError, 'Invalid number of matches' unless (round % 1).zero?

  round.to_i
end

#max_teams(rounds) ⇒ Integer

Calculates the maximum number of teams that can play in a single bracket tournament with a given number of rounds.

Parameters:

  • rounds (Integer)

    the number of rounds

Returns:

  • (Integer)

    number of teams that could play



23
24
25
# File 'lib/tournament_system/algorithm/single_bracket.rb', line 23

def max_teams(rounds)
  2**rounds
end

#padd_teams(teams) ⇒ Object

Deprecated.

Please use Util#padd_teams_pow2 instead.



48
49
50
51
52
53
54
55
# File 'lib/tournament_system/algorithm/single_bracket.rb', line 48

def padd_teams(teams)
  message = 'NOTE: padd_teams is now deprecated in favour of Util.padd_teams_even.'\
            'It will be removed in the next major version'\
            "SingleBracket.padd_teams called from #{Gem.location_of_caller.join(':')}"
  warn message unless Gem::Deprecate.skip

  Util.padd_teams_pow2(teams)
end

#seed(teams) ⇒ Array<team>

Seed teams for a single bracket tournament.

Seed in a way that teams earlier in teams always win against later ones, the first team plays the second in the finals, the 3rd and 4th get nocked out in the semi-finals, etc.

Designed to be used with GroupPairing#adjacent.

Parameters:

  • teams (Array<Team>)

Returns:

  • (Array<team>)

Raises:

  • (ArgumentError)

    when the number of teams is not a power of 2



68
69
70
71
72
73
74
75
# File 'lib/tournament_system/algorithm/single_bracket.rb', line 68

def seed(teams)
  raise ArgumentError, 'Need power-of-2 teams' unless (Math.log2(teams.length) % 1).zero?

  teams = teams.map.with_index do |team, index|
    OpenStruct.new(team: team, index: index)
  end
  seed_bracket(teams).map(&:team)
end

#total_rounds(teams_count) ⇒ Integer

Calculates the total number of rounds needed for a single bracket tournament with a certain number of teams.

Parameters:

  • teams_count (Integer)

    the number of teams

Returns:

  • (Integer)

    number of rounds needed for round robin



14
15
16
# File 'lib/tournament_system/algorithm/single_bracket.rb', line 14

def total_rounds(teams_count)
  Math.log2(teams_count).ceil
end