mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +00:00
Added mat assignment rules for the bout board and fixed the bug where a delegate making the tournamnet info public changes them to the owner
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
class Mat < ApplicationRecord
|
||||
belongs_to :tournament
|
||||
has_many :matches
|
||||
has_many :mat_assignment_rules, dependent: :destroy
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
@@ -21,11 +22,11 @@ class Mat < ApplicationRecord
|
||||
end
|
||||
|
||||
def assign_next_match
|
||||
t_matches = tournament.matches.select{|m| m.mat_id == nil && m.finished != 1 && m.bout_number != nil}.sort_by{|m| m.bout_number}
|
||||
if t_matches.size > 0 and self.unfinished_matches.size < 4
|
||||
match = t_matches.sort_by{|m| m.bout_number}.first
|
||||
match = next_eligible_match
|
||||
if match
|
||||
match.mat_id = self.id
|
||||
if match.save
|
||||
# Invalidate any wrestler caches
|
||||
if match.w1
|
||||
match.wrestler1.touch
|
||||
match.wrestler1.school.touch
|
||||
@@ -43,6 +44,31 @@ class Mat < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def next_eligible_match
|
||||
# Start with all matches that are either unfinished (nil or 0), have a bout number, and are ordered by bout_number
|
||||
filtered_matches = tournament.matches.where(finished: [nil, 0]).where(mat_id: nil).where.not(bout_number: nil).order(:bout_number)
|
||||
|
||||
# Sequentially apply each rule, narrowing down the matches
|
||||
mat_assignment_rules.each do |rule|
|
||||
if rule.weight_classes.any?
|
||||
filtered_matches = filtered_matches.where(weight_id: rule.weight_classes)
|
||||
end
|
||||
|
||||
if rule.bracket_positions.any?
|
||||
filtered_matches = filtered_matches.where(bracket_position: rule.bracket_positions)
|
||||
end
|
||||
|
||||
if rule.rounds.any?
|
||||
filtered_matches = filtered_matches.where(round: rule.rounds)
|
||||
end
|
||||
end
|
||||
|
||||
# Return the first match in filtered results, or nil if none are left
|
||||
result = filtered_matches.first
|
||||
result
|
||||
end
|
||||
|
||||
|
||||
def unfinished_matches
|
||||
matches.select{|m| m.finished != 1}.sort_by{|m| m.bout_number}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user