mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +00:00
Protect tournament from out of order seeding.
This commit is contained in:
@@ -205,6 +205,17 @@ class Tournament < ApplicationRecord
|
||||
return error_string
|
||||
end
|
||||
|
||||
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
|
||||
@@ -217,6 +228,8 @@ class Tournament < ApplicationRecord
|
||||
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
|
||||
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}"
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user