fix: correct Elo calculator winner validation logic
The Elo calculator was incorrectly rejecting valid scores (10-7) because it checked if team score >= 10 without verifying the opposing team's score. Changes: - Update winner determination to check both score >= 10 AND score > opponent - Add detailed error message with actual scores for debugging - Ensure only one team can win (not both reaching 10 points)
This commit is contained in:
@@ -1,28 +1,14 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "good_job"
|
||||||
|
|
||||||
module EuchreCamp
|
module EuchreCamp
|
||||||
module Jobs
|
module Jobs
|
||||||
class CalculateEloJob
|
class CalculateEloJob < ActiveJob::Base
|
||||||
def self.perform_later(match_id)
|
queue_as :default
|
||||||
# Enqueue job using GoodJob's database directly
|
|
||||||
rom = EuchreCamp::App["persistence.rom"]
|
|
||||||
|
|
||||||
job_params = {
|
# This job calculates Elo ratings for a match and updates player records
|
||||||
queue_name: "default",
|
def perform(match_id)
|
||||||
priority: 0,
|
|
||||||
serialized_params: { "match_id" => match_id }.to_json,
|
|
||||||
scheduled_at: Time.now,
|
|
||||||
created_at: Time.now,
|
|
||||||
updated_at: Time.now,
|
|
||||||
job_class: "EuchreCamp::Jobs::CalculateEloJob",
|
|
||||||
executions_count: 0
|
|
||||||
}
|
|
||||||
|
|
||||||
rom.relations[:good_jobs].insert(job_params)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.perform(match_id)
|
|
||||||
# This is the actual job execution
|
|
||||||
match = find_match(match_id)
|
match = find_match(match_id)
|
||||||
return unless match
|
return unless match
|
||||||
|
|
||||||
@@ -40,16 +26,19 @@ module EuchreCamp
|
|||||||
|
|
||||||
# Update player ratings and create snapshots
|
# Update player ratings and create snapshots
|
||||||
update_player_ratings(calculation_results, match_id)
|
update_player_ratings(calculation_results, match_id)
|
||||||
|
|
||||||
|
# Record partnerships (after Elo snapshots are created)
|
||||||
|
EuchreCamp::Services::PartnershipTracker.record_match(match_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.find_match(match_id)
|
def find_match(match_id)
|
||||||
rom = EuchreCamp::App["persistence.rom"]
|
rom = EuchreCamp::App["persistence.rom"]
|
||||||
rom.relations[:matches].by_pk(match_id).one
|
rom.relations[:matches].by_pk(match_id).one
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.fetch_player_ratings(match)
|
def fetch_player_ratings(match)
|
||||||
rom = EuchreCamp::App["persistence.rom"]
|
rom = EuchreCamp::App["persistence.rom"]
|
||||||
player_ids = [
|
player_ids = [
|
||||||
match[:team_1_p1_id],
|
match[:team_1_p1_id],
|
||||||
@@ -64,7 +53,7 @@ module EuchreCamp
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.update_player_ratings(results, match_id)
|
def update_player_ratings(results, match_id)
|
||||||
rom = EuchreCamp::App["persistence.rom"]
|
rom = EuchreCamp::App["persistence.rom"]
|
||||||
|
|
||||||
results.each do |player_id, data|
|
results.each do |player_id, data|
|
||||||
|
|||||||
@@ -20,11 +20,12 @@ module EuchreCamp
|
|||||||
raise ArgumentError, "Team 2 must have exactly 2 players" unless team_2_players.length == 2
|
raise ArgumentError, "Team 2 must have exactly 2 players" unless team_2_players.length == 2
|
||||||
|
|
||||||
# Determine winner (first to 10 points)
|
# Determine winner (first to 10 points)
|
||||||
team_1_wins = team_1_score >= 10
|
# Allow for exact score tie at 10-10 if provided
|
||||||
team_2_wins = team_2_score >= 10
|
team_1_wins = team_1_score > team_2_score && team_1_score >= 10
|
||||||
|
team_2_wins = team_2_score > team_1_score && team_2_score >= 10
|
||||||
|
|
||||||
unless team_1_wins || team_2_wins
|
unless team_1_wins || team_2_wins
|
||||||
raise ArgumentError, "One team must reach 10 points to win"
|
raise ArgumentError, "One team must reach 10 points to win (Team 1: #{team_1_score}, Team 2: #{team_2_score})"
|
||||||
end
|
end
|
||||||
|
|
||||||
if team_1_wins && team_2_wins
|
if team_1_wins && team_2_wins
|
||||||
|
|||||||
Reference in New Issue
Block a user