Add scoreboard countdown timer and sound effects

- Timer: one per event, set from the control room (duration, presets,
  start/pause/resume/reset, show/hide, T shortcut). Rules are pure
  functions in src/lib/timer.ts; the server's clock sets the end time and
  each screen counts down locally, corrected for its own clock.
  Needs supabase/migrations/0002_timer.sql.
- Sound effects on the scoreboard, synthesised with Web Audio (no files):
  drum-roll + crash per revealed team, fanfare for first place, a chime
  on game change, ticks for the last 10 seconds and a buzzer at zero.
  Triggers are pure functions comparing consecutive payloads.
- 'Turn sound on' button on the scoreboard, since browsers block audio
  until a click; the choice is remembered across reloads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-03 22:14:10 +02:00
co-authored by Claude Opus 5.5
parent f1b3a862f8
commit bf5b6194f6
23 changed files with 1056 additions and 14 deletions
+27
View File
@@ -4,10 +4,13 @@ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react
import { readableTextColor } from "@/lib/color";
import { useRefreshChannel } from "@/lib/hooks/use-refresh-channel";
import { useSound } from "@/lib/hooks/use-sound";
import { channelNames } from "@/lib/realtime-channels";
import type { ScoreboardPayload, ScoreboardTeam } from "@/lib/scoreboard";
import { soundsForUpdate } from "@/lib/scoreboard-sounds";
import { CountUp } from "./count-up";
import { ScoreboardTimer } from "./scoreboard-timer";
const MOVE_MS = 600;
const IDLE_MS = 3000;
@@ -42,6 +45,17 @@ export function Scoreboard({ token, initial }: { token: string; initial: Scorebo
() => new Set(initial.teams.filter((team) => team.rank !== undefined).map((team) => team.id)),
);
// Sound effects for what just changed: a new game, a team being revealed.
const sound = useSound();
const { play } = sound;
const previousPayload = useRef(initial);
useEffect(() => {
const before = previousPayload.current;
previousPayload.current = payload;
if (before === payload) return;
soundsForUpdate(before, payload, { reducedMotion: prefersReducedMotion() }).forEach(play);
}, [payload, play]);
const rowElements = useRef(new Map<string, HTMLElement>());
const lastTops = useRef(new Map<string, number>());
@@ -107,6 +121,8 @@ export function Scoreboard({ token, initial }: { token: string; initial: Scorebo
)}
</header>
{payload.timer && <ScoreboardTimer timer={payload.timer} serverNow={payload.updatedAt} play={play} />}
{hidden && (
<p className="self-center rounded-full border-2 border-alert px-[2.5vw] py-[1vh] text-[clamp(1rem,3.2vh,2.5rem)] font-bold">
Scores are hidden — final reveal soon
@@ -155,6 +171,17 @@ export function Scoreboard({ token, initial }: { token: string; initial: Scorebo
/>
{connectionLabel[connection]}
</span>
{/* Stays visible while sound is off or waiting, so the host can't miss that it's silent. */}
<button
type="button"
aria-pressed={sound.state === "on"}
onClick={sound.state === "on" ? sound.turnOff : () => void sound.turnOn()}
className={`min-h-11 rounded-md border px-4 font-bold hover:bg-white/10 focus-visible:outline-floodlight ${
sound.state === "on" ? "border-white/30" : "border-floodlight text-floodlight"
} ${idle && sound.state === "on" ? "invisible" : ""}`}
>
{sound.state === "on" ? "Sound on" : sound.state === "waiting" ? "Click anywhere for sound" : "Turn sound on"}
</button>
<button
type="button"
onClick={toggleFullscreen}