From c04128694309990d59cb1f308c00de3f097b7056 Mon Sep 17 00:00:00 2001 From: RJ Osborne Date: Tue, 26 May 2015 15:09:48 -0400 Subject: [PATCH] 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