diff --git a/app/jobs/calculate_elo_job.rb b/app/jobs/calculate_elo_job.rb index da5e125..3ab9363 100644 --- a/app/jobs/calculate_elo_job.rb +++ b/app/jobs/calculate_elo_job.rb @@ -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| diff --git a/app/services/elo_calculator.rb b/app/services/elo_calculator.rb index 07079c2..695dfdf 100644 --- a/app/services/elo_calculator.rb +++ b/app/services/elo_calculator.rb @@ -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