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:
@@ -0,0 +1,30 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import type { TimerState } from "@/lib/timer";
|
||||
|
||||
/**
|
||||
* Milliseconds left on a timer, ticking locally while it runs.
|
||||
* `serverNow` is the server's clock when `timer` was computed; the difference from this
|
||||
* device's clock is applied so a projector PC with a wrong clock still counts down correctly.
|
||||
*/
|
||||
export function useCountdown(timer: TimerState | null, serverNow: string): number {
|
||||
const [remainingMs, setRemainingMs] = useState(timer?.remainingMs ?? 0);
|
||||
const running = timer?.status === "running" && timer.endsAt !== null;
|
||||
const endsAt = timer?.endsAt ?? null;
|
||||
|
||||
useEffect(() => {
|
||||
if (!running || !endsAt) return;
|
||||
const clockOffset = Date.parse(serverNow) - Date.now();
|
||||
const endsAtMs = Date.parse(endsAt);
|
||||
const tick = () => setRemainingMs(Math.max(0, endsAtMs - (Date.now() + clockOffset)));
|
||||
// Sync to the clock at once rather than 100ms late.
|
||||
tick();
|
||||
const interval = setInterval(tick, 100);
|
||||
return () => clearInterval(interval);
|
||||
}, [running, endsAt, serverNow]);
|
||||
|
||||
// When not running the server's value is exact; only a running timer needs the local clock.
|
||||
return running ? remainingMs : (timer?.remainingMs ?? 0);
|
||||
}
|
||||
Reference in New Issue
Block a user