module Public module LiveHelper def live_score_sets_label(score_state) return nil unless score_state "Set #{score_state.current_set} · Set vinti #{score_state.home_sets}-#{score_state.away_sets}" end def live_score_partials_label(score_state) return nil unless score_state partials = Array(score_state.set_partials) return nil if partials.empty? partials.map { |p| format_set_partial_entry(p) }.join(" · ") end def live_scheduled_label(datetime) return nil unless datetime datetime.in_time_zone.strftime("%d/%m/%Y alle %H:%M") end def live_scheduled_relative(datetime) return nil unless datetime if datetime.to_date == Time.zone.today "Oggi alle #{datetime.strftime('%H:%M')}" elsif datetime.to_date == Time.zone.tomorrow "Domani alle #{datetime.strftime('%H:%M')}" else live_scheduled_label(datetime) end end def live_match_card_heading(match) team = match.team club_name = team.club&.name.presence || "Società" content_tag(:h3, class: "live-card__title") do safe_join([ content_tag(:span, club_name, class: "live-card__club"), content_tag(:span, "#{team.name} vs #{match.opponent_name}", class: "live-card__matchup") ]) end end def live_match_page_heading(match) team = match.team club_name = team.club&.name.presence || "Società" content_tag(:div, class: "live-page-heading") do safe_join([ content_tag(:p, club_name, class: "live-page-heading__club"), content_tag(:h1, "#{team.name} vs #{match.opponent_name}", class: "live-page-heading__matchup") ]) end end def live_score_points_label(match, score_state) return "—" unless score_state "#{match.team.name} #{score_state.home_points} - #{score_state.away_points} #{match.opponent_name}" end # Colore ospite sul tabellone: secondario società se leggibile su bianco, altrimenti blu visitatore. def live_scorebug_away_color(team) club = team.club candidate = club&.effective_secondary_color.presence || "#1565c0" return candidate unless light_hex_color?(candidate) "#1565c0" end private def light_hex_color?(hex) h = hex.to_s.delete_prefix("#") return false unless h.match?(/\A\h{6}\z/i) r = h[0..1].to_i(16) g = h[2..3].to_i(16) b = h[4..5].to_i(16) (0.299 * r + 0.587 * g + 0.114 * b) > 200 end def format_set_partial_entry(partial) data = partial.respond_to?(:with_indifferent_access) ? partial.with_indifferent_access : partial set_no = data[:set] || data["set"] home = data[:home] || data["home"] away = data[:away] || data["away"] label = set_no.present? ? "Set #{set_no}" : "Set" "#{label} #{home}-#{away}" end end end