1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-25 01:14:43 +00:00

Refactored match generation so I can eventually support multiple types of tournaments

This commit is contained in:
2016-05-14 13:28:28 +00:00
parent a763c966d1
commit 2f9c54df2e
14 changed files with 189 additions and 144 deletions

View File

@@ -0,0 +1,61 @@
class GenerateTournamentMatches
def initialize( tournament )
@tournament = tournament
end
def generate
standardStartingActions
PoolToBracketMatchGeneration.new(@tournament).generatePoolToBracketMatches if @tournament.tournament_type == "Pool to bracket"
postMatchCreationActions
PoolToBracketMatchGeneration.new(@tournament).assignLoserNames if @tournament.tournament_type == "Pool to bracket"
end
def standardStartingActions
@tournament.curently_generating_matches = 1
@tournament.save
WipeTournamentMatches.new(@tournament).setUpMatchGeneration
TournamentSeeding.new(@tournament).setSeeds
end
def postMatchCreationActions
moveFinalsMatchesToLastRound
assignBouts
assignFirstMatchesToMats
@tournament.curently_generating_matches = nil
@tournament.save
end
def assignBouts
bout_counts = Hash.new(0)
@tournament.matches.each do |m|
m.bout_number = m.round * 1000 + bout_counts[m.round]
bout_counts[m.round] += 1
m.save!
end
end
def moveFinalsMatchesToLastRound
finalsRound = @tournament.totalRounds
finalsMatches = @tournament.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 assignFirstMatchesToMats
matsToAssign = @tournament.mats
if matsToAssign.count > 0
until matsToAssign.sort_by{|m| m.id}.last.matches.count == 4
matsToAssign.sort_by{|m| m.id}.each do |m|
m.assignNextMatch
end
end
end
end
end

View File

@@ -0,0 +1,80 @@
class PoolBracketGeneration
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

View File

@@ -0,0 +1,34 @@
class PoolGeneration
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

View File

@@ -0,0 +1,68 @@
class PoolToBracketGenerateLoserNames
def initialize( tournament )
@tournament = tournament
end
def assignLoserNames
matches_by_weight = nil
@tournament.weights.each do |w|
matches_by_weight = @tournament.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

View File

@@ -0,0 +1,46 @@
class PoolToBracketMatchGeneration
def initialize( tournament )
@tournament = tournament
end
def generatePoolToBracketMatches
@tournament.weights.order(:max).each do |weight|
PoolGeneration.new(weight).generatePools()
last_match = @tournament.matches.where(weight: weight).order(round: :desc).limit(1).first
highest_round = last_match.round
PoolBracketGeneration.new(weight, highest_round).generateBracketMatches()
end
movePoolSeedsToFinalPoolRound
end
def movePoolSeedsToFinalPoolRound
@tournament.weights.each do |w|
setOriginalSeedsToWrestleLastPoolRound(w)
end
end
def setOriginalSeedsToWrestleLastPoolRound(weight)
pool = 1
until pool > weight.pools
wrestler1 = weight.poolSeedOrder(pool).first
wrestler2 = weight.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.new.swapWrestlers(match.w2,wrestler2.id)
elsif match.w2 == wrestler1.id
SwapWrestlers.new.swapWrestlers(match.w1,wrestler2.id)
end
end
pool += 1
end
end
def assignLoserNames
PoolToBracketGenerateLoserNames.new(@tournament).assignLoserNames
end
end

View File

@@ -0,0 +1,44 @@
class TournamentSeeding
def initialize( tournament )
@tournament = tournament
end
def setSeeds
@tournament.weights.each do |w|
resetAllSeeds(w)
setOriginalSeeds(w)
randomSeeding(w)
end
end
def randomSeeding(weight)
wrestlerWithSeeds = weight.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 = weight.wrestlers.select{|w| w.original_seed == nil }
wrestlersWithoutSeed.shuffle.each do |w|
w.seed = seed
w.save
seed += 1
end
end
def setOriginalSeeds(weight)
wrestlerWithSeeds = weight.wrestlers.select{|w| w.original_seed != nil }
wrestlerWithSeeds.each do |w|
w.seed = w.original_seed
w.save
end
end
def resetAllSeeds(weight)
weight.wrestlers.each do |w|
w.seed = nil
w.save
end
end
end

View File

@@ -0,0 +1,19 @@
class WipeTournamentMatches
def initialize( tournament )
@tournament = tournament
end
def setUpMatchGeneration
wipeMatches
resetSchoolScores
end
def wipeMatches
@tournament.matches.destroy_all
end
def resetSchoolScores
@tournament.schools.update_all("score = 0.0")
end
end