diff --git a/app/models/tournament.rb b/app/models/tournament.rb index c2d9d66..fe2b9e1 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -205,25 +205,38 @@ class Tournament < ApplicationRecord return error_string end - def match_generation_error - error_string = "" - if pool_to_bracket_number_of_wrestlers_error.length > 0 - error_string += pool_to_bracket_number_of_wrestlers_error - elsif modified_sixteen_man_number_of_wrestlers_error.length > 0 - error_string += modified_sixteen_man_number_of_wrestlers_error - elsif double_elim_number_of_wrestlers_error.length > 0 - error_string += double_elim_number_of_wrestlers_error + def wrestlers_with_out_of_order_seed_error + error_string = "" + weights.each do |weight| + original_seeds = weight.wrestlers.map(&:original_seed).compact.sort + if original_seeds.any? && original_seeds != (original_seeds.first..original_seeds.last).to_a + error_string += "The weight class #{weight.max} has wrestlers with out-of-order seeds: #{original_seeds}. There is a gap in the sequence." + end + end + return error_string + end + + def match_generation_error + error_string = "" + if pool_to_bracket_number_of_wrestlers_error.length > 0 + error_string += pool_to_bracket_number_of_wrestlers_error + elsif modified_sixteen_man_number_of_wrestlers_error.length > 0 + error_string += modified_sixteen_man_number_of_wrestlers_error + elsif double_elim_number_of_wrestlers_error.length > 0 + error_string += double_elim_number_of_wrestlers_error elsif wrestlers_with_higher_seed_than_bracket_size_error.length > 0 - error_string += wrestlers_with_higher_seed_than_bracket_size_error + error_string += wrestlers_with_higher_seed_than_bracket_size_error elsif wrestlers_with_duplicate_original_seed_error.length > 0 - error_string += wrestlers_with_duplicate_original_seed_error - end + error_string += wrestlers_with_duplicate_original_seed_error + elsif wrestlers_with_out_of_order_seed_error.length > 0 + error_string += wrestlers_with_out_of_order_seed_error + end if error_string.length > 0 return "There is a tournament error. #{error_string}" else return nil end - end + end def reset_and_fill_bout_board reset_mats diff --git a/test/controllers/tournaments_controller_test.rb b/test/controllers/tournaments_controller_test.rb index 5d58923..63dc132 100644 --- a/test/controllers/tournaments_controller_test.rb +++ b/test/controllers/tournaments_controller_test.rb @@ -760,4 +760,44 @@ class TournamentsControllerTest < ActionController::TestCase get :generate_matches, params: { id: @tournament.id } redirect_tournament_error end + + test "tournament generation error when a weight class has wrestlers with out-of-order original seeds" do + sign_in_owner + create_pool_tournament_single_weight(5) # Create a weight class with 5 wrestlers + @tournament.destroy_all_matches + @tournament.user_id = 1 + @tournament.save + + # Set seeds to have a gap: [1, 2, 3, 5, nil] + wrestlers = @tournament.weights.first.wrestlers + wrestlers[0].original_seed = 1 + wrestlers[1].original_seed = 2 + wrestlers[2].original_seed = 3 + wrestlers[3].original_seed = 5 + wrestlers[4].original_seed = nil # Unseeded wrestler + wrestlers.each(&:save) + + get :generate_matches, params: { id: @tournament.id } + redirect_tournament_error + end + + test "logged in tournament owner can generate matches with the correct seed order" do + sign_in_owner + create_pool_tournament_single_weight(5) # Create a weight class with 5 wrestlers + @tournament.destroy_all_matches + @tournament.user_id = 1 + @tournament.save + + # Set seeds to have a gap: [1, 2, 3, 5, nil] + wrestlers = @tournament.weights.first.wrestlers + wrestlers[0].original_seed = 1 + wrestlers[1].original_seed = 2 + wrestlers[2].original_seed = 3 + wrestlers[3].original_seed = 4 + wrestlers[4].original_seed = nil # Unseeded wrestler + wrestlers.each(&:save) + + get :generate_matches, params: { id: @tournament.id } + success + end end