Files
MatchLiveTv/backend/app/models/score_state.rb
Emiliano Frascaro e727069d43 Introduce architettura multi-sport con catalogo, engine scoring e overlay.
Catalogo sport in YAML, board implicito, engine per volley/basket/timed/racket/timer/generic, regia e app Android allineate.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-09 20:11:21 +02:00

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
(data || {})["period"].to_i.positive? ? (data || {})["period"].to_i : 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