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
@@ -0,0 +1,49 @@
"use client";
import { useEffect, useRef } from "react";
import { useCountdown } from "@/lib/hooks/use-countdown";
import { soundForCountdown } from "@/lib/scoreboard-sounds";
import type { SoundName } from "@/lib/sound";
import { formatClock, secondsLeft, type TimerState } from "@/lib/timer";
/** The big countdown on the scoreboard, with ticks for the last ten seconds and a buzzer at zero. */
export function ScoreboardTimer({
timer,
serverNow,
play,
}: {
timer: TimerState;
serverNow: string;
play: (name: SoundName) => void;
}) {
const remainingMs = useCountdown(timer, serverNow);
const seconds = secondsLeft(remainingMs);
// The local clock reaches zero before the next server update says "finished".
const finished = timer.status === "finished" || (timer.status === "running" && seconds === 0);
const urgent = timer.status === "running" && seconds <= 10 && !finished;
const previousSeconds = useRef<number | null>(null);
useEffect(() => {
const sound = soundForCountdown(timer.status, previousSeconds.current, seconds);
previousSeconds.current = seconds;
if (sound) play(sound);
}, [seconds, timer.status, play]);
return (
<div
role="timer"
aria-label={finished ? "Time's up" : `${formatClock(remainingMs)} left`}
className={`flex items-baseline justify-center gap-[2vw] font-display leading-none font-bold tabular-nums ${
finished ? "text-alert" : urgent ? "text-floodlight" : "text-white"
}`}
>
{/* Re-keyed each second in the last ten so the pulse restarts on every tick. */}
<span key={urgent ? seconds : "steady"} className={`text-[16vh] ${urgent ? "timer-pulse" : ""}`}>
{formatClock(remainingMs)}
</span>
{finished && <span className="timer-flash text-[7vh]">Time&apos;s up</span>}
{timer.status === "paused" && <span className="text-[5vh] text-white/70">paused</span>}
</div>
);
}
+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}