module Tournaments class RecordResult def self.call(match:, home_score:, away_score:, source: "manual", result_data: {}, walkover: nil) new( match: match, home_score: home_score, away_score: away_score, source: source, result_data: result_data, walkover: walkover ).call end def initialize(match:, home_score:, away_score:, source:, result_data:, walkover:) @match = match @home_score = home_score @away_score = away_score @source = source @result_data = result_data || {} @walkover = walkover end def call return @match unless @match.tournament_match? status = "played" winner_id = nil if @walkover == "home" status = "walkover_home" winner_id = @match.home_participant_id @home_score = @home_score.presence || 1 @away_score = 0 elsif @walkover == "away" status = "walkover_away" winner_id = @match.away_participant_id @away_score = @away_score.presence || 1 @home_score = 0 else winner_id = if @home_score.to_i > @away_score.to_i @match.home_participant_id elsif @away_score.to_i > @home_score.to_i @match.away_participant_id end end @match.update!( home_score: @home_score, away_score: @away_score, result_status: status, result_source: @source, result_data: @result_data, winner_participant_id: winner_id ) Tournaments::FillKnockoutSources.call(@match.tournament) @match end end end