Skip to content

Commit

Permalink
Fixed double elim bracket views, fixed double elim match generation a…
Browse files Browse the repository at this point in the history
…nd loser name generation for 4 man bracket, fixed seed page submission and added tests, added tests for tournament generation errors and added a new error for assigning a seed higher than the number of wrestlers.
  • Loading branch information
jcwimer committed Dec 9, 2024
1 parent f6ef471 commit f18802a
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 37 deletions.
10 changes: 8 additions & 2 deletions app/controllers/weights_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ class WeightsController < ApplicationController
# GET /weights/1.json
def show
if params[:wrestler]
check_access_manage
respond_to do |format|
Wrestler.update(params[:wrestler].keys, params[:wrestler].values)
# Sanitize the wrestler parameters
sanitized_wrestlers = params.require(:wrestler).to_unsafe_h.transform_values do |attributes|
ActionController::Parameters.new(attributes).permit(:original_seed)
end

Wrestler.update(sanitized_wrestlers.keys, sanitized_wrestlers.values)
format.html { redirect_to @weight, notice: 'Seeds were successfully updated.' }
end
end
@wrestlers = @weight.wrestlers
@tournament = @weight.tournament
session[:return_path] = "/weights/#{@weight.id}"
end
end

# GET /weights/new
def new
Expand Down
44 changes: 29 additions & 15 deletions app/models/tournament.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def remove_school_delegations
end
end

def pool_to_bracket_number_of_wrestlers
def pool_to_bracket_number_of_wrestlers_error
error_string = ""
if self.tournament_type.include? "Pool to bracket"
weights_with_too_many_wrestlers = weights.select{|w| w.wrestlers.size > 24}
Expand All @@ -144,7 +144,7 @@ def pool_to_bracket_number_of_wrestlers
return error_string
end

def modified_sixteen_man_number_of_wrestlers
def modified_sixteen_man_number_of_wrestlers_error
error_string = ""
if self.tournament_type.include? "Modified 16 Man Double Elimination"
weights_with_too_many_wrestlers = weights.select{|w| w.wrestlers.size > 16}
Expand All @@ -159,7 +159,7 @@ def modified_sixteen_man_number_of_wrestlers
return error_string
end

def double_elim_number_of_wrestlers
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}
Expand All @@ -173,21 +173,35 @@ def double_elim_number_of_wrestlers
end
return error_string
end

def wrestlers_with_higher_seed_than_bracket_size_error
error_string = ""
weights.each do |weight|
weight.wrestlers.each do |wrestler|
if wrestler.original_seed != nil && wrestler.original_seed > weight.wrestlers.size
error_string += "Wrestler: #{wrestler.name} has a seed of #{wrestler.original_seed} which is greater than the amount of wrestlers (#{weight.wrestlers.size}) in the weight class #{weight.max}."
end
end
end
return error_string
end

def match_generation_error
error_string = "There is a tournament error."
modified_sixteen_man_error = modified_sixteen_man_number_of_wrestlers
double_elim_error = double_elim_number_of_wrestlers
pool_to_bracket_error = pool_to_bracket_number_of_wrestlers
if pool_to_bracket_error.length > 0
return error_string + pool_to_bracket_error
elsif modified_sixteen_man_error.length > 0
return error_string + modified_sixteen_man_error
elsif double_elim_error.length > 0
return error_string + double_elim_error
else
nil
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
end
if error_string.length > 0
return "There is a tournament error. #{error_string}"
else
return nil
end
end

