From 58afc8a48564e384c702e5a5eb5de9356eb73230 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Thu, 23 Jan 2020 21:25:25 -0500 Subject: [PATCH] Added logic for 16 man dobule elminination match generation. --- app/models/tournament.rb | 2 +- .../bracket_advancement/advance_wrestler.rb | 3 +- .../generate_tournament_matches.rb | 2 + ...double_elimination_generate_loser_names.rb | 96 ++++++++++++ ...man_double_elimination_match_generation.rb | 93 ++++++++++++ .../wrestler_services/swap_wrestlers.rb | 38 +++-- .../tournaments/_bracket_partial.html.erb | 4 + ...en_man_double_elimination_bracket.html.erb | 143 ++++++++++++++++++ 8 files changed, 365 insertions(+), 16 deletions(-) create mode 100644 app/services/tournament_services/sixteen_man_double_elimination_generate_loser_names.rb create mode 100644 app/services/tournament_services/sixteen_man_double_elimination_match_generation.rb create mode 100644 app/views/tournaments/_sixteen_man_double_elimination_bracket.html.erb diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 51e41e9..5ec4d7b 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -29,7 +29,7 @@ class Tournament < ActiveRecord::Base end def tournament_types - ["Pool to bracket","Modified 16 Man Double Elimination"] + ["Pool to bracket","Modified 16 Man Double Elimination","Double Elimination 1-6"] end def create_pre_defined_weights(value) diff --git a/app/services/bracket_advancement/advance_wrestler.rb b/app/services/bracket_advancement/advance_wrestler.rb index 3892c77..b0b91ea 100644 --- a/app/services/bracket_advancement/advance_wrestler.rb +++ b/app/services/bracket_advancement/advance_wrestler.rb @@ -14,7 +14,8 @@ class AdvanceWrestler def advance_raw pool_to_bracket_advancement if @tournament.tournament_type == "Pool to bracket" - DoubleEliminationAdvance.new(@wrestler).bracket_advancement if @tournament.tournament_type == "Modified 16 Man Double Elimination" + DoubleEliminationAdvance.new(@wrestler).bracket_advancement if @tournament.tournament_type == "Modified 16 Man Double Elimination" or + @tournament.tournament_type == "Double Elimination 1-6" end def pool_to_bracket_advancement diff --git a/app/services/tournament_services/generate_tournament_matches.rb b/app/services/tournament_services/generate_tournament_matches.rb index 209371c..5776cc5 100644 --- a/app/services/tournament_services/generate_tournament_matches.rb +++ b/app/services/tournament_services/generate_tournament_matches.rb @@ -34,9 +34,11 @@ class GenerateTournamentMatches standardStartingActions PoolToBracketMatchGeneration.new(@tournament).generatePoolToBracketMatches if @tournament.tournament_type == "Pool to bracket" ModifiedSixteenManMatchGeneration.new(@tournament).generate_matches if @tournament.tournament_type == "Modified 16 Man Double Elimination" + SixteenManDoubleEliminationMatchGeneration.new(@tournament).generate_matches if @tournament.tournament_type == "Double Elimination 1-6" postMatchCreationActions PoolToBracketMatchGeneration.new(@tournament).assignLoserNames if @tournament.tournament_type == "Pool to bracket" ModifiedSixteenManGenerateLoserNames.new(@tournament).assign_loser_names if @tournament.tournament_type == "Modified 16 Man Double Elimination" + SixteenManDoubleEliminationGenerateLoserNames.new(@tournament).assign_loser_names if @tournament.tournament_type == "Double Elimination 1-6" end def standardStartingActions diff --git a/app/services/tournament_services/sixteen_man_double_elimination_generate_loser_names.rb b/app/services/tournament_services/sixteen_man_double_elimination_generate_loser_names.rb new file mode 100644 index 0000000..4535029 --- /dev/null +++ b/app/services/tournament_services/sixteen_man_double_elimination_generate_loser_names.rb @@ -0,0 +1,96 @@ +class SixteenManDoubleEliminationGenerateLoserNames + def initialize( tournament ) + @tournament = tournament + end + + def assign_loser_names + matches_by_weight = nil + @tournament.weights.each do |w| + matches_by_weight = @tournament.matches.where(weight_id: w.id) + conso_round_2(matches_by_weight) + conso_round_3(matches_by_weight) + conso_round_5(matches_by_weight) + fifth_sixth(matches_by_weight) + save_matches(matches_by_weight) + matches_by_weight = @tournament.matches.where(weight_id: w.id).reload + advance_bye_matches_championship(matches_by_weight) + save_matches(matches_by_weight) + end + end + + def conso_round_2(matches) + matches.select{|m| m.round == 2 and m.bracket_position == "Conso"}.sort_by{|m| m.bracket_position_number}.each do |match| + if match.bracket_position_number == 1 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 1 and m.round == 1 and m.bracket_position == "Bracket"}.first.bout_number}" + match.loser2_name = "Loser of #{matches.select{|m| m.bracket_position_number == 2 and m.round == 1 and m.bracket_position == "Bracket"}.first.bout_number}" + elsif match.bracket_position_number == 2 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 3 and m.round == 1 and m.bracket_position == "Bracket"}.first.bout_number}" + match.loser2_name = "Loser of #{matches.select{|m| m.bracket_position_number == 4 and m.round == 1 and m.bracket_position == "Bracket"}.first.bout_number}" + elsif match.bracket_position_number == 3 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 5 and m.round == 1 and m.bracket_position == "Bracket"}.first.bout_number}" + match.loser2_name = "Loser of #{matches.select{|m| m.bracket_position_number == 6 and m.round == 1 and m.bracket_position == "Bracket"}.first.bout_number}" + elsif match.bracket_position_number == 4 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 7 and m.round == 1 and m.bracket_position == "Bracket"}.first.bout_number}" + match.loser2_name = "Loser of #{matches.select{|m| m.bracket_position_number == 8 and m.round == 1 and m.bracket_position == "Bracket"}.first.bout_number}" + end + end + end + + def conso_round_3(matches) + matches.select{|m| m.round == 3 and m.bracket_position == "Conso"}.sort_by{|m| m.bracket_position_number}.each do |match| + if match.bracket_position_number == 1 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 4 and m.bracket_position == "Quarter"}.first.bout_number}" + elsif match.bracket_position_number == 2 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 3 and m.bracket_position == "Quarter"}.first.bout_number}" + elsif match.bracket_position_number == 3 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 2 and m.bracket_position == "Quarter"}.first.bout_number}" + elsif match.bracket_position_number == 4 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 1 and m.bracket_position == "Quarter"}.first.bout_number}" + end + end + end + + def conso_round_5(matches) + matches.select{|m| m.round == 5 and m.bracket_position == "Conso Semis"}.sort_by{|m| m.bracket_position_number}.each do |match| + if match.bracket_position_number == 1 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 1 and m.bracket_position == "Semis"}.first.bout_number}" + elsif match.bracket_position_number == 2 + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position_number == 2 and m.bracket_position == "Semis"}.first.bout_number}" + end + end + end + + def fifth_sixth(matches) + matches.select{|m| m.bracket_position == "5/6"}.sort_by{|m| m.bracket_position_number}.each do |match| + match.loser1_name = "Loser of #{matches.select{|m| m.bracket_position == "Conso Semis"}.first.bout_number}" + match.loser2_name = "Loser of #{matches.select{|m| m.bracket_position == "Conso Semis"}.second.bout_number}" + end + end + + def advance_bye_matches_championship(matches) + matches.select{|m| m.round == 1 and m.bracket_position == "Bracket"}.sort_by{|m| m.bracket_position_number}.each do |match| + if match.w1 == nil or match.w2 == nil + match.finished = 1 + match.win_type = "BYE" + if match.w1 != nil + match.winner_id = match.w1 + match.loser2_name = "BYE" + match.save + match.advance_wrestlers + elsif match.w2 != nil + match.winner_id = match.w2 + match.loser1_name = "BYE" + match.save + match.advance_wrestlers + end + end + end + end + + def save_matches(matches) + matches.each do |m| + m.save! + end + end + +end \ No newline at end of file diff --git a/app/services/tournament_services/sixteen_man_double_elimination_match_generation.rb b/app/services/tournament_services/sixteen_man_double_elimination_match_generation.rb new file mode 100644 index 0000000..c60b7ab --- /dev/null +++ b/app/services/tournament_services/sixteen_man_double_elimination_match_generation.rb @@ -0,0 +1,93 @@ +class SixteenManDoubleEliminationMatchGeneration + def initialize( tournament ) + @tournament = tournament + end + + def generate_matches + @tournament.weights.each do |weight| + generate_matches_for_weight(weight) + end + end + + def generate_matches_for_weight(weight) + round_one(weight) + round_two(weight) + round_three(weight) + round_four(weight) + round_five(weight) + round_six(weight) + end + + def round_one(weight) + create_matchup_from_seed(1,16, "Bracket", 1, 1,weight) + create_matchup_from_seed(8,9, "Bracket", 2, 1,weight) + create_matchup_from_seed(5,12, "Bracket", 3, 1,weight) + create_matchup_from_seed(4,14, "Bracket", 4, 1,weight) + create_matchup_from_seed(3,13, "Bracket", 5, 1,weight) + create_matchup_from_seed(6,11, "Bracket", 6, 1,weight) + create_matchup_from_seed(7,10, "Bracket", 7, 1,weight) + create_matchup_from_seed(2,15, "Bracket", 8, 1,weight) + end + + def round_two(weight) + create_matchup(nil,nil,"Quarter",1,2,weight) + create_matchup(nil,nil,"Quarter",2,2,weight) + create_matchup(nil,nil,"Quarter",3,2,weight) + create_matchup(nil,nil,"Quarter",4,2,weight) + create_matchup(nil,nil,"Conso",1,2,weight) + create_matchup(nil,nil,"Conso",2,2,weight) + create_matchup(nil,nil,"Conso",3,2,weight) + create_matchup(nil,nil,"Conso",4,2,weight) + end + + def round_three(weight) + create_matchup(nil,nil,"Conso",1,3,weight) + create_matchup(nil,nil,"Conso",2,3,weight) + create_matchup(nil,nil,"Conso",3,3,weight) + create_matchup(nil,nil,"Conso",4,3,weight) + end + + def round_four(weight) + create_matchup(nil,nil,"Semis",1,4,weight) + create_matchup(nil,nil,"Semis",2,4,weight) + create_matchup(nil,nil,"Conso Quarter",1,4,weight) + create_matchup(nil,nil,"Conso Quarter",2,4,weight) + end + + def round_five(weight) + create_matchup(nil,nil,"Conso Semis",1,5,weight) + create_matchup(nil,nil,"Conso Semis",2,5,weight) + end + + def round_six(weight) + create_matchup(nil,nil,"1/2",1,6,weight) + create_matchup(nil,nil,"3/4",1,6,weight) + create_matchup(nil,nil,"5/6",1,6,weight) + end + + def wrestler_with_seed(seed,weight) + wrestler = Wrestler.where("weight_id = ? and bracket_line = ?", weight.id, seed).first + if wrestler + return wrestler.id + else + return nil + end + end + + def create_matchup_from_seed(w1_seed, w2_seed, bracket_position, bracket_position_number,round,weight) + # if wrestler_with_seed(w1_seed,weight) and wrestler_with_seed(w2_seed,weight) + create_matchup(wrestler_with_seed(w1_seed,weight),wrestler_with_seed(w2_seed,weight), bracket_position, bracket_position_number,round,weight) + # end + end + + def create_matchup(w1, w2, bracket_position, bracket_position_number,round,weight) + @tournament.matches.create( + w1: w1, + w2: w2, + weight_id: weight.id, + round: round, + bracket_position: bracket_position, + bracket_position_number: bracket_position_number + ) + end +end \ No newline at end of file diff --git a/app/services/wrestler_services/swap_wrestlers.rb b/app/services/wrestler_services/swap_wrestlers.rb index 81ab486..25cf5d8 100644 --- a/app/services/wrestler_services/swap_wrestlers.rb +++ b/app/services/wrestler_services/swap_wrestlers.rb @@ -5,6 +5,7 @@ class SwapWrestlers def swap_wrestlers_bracket_lines(wrestler1_id,wrestler2_id) w1 = Wrestler.find(wrestler1_id) w2 = Wrestler.find(wrestler2_id) + weight_matches = w1.weight.matches #placeholder guy w3 = Wrestler.new @@ -12,34 +13,43 @@ class SwapWrestlers w3.original_seed = w1.original_seed w3.bracket_line = w1.bracket_line w3.pool = w1.pool - swapWrestlerMatches(w1.all_matches,w1.id,w3.id) + weight_matches = swapWrestlerMatches(weight_matches,w1.id,w3.id) #Swap wrestler 1 and wrestler 2 - swapWrestlerMatches(w2.all_matches,w2.id,w1.id) + weight_matches = swapWrestlerMatches(weight_matches,w2.id,w1.id) w1.bracket_line = w2.bracket_line w1.pool = w2.pool - swapWrestlerMatches(w3.all_matches,w3.id,w2.id) + weight_matches = swapWrestlerMatches(weight_matches,w3.id,w2.id) w2.bracket_line = w3.bracket_line w2.pool = w3.pool - + save_matches(weight_matches) w1.save w2.save end + + def save_matches(matches) + matches.each do |match| + match.save + end + end - def swapWrestlerMatches(matchesToSwap,w1_id,w2_id) - matchesToSwap.each do |m| - if m.bracket_position == "Pool" - if m.w1 == w1_id - m.w1 = w2_id - m.save - elsif m.w2 == w1_id - m.w2 = w2_id - m.save + def swapWrestlerMatches(matchesToSwap,from_id,to_id) + matchesToSwap.select{|m| m.w1 == from_id or m.w2 == from_id}.each do |m| + # if m.bracket_position == "Pool" or (m.bracket_position == "Bracket" and m.round == 1) + if m.w1 == from_id + m.w1 = to_id + elsif m.w2 == from_id + m.w2 = to_id end - end + if m.winner_id == from_id + m.winner_id = to_id + end + # m.save + # end end + return matchesToSwap end end diff --git a/app/views/tournaments/_bracket_partial.html.erb b/app/views/tournaments/_bracket_partial.html.erb index 8881a43..7b9d6eb 100644 --- a/app/views/tournaments/_bracket_partial.html.erb +++ b/app/views/tournaments/_bracket_partial.html.erb @@ -90,6 +90,10 @@ li:first-child,li:last-child { <%= render 'modified_sixteen_man_double_elimination_bracket' %> + <% elsif @tournament.tournament_type == "Double Elimination 1-6" %> + + <%= render 'sixteen_man_double_elimination_bracket' %> + <% end %> \ No newline at end of file diff --git a/app/views/tournaments/_sixteen_man_double_elimination_bracket.html.erb b/app/views/tournaments/_sixteen_man_double_elimination_bracket.html.erb new file mode 100644 index 0000000..207aa99 --- /dev/null +++ b/app/views/tournaments/_sixteen_man_double_elimination_bracket.html.erb @@ -0,0 +1,143 @@ +

Championship Bracket

+
+ + + + + + <% end %> +
+

Consolation Bracket

+
+ + + + + + + <% end %> +
+

5/6 place match

+
+ + + <% end %> +
\ No newline at end of file