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.
91 lines
6.6 KiB
JavaScript
91 lines
6.6 KiB
JavaScript
// The canned stream of a server-run network (tests/fixtures/mp-sim/*.json): what the session server sends a page, made
|
|
// here from a real seeded game the way the server's realm will make it (section 5 of the server-sim design): live S,
|
|
// a top-level key diff per update, slim R, the effects a day and two actions leave. The client tests (and the fake
|
|
// engine) use these until the integration step records real streams in their place; every one passes
|
|
// tests/lib/mp-sim-shape.js. Run `node tests/lib/mp-sim-fixtures.js` to write them again.
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const { load } = require('../harness');
|
|
|
|
const DIR = path.join(__dirname, '..', 'fixtures', 'mp-sim');
|
|
const CODE = 'K7Q2XD', PID = 'abcd1234', PID2 = 'efgh5678', EPOCH = 'k3f9', DEV = '9f8e7d6c', NOW = 1731000000000;
|
|
const MP = { code: CODE, pid: PID, asn: 201234, seed: 424242, preset: 'oncall', color: '#3a86ff', title: 'Friday' };
|
|
|
|
// In the page: the effects the realm would capture (its epilogue rebinds these the same way), and slim R (5.5).
|
|
const RIG = `window.__fx=[];
|
|
toast=(h,k='',a=[],o=null)=>{if(S)logNote(h,k,a,o);__fx.push(['toast',{h,k,a,o,tick:!!UI.tick}]);};
|
|
sfx=n=>{__fx.push(['sfx',n]);};haptic=n=>{__fx.push(['haptic',n]);};
|
|
floater=(x,y,t,c)=>{if(__fx.filter(f=>f[0]==='floater').length<40)__fx.push(['floater',[x,y,t,c]]);};
|
|
window.__slimR=()=>{const {o}=rwPack(R);for(const k of ['ef','fl','efp','flc','efg','egDesc','l2route','bbF','peered','cityBlocked'])delete o[k];
|
|
const tn={};for(const id in o.tNet)tn[id]=Object.fromEntries(Object.entries(o.tNet[id]).sort((a,b)=>b[1]-a[1]).slice(0,4));o.tNet=tn;
|
|
o.tTop8=transitTop(S.transit.map(t=>t.id),8);
|
|
const r6=v=>typeof v==='number'?(Number.isInteger(v)||!isFinite(v)?v:+v.toPrecision(6)):Array.isArray(v)?v.map(r6):v&&typeof v==='object'?Object.fromEntries(Object.entries(v).map(([k,x])=>[k,r6(x)])):v;
|
|
return r6(JSON.parse(JSON.stringify(o)));};
|
|
window.__keys=()=>Object.fromEntries(Object.keys(S).map(k=>[k,JSON.stringify(S[k])]));`;
|
|
|
|
function make() {
|
|
const { ev, errors, dom } = load({ seed: 4242 });
|
|
ev(`Date.now=()=>${NOW};`);
|
|
// A network of a server session, built the way the server builds one (beginState, then the server's own marks), with
|
|
// the intern on every order and a fiber cut it rushes: the snapshot carries a shift's log and the week's tally.
|
|
ev(`UI.pitched=true;beginState('Packet Pals','fra',{mode:'normal',preset:'oncall',mp:${JSON.stringify(MP)}});S.mp.sim=1;S.tut=null;compute();beginHello();
|
|
S.nextFail=S.nextEvent=1e9;UI.speed=0;UI.started=true;ACT.charter({k:'global'});
|
|
ACT.launchBB({city:'fra'});ACT.buyTransit({city:'fra',net:'cog',tier:1});ACT.buildPop({city:'ams'});ACT.launchBB({city:'ams'});
|
|
UI.pending={a:'fra',b:'ams'};ACT.buildLink({tier:0});closeModal();
|
|
S.lvl=Math.max(S.lvl,2);ACT.opsHire({id:'kevin'});for(const k in S.ops.on)S.ops.on[k]=true;S.ops.floor=0;closeModal();
|
|
for(let i=0;i<45;i++){if(i===20)dayGo(failureRun('cut'));dayTick();}`);
|
|
ev(RIG);
|
|
const snapS = JSON.parse(ev('JSON.stringify(S)'));
|
|
const r0 = JSON.parse(ev('JSON.stringify(__slimR())'));
|
|
const diff = (before) => { const now = JSON.parse(ev('JSON.stringify(__keys())')), s = {}, del = [];
|
|
for (const k of Object.keys(now)) if (now[k] !== before[k]) s[k] = JSON.parse(now[k]);
|
|
for (const k of Object.keys(before)) if (!(k in now)) del.push(k);
|
|
return { s, del, keys: now }; };
|
|
const fx = () => JSON.parse(ev('JSON.stringify(__fx.splice(0))'));
|
|
const clock = { t: 'clock', speed: 1, eff: 1, run: true, why: null, votes: { [PID2]: 4 }, tally: { 4: 1 }, need: 2, active: [PID, PID2], hold: [], idle: [],
|
|
day: 518, last: 1, max: 8, off: { [PID]: S0day(snapS) - 518, [PID2]: 0 } };
|
|
const card = JSON.parse(ev('JSON.stringify(mpCard())'));
|
|
const roster = { t: 'roster', code: CODE, title: 'Friday', preset: 'oncall', seed: MP.seed, created: NOW - 864e5,
|
|
players: [{ pid: PID, name: 'Packet Pals', asn: MP.asn, color: MP.color, card, seen: NOW, joined: NOW - 864e5 },
|
|
{ pid: PID2, name: 'Tubular Bells', asn: 207777, color: '#ff4f9a', card: null, seen: NOW - 6e4, joined: NOW - 36e5 }],
|
|
feed: [{ k: 'join', pid: PID, name: 'Packet Pals', at: NOW - 864e5 }, { k: 'join', pid: PID2, name: 'Tubular Bells', at: NOW - 36e5 }, { k: 'chat', pid: PID2, name: 'Tubular Bells', at: NOW - 6e4, text: 'peer with me?' }],
|
|
on: [PID, PID2] };
|
|
const snap = { t: 'snap', epoch: EPOCH, v: 412, now: NOW, S: snapS, r: r0, clock, roster, since: { days: 37, again: 0 }, keep: [['badge', { id: 'firstpop', v: 'oncall' }]] };
|
|
|
|
// A day: the keys it changed, a new R, what it said.
|
|
let base = JSON.parse(ev('JSON.stringify(__keys())'));
|
|
ev('UI.tick=false;dayTick();');
|
|
const d1 = diff(base), dayFx = fx();
|
|
if (!dayFx.length) dayFx.push(['sfx', 'coin']);
|
|
const updDay = { t: 'upd', from: 412, v: 413, now: NOW + 2500, day: JSON.parse(ev('S.day')), s: d1.s, del: d1.del, r: JSON.parse(ev('JSON.stringify(__slimR())')), fx: dayFx };
|
|
|
|
// Two quick taps in one update: a new POP (the panel moves to it) and a transit port there.
|
|
base = d1.keys;
|
|
ev("ACT.buildPop({city:'par'});");
|
|
const ui1 = { sel: { type: 'city', id: 'par' }, tab: 'cust' };
|
|
ev("ACT.buyTransit({city:'par',net:'cog',tier:1});");
|
|
const d2 = diff(base), actFx = fx();
|
|
const updAct = { t: 'upd', from: 413, v: 414, now: NOW + 3100, day: updDay.day, s: d2.s, del: d2.del, r: JSON.parse(ev('JSON.stringify(__slimR())')), fx: actFx,
|
|
acts: [{ rid: 'a1b2c3d4e5', dev: DEV, ui: ui1 }, { rid: 'a1b2c3d4e6', dev: DEV, ui: {} }] };
|
|
|
|
// A stream's flow records: the link it shows and its traffic's ways in, and a city's Peering tab.
|
|
const sub = JSON.parse(ev(`(()=>{const l=S.links[0],k=edgeKey(l),rec=R.ef[k],egd={};if(rec)for(const e in rec.eg||{})if(R.egDesc[e])egd[e]=R.egDesc[e];
|
|
return JSON.stringify({t:'sub',v:414,ef:rec?{[k]:rec}:{},egd,pf:{fra:pniFlows('fra')}});})()`));
|
|
|
|
if (errors.length) throw new Error('the game failed while making fixtures: ' + errors.join('; '));
|
|
dom.window.close();
|
|
return { snap, 'upd-day': updDay, 'upd-act': updAct, sub, clock, roster };
|
|
}
|
|
// The calendar offset of the network in the fixtures: its own date against the session day (open question 1).
|
|
function S0day(S) { return S.day; }
|
|
|
|
function write() {
|
|
fs.mkdirSync(DIR, { recursive: true });
|
|
const out = make();
|
|
for (const [k, v] of Object.entries(out)) fs.writeFileSync(path.join(DIR, k + '.json'), JSON.stringify(v) + '\n');
|
|
return Object.keys(out);
|
|
}
|
|
|
|
module.exports = { make, write, DIR, CODE, PID, PID2, EPOCH, DEV, NOW };
|
|
|
|
if (require.main === module) console.log('wrote', write().map((k) => `tests/fixtures/mp-sim/${k}.json`).join(', '));
|