seriesoftubes/scripts/lib/fuzz-page.js
jvanvik d1e127da67 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.
2026-11-07 10:26:06 +01:00

214 lines
18 KiB
JavaScript

// Runs inside a game page (scripts/route-fuzz.js loads two: main's and this checkout's).
// The driver picks every move with its own seeded random numbers and sends the same calls to both pages, so the pages
// stay in lockstep; this file only offers what a player could do now (buttons on screen, POPs, cities) and digests what
// routing gave, as strings the driver compares.
(() => {
const DENY = new Set(['animSet', 'cont', 'exportSave', 'importGo', 'importPick', 'install', 'langSet', 'mapSet', 'menu', 'mute', 'newgame', 'phonePlay', 'phoneSpeed',
'prefSet', 'pzShare', 'resetAsk', 'settings', 'settingsClose', 'share', 'shareDl', 'shareGo', 'slotBack', 'slotDel', 'slotDelGo', 'slotExport', 'slotMenu',
'slotRetry', 'slotSpace', 'slotSwitch', 'slotTake', 'soundSet', 'speed', 'start', 'toTitle', 'traceCopy', 'updateLater', 'updateNow', 'vpSet', 'wsShare',
// Leave the game or open what is not this game: the title screen, menus of other modes, AI helpers that fetch.
'closePanel', 'puzzleMenu', 'puzzleDoor', 'scnMenu', 'startScn', 'startWeekly', 'pzStart', 'pzArStart', 'arMenu', 'teSlop', 'help', 'helpClose', 'keepPlaying',
'logOpen', 'pitchSkip', 'pitchGo', 'feedback', 'tutSkip', 'tutNext']);
const sig = (b) => b.act + ' ' + JSON.stringify(b.args);
const btns = (root) => (root ? [...root.querySelectorAll('[data-act]')] : []).filter((b) => !b.disabled && !DENY.has(b.dataset.act) && typeof ACT[b.dataset.act] === 'function')
.map((b) => { let args = {}; try { args = JSON.parse(b.dataset.args || '{}'); } catch (e) {} return { act: b.dataset.act, args }; });
const modalOpen = () => !$('modal').hidden;
const errs = [];
const call = (act, args) => {
try { const r = ACT[act](args || {}); if (r && typeof r.then === 'function') r.catch((e) => errs.push(act + ': ' + e.message)); return ''; } catch (e) { return act + ': ' + (e && e.message); }
};
const fixClock = (t) => { Date.now = () => t; };
// ---------- starts ----------
const homes = () => CITIES.filter((c) => homeOk(c.id)).map((c) => c.id);
// Home cities with rules of their own: China, Korea, Russia, Australia, the Philippines, islands and far places.
const special = () => CITIES.filter((c) => homeOk(c.id) && (c.cn || c.kr || c.ru || ['AU', 'PH', 'NZ', 'JP', 'ID', 'IS', 'FJ', 'PF', 'MU', 'MV', 'CL', 'ZA', 'BR', 'IN', 'SG', 'HK', 'TW', 'KR', 'CN'].includes(c.cc))).map((c) => c.id);
const lists = () => ({ homes: homes(), special: special(), scn: SCN.map((s) => s.id), pz: PZ.map((p) => p.id), ar: AR_RUNS.map((r) => r.id) });
const settle = () => { UI.started = true; UI.pitched = true; UI.speed = 0; if (UI.pendGame) ACT.pitchSkip(); closeModal(); if (S && !pzOn()) compute(); renderAll(); };
const start = ({ kind, id, city, preset, seed, name }) => {
if (window.__reseed) window.__reseed(seed);
fixClock(1.7e12 + seed * 1000);
UI.pitched = true; UI.pendGame = null;
if (pzOn()) pzLeave();
if (kind === 'normal') beginGame(name, city, { mode: 'normal', preset });
else if (kind === 'sandbox') beginGame(name, city, { mode: 'sandbox', rules: { chaos: preset } });
else if (kind === 'scenario') { const sc = SCN.find((x) => x.id === id); beginGame(name, sc.city, { scn: sc }); }
else if (kind === 'weekly') { const wk = weekly(); beginGame(name, wk.city, { scn: wk, seed: wk.seed }); }
else if (kind === 'puzzle') pzBegin(pzDef(id));
else if (kind === 'architect') arBegin(arRunDef(id));
// A saved network (the big fixture): loaded and migrated as an import is, with its incidents and events still on.
else if (kind === 'save') { S = migrate(JSON.parse(window.__fzSave)); }
settle();
return { mode: S.mode, home: S.home, puzzle: pzOn() };
};
// ---------- what a player could do now ----------
// The nearest cities, and those a cable lands from here (subsea seats, IRUs and own cables start there).
const near = (c, n) => CITIES.filter((x) => x.id !== c).map((x) => [x.id, kmBetween(c, x.id)]).sort((a, b) => a[1] - b[1]).slice(0, n).map((x) => x[0])
.concat(CABLE_PAIRS.filter((p) => p.a === c || p.b === c).slice(0, 4).map((p) => (p.a === c ? p.b : p.a)));
const state = () => {
const pops = Object.keys(S.pops), nb = new Set();
for (const p of pops.slice(0, 12)) for (const c of near(p, 8)) if (!S.pops[c]) nb.add(c);
return { pops, near: [...nb], cities: CITIES.length, links: S.links.map((l) => l.id), day: S.day, puzzle: pzOn(), modal: modalOpen(), lost: !!S.lost,
clock: !!rule('clock'), core: S.core ? S.core.lv : 0, nets: NET_IDS.length, transit: S.transit.map((t) => t.id), pni: S.pni.map((p) => p.id), biz: S.biz.map((b) => b.id), offers: (S.offers || []).map((o) => o.id) };
};
// The buttons of one place on screen, after opening it.
const screen = (w) => {
if (w.where === 'modal') return btns($('modal'));
if (w.where === 'city') { UI.sel = { type: 'city', id: w.city }; UI.tab = w.tab; renderAll(); return btns($('panel')); }
if (w.where === 'link') { if (!S.links.some((l) => l.id === w.id)) return []; UI.sel = { type: 'link', id: w.id }; renderAll(); return btns($('panel')); }
if (w.where === 'dash') { $('dash').hidden = false; UI.dtab = w.tab; renderDash(); const b = btns($('dash')); return b; }
if (w.where === 'left') {
UI.left = w.tab; UI.leftOpen = true; renderLeft(); let b = btns($('leftBody'));
if (w.tab !== 'noc') return b;
// The backbone, Auto-TE and DDoS settings moved from the NOC tab into the Routing & DDoS modal: offer them here on
// both pages, in one order, so main's page (settings in the tab) and this one (behind the button) stay in lockstep.
if (typeof routingBody === 'function' && b.some((x) => x.act === 'routingOpen')) { const d = document.createElement('div'); d.innerHTML = routingBody(); b = b.filter((x) => x.act !== 'routingOpen').concat(btns(d)); }
// The traceroute moved into that modal too, and Connections left the tab (it is in the dashboard): drop it on both pages.
b = b.filter((x) => x.act !== 'connOpen');
return b.sort((x, y) => (sig(x) < sig(y) ? -1 : sig(x) > sig(y) ? 1 : 0));
}
if (w.where === 'openLink') { if (!S.pops[w.a]) return []; openLinkModal(w.a, w.b); return modalOpen() ? btns($('modal')) : []; }
return [];
};
const press = (b) => { const e = call(b.act, b.args); return e; };
const close = () => { if (modalOpen()) closeModal(); $('dash').hidden = true; };
// ---------- things a player does with no button handy here ----------
const RS = ['spill at 80%', 'spill at 60%', 'protect customers', 'blackhole ddos'];
const script = (rules) => {
const L = [];
for (const r of rules) {
const [k, a, b] = r;
const net = NETS[a % NETS.length], t = S.transit.length ? NET[S.transit[a % S.transit.length].net] : net;
const pops = Object.keys(S.pops), l = S.links.length ? S.links[a % S.links.length] : null;
if (k === 0) L.push(RS[a % RS.length]);
else if (k === 1) L.push(`${b % 2 ? 'prefer' : 'avoid'} ${['pni', 'ix', 'transit'][b % 3]} for ${net.name.toLowerCase()}`);
else if (k === 2) L.push(`cap transit ${t.name.toLowerCase()} at ${10 + (b % 50)} gbps`);
else if (k === 3) L.push(`${b % 2 ? 'prefer' : 'avoid'} transit ${t.name.toLowerCase()}`);
else if (k === 4) L.push(`prepend transit ${t.name.toLowerCase()} ${1 + (b % 3)}`);
else if (k === 5 && pops.length) L.push(`drain ${CITY[pops[a % pops.length]].name.toLowerCase()}`);
else if (k === 6 && l) L.push(`avoid link ${CITY[l.a].name.toLowerCase()}-${CITY[l.b].name.toLowerCase()}`);
}
return L.join('\n');
};
const poke = (p) => {
const [k, a, b] = p;
if (k === 'te') { if (!S.te) return 'no te'; S.te.script = script(a); S.te.on = !!b; S.core = S.core || { lv: 0, run: 0 }; after(); return S.te.script; }
if (k === 'teOff') { if (S.te) { S.te.on = false; after(); } return ''; }
if (k === 'core') { if (S.core) { const v = a % (S.core.lv + 1); return call('coreSet', { v }); } return ''; }
if (k === 'fail') { S.nextFail = S.day + 1; return ''; }
if (k === 'event') { S.nextEvent = S.day + 1; return ''; }
if (k === 'cash') { S.cash += a; after(); return ''; }
// One level up from the level the game reads (lvl(): S.lvl, or 1 when a page never wrote it down).
if (k === 'lvl') { S.lvl = Math.min(LEVELS.length, (S.lvl || 1) + 1); after(); return ''; }
// Events that move routes, as the game's own events set them.
if (k === 'ev') {
const n = NETS[a % NETS.length].id;
if (b === 0) S.ev.leak = { net: n, until: S.day + 3 };
else if (b === 1) S.ev.rpki = { nets: [n, NETS[(a * 7) % NETS.length].id], until: S.day + 3 };
else if (b === 2) S.ev.depeer = S.day + 3;
else if (b === 3) { const x = Object.keys(S.ixp); if (x.length) { S.ev.ixdown = S.ev.ixdown || {}; S.ev.ixdown[x[a % x.length]] = S.day + 2; } }
else if (b === 4 && S.links.length) S.links[a % S.links.length].cut = S.day + 2;
else if (b === 5 && S.transit.length) S.transit[a % S.transit.length].down = S.day + 2;
// After an update brings networks: the courtesy route to them (sanitize() sets it on load).
else if (b === 7) S.grace = { until: S.day + 4, nets: [n, NETS[(a * 13) % NETS.length].id, NETS[(a * 31) % NETS.length].id], fresh: 0 };
else if (b === 8) S.krExit = S.day + 2;
else if (b === 6) { const p = Object.keys(S.pops); if (p.length > 1) S.pops[p[1 + (a % (p.length - 1))]].down = S.day + 2; }
after(); return '';
}
return 'unknown poke';
};
/* Actions on what the network already has, with arguments a button somewhere would give (some sit in collapsed
lists, or on screens the walker rarely reaches): one list of calls, the driver picks one. */
const direct = () => {
const o = [], add = (act, args) => { if (typeof ACT[act] === 'function') o.push({ act, args }); };
const pops = Object.keys(S.pops), PR = ['high', 'low', 'normal'];
for (const t of S.transit) { add('setPref', { kind: 't', id: t.id, v: PR[(t.id.length + S.day) % 3] }); add('setCarry', { id: t.id, on: !t.carry }); add('lagT', { id: t.id, d: (S.day % 3) ? 1 : -1 }); add('cancelTransit', { id: t.id, ok: true }); add('togglePrem', { id: t.id }); add('cleanPipe', { id: t.id }); }
for (const p of S.pni) { add('setPref', { kind: 'p', id: p.id, v: PR[(p.id.length + S.day) % 3] }); add('lagP', { id: p.id, d: (S.day % 3) ? 1 : -1 }); add('cancelPni', { id: p.id, ok: true }); add('sacrif', { id: p.id }); }
for (const x of Object.keys(S.ixp)) { add('setPref', { kind: 'ix', id: x, v: PR[(x.length + S.day) % 3] }); add('lagIx', { city: x, d: (S.day % 3) ? 1 : -1 }); add('peerAll', { city: x }); add('rs', { city: x }); add('cancelIx', { city: x, sure: true, ok: true });
for (const n of ixMembers(x).slice(0, 30)) add(ixSessions(x).has(n) ? 'unpeer' : 'peer', { city: x, net: n }); }
for (const c of pops) {
for (const x of ixIdsIn(c)) if (S.ixp[x] === undefined) add('buyIx', { city: x, tier: S.day % 3 });
for (const n of NETS) if (n.pops.includes(c)) { if (S.day % 2 === 0 || n.cache) add('pni', { city: c, net: n.id, tier: n.id.length % 3 }); if (n.cache) add('cache', { city: c, net: n.id }); }
for (const n of NETS) if (sellsAt(n, c) && (n.id.length + S.day) % 4 === 0) add('buyTransit', { city: c, net: n.id, tier: (S.day >> 2) % 3 });
add('addRouter', { city: c }); add('upRouter', { city: c, g: 1 + (S.day % 4) }); if (!ixName(c) && !CITY[c].cn) add('foundIx', { city: c }); add('launchBB', { city: c }); add('addSite', { city: c });
}
for (const c of S.caches) add('rmCache', { id: c.id });
for (const l of S.links) { add('setStandby', { id: l.id, on: !l.standby }); add('setMetric', { id: l.id, v: ['backup', 'avoid', 'prefer', 'normal'][(l.id.length + S.day) % 4] }); add('upLink', { id: l.id, tier: (S.day % 4) }); add('rmLink', { id: l.id }); add('addParallel', { id: l.id }); }
for (const b of S.offers || []) add('accept', { id: b.id });
for (const b of S.biz) add('dropBiz', { id: b.id });
for (const l of S.loanOffers || []) add('takeLoan', { id: l.id });
for (const l of S.loans || []) add('repayLoan', { id: l.id });
return o;
};
// ---------- time ----------
const day = () => { if (S.lost || !rule('clock')) return 'no clock'; if (modalOpen()) closeModal(); try { dayTick(); } catch (e) { return 'dayTick: ' + e.message; } return ''; };
// ---------- digests ----------
// Everything routing produced: R in full (with every link's R.ef), the flows (R.fl), and what the PNI lists read.
// An attack's record names its attack by id (as rwPack does): the attack itself is game state, compared with S.
const rj = (v) => JSON.stringify(v, (k, x) => (k === 'attack' && x && typeof x === 'object' ? x.id : x));
const routing = () => {
if (!R) return 'noR';
const r = rj(R), fl = R.fl ? JSON.stringify(R.fl) : '';
let pf = '';
if (typeof pniFlows === 'function' && S && !pzOn()) for (const c of Object.keys(S.pops).slice(0, 4)) try { pf += c + JSON.stringify(pniFlows(c)); } catch (e) { pf += c + 'E' + e.message; }
return r + '\n' + fl + '\n' + pf;
};
const parts = () => { const o = {}; if (!R) return o; for (const k of Object.keys(R)) o[k] = rj(R[k]); o.fl = R.fl ? JSON.stringify(R.fl) : ''; return o; };
// A field only this checkout's game has (a new feature's state, on S or on its customers and offers) cannot match main
// yet: the driver passes those (drop) and they are left out; every field both games have must still match.
// The intern's state two levels deep (ops.on.sales, ops.wk.rev): a standing order one game has and the other retired.
// Compared as a reload makes it (migrate(), keys in order): the same game, whichever way each version writes the
// fields it fills in anyway (main may create a record without the defaults a reload gives it, in another order).
// One reload judges both: the driver passes the base page's live state (sLive) through this checkout's migrate()
// (sDigest(drop, json)), so a repair only one version's reload makes (main's re-aimed an attack whose customer left
// during the day) is no divergence. That reload runs with this page's own S in scope (sanitize reads S.rules through
// rule()): pages in lockstep have the same, and a difference in the rules shows up in the fields they decide. A POP without a router model plays the basic one (RG[p.rgen||0]): that is written
// down first, so the reload does not guess one from its ports (a free upgrade a page from before canonical state got).
// Puzzles are never saved: as they are. The rules stay as they are too: both pages may have set them alike (noIp4),
// and a base that does not know a rule would drop it on the way.
const sortK = (v) => (Array.isArray(v) ? v.map(sortK) : v && typeof v === 'object' ? Object.keys(v).sort().reduce((o, k) => ((o[k] = sortK(v[k])), o), {}) : v);
const sLive = () => JSON.stringify(S === undefined ? null : S);
const sOf = (json) => { const live = JSON.parse(json); if (!live || !S || pzOn() || live.mode === 'puzzle') return live; const u0 = uidN, r0 = S.rng;
try {
for (const c of Object.keys(live.pops || {})) if (live.pops[c] && live.pops[c].rgen === undefined) live.pops[c].rgen = 0;
const rules = live.rules === undefined ? undefined : JSON.parse(JSON.stringify(live.rules));
const s = migrate(live); s.rules = rules; return sortK(s);
} finally { uidN = u0; S.rng = r0; } };
const sNow = () => sOf(sLive());
const opsPaths = (s = sNow()) => { const O = s.ops, out = []; if (!O || typeof O !== 'object') return out;
for (const k of Object.keys(O)) { out.push(k); const v = O[k]; if (v && typeof v === 'object' && !Array.isArray(v)) for (const j of Object.keys(v)) out.push(k + '.' + j); } return out; };
const sKeys = () => { const s = sNow(); return { top: Object.keys(s), items: [...new Set([...(s.biz || []), ...(s.offers || [])].flatMap((x) => Object.keys(x)))], ops: opsPaths(s), orders: typeof OPSK === 'object' ? Object.keys(OPSK) : [] }; };
const opsDrop = (s, ops) => { const o = {}; for (const k of Object.keys(s.ops)) { if (ops.includes(k)) continue; const v = s.ops[k];
if (v && typeof v === 'object' && !Array.isArray(v)) { const w = {}; for (const j of Object.keys(v)) if (!ops.includes(k + '.' + j)) w[j] = v[j]; o[k] = w; } else o[k] = v; } return o; };
// json: a live state to judge (the other page's, from sLive); without it, this page's own.
const sDrop = (drop, json) => { const s = json === undefined ? sNow() : sOf(json); if (!s || typeof s !== 'object' || !drop || (!drop.top.length && !drop.items.length && !(drop.ops || []).length)) return s; const o = {};
for (const k of Object.keys(s)) if (!drop.top.includes(k)) o[k] = k === 'ops' && s.ops && (drop.ops || []).length ? opsDrop(s, drop.ops) : s[k];
for (const k of ['biz', 'offers']) if (Array.isArray(o[k])) o[k] = o[k].map((x) => { const y = {}; for (const f of Object.keys(x)) if (!drop.items.includes(f)) y[f] = x[f]; return y; });
return o; };
const sDigest = (drop, json) => JSON.stringify(sDrop(drop, json));
const sParts = (drop, json) => { const s = sDrop(drop, json), o = {}; if (s && typeof s === 'object') for (const k of Object.keys(s)) o[k] = JSON.stringify(s[k]); return o; };
/* After a move: what the move routed (a), routing again from the same state (b: on this checkout's page the kept
tables are used), and routing from that state with the cache off (c: RC.on=false where there is an RC). b and c
route from the same previous R; c's R is kept, as a plain compute would. */
const check = () => {
if (!S || pzOn() && !R) return { a: routing() };
const a = routing(), prior = R, keep = UI.rkR === prior;
compute(); const b = routing();
R = prior; const rc = typeof RC !== 'undefined';
if (rc) RC.on = false;
try { compute(); } finally { if (rc) RC.on = true; }
const c = routing();
if (keep) UI.rkR = R;
return { a, b, c };
};
const acts = () => Object.keys(ACT);
// Scarce IPv4 off (#150, rule ip4), the same rules object with its keys in the same order on both pages: the driver calls
// it only while the base has no such rule, so the pages play the same game until it lands.
const noIp4 = () => { if (S && S.rules) { const { ip4, mods, ...r } = S.rules; S.rules = { ...r, ip4: false, mods }; } };
window.__fz = { acts, direct, lists, start, state, screen, press, close, poke, day, routing, parts, sKeys, sLive, sDigest, sParts, check, errs, modalOpen, noIp4 };
})();