mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-31 19:45:45 +00:00
Refactored match generation so I can eventually support multiple types of tournaments
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
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
|
||||
saveMatches(matches_by_weight)
|
||||
end
|
||||
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
|
||||
|
||||
def saveMatches(matches)
|
||||
matches.each do |m|
|
||||
m.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,60 +0,0 @@
|
||||
module GeneratesTournamentMatches
|
||||
|
||||
def generateMatchups
|
||||
self.curently_generating_matches = 1
|
||||
self.save
|
||||
resetSchoolScores
|
||||
setSeedsAndRandomSeedingWrestlersWithoutSeeds
|
||||
poolToBracket() if tournament_type == "Pool to bracket"
|
||||
self.curently_generating_matches = nil
|
||||
self.save
|
||||
end
|
||||
if Rails.env.production?
|
||||
handle_asynchronously :generateMatchups
|
||||
end
|
||||
|
||||
def poolToBracket
|
||||
destroyAllMatches
|
||||
generatePoolToBracketMatches
|
||||
poolToBracketPostMatchCreation
|
||||
end
|
||||
|
||||
def generatePoolToBracketMatches
|
||||
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 poolToBracketPostMatchCreation
|
||||
moveFinalsMatchesToLastRound
|
||||
assignBouts
|
||||
assignLoserNames
|
||||
assignFirstMatchesToMats
|
||||
movePoolSeedsToFinalPoolRound
|
||||
end
|
||||
|
||||
def moveFinalsMatchesToLastRound
|
||||
finalsRound = self.totalRounds
|
||||
finalsMatches = self.matches.select{|m| m.bracket_position == "1/2" || m.bracket_position == "3/4" || m.bracket_position == "5/6" || m.bracket_position == "7/8"}
|
||||
finalsMatches. each do |m|
|
||||
m.round = finalsRound
|
||||
m.save
|
||||
end
|
||||
end
|
||||
|
||||
def setSeedsAndRandomSeedingWrestlersWithoutSeeds
|
||||
weights.each do |w|
|
||||
w.setSeeds
|
||||
end
|
||||
end
|
||||
|
||||
def movePoolSeedsToFinalPoolRound
|
||||
self.weights.each do |w|
|
||||
w.setOriginalSeedsToWrestleLastPoolRound
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,34 +0,0 @@
|
||||
class Pool
|
||||
def initialize(weight)
|
||||
@weight = weight
|
||||
@tournament = @weight.tournament
|
||||
@pool = 1
|
||||
end
|
||||
|
||||
def generatePools
|
||||
pools = @weight.pools
|
||||
while @pool <= pools
|
||||
roundRobin
|
||||
@pool += 1
|
||||
end
|
||||
end
|
||||
|
||||
def roundRobin
|
||||
wrestlers = @weight.wrestlersForPool(@pool)
|
||||
poolMatches = RoundRobinTournament.schedule(wrestlers).reverse
|
||||
poolMatches.each_with_index do |b, index|
|
||||
round = index + 1
|
||||
bouts = b.map
|
||||
bouts.each do |bout|
|
||||
if bout[0] != nil and bout[1] != nil
|
||||
@tournament.matches.create(
|
||||
w1: bout[0].id,
|
||||
w2: bout[1].id,
|
||||
weight_id: @weight.id,
|
||||
bracket_position: "Pool",
|
||||
round: round)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,80 +0,0 @@
|
||||
class PoolBracket
|
||||
|
||||
def initialize(weight, highest_round)
|
||||
@weight = weight
|
||||
@tournament = @weight.tournament
|
||||
@pool_bracket_type = @weight.pool_bracket_type
|
||||
@round = highest_round + 1
|
||||
end
|
||||
|
||||
def next_round
|
||||
@round += 1
|
||||
end
|
||||
|
||||
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 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 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 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
|
||||
@@ -1,7 +1,5 @@
|
||||
class Tournament < ActiveRecord::Base
|
||||
|
||||
include GeneratesLoserNames
|
||||
include GeneratesTournamentMatches
|
||||
belongs_to :user
|
||||
has_many :schools, dependent: :destroy
|
||||
has_many :weights, dependent: :destroy
|
||||
@@ -24,10 +22,6 @@ class Tournament < ActiveRecord::Base
|
||||
time
|
||||
end
|
||||
|
||||
def resetSchoolScores
|
||||
schools.update_all("score = 0.0")
|
||||
end
|
||||
|
||||
def tournament_types
|
||||
["Pool to bracket"]
|
||||
end
|
||||
@@ -50,19 +44,6 @@ class Tournament < ActiveRecord::Base
|
||||
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
|
||||
|
||||
def assignFirstMatchesToMats
|
||||
assignMats(mats)
|
||||
end
|
||||
|
||||
def totalRounds
|
||||
self.matches.sort_by{|m| m.round}.last.round
|
||||
|
||||
@@ -46,22 +46,6 @@ class Weight < ActiveRecord::Base
|
||||
wrestlersForPool(pool).sort_by{|w| [w.original_seed ? 0 : 1, w.original_seed || 0]}
|
||||
end
|
||||
|
||||
def setOriginalSeedsToWrestleLastPoolRound
|
||||
pool = 1
|
||||
until pool > self.pools
|
||||
wrestler1 = poolSeedOrder(pool).first
|
||||
wrestler2 = poolSeedOrder(pool).second
|
||||
match = wrestler1.poolMatches.sort_by{|m| m.round}.last
|
||||
if match.w1 != wrestler2.id or match.w2 != wrestler2.id
|
||||
if match.w1 == wrestler1.id
|
||||
swapWrestlers(match.w2,wrestler2.id)
|
||||
elsif match.w2 == wrestler1.id
|
||||
swapWrestlers(match.w1,wrestler2.id)
|
||||
end
|
||||
end
|
||||
pool += 1
|
||||
end
|
||||
end
|
||||
|
||||
def swapWrestlers(wrestler1_id,wrestler2_id)
|
||||
SwapWrestlers.new.swapWrestlers(wrestler1_id,wrestler2_id)
|
||||
@@ -180,38 +164,5 @@ class Weight < ActiveRecord::Base
|
||||
def poolOrder(pool)
|
||||
PoolOrder.new(wrestlersForPool(pool)).getPoolOrder
|
||||
end
|
||||
|
||||
def randomSeeding
|
||||
wrestlerWithSeeds = self.wrestlers.select{|w| w.original_seed != nil }.sort_by{|w| w.original_seed}
|
||||
if wrestlerWithSeeds.size > 0
|
||||
highestSeed = wrestlerWithSeeds.last.seed
|
||||
seed = highestSeed + 1
|
||||
else
|
||||
seed = 1
|
||||
end
|
||||
wrestlersWithoutSeed = self.wrestlers.select{|w| w.original_seed == nil }
|
||||
wrestlersWithoutSeed.shuffle.each do |w|
|
||||
w.seed = seed
|
||||
w.save
|
||||
seed += 1
|
||||
end
|
||||
end
|
||||
|
||||
def setSeeds
|
||||
resetAllSeeds
|
||||
wrestlerWithSeeds = self.wrestlers.select{|w| w.original_seed != nil }
|
||||
wrestlerWithSeeds.each do |w|
|
||||
w.seed = w.original_seed
|
||||
w.save
|
||||
end
|
||||
randomSeeding
|
||||
end
|
||||
|
||||
def resetAllSeeds
|
||||
self.wrestlers.each do |w|
|
||||
w.seed = nil
|
||||
w.save
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user