mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-16 21:11:38 +00:00
Re assign bouts to mats if mat is destroyed or created
This commit is contained in:
@@ -2,10 +2,26 @@ class Mat < ActiveRecord::Base
|
|||||||
belongs_to :tournament
|
belongs_to :tournament
|
||||||
has_many :matches
|
has_many :matches
|
||||||
|
|
||||||
|
before_destroy do
|
||||||
|
if tournament.matches.size > 0
|
||||||
|
tournament.resetMats
|
||||||
|
matsToAssign = tournament.mats.select{|m| m.id != self.id}
|
||||||
|
tournament.assignMats(matsToAssign)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
after_create do
|
||||||
|
if tournament.matches.size > 0
|
||||||
|
tournament.resetMats
|
||||||
|
matsToAssign = tournament.mats
|
||||||
|
tournament.assignMats(matsToAssign)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def assignNextMatch
|
def assignNextMatch
|
||||||
t_matches = tournament.matches.where(mat_id: nil)
|
t_matches = tournament.matches.select{|m| m.mat_id == nil}
|
||||||
if t_matches.size > 0
|
if t_matches.size > 0
|
||||||
match = t_matches.order(:bout_number).first
|
match = t_matches.sort_by{|m| m.bout_number}.first
|
||||||
match.mat_id = self.id
|
match.mat_id = self.id
|
||||||
match.save
|
match.save
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ class Tournament < ActiveRecord::Base
|
|||||||
has_many :wrestlers, through: :weights
|
has_many :wrestlers, through: :weights
|
||||||
has_many :matches, dependent: :destroy
|
has_many :matches, dependent: :destroy
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def tournament_types
|
def tournament_types
|
||||||
["Pool to bracket"]
|
["Pool to bracket"]
|
||||||
end
|
end
|
||||||
@@ -42,14 +44,29 @@ class Tournament < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def assignFirstMatchesToMats
|
def assignFirstMatchesToMats
|
||||||
until mats.order(:id).last.matches.count == 4
|
assignMats(mats)
|
||||||
mats.order(:id).each do |m|
|
|
||||||
m.assignNextMatch
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def totalRounds
|
def totalRounds
|
||||||
self.matches.sort_by{|m| m.round}.last.round
|
self.matches.sort_by{|m| m.round}.last.round
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def assignMats(matsToAssign)
|
||||||
|
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
|
||||||
|
|
||||||
|
def resetMats
|
||||||
|
matchesToReset = matches.select{|m| m.finished != 1 && m.mat_id != nil}
|
||||||
|
# matchesToReset.update_all( {:mat_id => nil } )
|
||||||
|
matchesToReset.each do |m|
|
||||||
|
m.mat_id = nil
|
||||||
|
m.save
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user