1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-04-25 15:31:39 +00:00

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.
This commit is contained in:
RJ Osborne
2015-05-26 15:09:48 -04:00
parent 20ef048f48
commit c041286943
6 changed files with 64 additions and 85 deletions

View File

@@ -1,27 +1,16 @@
class Boutgen class Boutgen
def matchesByRound(round, matches)
@matches = matches.select {|m| m.round == round} def matchesByRound(tournament, round)
return @matches tournament.matches.joins(:weight).where(round: round).order("weights.max")
end end
def giveBout(matches) def assignBouts(tournament)
@matches = matches.sort_by{|x|[x.weight_max]} bout_counts = Hash.new(0)
@matches.each_with_index do |m, i| matches = tournament.matches.each do |m|
@bout = m.round * 1000 + i m.bout_number = m.round * 1000 + bout_counts[m.round]
m.bout_number = @bout bout_counts[m.round] += 1
m.save!
end end
return @matches
end end
def assignBouts(matches,weights) end
@round = 1
until matchesByRound(@round, matches).blank? do
@matches = matchesByRound(@round, matches)
giveBout(@matches)
@round += 1
end
return matches
end
end

View File

@@ -1,56 +1,58 @@
class Losernamegen class Losernamegen
def assignLoserNames(matches,weights) def assignLoserNames(tournament)
matches = nil
weights = tournament.weights
weights.each do |w| 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" if w.pool_bracket_type == "twoPoolsToSemi"
twoPoolsToSemiLoser(@matches) twoPoolsToSemiLoser(matches)
elsif w.pool_bracket_type == "fourPoolsToQuarter" elsif w.pool_bracket_type == "fourPoolsToQuarter"
fourPoolsToQuarterLoser(@matches) fourPoolsToQuarterLoser(matches)
elsif w.pool_bracket_type == "fourPoolsToSemi" elsif w.pool_bracket_type == "fourPoolsToSemi"
fourPoolsToSemiLoser(@matches) fourPoolsToSemiLoser(matches)
end end
end end
return matches return matches
end end
def twoPoolsToSemiLoser(matches) def twoPoolsToSemiLoser(matches)
@match1 = matches.select{|m| m.loser1_name == "Winner Pool 1"}.first match1 = matches.select{|m| m.loser1_name == "Winner Pool 1"}.first
@match2 = matches.select{|m| m.loser1_name == "Winner Pool 2"}.first match2 = matches.select{|m| m.loser1_name == "Winner Pool 2"}.first
@matchChange = matches.select{|m| m.bracket_position == "3/4"}.first matchChange = matches.select{|m| m.bracket_position == "3/4"}.first
@matchChange.loser1_name = "Loser of #{@match1.bout_number}" matchChange.loser1_name = "Loser of #{match1.bout_number}"
@matchChange.loser2_name = "Loser of #{@match2.bout_number}" matchChange.loser2_name = "Loser of #{match2.bout_number}"
end end
def fourPoolsToQuarterLoser(matches) def fourPoolsToQuarterLoser(matches)
@quarters = matches.select{|m| m.bracket_position == "Quarter"} quarters = matches.select{|m| m.bracket_position == "Quarter"}
@consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"}
@semis = matches.select{|m| m.bracket_position == "Semis"} semis = matches.select{|m| m.bracket_position == "Semis"}
@thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first
@seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first
@consoSemis.each do |match| consoSemis.each do |match|
if match.bracket_position_number == 1 if match.bracket_position_number == 1
match.loser1_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 1}.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}" match.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 2}.first.bout_number}"
elsif match.bracket_position_number == 2 elsif match.bracket_position_number == 2
match.loser1_name = "Loser of #{@quarters.select{|m| m.bracket_position_number == 3}.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}" match.loser2_name = "Loser of #{quarters.select{|m| m.bracket_position_number == 4}.first.bout_number}"
end end
end end
@thirdFourth.loser1_name = "Loser of #{@semis.select{|m| m.bracket_position_number == 1}.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}" 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"} 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.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}" seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
end end
def fourPoolsToSemiLoser(matches) def fourPoolsToSemiLoser(matches)
@semis = matches.select{|m| m.bracket_position == "Semis"} semis = matches.select{|m| m.bracket_position == "Semis"}
@thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first thirdFourth = matches.select{|m| m.bracket_position == "3/4"}.first
@consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"} consoSemis = matches.select{|m| m.bracket_position == "Conso Semis"}
@seventhEighth = matches.select{|m| m.bracket_position == "7/8"}.first 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.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}" 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.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}" seventhEighth.loser2_name = "Loser of #{consoSemis.select{|m| m.bracket_position_number == 2}.first.bout_number}"
end end
end end

