Files
simba-rmic-2026/client/src/lib/match-preview.ts
T

48 lines
3.3 KiB
TypeScript

/** 仅被 dev-preview 引用的合成计时,不能作为网络同步测试。 */
import { projectMatch } from "./match-time";
import type { MatchAction, MatchClockView, MatchPhase } from "./types";
const scenario = new URLSearchParams(location.search).get("match");
const blank = (): MatchClockView => ({ phase: "idle", resumePhase: "idle", pauseKind: null, applicant: null,
reason: "", remainingMs: 0, elapsedMs: 0, pauseRemainingMs: null, pauseElapsedMs: 0,
connected: true, freshForMs: 1500, source: "预览 / 合成数据", syncError: null });
let view = blank();
let at = performance.now();
if (scenario === "offline") Object.assign(view, { phase: "running", remainingMs: 93_000, elapsedMs: 87_000, connected: false });
if (["preparation", "ready", "countdown", "running", "finished"].includes(scenario ?? "")) {
view.phase = scenario as MatchPhase;
view.remainingMs = scenario === "countdown" ? 5000 : scenario === "ready" || scenario === "finished" ? 0 : 180_000;
}
if (scenario?.startsWith("paused")) {
Object.assign(view, { phase: "paused", resumePhase: "running", remainingMs: 93_000, elapsedMs: 87_000,
pauseKind: scenario === "paused-official" ? "official" : "player", applicant: scenario === "paused-official" ? null : 0,
reason: "合成演示:检查图传接线", pauseRemainingMs: scenario === "paused-official" ? null : scenario === "paused-expired" ? 0 : 300_000 });
}
export function previewMatch(): MatchClockView {
const now = performance.now();
// 预览的合成来源持续在线;通过查询参数单独复现掉线。
view = projectMatch({ ...view, freshForMs: Number.MAX_SAFE_INTEGER }, now - at, true); at = now;
if (view.phase === "preparation" && view.remainingMs === 0) view.phase = "ready";
if (view.phase === "countdown" && view.remainingMs === 0) { view.phase = "running"; view.remainingMs = 180_000; }
if (view.phase === "running" && view.remainingMs === 0) view.phase = "finished";
return { ...view, connected: scenario !== "offline", freshForMs: scenario === "offline" ? 0 : 1500 };
}
export function previewMatchAction(action: MatchAction) {
previewMatch();
switch (action.type) {
case "prepare": view = { ...blank(), phase: "preparation", remainingMs: 180_000 }; break;
case "skipPreparation":
if (view.phase !== "preparation" && view.phase !== "ready") throw new Error("当前阶段不能跳过准备");
view.phase = "ready"; view.remainingMs = 0; break;
case "countdown": view.phase = "countdown"; view.remainingMs = action.seconds * 1000; break;
case "pausePlayer":
if (!action.approved) throw new Error("选手技术暂停须经主裁判批准");
view = { ...view, resumePhase: view.phase, phase: "paused", pauseKind: "player", applicant: action.team, reason: action.reason, pauseElapsedMs: 0, pauseRemainingMs: 300_000 }; break;
case "pauseOfficial": view = { ...view, resumePhase: view.phase, phase: "paused", pauseKind: "official", applicant: null, reason: action.reason, pauseElapsedMs: 0, pauseRemainingMs: null }; break;
case "resume": view = { ...view, phase: view.resumePhase, resumePhase: "idle", pauseKind: null, applicant: null, reason: "", pauseRemainingMs: null, pauseElapsedMs: 0 }; break;
case "end": view.phase = "finished"; view.pauseKind = null; break;
case "reset": view = blank(); break;
}
at = performance.now();
}