Files
game-scoreboard/src/lib/scoreboard-sounds.ts
T
joshuaandClaude Opus 5.5 bbcf84bbc3 Phase 10: team buzzers
Each team gets a buzzer link (/b/[token]); a game opts in with a
"Teams buzz in" checkbox. The database's clock orders the buzzes, so
the result doesn't depend on phone clocks. The control room lists the
order and clears it per question (B); the scoreboard shows who buzzed
first with a ding.

Buzzes follow the existing rules: token-gated route handlers with the
service-role client, RLS on the new table, and data-free realtime pings
on a new buzzer:{eventId} channel.

Needs supabase/migrations/0003_buzzer.sql applied before deploying.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 09:33:44 +02:00

59 lines
2.3 KiB
TypeScript

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";
}