View File

@@ -1,5 +1,8 @@
class Match < ActiveRecord::Base class Match < ActiveRecord::Base
belongs_to :tournament belongs_to :tournament
belongs_to :weight
WIN_TYPES = ["Decision", "Major", "Tech Fall", "Pin", "Forfeit", "Injury Default", "Default", "DQ"] WIN_TYPES = ["Decision", "Major", "Tech Fall", "Pin", "Forfeit", "Injury Default", "Default", "DQ"]

View File

@@ -2,52 +2,36 @@ class Tournamentmatchgen
def initialize(tournament) def initialize(tournament)
@tournament = tournament @tournament = tournament
@matches = @tournament.matches
end end
def genMatches def genMatches
if @tournament.tournament_type == "Pool to bracket" if @tournament.tournament_type == "Pool to bracket"
@matches = poolToBracket() poolToBracket()
end end
@matches @tournament.matches
end end
def poolToBracket def poolToBracket
destroyMatches destroyMatches
buildTournamentWeights buildTournamentWeights
generateMatches generateMatches
saveMatches
@matches
end end
def destroyMatches def destroyMatches
@tournament.destroyAllMatches @tournament.destroyAllMatches
@matches = []
end end
def buildTournamentWeights def buildTournamentWeights
@tournament.weights.sort_by{|x|[x.max]}.each do |weight| @tournament.weights.order(:max).each do |weight|
matches = Pool.new(weight).generatePools() Pool.new(weight).generatePools()
last_match = matches.sort_by{|m| m.round}.last last_match = @tournament.matches.where(weight: weight).order(round: :desc).limit(1).first
highest_round = last_match.round highest_round = last_match.round
Poolbracket.new(weight, highest_round).generateBracketMatches() Poolbracket.new(weight, highest_round).generateBracketMatches()
end end
@tournament.save!
@matches = @tournament.matches
end end
def generateMatches def generateMatches
@matches = Boutgen.new.assignBouts(@tournament)
Losernamegen.new.assignLoserNames( Losernamegen.new.assignLoserNames(@tournament)
Boutgen.new.assignBouts(@matches, @tournament.weights),
@tournament.weights)
end end
def saveMatches
@tournament.save!
@matches.each do |m|
m.save
end
end
end end

View File

@@ -1,6 +1,7 @@
class Weight < ActiveRecord::Base class Weight < ActiveRecord::Base
belongs_to :tournament belongs_to :tournament
has_many :wrestlers, dependent: :destroy has_many :wrestlers, dependent: :destroy
has_many :matches, dependent: :destroy
attr_accessor :pools attr_accessor :pools

View File

@@ -3,7 +3,7 @@ require 'test_helper'
class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
def setup def setup
@tournament = Tournament.find(1) @tournament = Tournament.find(1)
@genMatchups = @tournament.upcomingMatches @genMatchups = @tournament.generateMatchups
end end
def createTournament(numberOfWrestlers) def createTournament(numberOfWrestlers)
@@ -88,15 +88,15 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
assert_equal Weight::HS_WEIGHT_CLASSES.size, @tournament.weights.size assert_equal Weight::HS_WEIGHT_CLASSES.size, @tournament.weights.size
end end
test "tests bout_number matches round" do test "tests bout numbers correspond to round" do
@matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first
assert_equal 4, @matchup_to_test.round assert_equal 4, matchup_to_test.round
end end
test "tests bout_numbers are generated with smallest weight first regardless of id" do test "tests bout_numbers are generated with smallest weight first regardless of id" do
@weight = @tournament.weights.order(:max).limit(1).first weight = @tournament.weights.order(:max).limit(1).first
@matchup = @genMatchups.select{|m| m.bout_number == 1000}.first matchup = @tournament.matches.where(bout_number: 1000).limit(1).first
assert_equal @weight.max, @matchup.weight_max assert_equal weight.max, matchup.weight.max
end end
test "tests number of matches in 5 man one pool" do test "tests number of matches in 5 man one pool" do