Implements a new "standard" timer mode alongside the existing chess clock mode. In standard mode the per-pick timer resets to a fixed value after every pick (no carry-over), and the speed selector shows plain time values instead of named chess-clock presets. Key changes: - Add `draft_timer_mode` enum column to `seasons` table (migration 0053) - `draft.start`: standard mode seeds timers at `draftIncrementTime` (the per-pick value) rather than `draftInitialTime` - `draft.make-pick`: three-way branch — standard resets, chess clock owner earns increment, commissioner/admin pick leaves bank frozen - `draft.force-manual-pick`: commissioner picks never earn bank time; chess clock path uses a pre-pick snapshot to avoid a race window with the 1-second timer loop - `executeAutoPick` in draft-utils: auto picks never earn bank time; chess clock path skips the DB update (timer already at 0) - League creation and settings pages: mode-aware speed selector (raw seconds for standard, named presets for chess clock); shared `parseDraftSpeed` utility extracted to `app/lib/draft-timer.ts` - Tests added for draft.start timer init and make-pick timer mode behavior; force-manual-pick tests updated for new timer semantics Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
87 lines
3 KiB
TypeScript
87 lines
3 KiB
TypeScript
/**
|
|
* Pure utility functions for the draft clock/timer system.
|
|
* Extracted here to be independently testable and shared across
|
|
* route handlers and UI components.
|
|
*
|
|
* Clock model (chess-clock / Fischer increment):
|
|
* - Draft start: every team receives `initialTime` as their bank.
|
|
* - While on the clock: the active team's bank counts down each second.
|
|
* - After a pick is made: the picker's bank += `incrementTime`.
|
|
* - Between turns: all other teams' banks are left untouched.
|
|
*/
|
|
|
|
/**
|
|
* Formats a seconds value for display.
|
|
* undefined → "--:--"
|
|
* < 3600 s → "m:ss"
|
|
* >= 3600 s → "h:mm:ss"
|
|
*/
|
|
export function formatClockTime(seconds: number | undefined): string {
|
|
if (seconds === undefined) return "--:--";
|
|
const clamped = Math.max(0, seconds); // guard against negative values from timer drift
|
|
const h = Math.floor(clamped / 3600);
|
|
const m = Math.floor((clamped % 3600) / 60);
|
|
const s = clamped % 60;
|
|
if (h > 0)
|
|
return `${h}:${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
|
|
return `${m}:${String(s).padStart(2, "0")}`;
|
|
}
|
|
|
|
/**
|
|
* Returns a team's new bank time after they complete a pick.
|
|
* The increment is the post-pick reward (chess clock model).
|
|
*
|
|
* @param bankTime Current remaining seconds in the team's bank
|
|
* @param incrementTime Bonus seconds awarded after each pick
|
|
*/
|
|
export function calculateTimeAfterPick(
|
|
bankTime: number,
|
|
incrementTime: number
|
|
): number {
|
|
return bankTime + incrementTime;
|
|
}
|
|
|
|
/**
|
|
* Converts a draftSpeed form value + timerMode into DB time fields.
|
|
*
|
|
* Standard mode: speed is raw seconds (the per-pick time); both fields equal it.
|
|
* Chess clock mode: speed is a named preset that maps to (initial bank, increment).
|
|
*/
|
|
export function parseDraftSpeed(
|
|
draftSpeed: string | null,
|
|
draftTimerMode: "chess_clock" | "standard"
|
|
): { draftInitialTime: number; draftIncrementTime: number } {
|
|
if (draftTimerMode === "standard") {
|
|
const seconds = parseInt(draftSpeed ?? "", 10);
|
|
const time = isNaN(seconds) ? 90 : seconds;
|
|
return { draftInitialTime: time, draftIncrementTime: time };
|
|
}
|
|
|
|
switch (draftSpeed) {
|
|
case "fast":
|
|
return { draftInitialTime: 60, draftIncrementTime: 10 };
|
|
case "slow":
|
|
return { draftInitialTime: 28800, draftIncrementTime: 3600 };
|
|
case "very-slow":
|
|
return { draftInitialTime: 43200, draftIncrementTime: 3600 };
|
|
default:
|
|
return { draftInitialTime: 120, draftIncrementTime: 15 };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the Tailwind colour class(es) for a timer value.
|
|
*
|
|
* > 60 s → green (plenty of time)
|
|
* > 30 s → amber (getting tight)
|
|
* > 10 s → coral (urgent)
|
|
* ≤ 10 s → coral + animate-pulse (critical)
|
|
* undefined → muted (clock not running)
|
|
*/
|
|
export function getTimerColorClass(seconds: number | undefined): string {
|
|
if (seconds === undefined) return "text-muted-foreground";
|
|
if (seconds > 60) return "text-emerald-400";
|
|
if (seconds > 30) return "text-amber-accent";
|
|
if (seconds > 10) return "text-coral-accent";
|
|
return "text-coral-accent animate-pulse";
|
|
}
|