Files

68 lines
2.1 KiB
Ruby

module Tournaments
class SwapSides
class LiveBroadcastError < StandardError; end
def self.call(match:)
new(match).call
end
def initialize(match)
@match = match
end
def call
tournament = @match.tournament
raise ArgumentError, "not a tournament match" unless tournament
Tournaments::Entitlements.new(tournament.club).assert_writable!
unless tournament.writable?
raise Tournaments::EntitlementError.new(
I18n.t("flash.tournaments.archived_locked"),
code: "archived"
)
end
raise LiveBroadcastError, I18n.t("flash.matches.close_live_before_delete") unless @match.deletable?
home_id = @match.home_participant_id
away_id = @match.away_participant_id
home_score = @match.home_score
away_score = @match.away_score
home_source_kind = @match.home_source_kind
away_source_kind = @match.away_source_kind
home_source_match_id = @match.home_source_match_id
away_source_match_id = @match.away_source_match_id
home_source_group_id = @match.home_source_group_id
away_source_group_id = @match.away_source_group_id
home_source_rank = @match.home_source_rank
away_source_rank = @match.away_source_rank
@match.update!(
home_participant_id: away_id,
away_participant_id: home_id,
home_score: away_score,
away_score: home_score,
home_source_kind: away_source_kind,
away_source_kind: home_source_kind,
home_source_match_id: away_source_match_id,
away_source_match_id: home_source_match_id,
home_source_group_id: away_source_group_id,
away_source_group_id: home_source_group_id,
home_source_rank: away_source_rank,
away_source_rank: home_source_rank,
result_status: swapped_result_status
)
@match
end
private
def swapped_result_status
case @match.result_status
when "walkover_home" then "walkover_away"
when "walkover_away" then "walkover_home"
else @match.result_status
end
end
end
end