1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-24 17:04:43 +00:00

Protect tournament from out of order seeding.

This commit is contained in:
2025-01-20 10:23:27 -05:00
parent d57b2e6e6f
commit cc62e1c2f1
2 changed files with 65 additions and 12 deletions

View File

@@ -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

View File

@@ -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