Multiplayer for self-hosted copies: sessions, server-side saves, a leaderboard

Off unless the page is built with MP_API set (MP_API=/api/mp npm run
build): a plain build has no door, no chip and makes no requests. The
session server is scripts/mp-server.js (npm run mp), plain node:http,
which can also serve the built site so one process sits behind the
reverse proxy; docs/MULTIPLAYER.md has the nginx and Caddy setups.

A session has a six-character code, one seed and one rules preset, so
every player starts in the same world, and up to twelve players with
their own AS numbers. Saves go to the server on a new game day at most
every 15 s and when the page hides, ordered by a sequence number (409 on
a stale push, and the player picks which one to keep). Tokens live in
localStorage, never in the save; the server keeps only their hash. A
#mp= link moves a network to another device. The session panel shows
the leaderboard, the feed and a short chat; each player's public card
(tier, POPs, links, exchanges, transit, peak, customers, score) carries
what in-game peering between players will need next.

Server limits: body sizes, one upload per player, uploads per address
and in total, sessions per hour, wrong-code lockout; addresses counted
per IPv6 /64, X-Forwarded-For only with --trust-proxy.
This commit is contained in:
jvanvik 2026-11-07 00:42:39 +01:00
parent 2b48a6ad65
commit 95aa9b6532
39 changed files with 2757 additions and 24 deletions

View file

@ -14,8 +14,10 @@ const page = (dev) => PAGE[dev] || (PAGE[dev] = assemble({ dev }).html);
// opts.file: a built or older page to load instead (the perf bench, the release build test).
// opts.agent: the development page, with src/agent/.
// opts.mp: the page with multiplayer on, as MP_API=… npm run build stamps it; opts.fetch: the page's fetch, from the start.
function load(opts = {}) {
const html = opts.file ? fs.readFileSync(opts.file, 'utf8') : page(!!opts.agent);
let html = opts.file ? fs.readFileSync(opts.file, 'utf8') : page(!!opts.agent);
if (opts.mp) html = html.replace("/*__MP_API__*/''", () => JSON.stringify(opts.mp));
const errors = [];
const vc = new VirtualConsole();
vc.on('jsdomError', (e) => {
@ -38,6 +40,7 @@ function load(opts = {}) {
// channel and Web Locks, like real tabs; opts.tab is one tab's sessionStorage, which survives a reload.
if (opts.browser) opts.browser.attach(window, opts.tab || new Map(), opts);
for (const [k, v] of Object.entries(opts.storage || {})) window.localStorage.setItem(k, v);
if (opts.fetch) window.fetch = opts.fetch;
if (opts.navigatorLanguage) Object.defineProperty(window.navigator, 'language', { value: opts.navigatorLanguage });
// A non-English game: jsdom does not fetch the dictionary script, so register it up front.
// (opts.dir: the folder the dictionary comes from, e.g. a build's _site/i18n.)