seriesoftubes/scripts/lib/mcp-headless.js
Wolfgang Nagele 5b76685e5c MCP: the agent facade moves into src/agent/, assembled into the development page only
The command tables and the page-side reads, screens and dispatch (scripts/lib/mcp-commands.js and
mcp-page.js) become src/agent/, the facade window.TUBES.call(tool, args) in the page's own scope.
A new <!-- @include-dev agent --> line in src/index.html pastes it in after the game, but only in the
development page (npm run dev, the MCP server, i18n-check, tests that ask with load({agent:true})).
The build drops the line whole: _site/ is byte for byte what main builds.

Tests load the page players get by default, the build test fails if anything outside src/agent/ names
the facade's api/API_ names, and the pages test checks the built page carries none of it and still
plays. ADR 0004 records the rule.
2026-11-03 16:18:38 -05:00

34 lines
2.4 KiB
JavaScript

// Runs inside the MCP server's own game page (scripts/lib/mcp-game.js), in the game's global scope, after the page has
// loaded: the half of a session only that page has. It starts, loads and exports games, which in a player's browser
// are the player's alone, so it is not part of any page. Everything else a tool does goes through the page's own
// facade (window.TUBES, src/agent/).
(() => {
// Out of the way of the game: no sheet of a previous game, no title screen or pitch, the clock left to the server.
const settle = () => { UI.started = true; UI.pitched = true; UI.speed = 0; if (UI.pendGame) ACT.pitchSkip(); closeModal(); API_SHEET = null; compute(); renderAll(); };
const newGame = ({ city = 'fra', name, preset = 'oncall', seed, asn, chaos } = {}) => {
const c = apiCityOf(city).id;
if (!homeOk(c)) apiFail(`${apiCityName(c)} cannot be a home city.`);
if (!['soft', 'freeze', 'oncall', 'bgp', 'sandbox'].includes(preset)) apiFail('preset is one of soft, freeze, oncall, bgp, sandbox');
if (asn !== undefined && !asnOk(+asn)) apiFail(`AS${asn} cannot be yours: it is reserved, private or another network's. Leave asn out for a free one.`);
if (chaos !== undefined && preset !== 'sandbox') apiFail('chaos is the sandbox\'s dial: give preset "sandbox" with it.');
if (chaos !== undefined && !own(CHAOS, chaos)) apiFail(`chaos is one of ${Object.keys(CHAOS).join(', ')}.`);
if (seed !== undefined && window.__reseed) window.__reseed(Math.max(1, Math.floor(+seed) || 1));
if (UI.pendGame) UI.pendGame = null;
UI.pitched = true;
const nm = String(name || randomNetworkName()).slice(0, 24), was = UI.asnTxt;
// beginGame takes the ASN typed on the title screen.
UI.asnTxt = asn !== undefined ? String(+asn) : '';
try { beginGame(nm, c, preset === 'sandbox' ? { mode: 'sandbox', rules: { chaos: chaos || 'none' } } : { mode: 'normal', preset }); } finally { UI.asnTxt = was; }
settle();
return apiStatus();
};
const loadSave = (txt) => {
const r = parseSave(txt);
if (r.err) apiFail(apiText(r.err));
S = migrate(JSON.parse(JSON.stringify(r.s)));
settle();
return { savedBy: r.ver, ...apiStatus() };
};
const exportSave = (version) => JSON.stringify({ app: 'series-of-tubes', version: /^\d/.test(APP_VERSION) ? APP_VERSION : version, exported: new Date().toISOString(), S: packSave(S) });
window.__mcpHeadless = { newGame, loadSave, exportSave };
})();