Server-run play, part 1: game prerequisites and shared contracts

Groundwork for running multiplayer games on the session server; nothing
of it is switched on yet, and the plain build is unchanged.

- A new network in two halves (beginState/beginShow), the coach split into
  tutAdvance/coachDom, toastPop split out of toast, bootNotes() for the
  once-per-load toasts, the five text actions take their text as an
  argument, and srvOn() with its two checks in the loop.
- Live state is what a reload makes of it (migrate() is a no-op on it):
  new POPs, ports, PNIs, customers, attacks, dark fiber and the intern's
  notes are written in their saved shape; a 365-day test proves it.
- pniAll and bstdApply check the quote the sheet showed (bstdApply by its
  plan, within 1% for the daily hardware drift; utilityKit too).
- assemble({mp}) leaves src/game/98-server-play/ and its CSS out of every
  page but a multiplayer build's, which also gets a SIM_ID build id.
- Contracts for the server, protocol and browser parts: the action tables
  and envelope check (scripts/lib/mp-sim-acts.js), event fixtures, a shape
  checker, a fake engine, and load({mp:true}) in the test harness.
- route-fuzz judges both pages with one migrate(), so reload repairs of
  the base no longer count as divergences.
This commit is contained in:
jvanvik 2026-11-07 10:26:06 +01:00
parent e25f843526
commit d1e127da67
60 changed files with 2200 additions and 105 deletions

View file

@ -8,9 +8,14 @@
// MCP server's own page and the tests that ask for it. Everywhere else the line is dropped whole, so
// the page players get is exactly what it would be without it (docs/adr/0004-dev-only-code.md).
//
// The server-run play of a multiplayer build (src/game/98-server-play/ and src/styles/43-server-play.css) is only in a
// page assembled with mp: true, which MP_API=… npm run build asks for; every other page, the one players get on
// GitHub Pages included, has none of it.
//
// node scripts/lib/page.js [-o FILE] the page as it ships (stdout by default)
// node scripts/lib/page.js --source [-o FILE] assembled only, labels as in src/ (what tests use)
// node scripts/lib/page.js --source --dev [-o FILE] the development page: with src/agent/ (what npm run dev serves)
// node scripts/lib/page.js --mp [--source] [-o FILE] a multiplayer build's page: with the server-play client
// node scripts/lib/page.js --ref origin/main -o FILE the page as it ships at a git ref
//
// --ref uses that commit's own assembler, so it keeps working when this file changes; refs from before
@ -22,6 +27,9 @@ const { execFileSync } = require('node:child_process');
const ROOT = path.join(__dirname, '..', '..');
const INCLUDE = /^<!-- @include(-dev)? ([\w./-]+) -->$/;
// Parts of src/ only a multiplayer build's page has (paths as the line map names them; a trailing / is a folder).
const MP_ONLY = ['game/98-server-play/', 'styles/43-server-play.css'];
const mpOnly = (f) => MP_ONLY.some((p) => (p.endsWith('/') ? f.startsWith(p) : f === p));
// Every file under dir, as paths relative to dir with '/' separators, in a stable order
// (plain code-unit comparison, so the order never depends on the machine's locale).
@ -36,11 +44,11 @@ function listFiles(dir, rel = '') {
return out.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
}
// The partials that make up one include, in order: [{file, text}].
function partials(src, dir) {
// The partials that make up one include, in order: [{file, text}]. Without mp, the multiplayer-only ones are left out.
function partials(src, dir, mp = true) {
const base = path.join(src, dir);
if (!fs.existsSync(base)) throw new Error(`@include ${dir}: no such directory under ${path.relative(ROOT, src) || '.'}`);
return listFiles(base).map((r) => {
return listFiles(base).filter((r) => mp || !mpOnly(dir + '/' + r)).map((r) => {
const file = path.join(base, r);
const text = fs.readFileSync(file, 'utf8');
if (text.includes('\r')) throw new Error(`${path.relative(ROOT, file)}: use LF line endings`);
@ -52,7 +60,8 @@ function partials(src, dir) {
// The assembled page as a string, plus a map of which output line came from which source file.
// dev: also the @include-dev parts (the development page); without it, their lines are left out entirely.
function assemble({ src = path.join(ROOT, 'src'), dev = false } = {}) {
// mp: also the multiplayer-only parts (MP_ONLY); without it, those files are left out entirely.
function assemble({ src = path.join(ROOT, 'src'), dev = false, mp = false } = {}) {
const shell = fs.readFileSync(path.join(src, 'index.html'), 'utf8');
const out = [], map = [];
let line = 1;
@ -65,7 +74,7 @@ function assemble({ src = path.join(ROOT, 'src'), dev = false } = {}) {
return;
}
if (m[1] && !dev) return;
for (const p of partials(src, m[2])) {
for (const p of partials(src, m[2], mp)) {
const n = p.text.split('\n').length - 1;
map.push({ file: p.file, from: line, to: line + n - 1 });
out.push(p.text);
@ -95,10 +104,17 @@ function assembleRef(ref, cwd = ROOT) {
}
// The page as it ships: assembled, with labels shrunk to short ids (compile-labels.js). Not version-stamped.
// `dicts` ({lang: label-keyed dictionary}) come back with the same ids.
function shipped(dicts = {}) {
// `dicts` ({lang: label-keyed dictionary}) come back with the same ids. mp: a multiplayer build's page.
function shipped(dicts = {}, { mp = false } = {}) {
const D = require('./dict');
return require('./compile-labels').compile(assemble().html, D.read('en').dict, dicts);
return require('./compile-labels').compile(assemble({ mp }).html, D.read('en').dict, dicts);
}
/* The build id of server-run play (SIM_ID): the first 12 hex digits of sha256 of the multiplayer page as assembled from
src/, labels as written (game and server-play client together, so a change to either is a new id). The build stamps
it into MP pages and version.json; the session server works it out from its own src/ and refuses pages of another. */
function simId(html = assemble({ mp: true }).html) {
return require('node:crypto').createHash('sha256').update(html).digest('hex').slice(0, 12);
}
// The shipped page written to a file, for tools that need a path (perf in a real browser, base vs head).
@ -107,12 +123,13 @@ function pageFile(file = path.join(os.tmpdir(), `tubes-page-${process.pid}.html`
return file;
}
module.exports = { assemble, assembleRef, shipped, pageFile, locate, listFiles, ROOT };
module.exports = { assemble, assembleRef, shipped, simId, pageFile, locate, listFiles, MP_ONLY, ROOT };
if (require.main === module) {
const args = process.argv.slice(2);
const opt = (k) => { const i = args.indexOf(k); return i >= 0 ? args[i + 1] : null; };
const html = opt('--ref') ? assembleRef(opt('--ref')) : args.includes('--source') ? assemble({ dev: args.includes('--dev') }).html : shipped().html;
const mp = args.includes('--mp');
const html = opt('--ref') ? assembleRef(opt('--ref')) : args.includes('--source') ? assemble({ dev: args.includes('--dev'), mp }).html : shipped({}, { mp }).html;
const out = opt('-o');
if (out) { fs.mkdirSync(path.dirname(path.resolve(out)), { recursive: true }); fs.writeFileSync(out, html); }
else process.stdout.write(html);