mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-06 06:28:33 +00:00
Added head to head pool winner and tests
This commit is contained in:
46
app/models/poolorder.rb
Normal file
46
app/models/poolorder.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
class Poolorder
|
||||
def initialize(wrestlers)
|
||||
@wrestlers = wrestlers
|
||||
end
|
||||
attr_accessor :wrestlersWithSamePoints
|
||||
|
||||
def getPoolOrder
|
||||
setOriginalPoints
|
||||
until checkForTieBreakers == false
|
||||
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
|
||||
@wrestlers.each do |w|
|
||||
self.wrestlersWithSamePoints = @wrestlers.select{|wr| wr.poolAdvancePoints == w.poolAdvancePoints}
|
||||
if self.wrestlersWithSamePoints.size > 1
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
def breakTie
|
||||
headToHead
|
||||
end
|
||||
|
||||
|
||||
def headToHead
|
||||
self.wrestlersWithSamePoints.each do |wr|
|
||||
@otherWrestlers = self.wrestlersWithSamePoints.select{|w| w.id != wr.id}
|
||||
@otherWrestlers.each do |ow|
|
||||
if wr.matchAgainst(ow).first.winner_id == wr.id
|
||||
wr.poolAdvancePoints = wr.poolAdvancePoints + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -139,7 +139,6 @@ class Weight < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def poolOrder(pool)
|
||||
@wrestlers = wrestlersForPool(pool)
|
||||
@wrestlers.sort_by{|w| w.poolWins.size}.reverse!
|
||||
Poolorder.new(wrestlersForPool(pool)).getPoolOrder
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ class Wrestler < ActiveRecord::Base
|
||||
belongs_to :weight
|
||||
has_one :tournament, through: :weight
|
||||
has_many :matches, through: :weight
|
||||
attr_accessor :poolNumber
|
||||
attr_accessor :poolNumber, :poolAdvancePoints
|
||||
|
||||
before_save do
|
||||
self.tournament.destroyAllMatches
|
||||
@@ -22,6 +22,9 @@ class Wrestler < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def matchAgainst(opponent)
|
||||
allMatches.select{|m| m.w1 == opponent.id or m.w2 == opponent.id}
|
||||
end
|
||||
|
||||
def isWrestlingThisRound(matchRound)
|
||||
if allMatches.blank?
|
||||
|
||||
Reference in New Issue
Block a user