diff --git a/app/services/tournament_services/tournament_seeding.rb b/app/services/tournament_services/tournament_seeding.rb index f302be5..3219bbd 100644 --- a/app/services/tournament_services/tournament_seeding.rb +++ b/app/services/tournament_services/tournament_seeding.rb @@ -18,19 +18,14 @@ class TournamentSeeding def random_seeding(wrestlers, bracket_size) half_of_bracket = bracket_size / 2 available_bracket_lines = (1..bracket_size).to_a - first_half_available_bracket_lines = (1..half_of_bracket).to_a - first_line_of_second_half_of_bracket = half_of_bracket + 1 - second_half_available_bracket_lines = (first_line_of_second_half_of_bracket..bracket_size).to_a # remove bracket lines that are taken from available_bracket_lines wrestlers_with_bracket_lines = wrestlers.select{|w| w.bracket_line != nil } wrestlers_with_bracket_lines.each do |wrestler| available_bracket_lines.delete(wrestler.bracket_line) - first_half_available_bracket_lines.delete(wrestler.bracket_line) - second_half_available_bracket_lines.delete(wrestler.bracket_line) end - available_bracket_lines_to_use = set_random_seeding_bracket_line_order(first_half_available_bracket_lines, second_half_available_bracket_lines) + available_bracket_lines_to_use = set_random_seeding_bracket_line_order(available_bracket_lines) wrestlers_without_bracket_lines = wrestlers.select{|w| w.bracket_line == nil } if @tournament.tournament_type == "Pool to bracket" @@ -84,7 +79,7 @@ class TournamentSeeding private - def set_random_seeding_bracket_line_order(first_half_lines, second_half_lines) + def set_random_seeding_bracket_line_order(available_bracket_lines) # This method prevents double BYEs in round 1 # It also evenly distributes matches from the top half of the bracket to the bottom half # It does both of these while keeping the randomness of the line assignment @@ -94,22 +89,11 @@ class TournamentSeeding # sort by odd or even based on the sample above if odd_or_even_sample == 1 # odd numbers first - sorted_first_half_lines = first_half_lines.sort_by { |n| n.even? ? 1 : 0 } - sorted_second_half_lines = second_half_lines.sort_by { |n| n.even? ? 1 : 0 } + result = available_bracket_lines.sort_by { |n| n.even? ? 1 : 0 } else # even numbers first - sorted_first_half_lines = first_half_lines.sort_by { |n| n.odd? ? 1 : 0 } - sorted_second_half_lines = second_half_lines.sort_by { |n| n.odd? ? 1 : 0 } + result = available_bracket_lines.sort_by { |n| n.odd? ? 1 : 0 } end - - # zip requires either even arrays or the receiver to be the bigger list - if first_half_lines.size >= second_half_lines.size - result = sorted_first_half_lines.zip(sorted_second_half_lines).flatten - else - result = sorted_second_half_lines.zip(sorted_first_half_lines).flatten - end - result.compact - result.delete(nil) result end end \ No newline at end of file diff --git a/test/integration/random_seeding_test.rb b/test/integration/random_seeding_test.rb index a13eafb..69b08d5 100644 --- a/test/integration/random_seeding_test.rb +++ b/test/integration/random_seeding_test.rb @@ -6,7 +6,7 @@ class RandomSeedingTest < ActionDispatch::IntegrationTest end def clean_up_original_seeds(tournament) - tournament.wrestlers.select{|w| w.original_seed > 12}.each do |wrestler| + tournament.wrestlers.each do |wrestler| wrestler.original_seed = nil wrestler.save end @@ -33,4 +33,23 @@ class RandomSeedingTest < ActionDispatch::IntegrationTest round_one_bottom_half = round_one_matches.select{|m| !m.w1.nil? and !m.w2.nil? and m.bracket_position_number > 8} assert round_one_top_half.size == round_one_bottom_half.size end + + test "There are the same number of matches in the top half and bottom half of a double elimination tournament in round 1 in a 6 man bracket" do + create_double_elim_tournament_single_weight(6, "Regular Double Elimination 1-8") + clean_up_original_seeds(@tournament) + round_one_matches = @tournament.matches.reload.select{|m| m.round == 1} + # 8 man bracket there are 4 matches so top half is bracket_position_number 1-2 and bottom is 3-4 + round_one_top_half = round_one_matches.select{|m| !m.w1.nil? and !m.w2.nil? and m.bracket_position_number < 3} + round_one_bottom_half = round_one_matches.select{|m| !m.w1.nil? and !m.w2.nil? and m.bracket_position_number > 2} + assert round_one_top_half.size == round_one_bottom_half.size + end + + test "There are no double byes in a double elimination tournament in a 6 man bracket" do + create_double_elim_tournament_single_weight(6, "Regular Double Elimination 1-8") + clean_up_original_seeds(@tournament) + round_one_matches = @tournament.matches.reload.select{|m| m.round == 1} + conso_round_one_matches = @tournament.matches.reload.select{|m| m.bracket_position == "Conso Quarter"} + assert round_one_matches.select{|m| m.w1.nil? and m.w2.nil? }.size == 0 + assert conso_round_one_matches.select{|m| m.loser1_name == "BYE" and m.loser2_name == "BYE" }.size == 0 + end end \ No newline at end of file