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.
This commit is contained in:
jvanvik 2026-11-07 10:26:06 +01:00
parent e25f843526
commit d1e127da67
60 changed files with 2200 additions and 105 deletions

View file

@ -27,7 +27,8 @@ function lagsN(){const o=[];for(const t of S.transit)if((t.n||1)>=2&&!(t.down>S.
// Several attacks can be live at once: the primary slot keeps older saves compatible, the rest overlap it.
function ddosAll(s=S,live=true){return [s.ev.ddos,...(s.ev.ddosMore||[])].filter(a=>a&&(!live||a.until>s.day));}
const ddosFind=id=>ddosAll().find(a=>id===undefined||a.id===id);
function ddosDrop(a){const left=ddosAll(S,false).filter(x=>x!==a);S.ev.ddos=left[0]||null;S.ev.ddosMore=left.slice(1);}
// No primary attack is no ddos key at all, as sanitize() leaves it.
function ddosDrop(a){const left=ddosAll(S,false).filter(x=>x!==a);if(left[0])S.ev.ddos=left[0];else delete S.ev.ddos;S.ev.ddosMore=left.slice(1);}
const ddosBb=a=>!a.tgt||a.tgt.startsWith('bb:');
const ddosBiz=a=>ddosBb(a)?null:S.biz.find(b=>b.id===a.tgt)||null;
// An attack ends on its day, when its city is gone, or when its target is no longer your customer.
@ -77,11 +78,13 @@ function ddosTick(){if(scnRule('ddos')){if(S.mode==='sandbox')return;let next=+S
if(!ddosSpicy()&&ddosAll().length)return;
while(S.day>=next&&n++<2){ddosAttack(false);next+=floodGap();}S.scn.nextDdos=next<S.day?S.day+floodGap():next;return;}
if(ddosOff()||S.day<=40)return;if(ddosAll().length>=(ddosSpicy()?3:2))return;if(Math.random()<ddosRate())ddosAttack(false);}
// A start older than a week no longer counts toward the cap: forgotten the morning it ages out, as sanitize() forgets it.
function ddosForget(d){const s=S.ddosStarts;if(Array.isArray(s)&&s.length&&s[0]<=d-7)S.ddosStarts=s.filter(x=>x>d-7);}
// A new attack. With recompute, its outcome is worked out and announced now; otherwise after the day's routing.
function ddosAttack(recompute=true){const fl=scnRule('ddos');ddosExpire();
S.ddosStarts=(S.ddosStarts||[]).filter(d=>d>S.day-7&&d<=S.day);if(S.ddosStarts.length>=14||!Object.keys(S.pops).length)return null;S.ddosStarts.push(S.day);
const [tgt,c]=ddosPickTgt(),vec=pick(ddosVectors(ddGame(tgt))),r=Math.random(),dur=r<0.7?1:r<0.95?2:3;
const a={id:uid(),city:c,tgt,vec,gbps:0,until:S.day+dur,bh:false,start:S.day,carpet:Math.random()<0.2,fresh:1};a.gbps=ddosSize(a);
const a={id:uid(),city:c,tgt,vec,gbps:0,until:S.day+dur,bh:false,start:S.day,carpet:Math.random()<0.2,fresh:1,sac:0,colMax:0,sla:[]};a.gbps=ddosSize(a);
if(fl)a.who=pick(FLOOD_WHO);
if(!S.ev.ddos)S.ev.ddos=a;else(S.ev.ddosMore||(S.ev.ddosMore=[])).push(a);
addInc({type:'ddos',ref:c,atk:a.id,until:a.until,msg:tr('ddos.started',{g:fmtG(a.gbps),vec:DDOS_VEC[vec].n,who:ddosWho(a)}),cost:0});
@ -90,13 +93,15 @@ function ddosAttack(recompute=true){const fl=scnRule('ddos');ddosExpire();
/* After the day's routing: announce new attacks with what they are doing, pull the plug when Auto-RTBH says so, and
teach each lesson once. */
function ddosNews(){runNow(ddosNewsRun());}
// The lessons taught once a game, in the shape sanitize() gives them.
const ddosTaught=()=>S.stats.ddT||(S.stats.ddT={port:0,fs:0,rtbh:0});
// The same as a step of the day: its yield is the routing after Auto-RTBH pulled a plug.
function* ddosNewsRun(){let again=false;const auto=S.ddosAuto&&S.ddosAuto.rtbh;
for(const a of ddosAll()){const m=ddosReport(a);if(!m)continue;
if(auto&&!a.bh&&m.full>0&&ddosCol(a)>=0.01){a.bh=true;a.sac=1;S.stats.rtbh=(S.stats.rtbh||0)+1;ddosRtbhMood(a);again=true;
toast(tr('ddos.auto_rtbh',{who:esc(ddosWho(a))}),'bad',[{l:tr('common.open_noc'),act:'leftTab',args:{t:'noc'}}],{a:'city',v:a.city});}}
if(again)yield;
const T=S.stats.ddT||(S.stats.ddT={});
const T=ddosTaught();
for(const a of ddosAll()){const m=ddosReport(a);if(!m)continue;
if(a.fresh){delete a.fresh;const fl=scnRule('ddos'),col=ddosCol(a),sf=ddosSf(a),bad=col>=0.01||sf<0.97;
const how=a.bh?tr('ddos.news_rtbh'):col>=0.01?tr('ddos.news_ports',{n:m.full,k:m.slowed}):sf<0.97?tr('ddos.news_target',{p:pct(sf)}):tr('ddos.news_held');