mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-06 22:44:14 +00:00
Moved none database classes out of model folder. Still need to separate classes that are doing too much for bracket_advancement, for placement points, and for swapping wrestlers in pool/bracket.
This commit is contained in:
75
app/services/bracket_advancement/pool_advance.rb
Normal file
75
app/services/bracket_advancement/pool_advance.rb
Normal file
@@ -0,0 +1,75 @@
|
||||
class PoolAdvance
|
||||
|
||||
def initialize(wrestler,previousMatch)
|
||||
@wrestler = wrestler
|
||||
@previousMatch = previousMatch
|
||||
end
|
||||
|
||||
def advanceWrestler
|
||||
if @wrestler.weight.allPoolMatchesFinished(@wrestler.generatePoolNumber) && @wrestler.finishedBracketMatches.size == 0
|
||||
poolToBracketAdvancment
|
||||
end
|
||||
if @wrestler.finishedBracketMatches.size > 0
|
||||
bracketAdvancment
|
||||
end
|
||||
end
|
||||
|
||||
def poolToBracketAdvancment
|
||||
pool = @wrestler.generatePoolNumber
|
||||
if @wrestler.weight.wrestlers.size > 6
|
||||
poolOrder = @wrestler.weight.poolOrder(pool)
|
||||
#Take pool order and move winner and runner up to correct match based on w1_name and w2_name
|
||||
matches = @wrestler.weight.matches
|
||||
winnerMatch = matches.select{|m| m.loser1_name == "Winner Pool #{pool}" || m.loser2_name == "Winner Pool #{pool}"}.first
|
||||
runnerUpMatch = matches.select{|m| m.loser1_name == "Runner Up Pool #{pool}" || m.loser2_name == "Runner Up Pool #{pool}"}.first
|
||||
winner = poolOrder.first
|
||||
runnerUp = poolOrder.second
|
||||
runnerUpMatch.replaceLoserNameWithWrestler(runnerUp,"Runner Up Pool #{pool}")
|
||||
winnerMatch.replaceLoserNameWithWrestler(winner,"Winner Pool #{pool}")
|
||||
end
|
||||
end
|
||||
|
||||
def bracketAdvancment
|
||||
if @previousMatch.winner_id == @wrestler.id
|
||||
winnerAdvance
|
||||
end
|
||||
if @previousMatch.winner_id != @wrestler.id
|
||||
loserAdvance
|
||||
end
|
||||
end
|
||||
|
||||
def winnerAdvance
|
||||
if @wrestler.lastMatch.bracket_position == "Quarter"
|
||||
new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","Semis",@wrestler.nextMatchPositionNumber.ceil,@wrestler.weight_id).first
|
||||
updateNewMatch(new_match)
|
||||
end
|
||||
if @wrestler.lastMatch.bracket_position == "Semis"
|
||||
new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","1/2",@wrestler.nextMatchPositionNumber.ceil,@wrestler.weight_id).first
|
||||
updateNewMatch(new_match)
|
||||
end
|
||||
if @wrestler.lastMatch.bracket_position == "Conso Semis"
|
||||
new_match = Match.where("bracket_position = ? AND bracket_position_number = ? AND weight_id = ?","5/6",@wrestler.nextMatchPositionNumber.ceil,@wrestler.weight_id).first
|
||||
updateNewMatch(new_match)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def updateNewMatch(match)
|
||||
if @wrestler.nextMatchPositionNumber == @wrestler.nextMatchPositionNumber.ceil
|
||||
match.w2 = @wrestler.id
|
||||
match.save
|
||||
end
|
||||
if @wrestler.nextMatchPositionNumber != @wrestler.nextMatchPositionNumber.ceil
|
||||
match.w1 = @wrestler.id
|
||||
match.save
|
||||
end
|
||||
end
|
||||
|
||||
def loserAdvance
|
||||
bout = @wrestler.lastMatch.bout_number
|
||||
next_match = Match.where("loser1_name = ? OR loser2_name = ? AND weight_id = ?","Loser of #{bout}","Loser of #{bout}",@wrestler.weight_id)
|
||||
if next_match.size > 0
|
||||
next_match.first.replaceLoserNameWithWrestler(@wrestler,"Loser of #{bout}")
|
||||
end
|
||||
end
|
||||
end
|
||||
192
app/services/bracket_advancement/pool_order.rb
Normal file
192
app/services/bracket_advancement/pool_order.rb
Normal file
@@ -0,0 +1,192 @@
|
||||
class PoolOrder
|
||||
def initialize(wrestlers)
|
||||
@wrestlers = wrestlers
|
||||
end
|
||||
|
||||
def getPoolOrder
|
||||
setOriginalPoints
|
||||
while checkForTieBreakers == true
|
||||
breakTie
|
||||
end
|
||||
@wrestlers.sort_by{|w| w.poolAdvancePoints}.reverse!
|
||||
end
|
||||
|
||||
def setOriginalPoints
|
||||
@wrestlers.each do |w|
|
||||
w.poolAdvancePoints = w.poolWins.size
|
||||
end
|
||||
end
|
||||
|
||||
def checkForTieBreakers
|
||||
if wrestlersWithSamePoints.size > 1
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
def wrestlersWithSamePoints
|
||||
@wrestlers.each do |w|
|
||||
wrestlersWithSamePoints = @wrestlers.select{|wr| wr.poolAdvancePoints == w.poolAdvancePoints}
|
||||
if wrestlersWithSamePoints.size > 0
|
||||
return wrestlersWithSamePoints
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize)
|
||||
if wrestlersWithSamePoints.size == originalTieSize
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
def breakTie
|
||||
originalTieSize = wrestlersWithSamePoints.size
|
||||
ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { deductedPoints }
|
||||
if originalTieSize == 2
|
||||
ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { headToHead }
|
||||
end
|
||||
ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { teamPoints }
|
||||
ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { mostFalls }
|
||||
ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { mostTechs }
|
||||
ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { mostMajors }
|
||||
ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { mostDecisionPointsScored }
|
||||
ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { fastestPin }
|
||||
ifWrestlersWithSamePointsIsSameAsOriginal(originalTieSize) { coinFlip }
|
||||
end
|
||||
|
||||
|
||||
def headToHead
|
||||
wrestlersWithSamePoints.each do |wr|
|
||||
otherWrestler = wrestlersWithSamePoints.select{|w| w.id != wr.id}.first
|
||||
if wr.matchAgainst(otherWrestler).first.winner_id == wr.id
|
||||
addPointsToWrestlersAhead(wr)
|
||||
addPoints(wr)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def addPoints(wrestler)
|
||||
wrestler.poolAdvancePoints = wrestler.poolAdvancePoints + 1
|
||||
end
|
||||
|
||||
def addPointsToWrestlersAhead(wrestler)
|
||||
wrestlersAhead = @wrestlers.select{|w| w.poolAdvancePoints > wrestler.poolAdvancePoints}
|
||||
wrestlersAhead.each do |wr|
|
||||
wr.poolAdvancePoints = wr.poolAdvancePoints + 1
|
||||
end
|
||||
end
|
||||
|
||||
def deductedPoints
|
||||
pointsArray = []
|
||||
wrestlersWithSamePoints.each do |w|
|
||||
pointsArray << w.totalDeductedPoints
|
||||
end
|
||||
leastPoints = pointsArray.min
|
||||
wrestlersWithLeastDeductedPoints = wrestlersWithSamePoints.select{|w| w.totalDeductedPoints == leastPoints}
|
||||
addPointsToWrestlersAhead(wrestlersWithLeastDeductedPoints.first)
|
||||
wrestlersWithLeastDeductedPoints.each do |wr|
|
||||
addPoints(wr)
|
||||
end
|
||||
end
|
||||
|
||||
def mostDecisionPointsScored
|
||||
pointsArray = []
|
||||
wrestlersWithSamePoints.each do |w|
|
||||
pointsArray << w.decisionPointsScored
|
||||
end
|
||||
mostPoints = pointsArray.max
|
||||
wrestlersWithMostPoints = wrestlersWithSamePoints.select{|w| w.decisionPointsScored == mostPoints}
|
||||
addPointsToWrestlersAhead(wrestlersWithMostPoints.first)
|
||||
wrestlersWithMostPoints.each do |wr|
|
||||
addPoints(wr)
|
||||
end
|
||||
secondPoints = pointsArray.sort[-2]
|
||||
wrestlersWithSecondMostPoints = wrestlersWithSamePoints.select{|w| w.decisionPointsScored == secondPoints}
|
||||
addPointsToWrestlersAhead(wrestlersWithSecondMostPoints.first)
|
||||
wrestlersWithSecondMostPoints.each do |wr|
|
||||
addPoints(wr)
|
||||
end
|
||||
end
|
||||
|
||||
def fastestPin
|
||||
wrestlersWithSamePointsWithPins = []
|
||||
wrestlersWithSamePoints.each do |wr|
|
||||
if wr.pinWins.size > 0
|
||||
wrestlersWithSamePointsWithPins << wr
|
||||
end
|
||||
end
|
||||
if wrestlersWithSamePointsWithPins.size > 0
|
||||
fastest = wrestlersWithSamePointsWithPins.sort_by{|w| w.fastestPin.pinTime}.first.fastestPin
|
||||
secondFastest = wrestlersWithSamePointsWithPins.sort_by{|w| w.fastestPin.pinTime}.second.fastestPin
|
||||
wrestlersWithFastestPin = wrestlersWithSamePointsWithPins.select{|w| w.fastestPin.pinTime == fastest.pinTime}
|
||||
addPointsToWrestlersAhead(wrestlersWithFastestPin.first)
|
||||
wrestlersWithFastestPin.each do |wr|
|
||||
addPoints(wr)
|
||||
end
|
||||
|
||||
wrestlersWithSecondFastestPin = wrestlersWithSamePointsWithPins.select{|w| w.fastestPin.pinTime == secondFastest.pinTime}
|
||||
addPointsToWrestlersAhead(wrestlersWithSecondFastestPin.first)
|
||||
wrestlersWithSecondFastestPin.each do |wr|
|
||||
addPoints(wr)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def teamPoints
|
||||
pointsArray = []
|
||||
wrestlersWithSamePoints.each do |w|
|
||||
pointsArray << w.teamPointsEarned
|
||||
end
|
||||
mostPoints = pointsArray.max
|
||||
wrestlersWithLeastDeductedPoints = wrestlersWithSamePoints.select{|w| w.teamPointsEarned == mostPoints}
|
||||
addPointsToWrestlersAhead(wrestlersWithLeastDeductedPoints.first)
|
||||
wrestlersWithLeastDeductedPoints.each do |wr|
|
||||
addPoints(wr)
|
||||
end
|
||||
end
|
||||
|
||||
def mostFalls
|
||||
pointsArray = []
|
||||
wrestlersWithSamePoints.each do |w|
|
||||
pointsArray << w.pinWins.size
|
||||
end
|
||||
mostPoints = pointsArray.max
|
||||
wrestlersWithLeastDeductedPoints = wrestlersWithSamePoints.select{|w| w.pinWins.size == mostPoints}
|
||||
addPointsToWrestlersAhead(wrestlersWithLeastDeductedPoints.first)
|
||||
wrestlersWithLeastDeductedPoints.each do |wr|
|
||||
addPoints(wr)
|
||||
end
|
||||
end
|
||||
|
||||
def mostTechs
|
||||
pointsArray = []
|
||||
wrestlersWithSamePoints.each do |w|
|
||||
pointsArray << w.techWins.size
|
||||
end
|
||||
mostPoints = pointsArray.max
|
||||
wrestlersWithLeastDeductedPoints = wrestlersWithSamePoints.select{|w| w.techWins.size == mostPoints}
|
||||
addPointsToWrestlersAhead(wrestlersWithLeastDeductedPoints.first)
|
||||
wrestlersWithLeastDeductedPoints.each do |wr|
|
||||
addPoints(wr)
|
||||
end
|
||||
end
|
||||
|
||||
def mostMajors
|
||||
pointsArray = []
|
||||
wrestlersWithSamePoints.each do |w|
|
||||
pointsArray << w.majorWins.size
|
||||
end
|
||||
mostPoints = pointsArray.max
|
||||
wrestlersWithLeastDeductedPoints = wrestlersWithSamePoints.select{|w| w.majorWins.size == mostPoints}
|
||||
addPointsToWrestlersAhead(wrestlersWithLeastDeductedPoints.first)
|
||||
wrestlersWithLeastDeductedPoints.each do |wr|
|
||||
addPoints(wr)
|
||||
end
|
||||
end
|
||||
|
||||
def coinFlip
|
||||
wrestler = wrestlersWithSamePoints.sample
|
||||
addPointsToWrestlersAhead(wrestler)
|
||||
addPoints(wrestler)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user