Files
simba-rmic-2026/client/src/components/MatchClock.tsx
T
sloray 235fb1c73b
构建客户端 / 检查(类型 + 单元测试) (push) Canceled after 0s
构建客户端 / Linux x64(deb + AppImage) (push) Canceled after 0s
构建客户端 / Windows x64(NSIS 安装包) (push) Canceled after 0s
构建客户端 / macOS 通用(.app + .dmg) (push) Canceled after 0s
feat(client): 改进启动窗口、维护交互与操作图标
- feat(client): 增加无边框动画启动窗口,加载完成后恢复主窗口
- feat(ui): 为操作按钮引入本地 Material Symbols Rounded 图标
- fix(maintenance): 移除烧录前无效的原生确认框,保留页面检查与后端校验
- fix(maintenance): 统一板卡示意风格并修正装甲板比例和图传右侧 USB 接线
- docs: 按 humanizer-zh 润色项目文档并将写作约定纳入 AGENTS.md

验证:前端构建、Tauri 主机编译与启动配置测试通过;维护流程 4 项测试通过;目录回归 4 项通过。已检查浏览器受影响页面与 Windows 原生启动窗口切换。
未执行设备烧录、板上运行或多显示器与高 DPI 验证。
2026-10-24 00:36:38 +08:00

115 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useMemo, useRef, useState } from "react";
import { Dialog } from "./Dialog";
import { Icon } from "./Icon";
import { api } from "../lib/ipc";
import { useAppStore } from "../lib/store";
import { MATCH_PHASE, matchTime, projectMatch } from "../lib/match-time";
import type { MatchAction, MatchClockView, Snapshot } from "../lib/types";
function useClock(view: MatchClockView) {
const { connected } = useAppStore();
const origin = useMemo(() => ({ at: performance.now(), view }), [view]);
const [now, setNow] = useState(performance.now());
useEffect(() => { const timer = window.setInterval(() => setNow(performance.now()), 100); return () => window.clearInterval(timer); }, []);
return projectMatch(origin.view, Math.max(0, now - origin.at), connected);
}
function Readout({ clock, names }: { clock: MatchClockView; names: string[] }) {
const paused = clock.phase === "paused";
const expired = paused && clock.pauseKind === "player" && clock.pauseRemainingMs === 0;
const label = !paused ? MATCH_PHASE[clock.phase] : clock.pauseKind === "player" ? `选手技术暂停 · ${names[clock.applicant ?? 0]}` : "官方技术暂停";
return <div className={`match-readout${paused ? " is-paused" : ""}${!clock.connected ? " is-stale" : ""}`}>
<div className="match-readout__phase"><i />{label}</div>
<div className="match-readout__digits num">{paused ? matchTime(clock.pauseRemainingMs ?? clock.pauseElapsedMs, clock.pauseKind === "official") : clock.phase === "idle" ? "—:—" : matchTime(clock.remainingMs)}</div>
<div className="match-readout__detail">{!clock.connected ? clock.source ? "计时同步中断 · 已冻结" : "等待裁判计时同步" : expired ? "暂停时间已到 · 等待裁判恢复" : paused ? `${clock.pauseKind === "official" ? "暂停已用时" : "暂停剩余"} · 暂停前剩余 ${matchTime(clock.remainingMs)}` : clock.phase === "running" || clock.phase === "finished" ? `对局已用时 ${matchTime(clock.elapsedMs, true)}` : clock.phase === "ready" ? "等待裁判确认开赛" : clock.phase === "countdown" ? "倒计时结束后开始对局" : "准备 3 分钟 / 对局 3 分钟"}</div>
{paused && <p className="match-readout__reason" title={clock.reason}>{clock.reason}</p>}
</div>;
}
export function TeamMatchClock({ snapshot }: { snapshot: Snapshot }) {
const clock = useClock(snapshot.matchClock);
const paused = clock.phase === "paused";
const label = !clock.connected ? clock.source ? "同步中断" : "等待裁判" : paused ? clock.pauseKind === "official" ? "官方暂停" : "选手暂停"
: clock.phase === "running" ? "对局剩余" : clock.phase === "preparation" ? "准备剩余" : clock.phase === "ready" ? "等待开赛"
: clock.phase === "countdown" ? "即将开赛" : clock.phase === "finished" ? "计时结束" : "尚未开始";
const time = clock.phase === "idle" ? "—:—" : paused
? matchTime(clock.pauseRemainingMs ?? clock.pauseElapsedMs, clock.pauseKind === "official") : matchTime(clock.remainingMs);
const detail = clock.syncError ?? (!clock.connected ? clock.source ? "计时同步中断,时间已冻结" : "等待裁判发送比赛计时" : paused
? `${clock.reason};${clock.pauseRemainingMs === 0 ? "暂停时间已到,等待裁判恢复;" : ""}对局已用时 ${matchTime(clock.elapsedMs, true)}`
: `对局已用时 ${matchTime(clock.elapsedMs, true)}`);
return <div className={`team-match-clock${paused ? " is-paused" : ""}${!clock.connected ? " is-stale" : ""}`} role="timer" aria-label={`${label} ${time},${detail}`} title={detail}>
<span className="team-match-clock__label">{label}</span>
<strong className="team-match-clock__time num">{time}</strong>
</div>;
}
/** 最后 5 秒覆盖画面;仅收到权威的开赛状态后才显示“开始”。 */
export function TeamMatchCountdown({ snapshot }: { snapshot: Snapshot }) {
const clock = useClock(snapshot.matchClock);
const previous = useRef({ phase: clock.phase, connected: clock.connected });
const [started, setStarted] = useState(false);
useEffect(() => {
const was = previous.current;
previous.current = { phase: clock.phase, connected: clock.connected };
if (was.phase === "countdown" && was.connected && clock.connected && clock.phase === "running" && clock.elapsedMs < 1500) {
setStarted(true);
} else if (!clock.connected || clock.phase !== "running") setStarted(false);
}, [clock.phase, clock.connected, clock.elapsedMs]);
useEffect(() => {
if (!started) return;
const timer = window.setTimeout(() => setStarted(false), 750);
return () => window.clearTimeout(timer);
}, [started]);
const visible = clock.connected && clock.phase === "countdown" && clock.remainingMs > 0 && clock.remainingMs <= 5000;
if (!visible && !started) return null;
const seconds = Math.max(1, Math.ceil(clock.remainingMs / 1000));
return <div className={`match-countdown${started ? " is-started" : ""}${!started && seconds <= 2 ? " is-urgent" : ""}`} aria-label="开赛全屏倒计时">
<div className="match-countdown__scan" aria-hidden="true" />
<div className="match-countdown__center">
<span className="match-countdown__label">{started ? "对局开始" : "准备开赛"}</span>
<div className="match-countdown__dial" aria-hidden="true"><i /><i /></div>
<strong key={started ? "start" : seconds} className={`match-countdown__number${started ? " match-countdown__number--start" : " num"}`} aria-live="assertive" aria-atomic="true">{started ? "开始" : seconds}</strong>
<div className="match-countdown__steps" aria-hidden="true">{[5, 4, 3, 2, 1].map(step => <i key={step} className={seconds <= step ? "is-passed" : ""} />)}</div>
<span className="match-countdown__caption">{started ? "" : "请选手就位"}</span>
</div>
<div className="match-countdown__progress" aria-hidden="true"><i style={{ transform: `scaleX(${started ? 0 : clock.remainingMs / 5000})` }} /></div>
</div>;
}
export function RefereeMatchControl({ snapshot, onError }: { snapshot: Snapshot; onError: (message: string) => void }) {
const clock = useClock(snapshot.matchClock);
const [dialog, setDialog] = useState<"player" | "official" | "end" | null>(null);
const [reason, setReason] = useState("");
const [team, setTeam] = useState(0);
const [approved, setApproved] = useState(false);
const [seconds, setSeconds] = useState(5);
const [busy, setBusy] = useState(false);
const active = ["preparation", "ready", "countdown", "running"].includes(clock.phase);
const run = async (action: MatchAction, close?: () => void) => {
if (busy) return;
setBusy(true);
try { await api.matchAction(action); close?.(); } catch (error) { onError(error instanceof Error ? error.message : String(error)); }
finally { setBusy(false); }
};
const openPause = (kind: "player" | "official") => { setReason(""); setApproved(false); setDialog(kind); };
const reasonValid = reason.trim().length > 0 && new TextEncoder().encode(reason.trim()).length <= 192 && !/[\u0000-\u001f\u007f]/.test(reason);
return <section className="match-control" aria-label="裁判比赛计时控制">
<div className="match-control__identity"><span className="label">MATCH CONTROL</span><span>{snapshot.demo ? "本机演示 · 合成计时" : "裁判计时 / 局域网同步"}</span>{!snapshot.demo && <small className={snapshot.matchClock.syncError ? "is-error" : ""}>{snapshot.matchClock.syncError ?? "仅对局进行中扣血;准备与暂停时只闪灯"}</small>}</div>
<Readout clock={clock} names={snapshot.config.teamNames} />
<div className="match-control__actions">
{clock.phase === "idle" || clock.phase === "finished" ? <button className="btn btn--primary" disabled={busy} onClick={() => void run({ type: "prepare" })}><Icon name="timer" />开始 3 分钟准备</button> : clock.phase === "ready" ? <div className="match-control__start"><label>开赛倒计时 <input aria-label="开赛倒计时秒数" type="number" min="1" max="60" step="1" value={seconds} onChange={e => setSeconds(Number(e.target.value))} /> 秒</label><button className="btn btn--primary" disabled={busy || !Number.isInteger(seconds) || seconds < 1 || seconds > 60} onClick={() => void run({ type: "countdown", seconds })}><Icon name="play" />确认开赛</button></div> : clock.phase === "paused" ? <button className="btn btn--primary" disabled={busy} onClick={() => void run({ type: "resume" })}><Icon name="play" />恢复{clock.resumePhase === "preparation" ? "准备" : clock.resumePhase === "ready" ? "待开赛阶段" : clock.resumePhase === "countdown" ? "倒计时" : "对局"}</button> : <span className="match-control__hint">{clock.phase === "preparation" ? "准备结束后等待裁判确认开赛" : clock.phase === "countdown" ? "请选手就位" : "计时到零结束本局"}</span>}
<div className="match-control__buttons"><button className="btn" disabled={busy || !active} onClick={() => openPause("player")}><Icon name="pause" />选手暂停</button><button className="btn" disabled={busy || !active} onClick={() => openPause("official")}><Icon name="pause" />官方暂停</button>{active || clock.phase === "paused" ? <button className="btn btn--quiet" disabled={busy} onClick={() => setDialog("end")}><Icon name="stop" />结束本局</button> : clock.phase === "finished" && <button className="btn btn--quiet" disabled={busy} onClick={() => void run({ type: "reset" })}><Icon name="reset" />清除计时</button>}</div>
</div>
{dialog && <Dialog title={dialog === "end" ? "结束本局计时" : dialog === "player" ? "选手技术暂停" : "官方技术暂停"} closeDisabled={busy} onClose={() => setDialog(null)} className="match-dialog">
{close => <div className="match-dialog__body">{dialog === "end" ? <><p>结束当前计时并同步到选手端,同时停止装甲板扣血。比赛结果由裁判判定。</p><button className="btn btn--primary" disabled={busy} onClick={() => void run({ type: "end" }, close)}><Icon name="stop" />确认结束本局</button></> : <>
<p>{dialog === "player" ? "选手技术暂停为 5 分钟,须先获得主裁判批准。时间到后,等待裁判恢复比赛。" : "官方暂停显示已用时,由裁判手动恢复。规则页未规定固定时限。"}</p>
{dialog === "player" && <label>申请队伍<select value={team} onChange={e => setTeam(Number(e.target.value))}>{snapshot.config.teamNames.map((name, i) => <option key={i} value={i}>{name}</option>)}</select></label>}
<label>暂停原因<input data-initial-focus value={reason} onChange={e => setReason(e.target.value)} maxLength={192} placeholder="例如:图传中断,需要检查接线" /></label>
<small>最多 192 个 UTF-8 字节,约 64 个汉字。</small>
{dialog === "player" && <label className="match-dialog__approval"><input type="checkbox" checked={approved} onChange={e => setApproved(e.target.checked)} />已获得主裁判批准</label>}
<button className="btn btn--primary" disabled={busy || !reasonValid || (dialog === "player" && !approved)} onClick={() => void run(dialog === "player" ? { type: "pausePlayer", team, reason, approved } : { type: "pauseOfficial", reason }, close)}><Icon name="pause" />开始{dialog === "player" ? "5 分钟暂停" : "官方暂停"}</button>
</>}</div>}
</Dialog>}
</section>;
}