mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-24 17:04:43 +00:00
Reload last_match and wrestler in advanced_wrestler. Moved calculate team score to the end of advance_wrestler.
This commit is contained in:
@@ -2,11 +2,6 @@ class AdvanceWrestlerJob < ApplicationJob
|
|||||||
queue_as :default
|
queue_as :default
|
||||||
|
|
||||||
def perform(wrestler, match)
|
def perform(wrestler, match)
|
||||||
# Add a small delay to increase chance of transaction commit
|
|
||||||
# without this some matches were getting a deserialization error when running the rake task
|
|
||||||
# to finish tournaments
|
|
||||||
sleep(0.5) unless Rails.env.test?
|
|
||||||
|
|
||||||
# Get tournament from wrestler
|
# Get tournament from wrestler
|
||||||
tournament = wrestler.tournament
|
tournament = wrestler.tournament
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,17 @@ class Match < ApplicationRecord
|
|||||||
# Callback to update finished_at when a match is finished
|
# Callback to update finished_at when a match is finished
|
||||||
before_save :update_finished_at
|
before_save :update_finished_at
|
||||||
|
|
||||||
after_update :after_finished_actions, if: -> {
|
# Enqueue advancement and related actions after the DB transaction has committed.
|
||||||
saved_change_to_finished? ||
|
# Using after_commit ensures any background jobs enqueued inside these callbacks
|
||||||
saved_change_to_winner_id? ||
|
# will see the committed state of the match (e.g. finished == 1). Enqueuing
|
||||||
saved_change_to_win_type? ||
|
# jobs from after_update can cause jobs to run before the transaction commits,
|
||||||
saved_change_to_score? ||
|
# which leads to jobs observing stale data and not performing advancement.
|
||||||
saved_change_to_overtime_type?
|
after_commit :after_finished_actions, on: :update, if: -> {
|
||||||
|
saved_change_to_finished? ||
|
||||||
|
saved_change_to_winner_id? ||
|
||||||
|
saved_change_to_win_type? ||
|
||||||
|
saved_change_to_score? ||
|
||||||
|
saved_change_to_overtime_type?
|
||||||
}
|
}
|
||||||
|
|
||||||
def after_finished_actions
|
def after_finished_actions
|
||||||
@@ -30,7 +35,8 @@ class Match < ApplicationRecord
|
|||||||
self.mat.assign_next_match
|
self.mat.assign_next_match
|
||||||
end
|
end
|
||||||
advance_wrestlers
|
advance_wrestlers
|
||||||
calculate_school_points
|
# School point calculation has move to the end of advance wrestler
|
||||||
|
# calculate_school_points
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -12,12 +12,14 @@ class AdvanceWrestler
|
|||||||
end
|
end
|
||||||
|
|
||||||
def advance_raw
|
def advance_raw
|
||||||
|
@last_match.reload
|
||||||
|
@wrestler.reload
|
||||||
if @last_match && @last_match.finished?
|
if @last_match && @last_match.finished?
|
||||||
pool_to_bracket_advancement if @tournament.tournament_type == "Pool to bracket"
|
pool_to_bracket_advancement if @tournament.tournament_type == "Pool to bracket"
|
||||||
ModifiedDoubleEliminationAdvance.new(@wrestler, @last_match).bracket_advancement if @tournament.tournament_type.include? "Modified 16 Man Double Elimination"
|
ModifiedDoubleEliminationAdvance.new(@wrestler, @last_match).bracket_advancement if @tournament.tournament_type.include? "Modified 16 Man Double Elimination"
|
||||||
DoubleEliminationAdvance.new(@wrestler, @last_match).bracket_advancement if @tournament.tournament_type.include? "Regular Double Elimination"
|
DoubleEliminationAdvance.new(@wrestler, @last_match).bracket_advancement if @tournament.tournament_type.include? "Regular Double Elimination"
|
||||||
end
|
end
|
||||||
|
@wrestler.school.calculate_score
|
||||||
end
|
end
|
||||||
|
|
||||||
def pool_to_bracket_advancement
|
def pool_to_bracket_advancement
|
||||||
|
|||||||
@@ -31,14 +31,14 @@ namespace :tournament do
|
|||||||
while @tournament.reload.curently_generating_matches == 1
|
while @tournament.reload.curently_generating_matches == 1
|
||||||
puts "Waiting for tournament to finish generating matches..."
|
puts "Waiting for tournament to finish generating matches..."
|
||||||
sleep(5)
|
sleep(5)
|
||||||
|
@tournament.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
@tournament.reload # Ensure matches association is fresh before iterating
|
@tournament.reload # Ensure matches association is fresh before iterating
|
||||||
@tournament.matches.sort_by(&:bout_number).each do |match|
|
@tournament.matches.sort_by(&:bout_number).each do |match|
|
||||||
match.reload
|
if match.reload.loser1_name != "BYE" and match.reload.loser2_name != "BYE" && match.reload.finished != 1
|
||||||
if match.loser1_name != "BYE" and match.loser2_name != "BYE"
|
|
||||||
# Wait until both wrestlers are assigned
|
# Wait until both wrestlers are assigned
|
||||||
while match.w1.nil? || match.w2.nil?
|
while (match.w1.nil? || match.w2.nil?)
|
||||||
puts "Waiting for wrestlers in match #{match.bout_number}..."
|
puts "Waiting for wrestlers in match #{match.bout_number}..."
|
||||||
sleep(5) # Wait for 5 seconds before checking again
|
sleep(5) # Wait for 5 seconds before checking again
|
||||||
match.reload
|
match.reload
|
||||||
@@ -77,6 +77,8 @@ namespace :tournament do
|
|||||||
# Mark match as finished
|
# Mark match as finished
|
||||||
match.finished = 1
|
match.finished = 1
|
||||||
match.save!
|
match.save!
|
||||||
|
# sleep to prevent mysql locks when assign_next_match to a mat runs
|
||||||
|
sleep(0.5)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user