Module: TournamentSystem::Algorithm::DoubleBracket

Extended by:
DoubleBracket
Included in:
DoubleBracket
Defined in:
lib/tournament_system/algorithm/double_bracket.rb

Overview

This module provides algorithms for dealing with double bracket elimination tournaments.

Instance Method Summary collapse

Instance Method Details

#guess_round(teams_count, matches_count) ⇒ Number

Guess the next round number given the number of teams and matches played so far. Due to the complexity of double elimination, this practically runs through the tournament one round at a time, but is still faster as it only handles numbers and not concrete teams.

Parameters:

  • teams_count (Number)
  • matches_count (Number)

Returns:

  • (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
46
47
# File 'lib/tournament_system/algorithm/double_bracket.rb', line 33

def guess_round(teams_count, matches_count)
  counting_state = OpenStruct.new(winners: teams_count, losers: 0)

  round_number = count_iterations do |round|
    round_size = count_round(round, counting_state)

    next false if round_size > matches_count || round_size.zero?

    matches_count -= round_size
  end

  raise ArgumentError, "Invalid number of matches, was off by #{matches_count}" unless matches_count.zero?

  round_number
end

#major_round?(round) ⇒ Boolean

Determines whether a given round is a major round, ie. only the bottom bracket has a round. Use this in combination with #minor_round? to determine the type of round. The first round is neither major nor minor.

Parameters:

  • round (Number)

Returns:

  • (Boolean)


65
66
67
# File 'lib/tournament_system/algorithm/double_bracket.rb', line 65

def major_round?(round)
  round.even? && round.positive?
end

#max_teams(rounds) ⇒ Number

Get the maximum number of teams that can be processed in the given number of rounds.

Parameters:

  • rounds (Number)

Returns:

  • (Number)


21
22
23
# File 'lib/tournament_system/algorithm/double_bracket.rb', line 21

def max_teams(rounds)
  2**(rounds / 2)
end

#minor_round?(round) ⇒ Boolean

Determines whether a given round is a minor round, ie. the top and bottom bracket have a round. Use this in combination with #major_round? to determine the type of round. The first round is neither minor nor major.

Parameters:

  • round (Number)

Returns:

  • (Boolean)


55
56
57
# File 'lib/tournament_system/algorithm/double_bracket.rb', line 55

def minor_round?(round)
  round.odd?
end

#seed(teams) ⇒ Array<team>

Seed the given teams for a double bracket tournament. Identical to SingleBracket#seed.

Parameters:

  • teams (Array<team>)

Returns:

  • (Array<team>)


73
74
75
# File 'lib/tournament_system/algorithm/double_bracket.rb', line 73

def seed(teams)
  SingleBracket.seed(teams)
end

#total_rounds(teams_count) ⇒ Number

Get the number of rounds required for a double bracket tournament.

Parameters:

  • teams_count (Number)

Returns:

  • (Number)


13
14
15
# File 'lib/tournament_system/algorithm/double_bracket.rb', line 13

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