diff --git a/app/models/mat.rb b/app/models/mat.rb index 991c5dc..f43ea9e 100644 --- a/app/models/mat.rb +++ b/app/models/mat.rb @@ -46,7 +46,17 @@ class Mat < ApplicationRecord def next_eligible_match # Start with all matches that are either unfinished (nil or 0), have a bout number, and are ordered by bout_number - filtered_matches = tournament.matches.where(finished: [nil, 0]).where(mat_id: nil).where.not(bout_number: nil).order(:bout_number) + filtered_matches = tournament.matches + .where(finished: [nil, 0]) # finished is nil or 0 + .where(mat_id: nil) # mat_id is nil + .where.not(bout_number: nil) # bout_number is not nil + .order(:bout_number) + + # .where's are chained as ANDs + # cannot search for .where.not(loser1_name: "BYE") because it will not find any that are NULL + filtered_matches = filtered_matches + .where("loser1_name != ? OR loser1_name IS NULL", "BYE") + .where("loser2_name != ? OR loser2_name IS NULL", "BYE") # Sequentially apply each rule, narrowing down the matches mat_assignment_rules.each do |rule| diff --git a/app/services/bracket_advancement/double_elimination_advance.rb b/app/services/bracket_advancement/double_elimination_advance.rb index 1fd6982..bb02229 100644 --- a/app/services/bracket_advancement/double_elimination_advance.rb +++ b/app/services/bracket_advancement/double_elimination_advance.rb @@ -14,6 +14,7 @@ class DoubleEliminationAdvance loser_advance end advance_double_byes + set_bye_for_placement end def winner_advance @@ -94,25 +95,38 @@ class DoubleEliminationAdvance end def loser_advance - bout = @last_match.bout_number - next_match = Match.where("(loser1_name = ? OR loser2_name = ?) AND weight_id = ?","Loser of #{bout}","Loser of #{bout}",@wrestler.weight_id) - if next_match.size > 0 - next_match = next_match.first - next_match.replace_loser_name_with_wrestler(@wrestler,"Loser of #{bout}") - if next_match.loser1_name == "BYE" or next_match.loser2_name == "BYE" - next_match.winner_id = @wrestler.id - next_match.win_type = "BYE" - next_match.finished = 1 - next_match.save - next_match.advance_wrestlers - end + bout = @last_match.bout_number + next_match = Match.where("(loser1_name = ? OR loser2_name = ?) AND weight_id = ?", "Loser of #{bout}", "Loser of #{bout}", @wrestler.weight_id).first + + if next_match.present? + next_match.replace_loser_name_with_wrestler(@wrestler, "Loser of #{bout}") + next_match.reload + + if next_match.loser1_name == "BYE" || next_match.loser2_name == "BYE" + next_match.winner_id = @wrestler.id + next_match.win_type = "BYE" + next_match.score = "" + next_match.finished = 1 + # puts "Before save: winner_id=#{next_match.winner_id}" + + # if next_match.save + # puts "Save successful: winner_id=#{next_match.reload.winner_id}" + # else + # puts "Save failed: #{next_match.errors.full_messages}" + # end + next_match.save + + next_match.advance_wrestlers end + end end + def advance_double_byes weight = @wrestler.weight weight.matches.select{|m| m.loser1_name == "BYE" and m.loser2_name == "BYE" and m.finished != 1}.each do |match| match.finished = 1 + match.score = "" match.win_type = "BYE" next_match_position_number = (match.bracket_position_number / 2.0).ceil after_matches = weight.matches.select{|m| m.round > match.round and m.is_consolation_match == match.is_consolation_match }.sort_by{|m| m.round} @@ -135,4 +149,26 @@ class DoubleEliminationAdvance match.save end end + + def set_bye_for_placement + weight = @wrestler.weight + fifth_finals = weight.matches.select{|match| match.bracket_position == '5/6'}.first + seventh_finals = weight.matches.select{|match| match.bracket_position == '7/8'}.first + if seventh_finals + conso_quarter = weight.matches.select{|match| match.bracket_position == 'Conso Quarter'} + conso_quarter.each do |match| + if match.loser1_name == "BYE" or match.loser2_name == "BYE" + seventh_finals.replace_loser_name_with_bye("Loser of #{match.bout_number}") + end + end + end + if fifth_finals + conso_semis = weight.matches.select{|match| match.bracket_position == 'Conso Semis'} + conso_semis.each do |match| + if match.loser1_name == "BYE" or match.loser2_name == "BYE" + fifth_finals.replace_loser_name_with_bye("Loser of #{match.bout_number}") + end + end + end + end end diff --git a/db/seeds.rb b/db/seeds.rb index e2ea0bf..fadf44c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -98,6 +98,8 @@ number_of_wrestlers = 8 elsif index == 2 number_of_wrestlers = 32 + elsif index == 3 + number_of_wrestlers = 17 else number_of_wrestlers = 16 end @@ -119,6 +121,8 @@ number_of_wrestlers = 8 elsif index == 2 number_of_wrestlers = 32 + elsif index == 3 + number_of_wrestlers = 17 else number_of_wrestlers = 16 end diff --git a/test/integration/double_elimination_auto_byes_test.rb b/test/integration/double_elimination_auto_byes_test.rb new file mode 100644 index 0000000..b1f9ea8 --- /dev/null +++ b/test/integration/double_elimination_auto_byes_test.rb @@ -0,0 +1,86 @@ +require 'test_helper' + +class DoubleEliminationAutoByes < ActionDispatch::IntegrationTest + def setup + end + + def winner_by_name(winner_name,match) + wrestler = @tournament.weights.first.wrestlers.select{|w| w.name == winner_name}.first + match.winner_id = wrestler.id + match.finished = 1 + match.win_type = "Decision" + match.score = "0-0" + match.save + end + + test "8 man double elimination bracket with only 6 guys deletes one guy and auto advances byes and matches with a bye never get assigned to a mat" do + create_double_elim_tournament_single_weight_1_6(6) + mat = Mat.create!(name: "Mat 1", tournament_id: @tournament.id) + test3_wrestler = @tournament.weights.first.wrestlers.select{|w| w.name == "Test6"}.first.destroy + + GenerateTournamentMatches.new(@tournament.reload).generate + matches = @tournament.matches.reload + + round1 = matches.select{|m| m.round == 1} + assert round1.select{|m| m.bracket_position_number == 1}.first.wrestler1.name == "Test1" + assert round1.select{|m| m.bracket_position_number == 1}.first.loser2_name == "BYE" + assert round1.select{|m| m.bracket_position_number == 2}.first.wrestler1.name == "Test4" + assert round1.select{|m| m.bracket_position_number == 2}.first.wrestler2.name == "Test5" + assert round1.select{|m| m.bracket_position_number == 3}.first.wrestler1.name == "Test3" + assert round1.select{|m| m.bracket_position_number == 3}.first.loser2_name == "BYE" + assert round1.select{|m| m.bracket_position_number == 4}.first.wrestler1.name == "Test2" + assert round1.select{|m| m.bracket_position_number == 4}.first.loser2_name == "BYE" + winner_by_name("Test4", round1.select{|m| m.bracket_position_number == 2}.first) + assert mat.reload.unfinished_matches.first.loser1_name != "BYE" + assert mat.reload.unfinished_matches.first.loser2_name != "BYE" + + semis = matches.select{|m| m.bracket_position == "Semis"}.sort_by{|m| m.bracket_position_number} + assert semis.first.reload.wrestler1.name == "Test1" + assert semis.first.reload.wrestler2.name == "Test4" + assert semis.second.reload.wrestler1.name == "Test3" + assert semis.second.reload.wrestler2.name == "Test2" + winner_by_name("Test4",semis.first) + assert mat.reload.unfinished_matches.first.loser1_name != "BYE" + assert mat.reload.unfinished_matches.first.loser2_name != "BYE" + winner_by_name("Test2",semis.second) + assert mat.reload.unfinished_matches.first.loser1_name != "BYE" + assert mat.reload.unfinished_matches.first.loser2_name != "BYE" + + conso_quarter = matches.select{|m| m.bracket_position == "Conso Quarter"}.sort_by{|m| m.bracket_position_number} + assert conso_quarter.first.reload.loser1_name == "BYE" + assert conso_quarter.first.reload.wrestler2.name == "Test5" + assert conso_quarter.second.reload.loser1_name == "BYE" + assert conso_quarter.second.reload.loser2_name == "BYE" + + conso_semis = matches.select{|m| m.bracket_position == "Conso Semis"}.sort_by{|m| m.bracket_position_number} + assert conso_semis.first.reload.wrestler1.name == "Test3" + assert conso_semis.first.reload.wrestler2.name == "Test5" + assert conso_semis.second.reload.wrestler1.name == "Test1" + assert conso_semis.second.reload.loser2_name == "BYE" + winner_by_name("Test5",conso_semis.first) + assert mat.reload.unfinished_matches.first.loser1_name != "BYE" + assert mat.reload.unfinished_matches.first.loser2_name != "BYE" + + first_finals = matches.select{|m| m.bracket_position == "1/2"}.first + third_finals = matches.select{|m| m.bracket_position == "3/4"}.first + fifth_finals = matches.select{|m| m.bracket_position == "5/6"}.first + + assert first_finals.reload.wrestler1.name == "Test4" + assert first_finals.reload.wrestler2.name == "Test2" + + assert third_finals.reload.wrestler1.name == "Test5" + assert third_finals.reload.wrestler2.name == "Test1" + + test3_wrestler = @tournament.weights.first.wrestlers.select{|w| w.name == "Test3"}.first + assert fifth_finals.reload.wrestler1.name == "Test3" + assert fifth_finals.reload.loser2_name == "BYE" + assert_equal test3_wrestler.id, fifth_finals.reload.winner_id + + + # DEBUG + # matches.sort_by{|m| m.bout_number}.each do |match| + # match.reload + # puts "Round #{match.round} #{match.w1_bracket_name} vs #{match.w2_bracket_name}" + # end + end +end \ No newline at end of file