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

@ -0,0 +1,36 @@
/* Multiplayer (only in a copy built with MP_API): the title row, the start/join sheet, the session panel, the HUD chip. */
.mpdoor-row{margin-top:8px}.mpdoor{width:100%;background:var(--soft)}
.mpbox{border:2px dashed var(--line);border-radius:14px;padding:4px 12px 10px;margin-top:12px}
.mpbox h3,.mp-panel h3{font-family:var(--display);font-size:18px;margin:10px 0 4px}
.mpjoin,.mpchat{display:flex;gap:8px;align-items:center}
.mpjoin input.t,.mpchat input.t{flex:1 1 auto;min-width:0;width:auto}
#mpCode{text-transform:uppercase;letter-spacing:.12em;font-size:18px}
.mpchat input.t{font-size:16px;padding:6px 10px}
.mpcode{display:flex;align-items:center;gap:10px;border:3px solid var(--ink);border-radius:14px;padding:8px 12px;background:var(--soft);margin-top:10px}
.mpcode small{display:block;font-size:10.5px;font-weight:900;letter-spacing:.09em;text-transform:uppercase;color:var(--ink2)}
.mpcode b{font-family:var(--display);font-size:28px;letter-spacing:.08em;line-height:1.1}
.mpboard,.mpfeed{display:flex;flex-direction:column;gap:4px}
.mpfeed{max-height:220px;overflow:auto;margin-top:6px}
.mprow{display:flex;align-items:center;gap:8px;border:2px solid var(--line);border-radius:12px;padding:6px 10px;min-width:0}
.mprow.me{border-color:var(--me);background:#FFF2F8}
.mprow .grow,.mpf .grow{min-width:0;overflow-wrap:anywhere}
.mprow b{font-family:var(--display);font-size:15px}
.mprk{font:900 15px var(--display);width:20px;text-align:center;flex:0 0 auto}
.mpdot{display:inline-block;width:12px;height:12px;border-radius:50%;border:2px solid var(--ink);flex:0 0 auto}
.mpseen,.mpf small{color:var(--ink2);font-size:11px;white-space:nowrap;flex:0 0 auto}
.mpf{display:flex;align-items:baseline;gap:8px;font-size:13px;line-height:1.35}
.mpf .mpdot{width:9px;height:9px;border-width:1.5px}
.mpf.t1first{font-weight:800}
.mpmove{margin-top:12px}.mpmove summary{cursor:pointer;font-weight:800}
.mpmove input.t{font-size:12px;font-family:monospace;padding:6px 8px}
#hud .mpchip{border-width:3px}
/* No room for the chip on a phone (it is hide-sm), nor on a tablet whose top bar still wraps at fitHud's last step:
where you stand shows as a badge on the menu button, which also opens the session. */
#hud .menubtn{position:relative}
.mpbadge{display:none;position:absolute;top:-8px;right:-8px;background:var(--paper);color:var(--ink);border:2.5px solid var(--ink);border-radius:10px;font:900 11px Nunito,sans-serif;padding:0 5px;line-height:16px;pointer-events:none}
#hud.fit3 .mpchip{display:none !important}
#hud.fit3 .mpbadge{display:block}
@media (max-width:760px){
.mpcode b{font-size:24px}
.mpbadge{display:block}
}