From 98879c16b908f42d18f4a51c56e09ad5903c55bc Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sun, 24 May 2015 20:56:20 -0400 Subject: [PATCH 01/10] Make sure the max weight is defined during tests --- test/integration/poolbracket_matchups_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/integration/poolbracket_matchups_test.rb b/test/integration/poolbracket_matchups_test.rb index 86c3b4d..5f803b9 100644 --- a/test/integration/poolbracket_matchups_test.rb +++ b/test/integration/poolbracket_matchups_test.rb @@ -39,7 +39,8 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest def create_weight Weight.new( id: @id, - tournament_id: @id + tournament_id: @id, + max: @id ).save! end From 091b7c41810dfbf4efbfcd7741758fbb4f60045c Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Sun, 24 May 2015 23:04:22 -0400 Subject: [PATCH 02/10] Cut the runtime by queueing the changes in memory and deferring database writes Made some unspecified behavior explicit --- app/models/pool.rb | 3 ++- app/models/tournament.rb | 12 +++++------- app/models/tournamentmatchgen.rb | 1 + app/models/weight.rb | 16 +++++++++------- test/integration/poolbracket_matchups_test.rb | 8 +++++++- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/app/models/pool.rb b/app/models/pool.rb index cb823c8..36c26d5 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -1,6 +1,7 @@ class Pool def initialize(weight) @weight = weight + @tournament = @weight.tournament @pool = 1 end @@ -23,7 +24,7 @@ class Pool bouts = b.map bouts.each do |bout| if bout[0] != nil and bout[1] != nil - match = Match.new( + match = @tournament.matches.create( w1: bout[0].id, w2: bout[1].id, weight_id: @weight.id, diff --git a/app/models/tournament.rb b/app/models/tournament.rb index dd8c8b9..a211705 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -16,13 +16,11 @@ class Tournament < ActiveRecord::Base def createCustomWeights(value) self.weights.destroy_all if value == 'hs' - @weights = [106,113,120,132,138,145,152,160,170,182,195,220,285] - end - @weights.each do |w| - newWeight = Weight.new - newWeight.max = w - newWeight.tournament_id = self.id - newWeight.save + Weight::HS_WEIGHT_CLASSES.each do |w| + self.weights.create(max: w) + end + else + raise "Unspecified behavior" end end diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index 5285559..8e8b47f 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -42,6 +42,7 @@ class Tournamentmatchgen end def saveMatches + @tournament.save! @matches.each do |m| m.tournament_id = @tournament.id m.save diff --git a/app/models/weight.rb b/app/models/weight.rb index 3ae2cf8..bcb56f4 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -4,6 +4,8 @@ class Weight < ActiveRecord::Base attr_accessor :pools + HS_WEIGHT_CLASSES = [106,113,120,132,138,145,152,160,170,182,195,220,285] + before_save do self.tournament.destroyAllMatches end @@ -43,7 +45,7 @@ class Weight < ActiveRecord::Base end - def twoPoolNumbers(wrestlers) + def twoPoolNumbers(wrestlers) pool = 1 wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w| if w.original_seed == 1 @@ -65,8 +67,8 @@ class Weight < ActiveRecord::Base end return wrestlers end - - def fourPoolNumbers(wrestlers) + + def fourPoolNumbers(wrestlers) pool = 1 wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w| if w.original_seed == 1 @@ -98,20 +100,20 @@ class Weight < ActiveRecord::Base if self.wrestlers.size > 6 && self.wrestlers.size <= 8 return "twoPoolsToSemi" elsif self.wrestlers.size > 8 && self.wrestlers.size <= 10 - return "twoPoolsToFinal" + return "twoPoolsToFinal" elsif self.wrestlers.size == 11 || self.wrestlers.size == 12 return "fourPoolsToQuarter" elsif self.wrestlers.size > 12 && self.wrestlers.size <= 16 return "fourPoolsToSemi" end end - + def poolRounds(matches) @matchups = matches.select{|m| m.weight_id == self.id} @poolMatches = @matchups.select{|m| m.bracket_position == nil} return @poolMatches.sort_by{|m| m.round}.last.round end - + def totalRounds(matches) @matchups = matches.select{|m| m.weight_id == self.id} @lastRound = matches.sort_by{|m| m.round}.last.round @@ -125,5 +127,5 @@ class Weight < ActiveRecord::Base end return @count end - + end diff --git a/test/integration/poolbracket_matchups_test.rb b/test/integration/poolbracket_matchups_test.rb index 5f803b9..d5fec24 100644 --- a/test/integration/poolbracket_matchups_test.rb +++ b/test/integration/poolbracket_matchups_test.rb @@ -82,13 +82,19 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest refute_nil @tournament end + test "tournament can be set to high school weight classes" do + @tournament.weights.destroy_all + @tournament.createCustomWeights("hs") + assert_equal Weight::HS_WEIGHT_CLASSES.size, @tournament.weights.size + end + test "tests bout_number matches round" do @matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first assert_equal 4, @matchup_to_test.round end test "tests bout_numbers are generated with smallest weight first regardless of id" do - @weight = @tournament.weights.map.sort_by{|x|[x.max]}.first + @weight = @tournament.weights.order(:max).limit(1).first @matchup = @genMatchups.select{|m| m.bout_number == 1000}.first assert_equal @weight.max, @matchup.weight_max end From c855da0e6eb8831b5ab86629610ba80958b5b85b Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Mon, 25 May 2015 00:07:55 -0400 Subject: [PATCH 03/10] Ripped state out of Poolbracket Implicitly added tournament_id to matches generated by Poolbracket Removed explicit tournament assignment at save --- app/models/poolbracket.rb | 136 +++++++++++++++---------------- app/models/tournamentmatchgen.rb | 1 - 2 files changed, 66 insertions(+), 71 deletions(-) diff --git a/app/models/poolbracket.rb b/app/models/poolbracket.rb index 561b793..cf42bc2 100644 --- a/app/models/poolbracket.rb +++ b/app/models/poolbracket.rb @@ -1,80 +1,76 @@ class Poolbracket - def generateBracketMatches(matches,weight,highest_round) + def generateBracketMatches(matches, weight, highest_round) if weight.pool_bracket_type == "twoPoolsToSemi" - matches = twoPoolsToSemi(matches,weight,highest_round) + return twoPoolsToSemi(matches, weight, highest_round) elsif weight.pool_bracket_type == "twoPoolsToFinal" - matches = twoPoolsToFinal(matches,weight,highest_round) + return twoPoolsToFinal(matches, weight, highest_round) elsif weight.pool_bracket_type == "fourPoolsToQuarter" - matches = fourPoolsToQuarter(matches,weight,highest_round) + return fourPoolsToQuarter(matches, weight, highest_round) elsif weight.pool_bracket_type == "fourPoolsToSemi" - matches = fourPoolsToSemi(matches,weight,highest_round) + return fourPoolsToSemi(matches, weight, highest_round) end return matches end - - def twoPoolsToSemi(matches,weight,round) - @round = round + 1 - matches = createMatchup(matches,weight,@round,"Winner Pool 1","Runner Up Pool 2","Semis",1) - matches = createMatchup(matches,weight,@round,"Winner Pool 2","Runner Up Pool 1","Semis",2) - @round = @round + 1 - @matches = matches.select{|m| m.weight_id == weight.id} - matches = createMatchup(matches,weight,@round,"","","1/2",1) - matches = createMatchup(matches,weight,@round,"","","3/4",1) - return matches - end - - def twoPoolsToFinal(matches,weight,round) - @round = round + 1 - matches = createMatchup(matches,weight,@round,"Winner Pool 1","Winner Pool 2","1/2",1) - matches = createMatchup(matches,weight,@round,"Runner Up Pool 1","Runner Up Pool 2","3/4",1) - return matches - end - - def fourPoolsToQuarter(matches,weight,round) - @round = round + 1 - matches = createMatchup(matches,weight,@round,"Winner Pool 1","Runner Up Pool 2","Quarter",1) - matches = createMatchup(matches,weight,@round,"Winner Pool 4","Runner Up Pool 3","Quarter",2) - matches = createMatchup(matches,weight,@round,"Winner Pool 2","Runner Up Pool 1","Quarter",3) - matches = createMatchup(matches,weight,@round,"Winner Pool 3","Runner Up Pool 4","Quarter",4) - @round = @round + 1 - matches = createMatchup(matches,weight,@round,"","","Semis",1) - matches = createMatchup(matches,weight,@round,"","","Semis",2) - matches = createMatchup(matches,weight,@round,"","","Conso Semis",1) - matches = createMatchup(matches,weight,@round,"","","Conso Semis",2) - @round = @round + 1 - matches = createMatchup(matches,weight,@round,"","","1/2",1) - matches = createMatchup(matches,weight,@round,"","","3/4",1) - matches = createMatchup(matches,weight,@round,"","","5/6",1) - matches = createMatchup(matches,weight,@round,"","","7/8",1) - return matches - end - - def fourPoolsToSemi(matches,weight,round) - @round = round + 1 - matches = createMatchup(matches,weight,@round,"Winner Pool 1","Winner Pool 4","Semis",1) - matches = createMatchup(matches,weight,@round,"Winner Pool 2","Winner Pool 3","Semis",2) - matches = createMatchup(matches,weight,@round,"Runner Up Pool 1","Runner Up Pool 4","Conso Semis",1) - matches = createMatchup(matches,weight,@round,"Runner Up Pool 2","Runner Up Pool 3","Conso Semis",2) - @round = @round + 1 - matches = createMatchup(matches,weight,@round,"","","1/2",1) - matches = createMatchup(matches,weight,@round,"","","3/4",1) - matches = createMatchup(matches,weight,@round,"","","5/6",1) - matches = createMatchup(matches,weight,@round,"","","7/8",1) - return matches - end - - def createMatchup(matches,weight,round,w1_name,w2_name,bracket_position,bracket_position_number) - @match = Match.new - @match.loser1_name = w1_name - @match.loser2_name = w2_name - @match.weight_id = weight.id - @match.round = round - @match.bracket_position = bracket_position - @match.bracket_position_number = bracket_position_number - matches << @match - return matches - end - -end \ No newline at end of file + def twoPoolsToSemi(matches, weight, round) + round += 1 + matches = createMatchup(matches, weight, round, "Winner Pool 1", "Runner Up Pool 2", "Semis", 1) + matches = createMatchup(matches, weight, round, "Winner Pool 2", "Runner Up Pool 1", "Semis", 2) + round += 1 + matches = matches.select{|m| m.weight_id == weight.id} + matches = createMatchup(matches, weight, round,"","","1/2",1) + matches = createMatchup(matches, weight, round,"","","3/4",1) + end + + def twoPoolsToFinal(matches,weight,round) + round += 1 + matches = createMatchup(matches, weight, round, "Winner Pool 1", "Winner Pool 2", "1/2", 1) + matches = createMatchup(matches, weight, round, "Runner Up Pool 1", "Runner Up Pool 2", "3/4", 1) + end + + def fourPoolsToQuarter(matches,weight,round) + round += 1 + matches = createMatchup(matches, weight, round, "Winner Pool 1", "Runner Up Pool 2", "Quarter", 1) + matches = createMatchup(matches, weight, round, "Winner Pool 4", "Runner Up Pool 3", "Quarter", 2) + matches = createMatchup(matches, weight, round, "Winner Pool 2", "Runner Up Pool 1", "Quarter", 3) + matches = createMatchup(matches, weight, round, "Winner Pool 3", "Runner Up Pool 4", "Quarter", 4) + round += 1 + matches = createMatchup(matches, weight, round, "", "", "Semis", 1) + matches = createMatchup(matches, weight, round, "", "", "Semis", 2) + matches = createMatchup(matches, weight, round, "", "", "Conso Semis", 1) + matches = createMatchup(matches, weight, round, "", "", "Conso Semis", 2) + round += 1 + matches = createMatchup(matches, weight, round, "", "", "1/2", 1) + matches = createMatchup(matches, weight, round, "", "", "3/4", 1) + matches = createMatchup(matches, weight, round, "", "", "5/6", 1) + matches = createMatchup(matches, weight, round, "", "", "7/8", 1) + end + + def fourPoolsToSemi(matches,weight,round) + round += 1 + matches = createMatchup(matches, weight, round, "Winner Pool 1", "Winner Pool 4", "Semis", 1) + matches = createMatchup(matches, weight, round, "Winner Pool 2", "Winner Pool 3", "Semis", 2) + matches = createMatchup(matches, weight, round, "Runner Up Pool 1", "Runner Up Pool 4", "Conso Semis", 1) + matches = createMatchup(matches, weight, round, "Runner Up Pool 2", "Runner Up Pool 3", "Conso Semis", 2) + round += 1 + matches = createMatchup(matches, weight, round, "", "", "1/2", 1) + matches = createMatchup(matches, weight, round, "", "", "3/4", 1) + matches = createMatchup(matches, weight, round, "", "", "5/6", 1) + matches = createMatchup(matches, weight, round, "", "", "7/8", 1) + end + + def createMatchup(matches, weight, round, w1_name, w2_name, bracket_position, bracket_position_number) + tournament = weight.tournament + match = tournament.matches.create( + loser1_name: w1_name, + loser2_name: w2_name, + weight_id: weight.id, + round: round, + bracket_position: bracket_position, + bracket_position_number: bracket_position_number + ) + matches << match + end + +end diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index 8e8b47f..f28b107 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -44,7 +44,6 @@ class Tournamentmatchgen def saveMatches @tournament.save! @matches.each do |m| - m.tournament_id = @tournament.id m.save end end From 20ef048f48de3f6a6135c55c15707f7f1dcf11d6 Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Mon, 25 May 2015 00:45:31 -0400 Subject: [PATCH 04/10] Poolbracket does not keep a matches collection --- app/models/poolbracket.rb | 136 ++++++++++++++++--------------- app/models/tournamentmatchgen.rb | 4 +- 2 files changed, 73 insertions(+), 67 deletions(-) diff --git a/app/models/poolbracket.rb b/app/models/poolbracket.rb index cf42bc2..cdfc969 100644 --- a/app/models/poolbracket.rb +++ b/app/models/poolbracket.rb @@ -1,76 +1,80 @@ class Poolbracket - def generateBracketMatches(matches, weight, highest_round) - if weight.pool_bracket_type == "twoPoolsToSemi" - return twoPoolsToSemi(matches, weight, highest_round) - elsif weight.pool_bracket_type == "twoPoolsToFinal" - return twoPoolsToFinal(matches, weight, highest_round) - elsif weight.pool_bracket_type == "fourPoolsToQuarter" - return fourPoolsToQuarter(matches, weight, highest_round) - elsif weight.pool_bracket_type == "fourPoolsToSemi" - return fourPoolsToSemi(matches, weight, highest_round) - end - return matches - end + def initialize(weight, highest_round) + @weight = weight + @tournament = @weight.tournament + @pool_bracket_type = @weight.pool_bracket_type + @round = highest_round + 1 + end - def twoPoolsToSemi(matches, weight, round) - round += 1 - matches = createMatchup(matches, weight, round, "Winner Pool 1", "Runner Up Pool 2", "Semis", 1) - matches = createMatchup(matches, weight, round, "Winner Pool 2", "Runner Up Pool 1", "Semis", 2) - round += 1 - matches = matches.select{|m| m.weight_id == weight.id} - matches = createMatchup(matches, weight, round,"","","1/2",1) - matches = createMatchup(matches, weight, round,"","","3/4",1) - end + def next_round + @round += 1 + end - def twoPoolsToFinal(matches,weight,round) - round += 1 - matches = createMatchup(matches, weight, round, "Winner Pool 1", "Winner Pool 2", "1/2", 1) - matches = createMatchup(matches, weight, round, "Runner Up Pool 1", "Runner Up Pool 2", "3/4", 1) + def generateBracketMatches() + if @pool_bracket_type == "twoPoolsToSemi" + return twoPoolsToSemi() + elsif @pool_bracket_type == "twoPoolsToFinal" + return twoPoolsToFinal() + elsif @pool_bracket_type == "fourPoolsToQuarter" + return fourPoolsToQuarter() + elsif @pool_bracket_type == "fourPoolsToSemi" + return fourPoolsToSemi() end + return [] + end - def fourPoolsToQuarter(matches,weight,round) - round += 1 - matches = createMatchup(matches, weight, round, "Winner Pool 1", "Runner Up Pool 2", "Quarter", 1) - matches = createMatchup(matches, weight, round, "Winner Pool 4", "Runner Up Pool 3", "Quarter", 2) - matches = createMatchup(matches, weight, round, "Winner Pool 2", "Runner Up Pool 1", "Quarter", 3) - matches = createMatchup(matches, weight, round, "Winner Pool 3", "Runner Up Pool 4", "Quarter", 4) - round += 1 - matches = createMatchup(matches, weight, round, "", "", "Semis", 1) - matches = createMatchup(matches, weight, round, "", "", "Semis", 2) - matches = createMatchup(matches, weight, round, "", "", "Conso Semis", 1) - matches = createMatchup(matches, weight, round, "", "", "Conso Semis", 2) - round += 1 - matches = createMatchup(matches, weight, round, "", "", "1/2", 1) - matches = createMatchup(matches, weight, round, "", "", "3/4", 1) - matches = createMatchup(matches, weight, round, "", "", "5/6", 1) - matches = createMatchup(matches, weight, round, "", "", "7/8", 1) - end + def twoPoolsToSemi() + createMatchup("Winner Pool 1", "Runner Up Pool 2", "Semis", 1) + createMatchup("Winner Pool 2", "Runner Up Pool 1", "Semis", 2) + next_round + createMatchup("","","1/2",1) + createMatchup("","","3/4",1) + end - def fourPoolsToSemi(matches,weight,round) - round += 1 - matches = createMatchup(matches, weight, round, "Winner Pool 1", "Winner Pool 4", "Semis", 1) - matches = createMatchup(matches, weight, round, "Winner Pool 2", "Winner Pool 3", "Semis", 2) - matches = createMatchup(matches, weight, round, "Runner Up Pool 1", "Runner Up Pool 4", "Conso Semis", 1) - matches = createMatchup(matches, weight, round, "Runner Up Pool 2", "Runner Up Pool 3", "Conso Semis", 2) - round += 1 - matches = createMatchup(matches, weight, round, "", "", "1/2", 1) - matches = createMatchup(matches, weight, round, "", "", "3/4", 1) - matches = createMatchup(matches, weight, round, "", "", "5/6", 1) - matches = createMatchup(matches, weight, round, "", "", "7/8", 1) - end + def twoPoolsToFinal() + createMatchup("Winner Pool 1", "Winner Pool 2", "1/2", 1) + createMatchup("Runner Up Pool 1", "Runner Up Pool 2", "3/4", 1) + end - def createMatchup(matches, weight, round, w1_name, w2_name, bracket_position, bracket_position_number) - tournament = weight.tournament - match = tournament.matches.create( - loser1_name: w1_name, - loser2_name: w2_name, - weight_id: weight.id, - round: round, - bracket_position: bracket_position, - bracket_position_number: bracket_position_number - ) - matches << match - end + def fourPoolsToQuarter() + createMatchup("Winner Pool 1", "Runner Up Pool 2", "Quarter", 1) + createMatchup("Winner Pool 4", "Runner Up Pool 3", "Quarter", 2) + createMatchup("Winner Pool 2", "Runner Up Pool 1", "Quarter", 3) + createMatchup("Winner Pool 3", "Runner Up Pool 4", "Quarter", 4) + next_round + createMatchup("", "", "Semis", 1) + createMatchup("", "", "Semis", 2) + createMatchup("", "", "Conso Semis", 1) + createMatchup("", "", "Conso Semis", 2) + next_round + createMatchup("", "", "1/2", 1) + createMatchup("", "", "3/4", 1) + createMatchup("", "", "5/6", 1) + createMatchup("", "", "7/8", 1) + end + + def fourPoolsToSemi() + createMatchup("Winner Pool 1", "Winner Pool 4", "Semis", 1) + createMatchup("Winner Pool 2", "Winner Pool 3", "Semis", 2) + createMatchup("Runner Up Pool 1", "Runner Up Pool 4", "Conso Semis", 1) + createMatchup("Runner Up Pool 2", "Runner Up Pool 3", "Conso Semis", 2) + next_round + createMatchup("", "", "1/2", 1) + createMatchup("", "", "3/4", 1) + createMatchup("", "", "5/6", 1) + createMatchup("", "", "7/8", 1) + end + + def createMatchup(w1_name, w2_name, bracket_position, bracket_position_number) + @tournament.matches.create( + loser1_name: w1_name, + loser2_name: w2_name, + weight_id: @weight.id, + round: @round, + bracket_position: bracket_position, + bracket_position_number: bracket_position_number + ) + end end diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index f28b107..4e35bc4 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -30,8 +30,10 @@ class Tournamentmatchgen matches = Pool.new(weight).generatePools() last_match = matches.sort_by{|m| m.round}.last highest_round = last_match.round - @matches += Poolbracket.new.generateBracketMatches(matches, weight, highest_round) + Poolbracket.new(weight, highest_round).generateBracketMatches() end + @tournament.save! + @matches = @tournament.matches end def generateMatches From c04128694309990d59cb1f308c00de3f097b7056 Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Tue, 26 May 2015 15:09:48 -0400 Subject: [PATCH 05/10] rough cut of getting rid of the matches collection being passed around Associated Weight and Match-- the FK already existed in the domain Reduced argument counts on assignLoserNames and assignBouts; trying to pass a Tournament around more This was a tough nut to crack. Looking back, I tried to eat too much of the horse at once on this refactor. --- app/models/boutgen.rb | 31 +++----- app/models/losernamegen.rb | 70 ++++++++++--------- app/models/match.rb | 3 + app/models/tournamentmatchgen.rb | 30 ++------ app/models/weight.rb | 1 + test/integration/poolbracket_matchups_test.rb | 14 ++-- 6 files changed, 64 insertions(+), 85 deletions(-) diff --git a/app/models/boutgen.rb b/app/models/boutgen.rb index 12a0176..8f8c38a 100644 --- a/app/models/boutgen.rb +++ b/app/models/boutgen.rb @@ -1,27 +1,16 @@ class Boutgen - def matchesByRound(round, matches) - @matches = matches.select {|m| m.round == round} - return @matches + + def matchesByRound(tournament, round) + tournament.matches.joins(:weight).where(round: round).order("weights.max") end - def giveBout(matches) - @matches = matches.sort_by{|x|[x.weight_max]} - @matches.each_with_index do |m, i| - @bout = m.round * 1000 + i - m.bout_number = @bout + def assignBouts(tournament) + bout_counts = Hash.new(0) + matches = tournament.matches.each do |m| + m.bout_number = m.round * 1000 + bout_counts[m.round] + bout_counts[m.round] += 1 + m.save! end - return @matches end - def assignBouts(matches,weights) - @round = 1 - until matchesByRound(@round, matches).blank? do - @matches = matchesByRound(@round, matches) - giveBout(@matches) - @round += 1 - end - return matches - end - - -end \ No newline at end of file +end diff --git a/app/models/losernamegen.rb b/app/models/losernamegen.rb index fae2f90..b2de600 100644 --- a/app/models/losernamegen.rb +++ b/app/models/losernamegen.rb @@ -1,56 +1,58 @@ class Losernamegen - def assignLoserNames(matches,weights) + def assignLoserNames(tournament) + matches = nil + weights = tournament.weights weights.each do |w| - @matches = matches.select{|m| m.weight_id == w.id} + matches = tournament.matches.where(weight_id: w.id) if w.pool_bracket_type == "twoPoolsToSemi" - twoPoolsToSemiLoser(@matches) + twoPoolsToSemiLoser(matches) elsif w.pool_bracket_type == "fourPoolsToQuarter" - fourPoolsToQuarterLoser(@matches) + fourPoolsToQuarterLoser(matches) elsif w.pool_bracket_type == "fourPoolsToSemi" - fourPoolsToSemiLoser(@matches) + fourPoolsToSemiLoser(matches) end end return matches end def twoPoolsToSemiLoser(matches) - @match1 = matches.select{|m| m.loser1_name == "Winner Pool 1"}.first - @match2 = matches.select{|m| m.loser1_name == "Winner Pool 2"}.first - @matchChange = matches.select{|m| m.bracket_position == "3/4"}.first - @matchChange.loser1_name = "Loser of #{@match1.bout_number}" - @matchChange.loser2_name = "Loser of #{@match2.bout_number}" + match1 = matches.select{|m| m.loser1_name == "Winner Pool 1"}.first + match2 = matches.select{|m| m.loser1_name == "Winner Pool 2"}.first + matchChange = matches.select{|m| m.bracket_position == "3/4"}.first + matchChange.loser1_name = "Loser of #{match1.bout_number}" + matchChange.loser2_name = "Loser of #{match2.bout_number}" end def fourPoolsToQuarterLoser(matches) - @quarters = matches.select{|m| m.bracket_position == "Quarter"} - @consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} - @semis = matches.select{|m| m.bracket_position == "Semis"} - @thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first - @seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first - @consoSemis.each do |match| + quarters = matches.select{|m| m.bracket_position == "Quarter"} + consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} + semis = matches.select{|m| m.bracket_position == "Semis"} + thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first + seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first + consoSemis.each do |match| if match.bracket_position_number == 1 - match.loser1_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 1}.first.bout_number}" - match.loser2_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 2}.first.bout_number}" + match.loser1_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 1}.first.bout_number}" + match.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 2}.first.bout_number}" elsif match.bracket_position_number == 2 - match.loser1_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 3}.first.bout_number}" - match.loser2_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 4}.first.bout_number}" + match.loser1_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 3}.first.bout_number}" + match.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 4}.first.bout_number}" end end - @thirdFourth.loser1_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 1}.first.bout_number}" - @thirdFourth.loser2_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 2}.first.bout_number}" - @consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} - @seventhEighth.loser1_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}" - @seventhEighth.loser2_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}" + thirdFourth.loser1_name = "Loser of #{semis.select{|m| m.bracket_position_number == 1}.first.bout_number}" + thirdFourth.loser2_name = "Loser of #{semis.select{|m| m.bracket_position_number == 2}.first.bout_number}" + consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} + seventhEighth.loser1_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}" + seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}" end def fourPoolsToSemiLoser(matches) - @semis = matches.select{|m| m.bracket_position == "Semis"} - @thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first - @consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} - @seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first - @thirdFourth.loser1_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 1}.first.bout_number}" - @thirdFourth.loser2_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 2}.first.bout_number}" - @seventhEighth.loser1_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}" - @seventhEighth.loser2_name = "Loser of #{@consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}" + semis = matches.select{|m| m.bracket_position == "Semis"} + thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first + consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} + seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first + thirdFourth.loser1_name = "Loser of #{semis.select{|m| m.bracket_position_number == 1}.first.bout_number}" + thirdFourth.loser2_name = "Loser of #{semis.select{|m| m.bracket_position_number == 2}.first.bout_number}" + seventhEighth.loser1_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}" + seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}" end -end \ No newline at end of file +end diff --git a/app/models/match.rb b/app/models/match.rb index 7bad4a1..b5a95da 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -1,5 +1,8 @@ class Match < ActiveRecord::Base belongs_to :tournament + belongs_to :weight + + WIN_TYPES = ["Decision", "Major", "Tech Fall", "Pin", "Forfeit", "Injury Default", "Default", "DQ"] diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index 4e35bc4..182b5bf 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -2,52 +2,36 @@ class Tournamentmatchgen def initialize(tournament) @tournament = tournament - @matches = @tournament.matches end def genMatches if @tournament.tournament_type == "Pool to bracket" - @matches = poolToBracket() + poolToBracket() end - @matches + @tournament.matches end def poolToBracket destroyMatches buildTournamentWeights generateMatches - saveMatches - @matches end def destroyMatches @tournament.destroyAllMatches - @matches = [] end def buildTournamentWeights - @tournament.weights.sort_by{|x|[x.max]}.each do |weight| - matches = Pool.new(weight).generatePools() - last_match = matches.sort_by{|m| m.round}.last + @tournament.weights.order(:max).each do |weight| + Pool.new(weight).generatePools() + last_match = @tournament.matches.where(weight: weight).order(round: :desc).limit(1).first highest_round = last_match.round Poolbracket.new(weight, highest_round).generateBracketMatches() end - @tournament.save! - @matches = @tournament.matches end def generateMatches - @matches = - Losernamegen.new.assignLoserNames( - Boutgen.new.assignBouts(@matches, @tournament.weights), - @tournament.weights) + Boutgen.new.assignBouts(@tournament) + Losernamegen.new.assignLoserNames(@tournament) end - - def saveMatches - @tournament.save! - @matches.each do |m| - m.save - end - end - end diff --git a/app/models/weight.rb b/app/models/weight.rb index bcb56f4..42b3a97 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -1,6 +1,7 @@ class Weight < ActiveRecord::Base belongs_to :tournament has_many :wrestlers, dependent: :destroy + has_many :matches, dependent: :destroy attr_accessor :pools diff --git a/test/integration/poolbracket_matchups_test.rb b/test/integration/poolbracket_matchups_test.rb index d5fec24..b2a6c3f 100644 --- a/test/integration/poolbracket_matchups_test.rb +++ b/test/integration/poolbracket_matchups_test.rb @@ -3,7 +3,7 @@ require 'test_helper' class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest def setup @tournament = Tournament.find(1) - @genMatchups = @tournament.upcomingMatches + @genMatchups = @tournament.generateMatchups end def createTournament(numberOfWrestlers) @@ -88,15 +88,15 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest assert_equal Weight::HS_WEIGHT_CLASSES.size, @tournament.weights.size end - test "tests bout_number matches round" do - @matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first - assert_equal 4, @matchup_to_test.round + test "tests bout numbers correspond to round" do + matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first + assert_equal 4, matchup_to_test.round end test "tests bout_numbers are generated with smallest weight first regardless of id" do - @weight = @tournament.weights.order(:max).limit(1).first - @matchup = @genMatchups.select{|m| m.bout_number == 1000}.first - assert_equal @weight.max, @matchup.weight_max + weight = @tournament.weights.order(:max).limit(1).first + matchup = @tournament.matches.where(bout_number: 1000).limit(1).first + assert_equal weight.max, matchup.weight.max end test "tests number of matches in 5 man one pool" do From 902685ea516b8fd57688afe7e29dc39eb1f258bb Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Wed, 27 May 2015 22:14:19 -0400 Subject: [PATCH 06/10] Get us back to green on the tests. upcomingMatches doesn't (and shouldn't) change tournament state. --- test/integration/poolbracket_matchups_test.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/integration/poolbracket_matchups_test.rb b/test/integration/poolbracket_matchups_test.rb index b2a6c3f..1d0e682 100644 --- a/test/integration/poolbracket_matchups_test.rb +++ b/test/integration/poolbracket_matchups_test.rb @@ -59,7 +59,7 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest end def checkForByeInPool(tournament) - tournament.upcomingMatches + tournament.generateMatchups matchups = tournament.matches tournament.weights.each do |w| w.wrestlers.each do |wr| @@ -126,11 +126,9 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest test "test if a wrestler can exceed five matches" do - @count = 5 - until @count > 16 do - @tournament2 = createTournament(@count) - checkForByeInPool(@tournament2) - @count = @count + 1 + (5...16).each do |count| + tourney = createTournament(count) + checkForByeInPool(tourney) end end From 25c92a125d75b3421b80a7e65337a2673494eeba Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Wed, 27 May 2015 23:04:17 -0400 Subject: [PATCH 07/10] Boutgen is dead. Tournaments can assign their own bout numbers. --- app/models/boutgen.rb | 16 ---------------- app/models/tournament.rb | 13 +++++++++++++ app/models/tournamentmatchgen.rb | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) delete mode 100644 app/models/boutgen.rb diff --git a/app/models/boutgen.rb b/app/models/boutgen.rb deleted file mode 100644 index 8f8c38a..0000000 --- a/app/models/boutgen.rb +++ /dev/null @@ -1,16 +0,0 @@ -class Boutgen - - def matchesByRound(tournament, round) - tournament.matches.joins(:weight).where(round: round).order("weights.max") - end - - def assignBouts(tournament) - bout_counts = Hash.new(0) - matches = tournament.matches.each do |m| - m.bout_number = m.round * 1000 + bout_counts[m.round] - bout_counts[m.round] += 1 - m.save! - end - end - -end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index a211705..9dba39d 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -40,4 +40,17 @@ class Tournament < ActiveRecord::Base matches.destroy_all end + def matchesByRound(round) + matches.joins(:weight).where(round: round).order("weights.max") + end + + def assignBouts + bout_counts = Hash.new(0) + matches.each do |m| + m.bout_number = m.round * 1000 + bout_counts[m.round] + bout_counts[m.round] += 1 + m.save! + end + end + end diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index 182b5bf..cbbe5cb 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -31,7 +31,7 @@ class Tournamentmatchgen end def generateMatches - Boutgen.new.assignBouts(@tournament) + @tournament.assignBouts Losernamegen.new.assignLoserNames(@tournament) end end From 9b2c2dde609db5b994aadba98007c59792178bfc Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Wed, 27 May 2015 23:29:51 -0400 Subject: [PATCH 08/10] Converted class tournamentmatchgen into the GeneratesLoserNames module. Here is another way to handle functionality that you may not want bloating your ActiveRecord class, yet belongs _on_ the model. In this case, I feel there is an abstraction we are still sorely missing. --- app/models/generates_loser_names.rb | 57 ++++++++++++++++++++++++++++ app/models/losernamegen.rb | 58 ----------------------------- app/models/tournament.rb | 3 ++ app/models/tournamentmatchgen.rb | 2 +- 4 files changed, 61 insertions(+), 59 deletions(-) create mode 100644 app/models/generates_loser_names.rb delete mode 100644 app/models/losernamegen.rb diff --git a/app/models/generates_loser_names.rb b/app/models/generates_loser_names.rb new file mode 100644 index 0000000..7ac2d30 --- /dev/null +++ b/app/models/generates_loser_names.rb @@ -0,0 +1,57 @@ +module GeneratesLoserNames + def assignLoserNames + matches_by_weight = nil + weights.each do |w| + matches_by_weight = matches.where(weight_id: w.id) + if w.pool_bracket_type == "twoPoolsToSemi" + twoPoolsToSemiLoser(matches_by_weight) + elsif w.pool_bracket_type == "fourPoolsToQuarter" + fourPoolsToQuarterLoser(matches_by_weight) + elsif w.pool_bracket_type == "fourPoolsToSemi" + fourPoolsToSemiLoser(matches_by_weight) + end + end + return matches_by_weight + end + + def twoPoolsToSemiLoser(matches_by_weight) + match1 = matches_by_weight.select{|m| m.loser1_name == "Winner Pool 1"}.first + match2 = matches_by_weight.select{|m| m.loser1_name == "Winner Pool 2"}.first + matchChange = matches_by_weight.select{|m| m.bracket_position == "3/4"}.first + matchChange.loser1_name = "Loser of #{match1.bout_number}" + matchChange.loser2_name = "Loser of #{match2.bout_number}" + end + + def fourPoolsToQuarterLoser(matches_by_weight) + quarters = matches_by_weight.select{|m| m.bracket_position == "Quarter"} + consoSemis = matches_by_weight.select{|m| m.bracket_position == "Conso Semis"} + semis = matches_by_weight.select{|m| m.bracket_position == "Semis"} + thirdFourth = matches_by_weight.select{|m| m.bracket_position == "3/4"}.first + seventhEighth = matches_by_weight.select{|m| m.bracket_position == "7/8"}.first + consoSemis.each do |m| + if m.bracket_position_number == 1 + m.loser1_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 1}.first.bout_number}" + m.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 2}.first.bout_number}" + elsif m.bracket_position_number == 2 + m.loser1_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 3}.first.bout_number}" + m.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 4}.first.bout_number}" + end + end + thirdFourth.loser1_name = "Loser of #{semis.select{|m| m.bracket_position_number == 1}.first.bout_number}" + thirdFourth.loser2_name = "Loser of #{semis.select{|m| m.bracket_position_number == 2}.first.bout_number}" + consoSemis = matches_by_weight.select{|m| m.bracket_position == "Conso Semis"} + seventhEighth.loser1_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}" + seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}" + end + + def fourPoolsToSemiLoser(matches_by_weight) + semis = matches_by_weight.select{|m| m.bracket_position == "Semis"} + thirdFourth = matches_by_weight.select{|m| m.bracket_position == "3/4"}.first + consoSemis = matches_by_weight.select{|m| m.bracket_position == "Conso Semis"} + seventhEighth = matches_by_weight.select{|m| m.bracket_position == "7/8"}.first + thirdFourth.loser1_name = "Loser of #{semis.select{|m| m.bracket_position_number == 1}.first.bout_number}" + thirdFourth.loser2_name = "Loser of #{semis.select{|m| m.bracket_position_number == 2}.first.bout_number}" + seventhEighth.loser1_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}" + seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}" + end +end diff --git a/app/models/losernamegen.rb b/app/models/losernamegen.rb deleted file mode 100644 index b2de600..0000000 --- a/app/models/losernamegen.rb +++ /dev/null @@ -1,58 +0,0 @@ -class Losernamegen - def assignLoserNames(tournament) - matches = nil - weights = tournament.weights - weights.each do |w| - matches = tournament.matches.where(weight_id: w.id) - if w.pool_bracket_type == "twoPoolsToSemi" - twoPoolsToSemiLoser(matches) - elsif w.pool_bracket_type == "fourPoolsToQuarter" - fourPoolsToQuarterLoser(matches) - elsif w.pool_bracket_type == "fourPoolsToSemi" - fourPoolsToSemiLoser(matches) - end - end - return matches - end - - def twoPoolsToSemiLoser(matches) - match1 = matches.select{|m| m.loser1_name == "Winner Pool 1"}.first - match2 = matches.select{|m| m.loser1_name == "Winner Pool 2"}.first - matchChange = matches.select{|m| m.bracket_position == "3/4"}.first - matchChange.loser1_name = "Loser of #{match1.bout_number}" - matchChange.loser2_name = "Loser of #{match2.bout_number}" - end - - def fourPoolsToQuarterLoser(matches) - quarters = matches.select{|m| m.bracket_position == "Quarter"} - consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} - semis = matches.select{|m| m.bracket_position == "Semis"} - thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first - seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first - consoSemis.each do |match| - if match.bracket_position_number == 1 - match.loser1_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 1}.first.bout_number}" - match.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 2}.first.bout_number}" - elsif match.bracket_position_number == 2 - match.loser1_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 3}.first.bout_number}" - match.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 4}.first.bout_number}" - end - end - thirdFourth.loser1_name = "Loser of #{semis.select{|m| m.bracket_position_number == 1}.first.bout_number}" - thirdFourth.loser2_name = "Loser of #{semis.select{|m| m.bracket_position_number == 2}.first.bout_number}" - consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} - seventhEighth.loser1_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}" - seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}" - end - - def fourPoolsToSemiLoser(matches) - semis = matches.select{|m| m.bracket_position == "Semis"} - thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first - consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} - seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first - thirdFourth.loser1_name = "Loser of #{semis.select{|m| m.bracket_position_number == 1}.first.bout_number}" - thirdFourth.loser2_name = "Loser of #{semis.select{|m| m.bracket_position_number == 2}.first.bout_number}" - seventhEighth.loser1_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 1}.first.bout_number}" - seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}" - end -end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 9dba39d..b4b5c43 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,4 +1,7 @@ class Tournament < ActiveRecord::Base + + include GeneratesLoserNames + has_many :schools, dependent: :destroy has_many :weights, dependent: :destroy has_many :mats, dependent: :destroy diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb index cbbe5cb..b706778 100644 --- a/app/models/tournamentmatchgen.rb +++ b/app/models/tournamentmatchgen.rb @@ -32,6 +32,6 @@ class Tournamentmatchgen def generateMatches @tournament.assignBouts - Losernamegen.new.assignLoserNames(@tournament) + @tournament.assignLoserNames end end From 062396da0afc9633f45f3802c36bf2305741c8da Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Wed, 27 May 2015 23:57:37 -0400 Subject: [PATCH 09/10] A Tournament can now generate its own matches. This was again extracted as a module. Modules get more powerful as they get more generic. This is far from generic. --- app/models/generates_tournament_matches.rb | 28 ++++++++++++++ app/models/tournament.rb | 23 +++--------- app/models/tournamentmatchgen.rb | 37 ------------------- test/integration/poolbracket_matchups_test.rb | 1 - 4 files changed, 34 insertions(+), 55 deletions(-) create mode 100644 app/models/generates_tournament_matches.rb delete mode 100644 app/models/tournamentmatchgen.rb diff --git a/app/models/generates_tournament_matches.rb b/app/models/generates_tournament_matches.rb new file mode 100644 index 0000000..752292b --- /dev/null +++ b/app/models/generates_tournament_matches.rb @@ -0,0 +1,28 @@ +module GeneratesTournamentMatches + + def generateMatchups + poolToBracket() if tournament_type == "Pool to bracket" + matches + end + + def poolToBracket + destroyAllMatches + buildTournamentWeights + generateMatches + end + + def buildTournamentWeights + weights.order(:max).each do |weight| + Pool.new(weight).generatePools() + last_match = matches.where(weight: weight).order(round: :desc).limit(1).first + highest_round = last_match.round + Poolbracket.new(weight, highest_round).generateBracketMatches() + end + end + + def generateMatches + assignBouts + assignLoserNames + end + +end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index b4b5c43..5d935dd 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,26 +1,23 @@ class Tournament < ActiveRecord::Base - include GeneratesLoserNames + include GeneratesLoserNames + include GeneratesTournamentMatches has_many :schools, dependent: :destroy has_many :weights, dependent: :destroy has_many :mats, dependent: :destroy has_many :wrestlers, through: :weights - + has_many :matches, dependent: :destroy def tournament_types ["Pool to bracket"] end - def matches - @matches = Match.where(tournament_id: self.id) - end - - def createCustomWeights(value) - self.weights.destroy_all + def createCustomWeights(value) + weights.destroy_all if value == 'hs' Weight::HS_WEIGHT_CLASSES.each do |w| - self.weights.create(max: w) + weights.create(max: w) end else raise "Unspecified behavior" @@ -28,15 +25,7 @@ class Tournament < ActiveRecord::Base end def upcomingMatches - if matches.nil? - return nil - else matches - end - end - - def generateMatchups - @matches = Tournamentmatchgen.new(self).genMatches() end def destroyAllMatches diff --git a/app/models/tournamentmatchgen.rb b/app/models/tournamentmatchgen.rb deleted file mode 100644 index b706778..0000000 --- a/app/models/tournamentmatchgen.rb +++ /dev/null @@ -1,37 +0,0 @@ -class Tournamentmatchgen - - def initialize(tournament) - @tournament = tournament - end - - def genMatches - if @tournament.tournament_type == "Pool to bracket" - poolToBracket() - end - @tournament.matches - end - - def poolToBracket - destroyMatches - buildTournamentWeights - generateMatches - end - - def destroyMatches - @tournament.destroyAllMatches - end - - def buildTournamentWeights - @tournament.weights.order(:max).each do |weight| - Pool.new(weight).generatePools() - last_match = @tournament.matches.where(weight: weight).order(round: :desc).limit(1).first - highest_round = last_match.round - Poolbracket.new(weight, highest_round).generateBracketMatches() - end - end - - def generateMatches - @tournament.assignBouts - @tournament.assignLoserNames - end -end diff --git a/test/integration/poolbracket_matchups_test.rb b/test/integration/poolbracket_matchups_test.rb index 1d0e682..37db1c7 100644 --- a/test/integration/poolbracket_matchups_test.rb +++ b/test/integration/poolbracket_matchups_test.rb @@ -124,7 +124,6 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest assert_equal 32, @twentysix_matches.length end - test "test if a wrestler can exceed five matches" do (5...16).each do |count| tourney = createTournament(count) From 284439e2cfcc2a79f727cb23a6874cdf3b024773 Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Thu, 28 May 2015 00:25:14 -0400 Subject: [PATCH 10/10] Removed upcomingMatches because matches are an A-R relation of Tournament --- app/controllers/static_pages_controller.rb | 17 ++++++++--------- app/models/tournament.rb | 4 ---- app/views/static_pages/all_brackets.html.erb | 8 ++++---- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 2b3d3da..77d8e7b 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -8,10 +8,9 @@ class StaticPagesController < ApplicationController @tournament = Tournament.find(params[:tournament]) end if @tournament - if @tournament.matches.empty? + @matches = @tournament.matches + if @matches.empty? redirect_to "/static_pages/noMatches?tournament=#{@tournament.id}" - else - @matches = @tournament.upcomingMatches end end end @@ -30,12 +29,12 @@ class StaticPagesController < ApplicationController if params[:tournament] @tournament = Tournament.find(params[:tournament]) end - if @tournament - @matches = Match.where(tournament_id: @tournament.id) - end + if @tournament + @matches = @tournament.matches + end @matches = @matches.where(finished: 1) - end + def brackets if params[:weight] @weight = Weight.find(params[:weight]) @@ -50,7 +49,7 @@ class StaticPagesController < ApplicationController end end end - + def all_brackets if params[:tournament] @tournament = Tournament.find(params[:tournament]) @@ -85,7 +84,7 @@ class StaticPagesController < ApplicationController @tournament = Tournament.find(params[:tournament]) end end - + def generate_matches if !user_signed_in? redirect_to root_path diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 5d935dd..a103274 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -24,10 +24,6 @@ class Tournament < ActiveRecord::Base end end - def upcomingMatches - matches - end - def destroyAllMatches matches.destroy_all end diff --git a/app/views/static_pages/all_brackets.html.erb b/app/views/static_pages/all_brackets.html.erb index 8e712e6..57c013e 100644 --- a/app/views/static_pages/all_brackets.html.erb +++ b/app/views/static_pages/all_brackets.html.erb @@ -1,5 +1,5 @@ @@ -26,11 +26,11 @@ <% @tournament.weights.sort_by{|w| w.max}.each do |w| %>
<% @weight = w %> - <% @matches = @tournament.upcomingMatches.select{|m| m.weight_id == @weight.id} %> + <% @matches = @tournament.matches.select{|m| m.weight_id == @weight.id} %> <% @wrestlers = Wrestler.where(weight_id: @weight.id) %> <% @pools = w.poolRounds(@matches) %>
<%= @weight.max %> lbs Bracket
- + <%= render 'pool' %>
@@ -48,4 +48,4 @@ <% end %>
<% end %> - \ No newline at end of file +