1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-04-04 21:53:47 +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:
2024-11-25 16:25:59 -05:00
parent bb548be81b
commit ad8e486205
19 changed files with 769 additions and 43 deletions

View File

@@ -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

View File

@@ -0,0 +1,4 @@
class MatAssignmentRule < ApplicationRecord
belongs_to :mat
belongs_to :tournament
end

View File

@@ -7,6 +7,7 @@ class Tournament < ApplicationRecord
has_many :wrestlers, through: :weights
has_many :matches, dependent: :destroy
has_many :delegates, class_name: "TournamentDelegate"
has_many :mat_assignment_rules, dependent: :destroy
validates :date, :name, :tournament_type, :address, :director, :director_email , presence: true
@@ -17,25 +18,25 @@ class Tournament < ApplicationRecord
end
def self.search_date_name(pattern)
if pattern.blank? # blank? covers both nil and empty string
all
else
search_functions = []
search_variables = []
search_terms = pattern.split(' ').map{|word| "%#{word.downcase}%"}
search_terms.each do |word|
search_functions << '(LOWER(name) LIKE ? or LOWER(date) LIKE ?)'
# add twice for both ?'s in the function above
search_variables << word
search_variables << word
end
like_patterns = search_functions.join(' and ')
# puts "where(#{like_patterns})"
# puts *search_variables
# example: (LOWER(name LIKE ? or LOWER(date) LIKE ?) and (LOWER(name) LIKE ? or LOWER(date) LIKE ?), %test%, %test%, %2016%, %2016%
where("#{like_patterns}", *search_variables)
end
end
if pattern.blank? # blank? covers both nil and empty string
all
else
search_functions = []
search_variables = []
search_terms = pattern.split(' ').map{|word| "%#{word.downcase}%"}
search_terms.each do |word|
search_functions << '(LOWER(name) LIKE ? or LOWER(date) LIKE ?)'
# add twice for both ?'s in the function above
search_variables << word
search_variables << word
end
like_patterns = search_functions.join(' and ')
# puts "where(#{like_patterns})"
# puts *search_variables
# example: (LOWER(name LIKE ? or LOWER(date) LIKE ?) and (LOWER(name) LIKE ? or LOWER(date) LIKE ?), %test%, %test%, %2016%, %2016%
where("#{like_patterns}", *search_variables)
end
end
def days_until_start
time = (Date.today - self.date).to_i
@@ -188,4 +189,23 @@ end
nil
end
end
def reset_and_fill_bout_board
reset_mats
if mats.any?
4.times do
# Iterate over each mat and assign the next available match
mats.each do |mat|
match_assigned = mat.assign_next_match
# If no more matches are available, exit early
unless match_assigned
puts "No more eligible matches to assign."
return
end
end
end
end
end
end