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
-
#guess_round(teams_count, matches_count) ⇒ Integer
Guess the next round (starting at 0) for a single bracket tournament.
-
#max_teams(rounds) ⇒ Integer
Calculates the maximum number of teams that can play in a single bracket tournament with a given number of rounds.
-
#padd_teams(teams) ⇒ Object
deprecated
Deprecated.
Please use Util#padd_teams_pow2 instead.
-
#seed(teams) ⇒ Array<team>
Seed teams for a single bracket tournament.
-
#total_rounds(teams_count) ⇒ Integer
Calculates the total number of rounds needed for a single bracket tournament with a certain number of teams.
Instance Method Details
#guess_round(teams_count, matches_count) ⇒ Integer
Guess the next round (starting at 0) for a single bracket tournament.
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.
23 24 25 |
# File 'lib/tournament_system/algorithm/single_bracket.rb', line 23 def max_teams(rounds) 2**rounds end |
#padd_teams(teams) ⇒ Object
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) = '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 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.
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.
14 15 16 |
# File 'lib/tournament_system/algorithm/single_bracket.rb', line 14 def total_rounds(teams_count) Math.log2(teams_count).ceil end |