diff --git a/src/game/60-features/90-automation.js b/src/game/60-features/90-automation.js index aa4a274..528c15a 100644 --- a/src/game/60-features/90-automation.js +++ b/src/game/60-features/90-automation.js @@ -11,7 +11,14 @@ function autoOffer(o){if(!o||!own(BIZ_T,o.type)||!(o.exp>S.day)||!(o.bw>0)||o.ra // Trial the added demand before signing; failed trials leave the live game untouched. try{S={...S,biz:S.biz}; // shortcut: future events reserve capacity together; use per-date trials if separate bookings must share capacity. - if(future){S.biz=S.biz.map(x=>BIZ_T[x.type].event&&x.start>S.day?{...x,start:S.day}:x);compute();} + if(future){S.biz=S.biz.map(x=>BIZ_T[x.type].event&&x.start>S.day?{...x,start:S.day}:x); + /* Signed if the offer works and nobody who works without it breaks. compute() reads the last result only for + scenic circuits, so without them the pass with the offer can go first: when the offer fails, or nobody is + unhappy with it, the pass without it cannot change the answer and is skipped. */ + const fut=S.biz,all=fut.concat(b); + if(!all.some(x=>x.route==='scenic')){if(T.event)b.start=S.day;if(T.cap)b.live=true;S.biz=all;compute();if(bizWhy(b).length)return null; + const bad=fut.filter(x=>bizWhy(x).length);if(!bad.length)return b;S.biz=fut;compute();return bad.every(x=>bizWhy(x).length)?b:null;} + compute();} const healthy=S.biz.filter(x=>!bizWhy(x).length);if(T.event)b.start=S.day;if(T.cap)b.live=true;S.biz=S.biz.concat(b);compute(); return !bizWhy(b).length&&healthy.every(x=>!bizWhy(x).length)?b:null; }finally{S=state;R=result;UI.hotMap=hot;b.start=o.start;if(T.cap)delete b.live;}} diff --git a/src/game/80-simulation/30-compute.js b/src/game/80-simulation/30-compute.js index 9b35c84..568b6f6 100644 --- a/src/game/80-simulation/30-compute.js +++ b/src/game/80-simulation/30-compute.js @@ -14,6 +14,8 @@ function compute(){ // ALONE[i]: no other city lies within 1e-6 of POP i (so only its own exits can be at distance 0 from it). const SPD={},SPP={},SDi=[],SPi=[],ALONE=new Uint8Array(NC),wW=e=>e.w,wLat=e=>e.lat+0.05; for(const s of pops){const i=CIDX[s],r=dijkstra(adj,i,wW);SDi[i]=SPD[s]=r.D;SPi[i]=SPP[s]=r.P;ALONE[i]=1;for(let x=0;x=0)continue;const D=SDi[i],k=NCOMP++;for(let x=0;xday||scnRule('spat'), leak=ev.leak&&ev.leak.until>day?ev.leak.net:null; for(const t of S.transit){if(!popUp(t.city)||t.down>day||!liveN(t.city,t.site,t.n))continue; @@ -165,8 +167,15 @@ function compute(){ const PASSES=TR&&TR.protect?[x=>x.bz,x=>!x.bz]:null; // Each network's exits by the rank they route at (RouteScript can move an exit per network), in E's order. // Each list also knows whether it holds a preferred exit (hi) and its exits by city (at), in list order. + // With several islands (POPs no backbone joins), each list is split by island: a flow only scans exits it can reach. + const idx=L=>{L.hi=L.some(e=>e.hp===0);L.at=new Map();for(const e of L){const h=L.at.get(e.ci);if(h)h.push(e);else L.at.set(e.ci,[e]);}}; + // Networks with the same exits at a rank share one list (LIST, by content), so what one flow learns serves them all. + const LIST=new Map();for(let i=0;ie.hp===0);L.at=new Map();for(const e of L){const h=L.at.get(e.ci);if(h)h.push(e);else L.at.set(e.ci,[e]);}}} + for(const r in m){const L=m[r],key=L.map(e=>e.xi).join(','),o=LIST.get(key);if(o){m[r]=o;continue;} + let V=L;if(NCOMP>1){const cs=[];for(const e of L){const k=COMP[e.ci];if(k>=0)(cs[k]||(cs[k]=[])).push(e);}for(const s of cs)if(s)idx(s);V={cs};}else idx(L);LIST.set(key,V);m[r]=V;}} + // Attack flows pinned to their entry port route at its plain rank, which RouteScript may have left with no list. + for(const f of flows)if(f.pin)for(const e of ingressE[f.pin]||[])RSET.add(base(e)); // The flow being routed, and from its POP: distances, arriving edges and the paths walked so far, by city index. let f=null,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],REST=[],NEG=[]; // An exit the flow can still try: not visited yet, not full when the flow started, and reachable from the POP. @@ -216,7 +225,7 @@ function compute(){ if(f.rem<=1e-6||(rank===0&&!f.cache)||(pass&&!pass(f)))continue; Dv=SDi[f.ci];Pv=SPi[f.ci];PP=walked[f.ci]||(walked[f.ci]=[]);vis++; if(f.pin){for(const e of ingressE[f.pin]||[])if(base(e)===rank&&e.usede.used=e.cap){full++;continue;}const d=Dv[e.ci];if(d===Infinity)continue;const h=e.hp; if(h=lst.length&&full){let k=0;for(let li=0;li1)continue; - if(G.length>1)sortD(G);} + if(bh>1){gc[f.ci]=[];continue;} + if(G.length>1)sortD(G);gc[f.ci]=G.slice();}} sendGroup(G,0,G.length); if(f.rem<=1e-6)continue; REST.length=0;for(let li=0;li { + const { ev, dom, errors } = load(); + try { + network(ev); + ev("S.links=S.links.filter(l=>!['df4','df9','w3','w4','w8','w9'].includes(l.id));compute()"); + assert.ok(ev("Object.keys(S.pops).some(c=>!pathExists('fra',c))"), 'the network is in pieces'); + const changes = [ + '', + "S.pni.forEach((p,i)=>p.pref=i%3===0?'high':i%3===1?'low':'normal')", + "S.te={on:true,script:'prefer pni for goggle\\nspill at 80%'}", + "S.ev.ddos={city:'fra',until:S.day+3,gbps:300,vec:'syn',entry:{k:'edge'}}", + "S.links[0].cut=S.day+2", + "S.te=null;S.links[0].cut=0;S.links.push({id:'join',a:'par',b:'pek',kind:'wave',tier:2,km:8000,cut:0,mo:100})", + ]; + for (const change of changes) { + ev(change); + ev('window.priorR=JSON.stringify(R);referenceCompute();window.expectedR=JSON.stringify(R);R=JSON.parse(priorR);compute()'); + assert.deepEqual(JSON.parse(ev('JSON.stringify(R)')), JSON.parse(ev('expectedR')), change || 'baseline'); + } + assert.deepEqual(errors, []); + } finally { dom.window.close(); } +}); diff --git a/tests/sales-trial.test.js b/tests/sales-trial.test.js new file mode 100644 index 0000000..814b62b --- /dev/null +++ b/tests/sales-trial.test.js @@ -0,0 +1,73 @@ +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'); + +// The sales trial (#185, the intern's sales order) runs the pass with the offer first and skips the pass without +// it when it cannot change the answer. It must sign exactly the offers the original two-pass trial signs. +const ORIGINAL = `(function(o){if(!o||!own(BIZ_T,o.type)||!(o.exp>S.day)||!(o.bw>0)||o.rate<0||!bCities(o).length||!bCities(o).every(popUp)||!reqOk(o))return null; + const T=BIZ_T[o.type],b=offerCustomer(o);if(T.event&&(!(o.start>S.day)||!(o.dur>0)))return null; + if(o.type==='capfib'&&S.biz.some(x=>x.type==='capfib'&&capPend(x)&&!x.link&&((x.city===o.city&&x.city2===o.city2)||(x.city===o.city2&&x.city2===o.city))))return null; + if(T.fiber){const l=o.type==='capfib'?capFibLink(b):S.links.find(x=>x.id===o.link);if(!l)return null;b.link=l.id; + if(!fiberOk(b)||(o.type==='wwave'&&(!(o.slots>0)||freeCh(l)x.link===l.id&&['pairs','capfib','dfpair'].includes(x.type)))))return null;} + const state=S,result=R,hot=UI.hotMap,future=S.biz.some(x=>BIZ_T[x.type].event&&x.start>S.day); + try{S={...S,biz:S.biz}; + if(future){S.biz=S.biz.map(x=>BIZ_T[x.type].event&&x.start>S.day?{...x,start:S.day}:x);compute();} + const healthy=S.biz.filter(x=>!bizWhy(x).length);if(T.event)b.start=S.day;if(T.cap)b.live=true;S.biz=S.biz.concat(b);compute(); + return !bizWhy(b).length&&healthy.every(x=>!bizWhy(x).length)?b:null; + }finally{S=state;R=result;UI.hotMap=hot;b.start=o.start;if(T.cap)delete b.live;}})`; + +test('the faster sales trial signs exactly the offers the two-pass trial signs', () => { + const { ev, dom, errors } = load(); + try { + const save = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8'); + ev(`S=migrate(JSON.parse(${JSON.stringify(save)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();compute();window.slowOffer=${ORIGINAL};`); + assert.ok(ev('S.biz.some(x=>BIZ_T[x.type].event&&x.start>S.day)'), 'the fixture has events booked ahead, so the shortcut runs'); + const seen = { yes: 0, no: 0, skipped: 0, third: 0 }; + ev('window.__n=0;const _c=compute;window.compute=function(){__n++;return _c.apply(this,arguments)}'); + for (let day = 0; day < 6; day++) { + ev(`__reseed(${1000 + day});S.offers=[];for(let i=0;i<10;i++)spawnOffer();`); + // Small offers that fit, and on alternate days a squeeze, so offers fail on capacity and on others' service too. + ev(`for(const o of S.offers)o.bw=Math.max(1,Math.round(o.bw*${day % 2 ? 1 : 0.05}))`); + if (day % 2) ev('for(const c in S.pops)S.pops[c].bb&&(S.pops[c].bb.d*=1.3)'); + // And circuits over the busiest links, which take bandwidth from customers who had it. + ev(`for(const l of S.links.filter(l=>!linkDown(l)&&S.pops[l.a]&&S.pops[l.b]&&R.load[l.id]>1).sort((x,y)=>R.load[y.id]/capOf(y)-R.load[x.id]/capOf(x)).slice(${day},${day}+2)) + S.offers.push({id:'sq'+l.id,city:l.a,city2:l.b,name:'Squeeze',type:'l2vpn',bw:${day % 2 ? 300 : 2},rate:100,reqs:[],exp:S.day+30})`); + for (const id of JSON.parse(ev('JSON.stringify(S.offers.map(o=>o.id))'))) { + const o = `S.offers.find(x=>x.id===${JSON.stringify(id)})`; + const before = ev('JSON.stringify(R.load)+R.served'); + ev('__n=0');const fast = ev(`JSON.stringify(autoOffer(${o}))`);const n = ev('__n'); + assert.equal(ev('JSON.stringify(R.load)+R.served'), before, 'a trial leaves the live result alone'); + const slow = ev(`JSON.stringify(slowOffer(${o}))`); + assert.equal(fast, slow, `offer ${id} (${ev(`${o}.type`)}) on day ${day}`); + if (fast !== 'null') seen.yes++; else seen.no++; + if (n === 1) seen.skipped++; + if (n === 2) seen.third++; + } + ev('S.nextFail=S.day+99;S.nextEvent=S.day+99;dayTick()'); + } + assert.ok(seen.yes >= 3 && seen.no >= 3, `both answers come up (${JSON.stringify(seen)})`); + assert.ok(seen.skipped >= 5, `the second pass is often skipped (${JSON.stringify(seen)})`); + assert.ok(seen.third >= 3, `and run when the offer hurts someone (${JSON.stringify(seen)})`); + assert.deepEqual(errors, []); + } finally { dom.window.close(); } +}); + +test('with a scenic circuit the sales trial keeps both passes in their original order', () => { + const { ev, dom, errors } = load(); + try { + const save = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8'); + ev(`S=migrate(JSON.parse(${JSON.stringify(save)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();S.biz.find(b=>BIZ_T[b.type].l2).route='scenic';compute();window.slowOffer=${ORIGINAL};`); + ev('window.__n=0;const _c=compute;window.compute=function(){__n++;return _c.apply(this,arguments)}'); + ev('__reseed(77);S.offers=[];for(let i=0;i<6;i++)spawnOffer();'); + for (const id of JSON.parse(ev('JSON.stringify(S.offers.map(o=>o.id))'))) { + const o = `S.offers.find(x=>x.id===${JSON.stringify(id)})`; + ev('__n=0');const fast = ev(`JSON.stringify(autoOffer(${o}))`);const n = ev('__n'); + assert.equal(fast, ev(`JSON.stringify(slowOffer(${o}))`), id); + assert.ok(n === 0 || n === 2, 'both passes, as before'); + } + assert.deepEqual(errors, []); + } finally { dom.window.close(); } +});