Files
simba-rmic-2026/client/src/lib/dev-preview.ts
T
sloray 0a8f7bcf90
构建客户端 / 检查(类型 + 单元测试) (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
fix(client): 修复双实例镜像同步并调整底部工具布局
2026-11-01 23:58:27 +08:00

642 lines
21 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.
const previewMirrors: Record<string, { mirror: boolean; mirrorVertical: boolean }> = {};
/**
* 仅开发用的浏览器预览。
*
* # 它解决什么问题
*
* Tauri 的界面跑在 WKWebView 里,改一行 CSS 要走一次 cargo 构建;而且截图要靠 macOS 的
* 屏幕录制权限。预览模式让同一套 React 组件在浏览器里跑起来:状态由本文件合成,画面用
* `createImageBitmap` 从一块 canvas 上取——`VideoSurface`、血条、瞄准器、命中反馈全都是
* 真代码,只有"后端"是假的。
*
* # 它绝不可能进到正式构建里
*
* 两重门:
*
* 1. `import.meta.env.DEV` —— 只在 `vite dev` 下为真;`vite build` 时是 false,
* 2. 并且页面里没有 Tauri 注入的 `__TAURI_INTERNALS__`。
*
* 调用点还用 `await import(...)`:条件为假时整个模块不会被打进产物,而不是打进去再靠
* 运行时不走。
*
* # 用法
*
* ```sh
* pnpm dev # 用户端
* open 'http://127.0.0.1:1420/?preview=referee' # 裁判端
* ```
*/
import { DEFAULT_TEAM_NAMES } from "./format";
import { previewMatch, previewMatchAction } from "./match-preview";
import type { MatchAction, OperatorControlView, OperatorInput } from "./types";
import type { StreamClient } from "./stream";
import type {
ConfigView,
HudView,
Snapshot,
WireMsg,
} from "./types";
/** 页面里有没有 Tauri 注入。 */
function hasTauri(): boolean {
return typeof window !== "undefined" && "__TAURI_INTERNALS__" in window;
}
/** 现在是不是在预览模式。 */
export function isPreview(): boolean {
return import.meta.env.DEV && !hasTauri();
}
const params = new URLSearchParams(typeof location === "undefined" ? "" : location.search);
const startAsReferee = params.get("preview") === "referee";
/**
* 首次启动的两道门。
*
* `?preview=first-run` 让 `roleChosen` 为假、`?preview=password` 让 `passwordSet` 为假,
* 这样不删配置也能看到那两块界面。它们只在预览里存在,正式构建里这段代码不会被打进去。
*/
const firstRun = params.get("preview") === "first-run";
const needsPassword = params.get("preview") === "password";
// 复现无信号时的 HUD;仍然使用合成状态,不连接真实设备。
const offlinePreview = params.get("preview") === "offline";
// 前 12 秒收流,随后断流 12 秒,再恢复;用于检查最后一帧的灰度遮罩。
const reconnectPreview = params.get("preview") === "reconnect";
const previewStartedAt = performance.now();
const missingBindingPreview = params.get("preview") === "missing-binding";
const wideLayoutPreview = params.get("layout") === "wide";
function videoOffline(): boolean {
const elapsed = performance.now() - previewStartedAt;
return offlinePreview || (reconnectPreview && elapsed >= 12_000 && elapsed < 24_000);
}
// ---------------------------------------------------------------------------
// 可变状态:设置面板里的改动会落到这里,界面因此是可交互的
// ---------------------------------------------------------------------------
const DEFAULT_HUD: HudView = {
health: {
visible: true,
showNumbers: true,
showTeamName: true,
lowThreshold: 30,
pulse: true,
width: 0.42,
},
crosshair: {
visible: true,
style: "cross-dot",
size: 0.045,
thickness: 0.004,
gap: 0.012,
color: "#3dff88",
offsetX: 0,
offsetY: 0,
},
grid: { visible: false, style: "thirds", divisions: 6, opacity: 0.22 },
hitFeedback: { flash: true, shake: false, marker: true, intensity: 1 },
showZoom: true,
showStats: true,
};
const CAMERA_A = 0x0de0_0001;
const CAMERA_B = 0x0de0_0002;
const ARMOR_A = 0x0a20_0001;
const ARMOR_B = 0x0a20_0002;
interface PreviewState {
role: "team" | "referee";
hud: HudView;
cameraId: number | null;
armorId: number | null;
team: number;
/** 两边显示的名字,与后端配置里的 `teamNames` 对应。 */
teamNames: [string, string];
mirror: boolean;
mirrorVertical: boolean;
unlocked: boolean;
password: string | null;
calibrating: boolean;
recordings: { camera: number; frames: number; startedMs: number }[];
hp: [number, number];
matchControlled: boolean;
finalOutcomes: ["victory" | "defeat" | "draw" | null, "victory" | "defeat" | "draw" | null] | null;
}
const state: PreviewState = {
role: startAsReferee ? "referee" : "team",
hud: { ...structuredClone(DEFAULT_HUD), health: { ...DEFAULT_HUD.health, width: wideLayoutPreview ? 0.9 : DEFAULT_HUD.health.width } },
cameraId: startAsReferee ? null : missingBindingPreview ? 0x0de0_9999 : CAMERA_A,
armorId: startAsReferee ? null : ARMOR_A,
team: 0,
teamNames: wideLayoutPreview ? ["用户端长队名布局验证一队", "用户端长队名布局验证二队"] : ["队伍一", "队伍二"],
mirror: true,
mirrorVertical: false,
// 预览里直接是已解锁状态,免得每次刷新都要输密码。
unlocked: false,
password: "1234",
calibrating: false,
recordings: [],
hp: [100, 100],
matchControlled: new URLSearchParams(location.search).has("match"),
finalOutcomes: null,
};
if (new URLSearchParams(location.search).get("match") === "finished") {
state.hp = [0, 76]; state.finalOutcomes = ["defeat", "victory"];
}
if (new URLSearchParams(location.search).get("health") === "blue-low") {
state.hp = [73, 10]; state.matchControlled = true;
}
function finishPreviewKnockout() {
const clock = previewMatch();
if (state.finalOutcomes || !state.matchControlled || (clock.phase !== "running" && !(clock.phase === "paused" && clock.resumePhase === "running")) || !state.hp.includes(0)) return;
state.finalOutcomes = state.hp[0] === 0 && state.hp[1] === 0 ? ["draw", "draw"]
: state.hp[0] === 0 ? ["defeat", "victory"] : ["victory", "defeat"];
previewMatchAction({ type: "end" });
}
// ---------------------------------------------------------------------------
// 假的命令实现
// ---------------------------------------------------------------------------
/** 供 `ipc.ts` 在有 Tauri 时不会走到的分支上用。 */
export async function previewInvoke<T>(command: string, args?: Record<string, unknown>): Promise<T> {
const value = handle(command, args ?? {});
return value as T;
}
let previewCapture = 0;
let previewCaptureAt = 0;
let previewInputSequence = 0;
let previewOperator: OperatorControlView = { active: false, captureId: null, mode: null, reason: "预览 / 未发送真实操作" };
function handle(command: string, args: Record<string, unknown>): unknown {
switch (command) {
case "operator_start": {
if (state.role !== "team") throw new Error("只有选手端可以开启操作");
const mode = args.mode === "debug" ? "debug" : "match";
previewInputSequence = 0;
previewCaptureAt = performance.now();
previewOperator = { active: true, captureId: ++previewCapture, mode, reason: "预览 / 合成输入,不发送到装甲板" };
return previewCapture;
}
case "operator_input": {
const input = args.input as OperatorInput;
if (input.captureId !== previewOperator.captureId || input.sequence <= previewInputSequence) return;
previewInputSequence = input.sequence;
if (!input.active) previewOperator = { ...previewOperator, active: false, reason: "预览操作已停止" };
return;
}
case "match_action": {
if (state.role !== "referee") throw new Error("只有裁判端可以控制比赛计时");
const action = args.action as MatchAction;
if (action.type === "prepare" && state.finalOutcomes) throw new Error("请先重置本局");
previewMatchAction(action);
state.matchControlled = true;
if (action.type === "reset") { state.hp = [100, 100]; state.finalOutcomes = null; }
return;
}
case "release_match_source": return;
case "stream_info":
return { url: "preview://", port: 0, clients: 1 };
case "get_snapshot":
return snapshot();
case "get_config":
return configView();
case "list_media_files":
return [];
case "config_path":
return "/预览模式/没有真实配置文件";
case "unlock":
state.unlocked = String(args.password) === state.password;
return state.unlocked;
case "lock":
state.unlocked = false;
return undefined;
case "set_password":
state.password = String(args.fresh);
state.unlocked = true;
return configView();
case "set_discovery_open":
return undefined;
case "bind_camera":
guard();
state.cameraId = (args.cameraId as number | null) ?? null;
return configView();
case "bind_armor":
guard();
state.armorId = (args.armorId as number | null) ?? null;
return configView();
case "set_team":
guard();
state.team = Number(args.team);
return configView();
case "set_team_name": {
guard();
const team = Number(args.team);
if (team !== 0 && team !== 1) throw new Error("队伍号只能是 0 或 1");
// 与后端一样:合并空白、按字符截断、留空回落默认名。
state.teamNames[team] = sanitizeName(String(args.name ?? ""), team);
return configView();
}
case "set_role":
state.role = args.role as "team" | "referee";
return configView();
case "set_camera_mirror":
previewMirrors[String(args.camera)] = { mirror: Boolean(args.mirror), mirrorVertical: Boolean(args.mirrorVertical) };
return;
case "set_mirror":
state.mirror = Boolean(args.mirror);
return configView();
case "set_mirror_vertical":
state.mirrorVertical = Boolean(args.mirrorVertical);
return configView();
case "save_hud":
state.hud = args.hud as HudView;
return configView();
case "set_referee_slots":
return configView();
case "set_recording_options":
return configView();
case "screenshot":
return "/预览模式/没有截图";
case "start_recording": {
const cameras = (args.cameras as number[]) ?? [];
const startedMs = Date.now();
for (const camera of cameras) {
if (!state.recordings.some((r) => r.camera === camera)) {
state.recordings.push({ camera, frames: 0, startedMs });
}
}
return recordings();
}
case "stop_recording": {
const cameras = (args.cameras as number[]) ?? [];
state.recordings = cameras.length
? state.recordings.filter((r) => !cameras.includes(r.camera))
: [];
return recordings();
}
case "refresh":
case "reveal":
case "open_file":
return undefined;
case "clear_config":
return "预览模式:没有真实配置可清空";
case "quit":
return undefined;
default:
throw new Error(`预览模式没有实现命令 ${command}`);
}
}
/** 预览里模拟后端的密码闸门,好让解锁流程也能被看到。 */
function guard() {
if (state.role === "team" && state.password !== null && !state.unlocked) {
throw new Error("修改设置需要密码。请先解锁,预览模式密码为 1234。");
}
}
function configView(): ConfigView {
return {
role: state.role,
roleChosen: !firstRun,
team: state.team,
teamNames: [...state.teamNames] as [string, string],
cameraId: state.cameraId,
cameraIdText: state.cameraId === null ? null : hex(state.cameraId),
armorId: state.armorId,
armorIdText: state.armorId === null ? null : hex(state.armorId),
mirror: state.mirror,
mirrorVertical: state.mirrorVertical,
passwordSet: needsPassword ? false : state.password !== null,
hud: structuredClone(state.hud),
recordingDir: "/预览模式/recordings",
ffmpeg: "ffmpeg",
};
}
function recordings() {
return state.recordings.map((recording) => ({
camera: recording.camera,
cameraText: hex(recording.camera),
active: true,
path: `/预览模式/rec-${recording.camera.toString(16)}.mp4`,
inputFps: 20,
framesWritten: recording.frames,
framesDropped: 0,
startedMs: recording.startedMs,
error: null,
}));
}
/** 与后端 `config::sanitize_team_name` 一致:合并空白、按字符截断、留空回落默认名。 */
function sanitizeName(name: string | undefined, team: number): string {
const collapsed = (name ?? "").split(/\s+/).filter(Boolean).join(" ");
const text = collapsed.length > 0 ? collapsed : DEFAULT_TEAM_NAMES[team];
return [...text].slice(0, 12).join("");
}
function hex(id: number): string {
return `0x${id.toString(16).toUpperCase().padStart(8, "0")}`;
}
function snapshot(): Snapshot {
// 合成联动场景:显式开启调试后 3 秒切入倒数,检查自动释放输入。
if (params.get("control") === "debug-to-countdown" && previewOperator.active && previewOperator.mode === "debug"
&& performance.now() - previewCaptureAt > 3000 && ["idle", "preparation", "ready"].includes(previewMatch().phase)) {
previewMatchAction({ type: "countdown", seconds: 10 });
}
finishPreviewKnockout();
const cameraOffline = videoOffline();
const cameras = [
{ id: CAMERA_A, team: 0 },
{ id: CAMERA_B, team: 1 },
].map(({ id, team }) => ({
id,
idText: hex(id),
addr: team === 0 ? "192.168.1.41" : "192.168.1.42",
online: !cameraOffline,
silenceMs: 120,
// 预览里模拟一块"版本对得上"的板子:界面上的固件标签因此能被看到。
firmwareVersion: "0.2.0",
firmwareBuild: 34,
protocol: 1,
compatible: true,
}));
const boards = [
{ id: ARMOR_A, team: 0 },
{ id: ARMOR_B, team: 1 },
].map(({ id, team }, index) => ({
id,
idText: hex(id),
addr: team === 0 ? "192.168.1.51" : "192.168.1.52",
online: !offlinePreview,
team,
hp: state.hp[index],
platesSeen: 4,
reportsAllPlates: true,
silenceMs: 90,
}));
const stats = cameras.map((camera) => ({
camera: camera.id,
cameraText: camera.idText,
online: !cameraOffline,
status: camera.idText,
hasFrames: !offlinePreview,
fps: cameraOffline ? 0 : 20,
width: 640,
height: 480,
framesShown: 1000,
framesDropped: 3,
lossPercent: 0.3,
datagrams: 17000,
lastFrameBytes: 22000,
gapMs: 50,
}));
// 与后端 `cam_core::snapshot::resolve_outcomes` 同一套规则,包括那条不对称:
// 输只看自己,赢要看对手归零**且**自己还活着。
const outcomeFor = (team: number) => {
if (state.finalOutcomes) return state.finalOutcomes[team];
if (state.matchControlled && previewMatch().phase === "finished") return null;
const mine = state.hp[team];
const theirs = state.hp[team === 0 ? 1 : 0];
if (mine === 0 && theirs === 0) return "draw" as const;
if (theirs === 0 && mine > 0) return "victory" as const;
if (mine === 0) return "defeat" as const;
return null;
};
const healthFor = (team: number) => {
const hp = state.hp[team];
const board = team === 0 ? ARMOR_A : ARMOR_B;
return {
team,
name: state.teamNames[team],
available: !offlinePreview,
placeholder: offlinePreview ? "装甲板未接入" : "",
hp: offlinePreview ? null : hp,
hps: [hp, hp, hp, hp],
board,
boardText: hex(board),
critical: hp === 0 || hp <= state.hud.health.lowThreshold,
outcome: outcomeFor(team),
};
};
return {
role: state.role,
team: state.team,
mirrored: state.mirror,
mirroredVertical: state.mirrorVertical,
cameraMirrors: previewMirrors,
matchClock: previewMatch(),
operatorControl: { ...previewOperator },
demo: true,
diagnostics: true,
unlocked: state.unlocked || state.role === "referee",
discoveryOpen: true,
uptimeMs: performance.now(),
clients: 1,
config: configView(),
cameras,
armorBoards: boards,
stats,
slots: {
cameraA: state.role === "referee" ? CAMERA_A : state.cameraId,
cameraB: state.role === "referee" ? CAMERA_B : null,
armorA: state.role === "referee" ? ARMOR_A : state.armorId,
armorB: state.role === "referee" ? ARMOR_B : null,
manual: false,
},
// 两边都给:用户端也要显示对手的血量(画面右上角那一条)。
health: [healthFor(0), healthFor(1)],
recordings: recordings(),
notes: ["浏览器预览:状态与画面均为合成数据,不代表真实链路。"],
};
}
// ---------------------------------------------------------------------------
// 假的画面与心跳
// ---------------------------------------------------------------------------
/** 启动预览的数据源:每 200ms 一份快照,每 50ms 一帧画面。 */
export function startPreview(client: StreamClient, emit: (msg: WireMsg) => void) {
let frameId = 0;
const started = performance.now();
// 快照。
const snapshotTimer = window.setInterval(() => {
emit({ t: "snapshot", ...snapshot() } as WireMsg);
}, 200);
// 两路画面,各 20fps。
const canvases = new Map<number, HTMLCanvasElement>();
const render = async (camera: number, team: number) => {
const canvas = canvases.get(camera) ?? makeCanvas();
canvases.set(camera, canvas);
draw(canvas, team, frameId);
const bitmap = await createImageBitmap(canvas);
client.injectFrame(camera, frameId, canvas.width, canvas.height, bitmap);
};
const frameTimer = window.setInterval(() => {
if (videoOffline()) return;
frameId += 1;
const cameras =
state.role === "referee"
? [
[CAMERA_A, 0],
[CAMERA_B, 1],
]
: state.cameraId === CAMERA_A || state.cameraId === CAMERA_B
? [[state.cameraId, state.cameraId === CAMERA_A ? 0 : 1]]
: [];
for (const [camera, team] of cameras as [number, number][]) {
void render(camera, team);
}
for (const recording of state.recordings) recording.frames += 1;
}, 50);
// 掉血:每 3 秒一次,轮流打两块板子,顺便把命中反馈也验证了。
//
// 打空的那一拍只做重置、不出命中(胜负横幅要真的能看到),其余每一拍都出一次
// 命中。早先的写法是 `to = from === 0 ? 100 : ...` 然后按 `to >= from` 判断——那样一旦
// 两块都打空,之后每次都是重置,命中就再也不会出现,盯着界面等闪红会等到超时。
let tick = 0;
const hitTimer = window.setInterval(() => {
if (offlinePreview) return;
if (state.matchControlled && previewMatch().phase !== "running") return;
tick += 1;
const team = tick % 2;
if (state.hp[team] === 0) {
if (!state.matchControlled) state.hp[team] = 100;
return;
}
const from = state.hp[team];
const damage = 3 + ((tick * 7) % 20);
const to = Math.max(0, from - damage);
state.hp[team] = to;
finishPreviewKnockout();
emit({
t: "hit",
board: team === 0 ? ARMOR_A : ARMOR_B,
team,
index: 0,
from,
to,
damage: from - to,
atMs: Date.now(),
} as WireMsg);
}, 3000);
// 第一帧不必等 200ms。
emit({ t: "snapshot", ...snapshot() } as WireMsg);
return () => {
window.clearInterval(snapshotTimer);
window.clearInterval(frameTimer);
window.clearInterval(hitTimer);
void started;
};
}
function makeCanvas(): HTMLCanvasElement {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 480;
return canvas;
}
/** 与 `src-tauri/src/demo.rs` 画的是同一类东西:底色区分两路、中心靶心、二进制帧号。 */
function draw(canvas: HTMLCanvasElement, team: number, frameId: number) {
const ctx = canvas.getContext("2d");
if (!ctx) return;
const { width, height } = canvas;
const tint = team === 0 ? [0, 46, 40] : [58, 30, 0];
const gradient = ctx.createLinearGradient(0, 0, 0, height);
gradient.addColorStop(0, `rgb(${18 + tint[0] / 3}, ${18 + tint[1] / 3}, ${22 + tint[2] / 3})`);
gradient.addColorStop(1, `rgb(${78 + tint[0] / 3}, ${78 + tint[1] / 3}, ${82 + tint[2] / 3})`);
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = "rgba(255,255,255,0.07)";
ctx.lineWidth = 1;
for (let x = 0; x < width; x += 40) {
ctx.beginPath();
ctx.moveTo(x + 0.5, 0);
ctx.lineTo(x + 0.5, height);
ctx.stroke();
}
for (let y = 0; y < height; y += 40) {
ctx.beginPath();
ctx.moveTo(0, y + 0.5);
ctx.lineTo(width, y + 0.5);
ctx.stroke();
}
const cx = width / 2;
const cy = height / 2;
ctx.strokeStyle = "#c8c8d2";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(cx, cy, 60, 0, Math.PI * 2);
ctx.stroke();
ctx.strokeStyle = "#96969f";
ctx.beginPath();
ctx.arc(cx, cy, 28, 0, Math.PI * 2);
ctx.stroke();
ctx.strokeStyle = "#78788a";
for (const [dx, dy] of [
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
]) {
ctx.beginPath();
ctx.moveTo(cx + dx * 40, cy + dy * 40);
ctx.lineTo(cx + dx * 90, cy + dy * 90);
ctx.stroke();
}
for (let bit = 0; bit < 16; bit += 1) {
const on = ((frameId >> bit) & 1) === 1;
ctx.fillStyle = on ? "#78ffa0" : "#34343c";
ctx.fillRect(12 + bit * 14, 12, 10, 10);
}
const t = frameId / 20;
const phase = team === 0 ? 0 : 1.7;
ctx.fillStyle = "#ffd25a";
ctx.beginPath();
ctx.arc(cx + 250 * Math.sin(t * 0.9 + phase), cy + 150 * Math.cos(t * 1.3 + phase), 26, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#5ac8ff";
ctx.fillRect(((t * 60) % (width - 80)) | 0, height - 24, 60, 8);
ctx.font = "600 20px ui-monospace, monospace";
ctx.fillStyle = "#e8e8ec";
ctx.fillText(team === 0 ? "PREVIEW / TEAM 1" : "PREVIEW / TEAM 2", 14, height - 40);
ctx.font = "500 16px ui-monospace, monospace";
ctx.fillStyle = "#8a8a95";
ctx.fillText(`frame ${frameId}`, 14, height - 16);
}