Normalizza il periodo nel backend quando l'app invia "Q1" via cable, così advance_period aggiorna regia e overlay; rimuove i controlli cronometro dalla regia e rende più tollerante il decode iOS al resume sessione. Co-authored-by: Cursor <cursoragent@cursor.com>
88 lines
2.0 KiB
Ruby
88 lines
2.0 KiB
Ruby
class ScoreState < ApplicationRecord
|
|
belongs_to :stream_session
|
|
|
|
before_validation :sync_board_type_from_match, on: :create
|
|
|
|
def as_cable_payload
|
|
payload = {
|
|
type: "score_update",
|
|
board_type: effective_board_type,
|
|
home_sets: home_sets,
|
|
away_sets: away_sets,
|
|
home_points: home_points,
|
|
away_points: away_points,
|
|
current_set: current_set,
|
|
set_partials: set_partials || [],
|
|
timeout_home: timeout_home,
|
|
timeout_away: timeout_away,
|
|
period: period,
|
|
data: data || {}
|
|
}
|
|
|
|
case effective_board_type
|
|
when "basket", "timed"
|
|
payload[:home_points] = basket_home_score
|
|
payload[:away_points] = basket_away_score
|
|
payload[:period] = current_period_label
|
|
payload[:clock_secs] = clock_secs
|
|
payload[:clock_running] = clock_running?
|
|
when "timer"
|
|
payload[:clock_secs] = clock_secs
|
|
payload[:clock_running] = clock_running?
|
|
end
|
|
|
|
payload
|
|
end
|
|
|
|
def effective_board_type
|
|
board_type.presence || stream_session&.match&.effective_board_type || "volley"
|
|
end
|
|
|
|
def basket_home_score
|
|
(data || {})["home_score"].to_i
|
|
end
|
|
|
|
def basket_away_score
|
|
(data || {})["away_score"].to_i
|
|
end
|
|
|
|
def clock_secs
|
|
(data || {})["clock_secs"].to_i
|
|
end
|
|
|
|
def clock_running?
|
|
!!(data || {})["clock_running"]
|
|
end
|
|
|
|
def current_period
|
|
Scoring::PeriodData.number_from((data || {})["period"]) || 1
|
|
end
|
|
|
|
def current_period_label
|
|
case effective_board_type
|
|
when "basket"
|
|
ot = (data || {})["overtime"]
|
|
return "OT#{current_period - rules_periods}" if ot
|
|
|
|
"Q#{current_period}"
|
|
when "timed"
|
|
ot = (data || {})["overtime"]
|
|
return "Suppl." if ot
|
|
|
|
"#{current_period}° tempo"
|
|
else
|
|
period.presence || "#{current_set}° set"
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def sync_board_type_from_match
|
|
self.board_type = stream_session.match.effective_board_type if board_type.blank? && stream_session&.match
|
|
end
|
|
|
|
def rules_periods
|
|
stream_session&.match&.effective_scoring_rules&.dig("periods").to_i
|
|
end
|
|
end
|