import type { ScoreboardPayload } from "@/lib/scoreboard"; import type { SoundName } from "@/lib/sound"; import type { TimerStatus } from "@/lib/timer"; // Which sound effects a change on the scoreboard should trigger. Pure, so the rules // are testable without a browser; the scoreboard component just plays what this returns. const revealedIds = (payload: ScoreboardPayload) => new Set( payload.reveal?.active ? payload.teams.filter((team) => team.rank !== undefined).map((team) => team.id) : [], ); /** Sounds for going from one payload to the next. */ export function soundsForUpdate( before: ScoreboardPayload, after: ScoreboardPayload, options: { reducedMotion: boolean }, ): SoundName[] { const sounds: SoundName[] = []; // A new current game (including the first one of the night). if (after.currentGame && after.currentGame.id !== before.currentGame?.id) sounds.push("game"); if (after.reveal?.active) { const already = revealedIds(before); const newlyRevealed = [...revealedIds(after)].some((id) => !already.has(id)); if (newlyRevealed) { // The last team to appear is first place: it gets the fanfare. const isWinner = after.reveal.revealedCount >= after.teams.length; // With reduced motion the row appears at once, so a drum-roll would trail behind it. if (options.reducedMotion) sounds.push(isWinner ? "revealWinner" : "crash"); else sounds.push(isWinner ? "revealWinner" : "reveal"); } } // A new first buzz. Later buzzes are silent: the room only needs to hear who won the race. const firstBuzz = after.buzzer?.buzzes[0]?.teamId; if (firstBuzz && firstBuzz !== before.buzzer?.buzzes[0]?.teamId) sounds.push("buzzIn"); return sounds; } /** * Sound for the countdown reaching a new whole second. `previousSeconds` is what the clock * showed a moment ago; null on the first look, so loading the page mid-countdown or after * time is up stays silent. */ export function soundForCountdown( status: TimerStatus, previousSeconds: number | null, seconds: number, ): SoundName | null { if (previousSeconds === null || seconds >= previousSeconds) return null; if (status !== "running" && status !== "finished") return null; if (seconds === 0) return "buzzer"; if (status !== "running" || seconds > 10) return null; return seconds <= 3 ? "tickFinal" : "tick"; }