diff --git a/CHANGELOG.md b/CHANGELOG.md index a1b51b1..a94afc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project are documented here. The project follows ## [Unreleased] +- Transit bills no longer change when you click something later in the day. After each day, the 95th percentile counted that day twice and dropped the oldest day, so building or changing anything could nudge your transit bill (and the bank's view of it) until the next day came round. - A new start screen with four doors: Puzzles, Sandbox, Normal, and Scenarios & weekly. Every network now picks its rules when it starts, and they stay locked for that network; your saved games carry on exactly as before. - Normal comes in three flavours. Change Freeze has a third of the outages and no DDoS floods, delivers PNIs, cross-connects and builds in days instead of months (#151), and skips the peering inbox, hardware shortages and sold-out routes. On-call is the classic game. BGP at 3 a.m. brings almost twice the outages, lasting half as long again, and twice the DDoS. The HUD and the share card show which one you picked. - Sandbox is a real sandbox now (#188): everything is unlocked from day one, your own subsea cable included, money never runs out, deliveries are quick, and you pick how much chaos you want, from none to spicy. diff --git a/src/game/80-simulation/30-compute.js b/src/game/80-simulation/30-compute.js index 568b6f6..eebd431 100644 --- a/src/game/80-simulation/30-compute.js +++ b/src/game/80-simulation/30-compute.js @@ -93,6 +93,8 @@ function compute(){ const carry=(l,eg,nr,ci,a)=>{let t=l.tr;if(!t){t=l.tr={g:new Map(),n:[],d:[]};EL.push(l);}t.g.set(eg,(t.g.get(eg)||0)+a); let k=l.i*NKN+nr;if(!SEN[k]){SEN[k]=1;t.n.push(nr);}ACN[k]=(ACN[k]||0)+a;k=l.i*NC+ci;if(!SED[k]){SED[k]=1;t.d.push(ci);}ACD[k]=(ACD[k]||0)+a;}; R2.l2route={};R2.l2split={}; + /* compute() reads only S, except here: scenic circuits remember yesterday's path in R.l2route and R.efg (deliberate + hysteresis, so they do not flap). A second call can still move one; the third gives the same answer as the second. */ /* Scenic routing (#28). "Busy" is judged without the customer's own traffic, or it flees its own load and flips paths every day. Two paths that are about as quiet share the load, and yesterday's choice is kept unless today's is clearly better. */ @@ -280,7 +282,7 @@ function compute(){ C.optical=S.links.filter(l=>l.kind==='df').reduce((a,l)=>a+(l.ready>S.day?0:ampCount(l.km)*AMP_MO*(l.own&&isRdm(l.sys)&&!l.sub?Math.max(1,l.pairs|0):1)+(l.regen|0)*REGEN_MO)+(l.spares?Math.round(150+(l.ch||[]).length*80):0),0); C.links=S.links.reduce((a,l)=>a+(l.mo||0),0); C.nni=(S.nni||[]).reduce((a,x)=>a+NNI[x.tier].mo,0); - C.transit=S.transit.reduce((a,t)=>{const q=p95(t,R2.useT[t.id]||0);R2.p95[t.id]=q;return a+transitMo(t,q);},0); + const htd=histToday();C.transit=S.transit.reduce((a,t)=>{const q=p95(t,R2.useT[t.id]||0,htd);R2.p95[t.id]=q;return a+transitMo(t,q);},0); C.ix=Object.keys(S.ixp).reduce((a,c)=>a+ixMo(c),0); C.pni=S.pni.reduce((a,p)=>a+pniMo(p,R2.usePni[p.id]||0),0); C.caches=S.caches.length*CACHE_MO; diff --git a/src/game/80-simulation/40-billing.js b/src/game/80-simulation/40-billing.js index d86b619..c46efa0 100644 --- a/src/game/80-simulation/40-billing.js +++ b/src/game/80-simulation/40-billing.js @@ -1,4 +1,7 @@ -function p95(t,cur){const h=(t.hist||[]).slice(-29).concat([cur]).sort((a,b)=>a-b);return h[Math.max(0,Math.ceil(.95*h.length)-1)];} +// The last 29 days plus today. Once dayTick() has written today into t.hist (S.hist then ends on today too), that +// sample is today's own: leave it out, or every recompute later that day counts today twice and drops a day. +const histToday=()=>{const H=S.hist,l=H&&H[H.length-1];return !!l&&l.d===S.day;}; +function p95(t,cur,today=histToday()){const a=t.hist||[],n=Math.max(0,a.length-(today&&a.length?1:0)),h=a.slice(Math.max(0,n-29),n).concat([cur]).sort((a,b)=>a-b);return h[Math.max(0,Math.ceil(.95*h.length)-1)];} function linkDown(l){return l.cut>S.day||(S.ev.runet>S.day&&ruCity(l.a)!==ruCity(l.b))||(S.ev.gulf>S.day&&((CITY[l.a].qa&&(CITY[l.b].gcc||CITY[l.b].uae))||(CITY[l.b].qa&&(CITY[l.a].gcc||CITY[l.a].uae))))||(l.kind==='vxc'&&!!S.ev.fab&&S.ev.fab[l.prov]>S.day)||(l.kind==='df'&&dfCap(l)<=0);} function remoteCities(){return [...new Set(S.links.filter(l=>l.kind==='vxc').flatMap(l=>[l.a,l.b]))].filter(c=>!S.pops[c]);} function nniUsed(c,prov){return S.links.filter(l=>l.kind==='vxc'&&l.prov===prov&&(l.a===c||l.b===c)).reduce((a,l)=>a+VXC[l.vt].g,0)+S.biz.filter(b=>BIZ_T[b.type].nni&&b.prov===prov&&b.city===c).reduce((a,b)=>a+b.bw,0);} diff --git a/tests/compute-idempotent.test.js b/tests/compute-idempotent.test.js new file mode 100644 index 0000000..4a2470f --- /dev/null +++ b/tests/compute-idempotent.test.js @@ -0,0 +1,42 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { load } = require('./harness'); + +const BIG = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8'); +const big = (t) => { const h = load({ seed: 777 }); t.after(() => h.dom.window.close()); + h.ev(`S=migrate(JSON.parse(${JSON.stringify(BIG)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();compute();`); return h; }; +// R as JSON; DDoS reports hold the live attack, whose `worst` dayTick updates, so it is named by id. +const rj = (ev) => ev("JSON.stringify(R,(k,v)=>k==='attack'||k==='parent'?(v&&v.id):v)"); + +test('95th percentile: a recompute later in the day bills the same 30 days as the day tick did', () => { + const { ev, errors } = load(); + ev("ACT.start({});S.cash=1e9;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});ACT.buyTransit({city:'fra',net:'lvl',tier:1});closeModal()"); + // Thirty days on file, two of them huge: counting today twice (and dropping the oldest day left) would lose one. + ev("S.offers=[];const t=S.transit[0];t.hist=Array.from({length:30},(_,i)=>i===1||i===3?5000:1);S.nextFail=S.nextEvent=S.day+99;dayTick()"); + const tick = ev('R.p95[S.transit[0].id]'), cost = ev('R.cost.transit'); + assert.equal(tick, 5000, 'the tick bills the 29 days before today and today'); + ev('compute()'); + assert.equal(ev('R.p95[S.transit[0].id]'), tick); + assert.equal(ev('R.cost.transit'), cost); + assert.deepEqual(errors, []); +}); + +test('compute() is a pure function of the game: a second call right after a day changes nothing', (t) => { + const { ev, errors } = big(t); + for (let d = 0; d < 4; d++) { + ev('S.nextFail=S.day+99;S.nextEvent=S.day+99;dayTick();compute()'); + const a = rj(ev); ev('compute()'); + assert.equal(rj(ev), a, `day ${d}`); + } + assert.deepEqual(errors, []); +}); + +test('scenic circuits keep the last path unless a new one is clearly better, so two calls converge', (t) => { + const { ev, errors } = big(t); + ev("for(const b of S.biz)if(BIZ_T[b.type].l2)b.route='scenic';R=emptyR();compute()"); + ev('compute()'); const b = rj(ev); + ev('compute()'); assert.equal(rj(ev), b); + assert.deepEqual(errors, []); +});