25 KiB
Multiplayer phase 2: players inside each other's simulations
Status: design only, not built. Phase 1 (sessions, server-side saves, leaderboard) is in docs/MULTIPLAYER.md.
Conventions:
- Line numbers are
mainat1e7638c. - Phase 1 (multiplayer sessions, merged on
main) providesS.mp,MP,mpCard/mpCardOk, andcleanCardinscripts/lib/mp.js. - Everything runs only when
MP_APIis stamped andS.mpxis not null. The plain GitHub Pages build is unchanged (pages.test.js,build.test.js).
1. Starting point and grafts
Base: "contracts". It had the best totals in two of the three scorings, and the best feasibility and risk.
- Players are pseudo carriers
mp:<pid>, registered only inNETwith single-entryCONE/CONE_S/DOWN/ANC. NETS,NET_IDS,NSH,t1Need(), seen/grace and the gameplay-rules tests never see a player.- No catalog id contains
:, and nothing iteratesNET.
I checked this on big-network (a throwaway probe, not committed): a buyer row on mp:p1 in S.transit plus a player PNI, through compute, 5 day ticks, the intern, every panel and tab, traceroute, t1Why/isTier1 and a migrate round trip.
Results:
- No errors anywhere.
- The rows were kept while registered and dropped once unregistered.
- Warm compute went from 32.3 to 33.2 ms (median of 25 alternating runs).
R.sessionsrose by 1. That confirms the exploit fixed below.
How each fatal flaw the judges found is removed:
| Judged flaw | Fix |
|---|---|
The seller's S.biz row is exposed to dropBiz, decommission, ddosTargets, Pie moods and the mood loop |
The seller side is a deal entry read in exactly two places, demand and money (nets) |
Peering is cosmetic, yet player PNIs count toward isTier2/t1Club |
Player traffic ships with peering (§2.3). Player sessions never enter R.sessions |
| Health is all or nothing, and bought transit reaches every network | A capacity factor (t.mpf) and a miss id list, from day one |
| A↔B transit loops | The server refuses with 409; clients drop loop links in a fixed pid order (nets) |
| A stale or alt buyer keeps paying the commit forever | The seller earns, and carries load, only while the buyer's clock runs. Prices are capped against the catalog |
| A bought wave can be upgraded past its agreed price | upLink/rmLink/setStandby refuse it (gameplay) |
| "An active deal with no row" is ambiguous | A per-deal made flag (§5) |
NKN flips inside compute() |
It is always NN+4 |
Grafted from the other designs:
- From nets: the two-sided route-server rule; cones through players; PNI lead from
hstr, with capacity set by the smaller side; the Tier 1 clique star. - From gameplay: waves, tenders, poaching, consortium seats, notice on each player's own clock, and the LOA handshake.
Rejected: players as NETS entries (a mutable catalog, a rescaled NSH), reach bitmaps (they break between builds), a Tier 1 that depends on other players, automatic ratio strikes, and own-day timers that end a deal (they fire within seconds at 8×; own-day notice only paces each side's own teardown).
2. Mechanics
2.1 Pseudo carriers (src/game/30-networks/45-player-carriers.js, new)
mpBind(s) does three things:
- deletes every
mp:*key; - registers
{id:'mp:'+pid, name, asn, type:'t2', pol:'open', share:0, price:1, pops:[], up:[], ix:[], cc:'', col, blurb:tr('mp.another_player'), mp:1}for eachs.mpx.whoentry; - sets
MPB.k.
It runs at the start of every sanitize and every compute() (if(MPB.k!==(S.mpx?S.mpx.k:''))mpBind(S)). That covers the route worker, slot switches, imports, the failed-load rollback (91-slots.js:105) and puzzles. nocWinState (94-dashboard/90-noc-window.js:45) calls it too.
Because the carrier has pops:[] and is not in NETS, no catalog list ever offers a player:
sellsAtandbuyTransit;- the intern's providers,
peerAllandpniAll; - the peering desk and own-exchange joins.
The peer action returns at ixMembers(city).includes(net) (40-sites-peering-caches.js:14).
2.2 Transit between players (the Tier 1 endgame)
Who can sell: a player publishes tp ($/Gbps/month) while R.reach >= 0.99 and they have at least one POP.
Buying: "Buy transit from X here" posts a t deal (city, tier, LAG). The server makes it live at once (a price list is a standing offer) when all of these hold:
- the seller's card has
tp; - both cards list the city;
- there is no cycle;
- the limits hold.
Price: min(tp, mpCap(city)), where mpCap = 2 × the cheapest catalog transitPerG(n,city,false) there, or the catalog median if nobody sells there.
- Both sides compute it from the static catalog, ignoring each save's own
priceMul. - Both clamp again when they receive the deal.
Buyer side: an ordinary S.transit row {id:'mt-'+deal, net:'mp:'+seller, city, tier, n, mp:deal, mpp:price}.
- Comes free: the router port (
40-ports.js:20), p95 history (10-day-tick.js:37), hot ports, traceroute, incidents, the panels, andisTier1()===false. - Billing:
transitMo(40-billing.js:9) usest.mppinstead oftransitPerG(...)*S.priceMul. The commit floor,PORT.tmoandkrFeestill apply. - Capacity:
30-compute.js:53multiplies byt.mpf= min(1, the seller'ssv[city]/0.95), in 0.05 steps. - Reach:
:54'sokalso needs!mm.has(n)(the seller'smiss). - Route cache: the hash of every miss set joins the flags at
:70(+(S.mpx?S.mpx.xk:'')), sorcExitKeyrebuilds when a seller's reach changes. - Outage:
t.down=S.day+1when the seller's POP is down or the seller has left.
Seller side: a sell entry in S.mpx.deals, never in S.biz.
- Load:
mpDem(bizBw)after the biz loop (:95) addsu, the buyer's reported use. It leaves by catalog share over the seller's own backbone, peering and upstreams; a Tier 1 carries it settlement-free. - Money:
V.mp = Σ price × max(q, 20% of port) × min(1, sfOf(R2,city)/0.95).qis the buyer's p95; the last factor is the business customers' SLA credit.V.mpis added torevMo(:382) and to the revshare base (:380). - Port: the seller's port counts in
portsUsed(10-catalog/40-ports.js, after:21). - Idle buyer: if the buyer's roster
idleis ≥ 300 s (no newer game day reached the server), load and revenue are 0.- A paused or closed buyer has no traffic to bill, and a fast seller cannot bill a frozen buyer.
- An alt account has to keep playing, and still pays at most the capped price.
Loops:
- The server returns 409
loopif the seller already buys from the buyer, directly or through others (a search over at most 12 players). - Clients build the player-transit graph from
who[].ptand add edges sorted by (buyer pid, seller pid), dropping any that closes a cycle, so every client drops the same edge. - So reach passes down a reseller chain one push per hop, never settling at a false 1.
Tier 1 stays defined by the built-in networks.
R.reachonly sumsNSH, so neitherisTier1(80-hot-ports-and-rank.js:20) nor the mood checkR.reach>0.99(10-day-tick.js:51) can depend on players.- Buying player transit costs Tier 1, and
t1Whyalready says so. Selling never does. - Refusing to peer blocks nobody's win.
2.3 Peering between players, carrying traffic
PNI (pni deal, always accepted by hand). Each side adds S.pni {id:'mn-'+deal, net:'mp:'+other, city, tier, n, mp:deal, ready:S.day+lead}.
- Lead time:
lead = pniNow() ? 0 : leadDays(14 + hstr(deal+myPid) % 29), with no random numbers. - Cost: each side pays its own
pniMo. - Capacity: the smaller of the two sides (theirs comes from
dl[].g). It is 0 until both cards reportok: the LOA and cross-connect handshake.
IX (ix deal). The server makes it live if the responder's policy is open, no if closed, and offer if selective. Once live, both sides set S.peers['mp:P@x'].
Route server: no deal. P peers with me at x when both S.rs[x] and P.rs ∋ x hold and both policies are open. That is the same rule on both clients.
Sessions:
mpIx(x)lists the player peers at an exchange.ixSessions(20-exchanges.js:29) stays catalog-only.- At
30-compute.js:61, player PNIs skipR2.sessions++(if(!p.mp)) and count inR.mpSessinstead. - So friends can never inflate
isTier2/t1Club.
Player traffic (new src/game/80-simulation/36-mp-traffic.js). compute.js:343 calls mpTraffic({E,SPP,NRES,HAS,R2,carry,NN}) once, after citySrv (:341-342). The parts are handed in the way ddosLoad gets them (:188).
- Demand: each POP sends
cityEye[c]·w_Pto each active player P.w_P = min(0.02, (biz_P + 0.3·eye_P)/75000), andΣw ≤ 0.1.
- Exits that reach P:
- rank 2: a ready PNI with P, or an IX exit with
mpIx(x) ∋ P; - rank 2.5: the same exits toward Q when P buys from Q, which gives cones through players;
- rank 3: player transit from Q when
P===QorQ.mr ∋ P, and catalog transit whenP.on; pref:'low'moves an exit to rank 4.
- rank 2: a ready PNI with P, or an IX exit with
- Pushing: exits are sorted by distance once per POP. Traffic goes along
walkI(SPP[c],ci,e.ci)and is limited byl.res,NRESande.cap-e.used. - Recording: each push calls
carry(l,e.ek,NN+3,…).NKNis alwaysNN+4(:130), and that column is named'players'at:16. - Effects:
- Transit p95, hot ports and link loads include player traffic, so peering saves transit money.
R2.mp={dem,srv,peer,lost,full,by}records the totals.- The pass runs after every catalog flow, so
R.served,R.demand, moods, levels,peerShareand Tier 1 never change.
Cost of a de-peering war:
computeMoneyappliesV.eyeballs *= 1 - mp.lost/(demand+mp.dem), wherelostis traffic with no route at all.- Two transit-free Tier 1 players who won't peer cannot reach each other: catalog Tier 1s don't pass on routes they learn from peers. Each loses up to 2% of eyeball revenue, which is real Level 3 vs Cogent drama at a bounded cost. Neither loses Tier 1.
- Two eyeball players who don't peer pay transit p95 for each other's traffic.
Clique star (cosmetic): card cq=1 when you are Tier 1 and reach every player whose card says t1 by your own PNI or IX session. The leaderboard sorts the star first.
De-peering: end the deal. Each side removes its rows 7 own days after it first sees the end.
2.4 Competition
Overlap: clComp (75-customer-lifecycle.js:30) also counts rival players whose card POPs cover the customer's cities. The rolls stay hash-stable (clR).
Poaching:
clLeave(:64), for "cheap" or "multi" with a rival POP in the city, posts the customer to/lost.- The server gives it to one rival, chosen by
hstr, as a gift. - The rival checks it with
okBiz,BIZ_Tandrate ≤ catalog×mkt()×1.2, then puts it inS.offerswithrefset to the loser's name.
Tenders:
- Issue: while at least two players are active (
idle< 300 s), the server issues{n, city}every 5 real minutes. The city is seeded from the active players' POPs. - Content: derived from
hstr(seed|n)and theBIZ_Tlists, so every client sees the same tender. - Bids: a ratio r from 0.6 to 1.0. You can bid while paused, and an optional standing auto-bid covers your cities.
- Closing: after 10 real minutes, or once every active player has bid. Wall-clock time, because no game clock is shared.
- Winner: the best
(1.2-r)·sq, wheresqis served share. The tender goes intoS.offerswithmp:1, expiring in 10 own days, and is signed with the normalaccept.
2.5 Shared fiber
Waves and IRUs (wave/iru deals):
- Seller side: books spectrum or pairs via
+mpSold(l.id)in the three counters (60-requirements.js:72,75,37-fiber-broker.js:16). It earnsV.mp += rate×(fiberOk?1:0). - Buyer side: an
S.linksrow{kind:'wave'|'df', own:false, mo:price, mp:deal}, which copies cut/eqDown/ready fromdl.
Consortium seats: built like seatBuy (86-specialist.js:4), at ownCabCost(km)×myPairs/totalPairs (36-specialist.js:54).
Deferred:
- Joint exchanges, which need an id namespace (
isIx,50-sanitize.js:2). - An opt-in "Tier 1 must reach every Tier 1 player" rule.
3. Server (scripts/lib/mp-deals.js, pure logic, with routes in phase 1's scripts/lib/mp.js)
The deal record is public to the session, like PeeringDB. The session keeps a counter dv, bumped on every change.
{id:'d'+base36, k:'t'|'pni'|'ix'|'wave'|'iru', a, b, s (seller), at (city|ix|'a|b'), tier, n, price, slots, st, rev, t0, tl, te, by, why}
| from | event | to | allowed for |
|---|---|---|---|
| – | create t |
live, or 409 not_selling/no_pop/loop/dup/limit |
the buyer |
| – | create ix |
live (b open and present) / no (b closed) / offer |
a |
| – | create pni/wave/iru |
offer |
a |
offer |
accept / decline / withdraw / 7 days pass | live / no / gone / gone |
b / b / a / server |
live |
end | end (quit) |
a or b |
live |
a side's card never reports ok within 7 days |
end (unbuilt) |
server |
live |
no card from a party for 14 days | end (idle) |
server |
| any open deal | a party leaves or is pruned | end (left) |
server |
no/gone/end |
7 days pass | deleted | server |
Errors: every write sends rev. A stale rev or an illegal transition gets 409, and the wrong party gets 403.
Limits:
- one open deal per (pair, kind, place);
- 10 open offers and 24 live deals per player;
- 300 deals per session;
- price 0.05–50, tier 0–15, n 1–8;
- 30 writes per minute per player.
Feed: the server writes a line for each transition ("X sells transit to Y in Frankfurt", "X de-peered Y").
Endpoints:
POST /api/mp/sessions/:code/deals {k,to,at,tier,n,price?,slots?}returns 201{deal,dv}.POST …/deals/:id {op,rev}withopone of accept, decline, withdraw or end.POST …/lostreports a lost customer;POST …/tenders/:n/bid {r};PUT …/autobid.- The roster responses (
GETthe session, andPUTthe card or save) add:deals[](my deals, any state) anddv. Other players' links come from their cards'pt;tenders[]and mygifts[];- each player's
idle: seconds since their card last carried a newer day.
So nothing needs extra polling.
4. Card fields beyond phase 1
Every field below is added to both cleanCard and mpCardOk. Phase 1's transit field stays catalog-only, because NET_RE would silently drop mp: ids.
| field | content |
|---|---|
nv |
build version |
pt |
pids I buy transit from |
rs |
exchanges with my route server on |
tp |
my transit price, or null |
miss |
catalog ids I can't reach, biggest share first, ≤160 (a reach of 0.99 can still miss 72) |
on |
1 if the internet of built-in networks can reach me: catalog transit, PNIs with every t1Need() network, or a seller with on |
mr |
pids I reach |
eye, biz |
Gbps, 2 significant digits |
sq |
served share |
sv |
{city: delivered share} where I sell, ≤64, in 0.05 steps |
dl |
[{id,u,q,g,ok,up,cut,eq,rdy}], one per live deal, ≤64 |
cq |
clique star |
The card grows by about 4 KB at most. ASNs need nothing new: phase 1 already sends the catalog ASNs to avoid (mpAvoid, mp.js:238).
5. Client day flow (src/game/60-features/89-player-deals.js, new)
Phase 1's mpRosterOk is extended to whitelist deals, dv, tenders, gifts and idle (plus new MP_FEED kinds). The validated roster stays in MP.roster, never in S. mpDay(d) runs first in dayPre, right after S.day++ (10-day-tick.js:10):
- Copy changes in. If
dv,idleor any card changed, rebuildS.mpx.whoandS.mpx.dealsfrom the validated data, with values in steps. Then recomputek/xkand callmpBind(S). - Reconcile each deal:
- live, not
made: create my row (S.pops[at],portOk,portGate,spend(setup)) and setmade. If that fails, the deal waits under a "waiting on you" badge, with no client timer; the server ends it asunbuilt. - live,
made, but the row is gone (for example after a decommission, whichdecomImpactwarns about): postendwithwhy:'gone'. Decommissioning a city where I sell also ends those sell deals. - ended (or missing from the server's list): set
until=d+notice(14 own days for transit, 7 for the rest) and remove the rows when it passes.
- live, not
- Copy the coupling values onto the rows:
- on transit rows:
t.mpfandt.down; - on PNI rows: their side's capacity and whether it is ready;
- on link rows: cut, eqDown and ready;
- on my seller entries:
u,qand whether the buyer is active.
- on transit rows:
compute()runs (worker or not), thendayPost, then phase 1's push carries the new fields.dlcomes from yesterday's R.
My own clicks (mpOffer, mpAnswer, mpEnd, mpSell, mpBid) apply as soon as the server answers, with mpApply();after(). If RW.job is set, they apply at the next dayPre instead.
Multiplayer code never calls Math.random.
6. Saves and sanitize
New field: S.mpx = null | {k, xk, dv, who:[≤11], deals:[≤64]}. deals holds only my own deals. Cones, on and who-buys-from-whom come from who[].pt.
mpx:nullis the default innewGame(80-new-game.js:4), so old saves get null.
Order in sanitize: move phase 1's mpSanitize(s) from 50-sanitize.js:113 up to :21, then call mpxSanitize(s). Both now run before opsSanitize (:22) and every isNet filter (:34-35, :54-55).
mpxSanitize:
- When it is null: unless
MP_APIis stamped ands.mpis valid in normal mode. An imported multiplayer save then just loses its player rows. - Players (
who): pids checked against the regex, unique, never my own. - Names: stripped like the phase 1 server does (control characters and
<>&"'\``, at most 32). They are not escaped, becauselabOf/hotPorts` print catalog names unescaped too. - Ids: pass
isCity/isIx.missmust be catalog ids, andpt/mrmust be pids inwho. - Numbers: clamped and stepped.
- Deals: kinds from a whitelist, the other party present,
atchecked,tierwithinPORT, the price at mostmpCap. - Loops:
ptedges are dropped in (buyer, seller) pid order. - Keys:
k/xkare recomputed, never trusted. - Last step: it ends with
mpBind(s).
mpOrphans(s) runs right after :113:
- it drops tagged
transit/pni/links/peersentries that have no deal; - it drops untagged rows on
mp:networks; - it strips
mp/mpp/mpffrom untagged rows, so a forgedmpp:0gives no free transit.
RK_SKIP: mpx stays off it, because compute reads it. Phase 1's mp stays on, because compute must never read it; the route-skip.test.js proxy enforces both.
Size: at most +15 KB per save.
7. Ordered steps
Each step ends with npm test green, the i18n check clean, MCP classification, a CHANGELOG bullet and perf numbers.
1. Dormant core (no server, no UI).
45-player-carriers.js.80-new-game.js:4;50-sanitize.js:21/:113.30-compute.js::28bind check;:53-54mpfandmiss;:61if(!p.mp);:62*(p.mp?mpPniF(p):1);:70xk;:95mpDem;:130/:16the players column;:375-382V.mp.
40-billing.js:9;40-ports.js:21.- Guards:
lagT/lagP/cancelTransit(20-pops-transit-ix.js:6,7,11),unpeer/cancelPni(40-sites-peering-caches.js:16,30) anddownT(21-ix-downgrade.js:6) route tompEndor a renegotiation offer;cleanPipe(80-security-and-china.js:8) refusesmprows;trExits(50-traceroute.js:20) appliesmiss, andhotPorts(80-hot-ports-and-rank.js:4) appliesmpf;connUp(40-connections.js:18) shows no upgrade for these rows;- the intern (
95-standing-orders.js:155,180,250) skipsmprows; upLink/rmLink/setStandby(60-links.js:2,6).
src/agent/30-docs.js.- Tests:
mp-sanitize, andmp-transitwithS.mpxinjected.
2. Deals and transit, end to end.
scripts/lib/mp-deals.jsand themp.jsroutes and timers.- The card fields.
89-player-deals.js, called from10-day-tick.js:10.90-actions/92-player-deals.js, classified insrc/agent/20-commands.js.95-modals/49-player-deals.js: a Deals tab in the session sheet, a bottom sheet at 360 px.90-noc-window.js:45;decomImpact; all six languages.- Tests:
mp-deals-server, andmp-transitwithfetchmocked.
3. Peering and player traffic.
pni/ixdeals,mpIx, and36-mp-traffic.jswith its hook at:343.- The
V.eyeballsfactor,R.mpSessand the star. - Player Gbps in the PNI list and the connections list.
- Test:
mp-peering.
4. Competition: clComp, poaching, tenders.
5. Shared fiber: waves, IRUs, seats.
6. scripts/mp-soak.js: opt-in, never part of npm test.
8. Performance budget
Single-player. Each compute() costs one string comparison, one property read per transit and PNI row, and one more ACN column per edge.
npm run perf -- --base-ref origin/mainmust stay within noise (≤1%).emptyR()gains no keys;R.mp,R.mpSessandV.mpexist only whenS.mpxis set.
Multiplayer. The carrier mechanics are measured above: 32.3 → 33.2 ms. A new big-network-mp workload (the fixture plus 11 players and 20 deals, built by scripts/perf-mp-fixture.js) must meet:
mpTraffic≤ 1.5 ms (one exit sort per POP, then O(players×exits), with paths cached for each pair of cities);mpBind≤ 0.1 ms;- warm
compute≤ +5% compared with the same fixture without players.
A cold route happens only when xk changes, at most once a game day. Report jsdom and --browser numbers.
9. Test plan
mp-deals-server (spawned server, temporary directory):
- every transition and who may make it (403/409);
- the policy outcomes for
ix(live, no, offer); - 409
loopon A→B→C→A; - the limits;
- the
unbuilt,idleandleftends, and expiry, with an injected clock; - card validation;
- restart persistence.
mp-transit:
- the buyer's exit and label,
transitMousingmpp,isTier1false; mpfscales capacity, andmissblocks reach and changesxk;- the seller's demand rises by
u,V.mpfollows the formula, and the seller keeps Tier 1; - an idle buyer gives zero load and revenue;
- a forged
mppis stripped; - worker parity (the
route-worker.test.jspattern); - no
Math.randomcalls; - the inbox is never applied mid-day.
mp-peering:
- a PNI carries traffic only once both ends are ready;
- de-peering raises transit p95;
- two unpeered Tier 1 players lose revenue, and both stay Tier 1;
- 24 player PNIs leave
R.sessionsunchanged; - C reaches B through A's cone;
- the route-server rule is two-sided;
- the PNI lead time is stable.
mp-sanitize:
- fuzzing does not throw and stays bounded: 1,000 entries, HTML in names, loops, unknown ids,
mp:rows in a single-player save; - old saves load;
- a multiplayer-to-single-player slot switch clears every
mp:*; <img onerror>stays text in the panels and the Deals sheet at 360 px.
mp-competition and mp-fiber:
- rival players raise
clComp; - gifts are validated;
- every client derives the same tender;
- cuts are copied;
- a bought wave can't be upgraded.
Existing gates:
route-skip(same digest withS.mpxnull or empty),compute-idempotent,routing-reference,route-cache;gameplay-rules, plus a check that nomp:id is inNETS/NSH;persist,save-size,save-text,mcp-commands,i18n,pages,build,multiplayer,mp-server.
Soak test (the "hardcore" local run):
- The server plus 12 seeded jsdom players in
worker_threads(about 0.5 GB each, with--jobscapped). - Each plays about 2,000 days at its own speed, with a random agent making, answering and ending deals.
- During the run: server restarts, reloads through
migrate, a player leaving, tampered cards and a burst of 409s. - It checks: no errors or NaN, an acyclic deal graph, each seller's
V.mpmatching the buyer's bill, every save reloading, and a single-player control game keepingmain's digest.
10. Failure modes
| Case | Handling |
|---|---|
| The other player is paused or offline | Their last card stands: an idle buyer brings no load or money, and a buyer keeps the seller's last values. After 14 days the server ends the deal (idle) |
| A player leaves | The deal ends (left) and each side tears down after notice. mpBind drops the carrier on the next sanitize |
| Server down | Play goes on with the frozen S.mpx. New deals fail with one toast |
| Two devices | Phase 1's seq/409 picks the save; made settles the rows |
| Version skew | Unknown miss ids are dropped; networks the seller's build lacks count as reachable |
| Tampered card or save | Whitelists, clamps, steps, local price caps, orphan stripping. Money comes only from local formulas |