From 13bb8067fbbc31b0a627cd208e391572d9e9b657 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Mon, 23 Dec 2024 22:12:44 -0500 Subject: [PATCH] Added logic for 32 man bracket --- app/models/tournament.rb | 2 +- ...double_elimination_generate_loser_names.rb | 63 ++--- .../double_elimination_match_generation.rb | 93 +++++--- app/views/static_pages/about.html.erb | 4 +- db/seeds.rb | 14 +- ...nation_32_man_1_8_match_generation_test.rb | 225 ++++++++++++++++++ ...elimination_32_man_1_8_run_through_test.rb | 201 ++++++++++++++++ test/models/tournament_test.rb | 4 +- 8 files changed, 513 insertions(+), 93 deletions(-) create mode 100644 test/integration/double_elimination_32_man_1_8_match_generation_test.rb create mode 100644 test/integration/double_elimination_32_man_1_8_run_through_test.rb diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 8f0dc9f..0c06115 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -168,7 +168,7 @@ class Tournament < ApplicationRecord def double_elim_number_of_wrestlers_error error_string = "" if self.tournament_type == "Double Elimination 1-6" or self.tournament_type == "Double Elimination 1-8" - weights_with_too_many_wrestlers = weights.select{|w| w.wrestlers.size > 16} + weights_with_too_many_wrestlers = weights.select{|w| w.wrestlers.size > 32} weight_with_too_few_wrestlers = weights.select{|w| w.wrestlers.size < 4} weights_with_too_many_wrestlers.each do |weight| error_string = error_string + " The weight class #{weight.max} has more than 16 wrestlers." diff --git a/app/services/tournament_services/double_elimination_generate_loser_names.rb b/app/services/tournament_services/double_elimination_generate_loser_names.rb index eb5521a..c0b3dd8 100644 --- a/app/services/tournament_services/double_elimination_generate_loser_names.rb +++ b/app/services/tournament_services/double_elimination_generate_loser_names.rb @@ -15,60 +15,27 @@ class DoubleEliminationGenerateLoserNames case bracket_size when 4 [ - { - conso_round: @tournament.total_rounds, - conso_bracket_position: "3/4", - championship_round: 1, - championship_bracket_position: "Semis", - cross_bracket: false, - both_wrestlers: true - } + # have to use total rounds here because if other weights have bigger brackets, then the finals round 3/4 won't be round 2 + { conso_round: @tournament.total_rounds, conso_bracket_position: "3/4", championship_round: 1, championship_bracket_position: "Semis", cross_bracket: false, both_wrestlers: true } ] when 8 [ - { - conso_round: 2, - conso_bracket_position: "Conso Quarter", - championship_round: 1, - championship_bracket_position: "Quarter", - cross_bracket: false, - both_wrestlers: true - }, - { - conso_round: 3, - conso_bracket_position: "Conso Semis", - championship_round: 2, - championship_bracket_position: "Semis", - cross_bracket: true, - both_wrestlers: false - } + { conso_round: 2, conso_bracket_position: "Conso Quarter", championship_round: 1, championship_bracket_position: "Quarter", cross_bracket: false, both_wrestlers: true }, + { conso_round: 3, conso_bracket_position: "Conso Semis", championship_round: 2, championship_bracket_position: "Semis", cross_bracket: true, both_wrestlers: false } ] when 16 [ - { - conso_round: 2, - conso_bracket_position: "Conso", - championship_round: 1, - championship_bracket_position: "Bracket", - cross_bracket: false, - both_wrestlers: true - }, - { - conso_round: 3, - conso_bracket_position: "Conso", - championship_round: 2, - championship_bracket_position: "Quarter", - cross_bracket: true, - both_wrestlers: false - }, - { - conso_round: 5, - conso_bracket_position: "Conso Semis", - championship_round: 4, - championship_bracket_position: "Semis", - cross_bracket: false, - both_wrestlers: false - } + { conso_round: 2, conso_bracket_position: "Conso", championship_round: 1, championship_bracket_position: "Bracket", cross_bracket: false, both_wrestlers: true }, + { conso_round: 3, conso_bracket_position: "Conso", championship_round: 2, championship_bracket_position: "Quarter", cross_bracket: true, both_wrestlers: false }, + { conso_round: 5, conso_bracket_position: "Conso Semis", championship_round: 4, championship_bracket_position: "Semis", cross_bracket: false, both_wrestlers: false } + ] + when 32 + [ + { conso_round: 2, conso_bracket_position: "Conso", championship_round: 1, championship_bracket_position: "Bracket", cross_bracket: false, both_wrestlers: true }, + { conso_round: 3, conso_bracket_position: "Conso", championship_round: 2, championship_bracket_position: "Bracket", cross_bracket: true, both_wrestlers: false }, + { conso_round: 5, conso_bracket_position: "Conso", championship_round: 4, championship_bracket_position: "Quarter", cross_bracket: false, both_wrestlers: false }, + { conso_round: 7, conso_bracket_position: "Conso Semis", championship_round: 6, championship_bracket_position: "Semis", cross_bracket: true, both_wrestlers: false }, + ] else nil diff --git a/app/services/tournament_services/double_elimination_match_generation.rb b/app/services/tournament_services/double_elimination_match_generation.rb index 562f9df..f833540 100644 --- a/app/services/tournament_services/double_elimination_match_generation.rb +++ b/app/services/tournament_services/double_elimination_match_generation.rb @@ -10,38 +10,62 @@ class DoubleEliminationMatchGeneration end def define_bracket_matches(bracket_size) - # Use a hash instead of Struct + # Use detailed hashes for rounds, number of matches, and bracket positions case bracket_size when 4 { - round_one_matchups: [[1, 4, "Semis"], [2, 3, "Semis"]], - championship_rounds: [[2, 1, "1/2"]], - consolation_rounds: [[2, 1, "3/4"]] + round_one_matchups: [ + { seeds: [1, 4], bracket_position: "Semis" }, { seeds: [2, 3], bracket_position: "Semis" } + ], + championship_rounds: [ + { round: 2, number_of_matches: 1, bracket_position: "1/2" } + ], + consolation_rounds: [ + { round: 2, number_of_matches: 1, bracket_position: "3/4" } + ] } when 8 { round_one_matchups: [ - [1, 8, "Quarter"], [4, 5, "Quarter"], [3, 6, "Quarter"], [2, 7, "Quarter"] + { seeds: [1, 8], bracket_position: "Quarter" }, { seeds: [4, 5], bracket_position: "Quarter" }, + { seeds: [3, 6], bracket_position: "Quarter" }, { seeds: [2, 7], bracket_position: "Quarter" } ], championship_rounds: [ - [2, 2, "Semis"], [4, 1, "1/2"] + { round: 2, number_of_matches: 2, bracket_position: "Semis" }, { round: 4, number_of_matches: 1, bracket_position: "1/2" } ], consolation_rounds: [ - [2, 2, "Conso Quarter"], [3, 2, "Conso Semis"], [4, 1, "3/4"] + { round: 2, number_of_matches: 2, bracket_position: "Conso Quarter" }, { round: 3, number_of_matches: 2, bracket_position: "Conso Semis" }, { round: 4, number_of_matches: 1, bracket_position: "3/4" } ] } when 16 { round_one_matchups: [ - [1, 16, "Bracket"], [8, 9, "Bracket"], [5, 12, "Bracket"], [4, 13, "Bracket"], - [3, 14, "Bracket"], [6, 11, "Bracket"], [7, 10, "Bracket"], [2, 15, "Bracket"] + { seeds: [1, 16], bracket_position: "Bracket" }, { seeds: [8, 9], bracket_position: "Bracket" }, { seeds: [5, 12], bracket_position: "Bracket" }, { seeds: [4, 13], bracket_position: "Bracket" }, + { seeds: [3, 14], bracket_position: "Bracket" }, { seeds: [6, 11], bracket_position: "Bracket" },{ seeds: [7, 10], bracket_position: "Bracket" }, { seeds: [2, 15], bracket_position: "Bracket" } ], championship_rounds: [ - [2, 4, "Quarter"], [4, 2, "Semis"], [6, 1, "1/2"] + { round: 2, number_of_matches: 4, bracket_position: "Quarter" }, { round: 4, number_of_matches: 2, bracket_position: "Semis" }, { round: 6, number_of_matches: 1, bracket_position: "1/2" } ], consolation_rounds: [ - [2, 4, "Conso"], [3, 4, "Conso"], [4, 2, "Conso Quarter"], - [5, 2, "Conso Semis"], [6, 1, "3/4"] + { round: 2, number_of_matches: 4, bracket_position: "Conso" }, { round: 3, number_of_matches: 4, bracket_position: "Conso" }, { round: 4, number_of_matches: 2, bracket_position: "Conso Quarter" }, + { round: 5, number_of_matches: 2, bracket_position: "Conso Semis" }, { round: 6, number_of_matches: 1, bracket_position: "3/4" } + ] + } + when 32 + { + round_one_matchups: [ + { seeds: [1, 32], bracket_position: "Bracket" }, { seeds: [16, 17], bracket_position: "Bracket" }, { seeds: [9, 24], bracket_position: "Bracket" }, { seeds: [8, 25], bracket_position: "Bracket" }, + { seeds: [5, 28], bracket_position: "Bracket" }, { seeds: [12, 21], bracket_position: "Bracket" }, { seeds: [13, 20], bracket_position: "Bracket" }, { seeds: [4, 29], bracket_position: "Bracket" }, + { seeds: [3, 30], bracket_position: "Bracket" }, { seeds: [14, 19], bracket_position: "Bracket" }, { seeds: [11, 22], bracket_position: "Bracket" }, { seeds: [6, 27], bracket_position: "Bracket" }, + { seeds: [7, 26], bracket_position: "Bracket" }, { seeds: [10, 23], bracket_position: "Bracket" }, { seeds: [15, 18], bracket_position: "Bracket" }, { seeds: [2, 31], bracket_position: "Bracket" } + ], + championship_rounds: [ + { round: 2, number_of_matches: 8, bracket_position: "Bracket" }, { round: 4, number_of_matches: 4, bracket_position: "Quarter" }, + { round: 6, number_of_matches: 2, bracket_position: "Semis" }, { round: 8, number_of_matches: 1, bracket_position: "1/2" } + ], + consolation_rounds: [ + { round: 2, number_of_matches: 8, bracket_position: "Conso" }, { round: 3, number_of_matches: 8, bracket_position: "Conso" }, { round: 4, number_of_matches: 4, bracket_position: "Conso" }, { round: 5, number_of_matches: 4, bracket_position: "Conso" }, + { round: 6, number_of_matches: 2, bracket_position: "Conso Quarter" }, { round: 7, number_of_matches: 2, bracket_position: "Conso Semis" }, { round: 8, number_of_matches: 1, bracket_position: "3/4" } ] } else @@ -53,44 +77,43 @@ class DoubleEliminationMatchGeneration number_of_placers = @tournament.number_of_placers bracket_size = weight.calculate_bracket_size bracket_matches = define_bracket_matches(bracket_size) - + # Generate round 1 matches bracket_matches[:round_one_matchups].each_with_index do |matchup, index| - matches_this_round = bracket_matches[:round_one_matchups].size - bracket_position = matchup[2] - create_matchup_from_seed( - matchup[0], - matchup[1], - bracket_position, - index + 1, - 1, - weight - ) + seed_1 = matchup[:seeds][0] + seed_2 = matchup[:seeds][1] + bracket_position = matchup[:bracket_position] + round = 1 + bracket_position_number = index + 1 + + create_matchup_from_seed(seed_1, seed_2, bracket_position, bracket_position_number, round, weight) end - + # Generate remaining championship rounds - bracket_matches[:championship_rounds].each do |matchup| - round = matchup[0] - matches_this_round = matchup[1] - bracket_position = matchup[2] - + bracket_matches[:championship_rounds].each do |round_info| + round = round_info[:round] + matches_this_round = round_info[:number_of_matches] + bracket_position = round_info[:bracket_position] + matches_this_round.times do |bracket_position_number| create_matchup(nil, nil, bracket_position, bracket_position_number + 1, round, weight) end end - + # Generate consolation matches - bracket_matches[:consolation_rounds].each do |matchup| - round = matchup[0] - matches_this_round = matchup[1] - bracket_position = matchup[2] - + bracket_matches[:consolation_rounds].each do |round_info| + round = round_info[:round] + matches_this_round = round_info[:number_of_matches] + bracket_position = round_info[:bracket_position] + matches_this_round.times do |bracket_position_number| create_matchup(nil, nil, bracket_position, bracket_position_number + 1, round, weight) end + if weight.wrestlers.size >= 5 create_matchup(nil, nil, "5/6", 1, round, weight) if @tournament.number_of_placers >= 6 && matches_this_round == 1 end + if weight.wrestlers.size >= 7 create_matchup(nil, nil, "7/8", 1, round, weight) if @tournament.number_of_placers >= 8 && matches_this_round == 1 end diff --git a/app/views/static_pages/about.html.erb b/app/views/static_pages/about.html.erb index ae3032c..fbeba65 100644 --- a/app/views/static_pages/about.html.erb +++ b/app/views/static_pages/about.html.erb @@ -8,7 +8,7 @@
  • Tournament Types
  • @@ -62,7 +62,7 @@

    Regular Double Elimination Information

    -

    Right now, double elimination brackets only support 8 and 16 man brackets (4-16 wrestlers). Cross bracketing will happen every other round. 16 man in quarter finals, 8 man in semi finals.

    +

    Right now, double elimination brackets only support 8, 16, and 32 man brackets (4-32 wrestlers). Cross bracketing will happen every other round. 16 man in quarter finals, 8 man in semi finals.

    Regular Double Elimination 1-6 places 1st through 6th. Regular Double Elimination 1-8 places 1st through 8th.

    Regular Double Elimination scoring