// The menu, the play screen and the HUD. A game is a module exporting // { id, title, blurb, tags, camera, typed?, start(ctx), stop() }; `ctx` gives it the // panel to fill, the view (the camera, or its own surface when `camera` is // false), a caption over the view, and the camera's frame stream. import { Stream, summary } from "./stream.js"; import { el } from "./ui.js"; import { THUMBS } from "./thumbs.js"; import flappy from "./flappy.js"; import pong from "./pong.js"; import reps from "./reps.js"; import clean from "./clean.js"; import hunt from "./hunt.js"; import boxes from "./boxes.js"; import draw from "./draw.js"; import redlight from "./redlight.js"; import duel from "./duel.js"; import triage from "./triage.js"; import fitcheck from "./fitcheck.js"; import ask from "./ask.js"; const GAMES = [flappy, pong, reps, clean, hunt, boxes, draw, redlight, duel, triage, fitcheck, ask]; const $ = (id) => document.getElementById(id); const video = $("cam"); const view = $("view"); const caption = $("caption"); const panel = $("panel"); const stream = new Stream(video); let current = null; let camera = null; // the getUserMedia promise, asked for on the first camera game // a game that asks typed questions (Boxes) is shown only when the server's model answers them const info = await fetch("info").then((r) => r.json()).catch(() => ({ typed: false })); for (const game of GAMES.filter((g) => !g.typed || info.typed)) { $("menu").append(el("button", { class: "card", onclick: () => open(game) }, el("div", { class: "thumb", html: THUMBS[game.id] }), el("div", { class: "body" }, el("span", { class: "name" }, game.title), el("span", { class: "blurb" }, game.blurb), el("div", { class: "tags" }, ...game.tags.map((t, i) => el("span", { class: i ? "" : "cap" }, t)))))); } function startCamera() { camera ??= navigator.mediaDevices.getUserMedia({ video: { width: 1280, height: 720, frameRate: { ideal: 60 } }, audio: false }) .then((media) => { video.srcObject = media; }) .catch((err) => { camera = null; throw err; }); return camera; } function open(game) { current = game; $("home").hidden = true; $("play").hidden = false; $("where").hidden = true; for (const id of ["back", "game-title", "game-tag"]) $(id).hidden = false; $("game-title").textContent = game.title; $("game-tag").textContent = game.tags[0]; document.title = `${game.title} · System One Arcade`; view.className = game.camera === "pip" ? "view covered" : game.camera ? "view" : "view nocam"; panel.replaceChildren(); say(""); scrollTo(0, 0); const ctx = { panel, view, video, stream, say }; if (!game.camera) { game.start(ctx); return; } const hint = el("div", { class: "camera-hint" }, "Allow the camera to play. The frames go to the model and are dropped once answered."); view.append(hint); startCamera() .then(() => { hint.remove(); if (current === game) game.start(ctx); }) .catch((err) => { hint.textContent = `Camera unavailable: ${err.message}`; }); } function say(text, tone = "") { caption.textContent = text; caption.dataset.tone = tone; } function close() { if (!current) return; current.stop(); stream.stop(); current = null; // what a game added to the view goes with it for (const child of [...view.children]) if (child !== video && child !== caption) child.remove(); $("play").hidden = true; $("home").hidden = false; $("where").hidden = false; for (const id of ["back", "game-title", "game-tag"]) $(id).hidden = true; document.title = "System One Arcade"; } $("back").onclick = close; addEventListener("keydown", (e) => { if (e.key === "Escape") close(); }); const hud = $("hud"); setInterval(() => { const s = summary(); hud.classList.toggle("live", s.rate > 0); hud.lastChild.textContent = s.rate ? `${s.rate.toFixed(0)} decisions/s · model ${s.model.toFixed(0)} ms · round trip ${s.latency.toFixed(0)} ms` : "d1-3B"; }, 250); // "Run it locally": the button on this page and the links in the games' notes document.addEventListener("click", (e) => { if (!e.target.closest("[data-local]")) return; e.preventDefault(); $("local").showModal(); });