def reset_and_fill_bout_board
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ def define_losername_championship_mappings(bracket_size)
# cross_bracket is true or false. crossing should happen every other round.
# both_wrestlers defines if you're filling out loser1_name and loser2_name or just loser1_name
# don't need to define 3/4, 5/6, or 7/8 those happen in the assign_loser_names_for_weight method
# except for bracket_size of 4
case bracket_size
when 4 then
return [
[@tournament.total_rounds, "3/4", 1, "Semis", false, true]
]
when 8 then
return [
[2, "Conso Quarter", 1, "Quarter", false, true],
Expand All @@ -24,8 +29,8 @@ def define_losername_championship_mappings(bracket_size)
when 16 then
return [
[2, "Conso", 1, "Bracket", false, true],
[3, "Conso", 2, "Quarter", true],
[5, "Conso Semis", 4, "Semis", false]
[3, "Conso", 2, "Quarter", true, false],
[5, "Conso Semis", 4, "Semis", false, false]
]
else
return nil
Expand All @@ -35,7 +40,7 @@ def define_losername_championship_mappings(bracket_size)
def assign_loser_names_for_weight(weight)
number_of_placers = @tournament.number_of_placers
bracket_size = weight.calculate_bracket_size
matches_by_weight = weight.matches
matches_by_weight = weight.matches.reload

loser_name_championship_mappings = define_losername_championship_mappings(bracket_size)

Expand Down Expand Up @@ -68,14 +73,14 @@ def assign_loser_names_for_weight(weight)

conso_semi_matches = matches_by_weight.select{|match| match.bracket_position == "Conso Semis"}
conso_quarter_matches = matches_by_weight.select{|match| match.bracket_position == "Conso Quarter"}
if number_of_placers >= 6
if number_of_placers >= 6 && weight.wrestlers.size >= 5
five_six_match = matches_by_weight.select{|match| match.bracket_position == "5/6"}.first
bout_number1 = conso_semi_matches.select{|match| match.bracket_position_number == 1}.first.bout_number
bout_number2 = conso_semi_matches.select{|match| match.bracket_position_number == 2}.first.bout_number
five_six_match.loser1_name = "Loser of #{bout_number1}"
five_six_match.loser2_name = "Loser of #{bout_number2}"
end
if number_of_placers >= 8
if number_of_placers >= 8 && weight.wrestlers.size >= 7
seven_eight_match = matches_by_weight.select{|match| match.bracket_position == "7/8"}.first
bout_number1 = conso_quarter_matches.select{|match| match.bracket_position_number == 1}.first.bout_number
bout_number2 = conso_quarter_matches.select{|match| match.bracket_position_number == 2}.first.bout_number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def define_bracket_matches(bracket_size)
# consolation_rounds define [round_number, number_of_matches_in_round, bracket_position]

case bracket_size
when 4 then
return BracketMatchups.new(
[[1, 4, "Semis"], [2, 3, "Semis"]],
[[2, 1, "1/2"]],
[[2, 1, "3/4"]]
)
when 8 then
return BracketMatchups.new(
[[1, 8, "Quarter"], [4, 5, "Quarter"], [3, 6, "Quarter"], [2, 7, "Quarter"]],
Expand Down Expand Up @@ -75,8 +81,12 @@ def generate_matches_for_weight(weight)
matches_this_round.times do |bracket_position_number|
create_matchup(nil, nil, bracket_position, bracket_position_number + 1, round, weight)
end
create_matchup(nil, nil, "5/6", 1, round, weight) if @tournament.number_of_placers >= 6 && matches_this_round == 1
create_matchup(nil, nil, "7/8", 1, round, weight) if @tournament.number_of_placers >= 8 && matches_this_round == 1
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
end
end

Expand Down
10 changes: 2 additions & 8 deletions app/views/tournaments/_bracket_final.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
<% @final_match.each do |match| %>
<div class="round">
<% @final_match.each do |match| %>
<div class="game">
<div class="game-top "><%= match.w1_bracket_name.html_safe %> <span></span></div>
<div class="bout-number"><p><%= match.bout_number %> <%= match.bracket_score_string %></p><p><%= @winner_place %> Place Winner</p></div>
<div class="game-bottom "><%= match.w2_bracket_name.html_safe %><span></span></div>
</div>
</div>
<!-- <div class="round">
<div class="game">
<div class="bracket-winner"> <%= match.bracket_winner_name %></div>
<div class="bout_number"><%= @winner_place %> Place Winner</div>
</div>
<% end %>
</div> -->
<% end %>
2 changes: 0 additions & 2 deletions app/views/tournaments/_double_elimination_bracket.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@
</div>
<h4>5/6 place match</h4>
<div class="bracket">
<!--Round 1-->
<% @final_match = @matches.select{|m|m.bracket_position == "5/6"} %>
<% @winner_place = "5th" %>
<%= render 'bracket_final' %>
</div>
<% if @tournament.number_of_placers >= 8 %>
<h4>7/8 place match</h4>
<div class="bracket">
<!--Round 1-->
<% @final_match = @matches.select{|m|m.bracket_position == "7/8"} %>
<% @winner_place = "7th" %>
<%= render 'bracket_final' %>
Expand Down
14 changes: 11 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def create_wrestlers_for_weight(weight, tournament, number_of_wrestlers, naming_
number_of_wrestlers = 12
elsif index == 4
number_of_wrestlers = 24
elsif index == 5
number_of_wrestlers = 2
else
number_of_wrestlers = 16
end
Expand All @@ -72,9 +74,15 @@ def create_wrestlers_for_weight(weight, tournament, number_of_wrestlers, naming_
weight_classes=Weight::HS_WEIGHT_CLASSES.split(",")
tournament.create_pre_defined_weights(weight_classes)
wrestler_name_number = 1
tournament.weights.each do |weight|
create_wrestlers_for_weight(weight, tournament, 16, wrestler_name_number)
wrestler_name_number += 16
tournament.weights.each_with_index do |weight, index|
if index == 0
number_of_wrestlers = 12
else
number_of_wrestlers = 16
end

create_wrestlers_for_weight(weight, tournament, number_of_wrestlers, wrestler_name_number)
wrestler_name_number += number_of_wrestlers
end

# Regular Double Elimination 1-6
Expand Down
83 changes: 83 additions & 0 deletions test/controllers/tournaments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def redirect
assert_redirected_to '/static_pages/not_allowed'
end

def redirect_tournament_error
assert_redirected_to "/tournaments/#{@tournament.id}/error"
end

def destroy
delete :destroy, params: { id: 1 }
end
Expand Down Expand Up @@ -664,4 +668,83 @@ def wipe
updated_tournament = Tournament.find(new_tournament.id)
assert_equal users(:one).id, updated_tournament.user_id, "The user_id should not change when the tournament is edited by a non-owner"
end

test "tournament generation error when a wrestler has an original seed higher than the amount of wrestlers in the weight" do
sign_in_owner
create_double_elim_tournament_single_weight(14, "Regular Double Elimination 1-8")
@tournament.destroy_all_matches
@tournament.user_id = 1
@tournament.save
wrestler = @tournament.weights.first.wrestlers.first
wrestler.original_seed = 15
wrestler.save
get :generate_matches, params: { id: @tournament.id }
redirect_tournament_error
end

test "tournament generation error when a double elimination tournament has too many wrestlers" do
sign_in_owner
create_double_elim_tournament_single_weight(16, "Regular Double Elimination 1-8")
@tournament.destroy_all_matches
@tournament.user_id = 1
@tournament.save
create_wrestlers_for_weight_for_double_elim(@tournament.weights.first, @tournament.schools.first, 1, 20)
get :generate_matches, params: { id: @tournament.id }
redirect_tournament_error
end

test "tournament generation error when a double elimination tournament has too few wrestlers" do
sign_in_owner
create_double_elim_tournament_single_weight(4, "Regular Double Elimination 1-8")
@tournament.destroy_all_matches
@tournament.user_id = 1
@tournament.save
@tournament.weights.first.wrestlers.first.destroy
get :generate_matches, params: { id: @tournament.id }
redirect_tournament_error
end

test "tournament generation error when a Modified 16 Man Double Elimination tournament has too many wrestlers" do
sign_in_owner
create_double_elim_tournament_single_weight(16, "Modified 16 Man Double Elimination 1-8")
@tournament.destroy_all_matches
@tournament.user_id = 1
@tournament.save
create_wrestlers_for_weight_for_double_elim(@tournament.weights.first, @tournament.schools.first, 1, 20)
get :generate_matches, params: { id: @tournament.id }
redirect_tournament_error
end

test "tournament generation error when a Modified 16 Man Double Elimination tournament has too few wrestlers" do
sign_in_owner
create_double_elim_tournament_single_weight(12, "Modified 16 Man Double Elimination 1-8")
@tournament.destroy_all_matches
@tournament.user_id = 1
@tournament.save
@tournament.weights.first.wrestlers.first.destroy
get :generate_matches, params: { id: @tournament.id }
redirect_tournament_error
end

test "tournament generation error when a pool to bracket tournament has too many wrestlers" do
sign_in_owner
create_pool_tournament_single_weight(24)
@tournament.destroy_all_matches
@tournament.user_id = 1
@tournament.save
create_wrestlers_for_weight(@tournament.weights.first, @tournament.schools.first, 1, 20)
get :generate_matches, params: { id: @tournament.id }
redirect_tournament_error
end

test "tournament generation error when a pool to bracket tournament has too few wrestlers" do
sign_in_owner
create_pool_tournament_single_weight(2)
@tournament.destroy_all_matches
@tournament.user_id = 1
@tournament.save
@tournament.weights.first.wrestlers.first.destroy
get :generate_matches, params: { id: @tournament.id }
redirect_tournament_error
end
end
Loading

0 comments on commit f18802a

Please sign in to comment.