diff --git a/app/services/tournament_services/double_elimination_generate_loser_names.rb b/app/services/tournament_services/double_elimination_generate_loser_names.rb new file mode 100644 index 0000000..a8a7330 --- /dev/null +++ b/app/services/tournament_services/double_elimination_generate_loser_names.rb @@ -0,0 +1,13 @@ +class DoubleEliminationGenerateLoserNames + def initialize( tournament ) + @tournament = tournament + end + + def assign_loser_names + @tournament.weights.each do |weight| + SixteenManDoubleEliminationGenerateLoserNames.new(weight).assign_loser_names_for_weight if weight.wrestlers.size >= 9 and weight.wrestlers.size <= 16 + EightManDoubleEliminationGenerateLoserNames.new(weight).assign_loser_names_for_weight if weight.wrestlers.size >= 4 and weight.wrestlers.size <= 8 + end + end + +end \ No newline at end of file diff --git a/app/services/tournament_services/double_elimination_match_generation.rb b/app/services/tournament_services/double_elimination_match_generation.rb new file mode 100644 index 0000000..64c65c1 --- /dev/null +++ b/app/services/tournament_services/double_elimination_match_generation.rb @@ -0,0 +1,12 @@ +class DoubleEliminationMatchGeneration + def initialize( tournament ) + @tournament = tournament + end + + def generate_matches + @tournament.weights.each do |weight| + SixteenManDoubleEliminationMatchGeneration.new(weight).generate_matches_for_weight if weight.wrestlers.size >= 9 and weight.wrestlers.size <= 16 + EightManDoubleEliminationMatchGeneration.new(weight).generate_matches_for_weight if weight.wrestlers.size >= 4 and weight.wrestlers.size <= 8 + end + end +end \ No newline at end of file diff --git a/app/services/tournament_services/eight_man_double_elimination_generate_loser_names.rb b/app/services/tournament_services/eight_man_double_elimination_generate_loser_names.rb new file mode 100644 index 0000000..f7331ac --- /dev/null +++ b/app/services/tournament_services/eight_man_double_elimination_generate_loser_names.rb @@ -0,0 +1,73 @@ +class EightManDoubleEliminationGenerateLoserNames + def initialize( weight ) + @weight = weight + end + + def assign_loser_names_for_weight + matches_by_weight = nil + matches_by_weight = @weight.matches + conso_round_2(matches_by_weight) + conso_round_4(matches_by_weight) + fifth_sixth(matches_by_weight) + save_matches(matches_by_weight) + matches_by_weight = @weight.matches.reload + advance_bye_matches_championship(matches_by_weight) + save_matches(matches_by_weight) + end + + def conso_round_2(matches) + matches.select{|m| m.bracket_position == "Conso Quarter"}.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 == "Quarter"}.first.bout_number}" + match.loser2_name = "Loser of #{matches.select{|m| m.bracket_position_number == 2 and m.round == 1 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.round == 1 and m.bracket_position == "Quarter"}.first.bout_number}" + match.loser2_name = "Loser of #{matches.select{|m| m.bracket_position_number == 4 and m.round == 1 and m.bracket_position == "Quarter"}.first.bout_number}" + end + end + end + + def conso_round_4(matches) + matches.select{|m| 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 == "Quarter"}.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/eight_man_double_elimination_match_generation.rb b/app/services/tournament_services/eight_man_double_elimination_match_generation.rb new file mode 100644 index 0000000..d39ed66 --- /dev/null +++ b/app/services/tournament_services/eight_man_double_elimination_match_generation.rb @@ -0,0 +1,63 @@ +class EightManDoubleEliminationMatchGeneration + def initialize( weight ) + @weight = weight + end + + def generate_matches_for_weight + round_one(@weight) + round_two(@weight) + round_three(@weight) + round_four(@weight) + end + + def round_one(weight) + create_matchup_from_seed(1,8,"Quarter",1,1,weight) + create_matchup_from_seed(4,5,"Quarter",2,1,weight) + create_matchup_from_seed(3,6,"Quarter",3,1,weight) + create_matchup_from_seed(2,7,"Quarter",4,1,weight) + end + + def round_two(weight) + create_matchup(nil,nil,"Semis",1,2,weight) + create_matchup(nil,nil,"Semis",2,2,weight) + create_matchup(nil,nil,"Conso Quarter",1,2,weight) + create_matchup(nil,nil,"Conso Quarter",2,2,weight) + end + + def round_three(weight) + create_matchup(nil,nil,"Conso Semis",1,3,weight) + create_matchup(nil,nil,"Conso Semis",2,3,weight) + end + + def round_four(weight) + create_matchup(nil,nil,"1/2",1,4,weight) + create_matchup(nil,nil,"3/4",1,4,weight) + create_matchup(nil,nil,"5/6",1,4,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) + @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/tournament_services/generate_tournament_matches.rb b/app/services/tournament_services/generate_tournament_matches.rb index 5776cc5..4dc151b 100644 --- a/app/services/tournament_services/generate_tournament_matches.rb +++ b/app/services/tournament_services/generate_tournament_matches.rb @@ -34,11 +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" + DoubleEliminationMatchGeneration.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" + DoubleEliminationGenerateLoserNames.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 index 4535029..0a877f8 100644 --- 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 @@ -1,21 +1,19 @@ class SixteenManDoubleEliminationGenerateLoserNames - def initialize( tournament ) - @tournament = tournament + def initialize( weight ) + @weight = weight end - def assign_loser_names + def assign_loser_names_for_weight 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 + matches_by_weight = @weight.matches + 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 = @weight.matches.reload + advance_bye_matches_championship(matches_by_weight) + save_matches(matches_by_weight) end def conso_round_2(matches) 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 index c60b7ab..6715cfe 100644 --- a/app/services/tournament_services/sixteen_man_double_elimination_match_generation.rb +++ b/app/services/tournament_services/sixteen_man_double_elimination_match_generation.rb @@ -1,21 +1,15 @@ class SixteenManDoubleEliminationMatchGeneration - def initialize( tournament ) - @tournament = tournament + def initialize( weight ) + @weight = weight 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) + def generate_matches_for_weight + round_one(@weight) + round_two(@weight) + round_three(@weight) + round_four(@weight) + round_five(@weight) + round_six(@weight) end def round_one(weight) @@ -81,7 +75,7 @@ class SixteenManDoubleEliminationMatchGeneration end def create_matchup(w1, w2, bracket_position, bracket_position_number,round,weight) - @tournament.matches.create( + @weight.tournament.matches.create( w1: w1, w2: w2, weight_id: weight.id, diff --git a/app/views/tournaments/_bracket_partial.html.erb b/app/views/tournaments/_bracket_partial.html.erb index 7b9d6eb..6436acf 100644 --- a/app/views/tournaments/_bracket_partial.html.erb +++ b/app/views/tournaments/_bracket_partial.html.erb @@ -92,7 +92,7 @@ li:first-child,li:last-child { <% elsif @tournament.tournament_type == "Double Elimination 1-6" %> - <%= render 'sixteen_man_double_elimination_bracket' %> + <%= render 'double_elimination_bracket' %> <% end %> diff --git a/app/views/tournaments/_double_elimination_bracket.html.erb b/app/views/tournaments/_double_elimination_bracket.html.erb new file mode 100644 index 0000000..289e82c --- /dev/null +++ b/app/views/tournaments/_double_elimination_bracket.html.erb @@ -0,0 +1,5 @@ +<% if @weight.wrestlers.size <= 16 and @weight.wrestlers.size >= 9 %> + <%= render 'sixteen_man_double_elimination_bracket' %> +<% elsif @weight.wrestlers.size >= 4 and @weight.wrestlers.size <= 8 %> + <%= render 'eight_man_double_elimination_bracket' %> + <% end %> \ No newline at end of file diff --git a/app/views/tournaments/_eight_man_double_elimination_bracket.html.erb b/app/views/tournaments/_eight_man_double_elimination_bracket.html.erb new file mode 100644 index 0000000..ece22e8 --- /dev/null +++ b/app/views/tournaments/_eight_man_double_elimination_bracket.html.erb @@ -0,0 +1,109 @@ +

Championship Bracket

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

Consolation Bracket

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

5/6 place match

+
+ + + <% 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 index 207aa99..849f6c0 100644 --- a/app/views/tournaments/_sixteen_man_double_elimination_bracket.html.erb +++ b/app/views/tournaments/_sixteen_man_double_elimination_bracket.html.erb @@ -120,7 +120,7 @@

5/6 place match

-