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
|
||||
|
||||
require "good_job"
|
||||
|
||||
module EuchreCamp
|
||||
module Jobs
|
||||
class CalculateEloJob
|
||||
def self.perform_later(match_id)
|
||||
# Enqueue job using GoodJob's database directly
|
||||
rom = EuchreCamp::App["persistence.rom"]
|
||||
|
||||
job_params = {
|
||||
queue_name: "default",
|
||||
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
|
||||
class CalculateEloJob < ActiveJob::Base
|
||||
queue_as :default
|
||||
|
||||
def self.perform(match_id)
|
||||
# This is the actual job execution
|
||||
# This job calculates Elo ratings for a match and updates player records
|
||||
def perform(match_id)
|
||||
match = find_match(match_id)
|
||||
return unless match
|
||||
|
||||
@@ -40,16 +26,19 @@ module EuchreCamp
|
||||
|
||||
# Update player ratings and create snapshots
|
||||
update_player_ratings(calculation_results, match_id)
|
||||
|
||||
# Record partnerships (after Elo snapshots are created)
|
||||
EuchreCamp::Services::PartnershipTracker.record_match(match_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.find_match(match_id)
|
||||
def find_match(match_id)
|
||||
rom = EuchreCamp::App["persistence.rom"]
|
||||
rom.relations[:matches].by_pk(match_id).one
|
||||
end
|
||||
|
||||
def self.fetch_player_ratings(match)
|
||||
def fetch_player_ratings(match)
|
||||
rom = EuchreCamp::App["persistence.rom"]
|
||||
player_ids = [
|
||||
match[:team_1_p1_id],
|
||||
@@ -64,7 +53,7 @@ module EuchreCamp
|
||||
end
|
||||
end
|
||||
|
||||
def self.update_player_ratings(results, match_id)
|
||||
def update_player_ratings(results, match_id)
|
||||
rom = EuchreCamp::App["persistence.rom"]
|
||||
|
||||
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
|
||||
|
||||
# Determine winner (first to 10 points)
|
||||
team_1_wins = team_1_score >= 10
|
||||
team_2_wins = team_2_score >= 10
|
||||
# Allow for exact score tie at 10-10 if provided
|
||||
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
|
||||
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
|
||||
|
||||
if team_1_wins && team_2_wins
|
||||
|
||||
Reference in New Issue
Block a user