diff --git a/src/game/93-ui/70-link-panels.js b/src/game/93-ui/70-link-panels.js index 85a3af8..9167cb4 100644 --- a/src/game/93-ui/70-link-panels.js +++ b/src/game/93-ui/70-link-panels.js @@ -34,10 +34,11 @@ function vxcPanel(l){ return h; } function prefCtl(kind,id,cur){cur=cur||'normal';return `
${tr('common.egress')}${Object.keys(PREF).map(v=>btn(PREF[v],'setPref',{kind,id,v},cur===v?'pink sm':'ghost sm')).join('')}
`;} -// Trial routing is isolated from the live save, counters and scenic-route memory. +// Trial routing is isolated from the live save, counters and scenic-route memory. "Before" is the live routing, which +// after() keeps current, so a check costs one compute() on a big network, not two. function linkTrial(id){const save=S,live=R,hot=UI.hotMap,cache=TE_CACHE; - try{S=JSON.parse(JSON.stringify(save));const l=S.links.find(x=>x.id===id);if(!l||linkDown(l))return null; - compute();const before=R;l.cut=S.day+1;compute(); + try{const l0=save.links.find(x=>x.id===id);if(!l0||linkDown(l0))return null; + S=JSON.parse(JSON.stringify(save));const l=S.links.find(x=>x.id===id),before=live;l.cut=S.day+1;compute(); const allowed=new Set(Object.keys(S.pops).filter(popUp).concat(remoteCities())),adj={}; for(const x of S.links)if(!linkDown(x)&&allowed.has(x.a)&&allowed.has(x.b)){(adj[x.a]=adj[x.a]||[]).push(x.b);(adj[x.b]=adj[x.b]||[]).push(x.a);} const seen=new Set([l.a]),q=[l.a];for(let i=0;i { + const { ev, errors } = load(); + ev("ACT.start({});S.cash=1e8;ACT.buildPop({city:'bde'});ACT.buildPop({city:'ams'});S.biz=[{id:'circuit',name:'Test Circuit',type:'l2vpn',city:'fra',city2:'bde',bw:80,rate:1,reqs:[]}]"); + ev("S.links=[{id:'main',a:'fra',b:'bde',kind:'wave',tier:1,km:100,mo:100,cut:0},{id:'a',a:'fra',b:'ams',kind:'wave',tier:0,km:100,mo:100,cut:0},{id:'b',a:'ams',b:'bde',kind:'wave',tier:0,km:100,mo:100,cut:0}];compute()"); + ev("window.__n=0;window.__c=compute;compute=function(){window.__n++;return window.__c.apply(this,arguments);}"); + const t = JSON.parse(ev("JSON.stringify(linkTrial('main'))")); + assert.equal(ev('window.__n'), 1, 'one routing pass per check'); + assert.equal(t.split, false); + assert.equal(t.loss, 70); + assert.equal(ev("linkTrial('nope')"), null); + assert.equal(ev('window.__n'), 1, 'an unknown link costs nothing'); + ev('compute=window.__c'); + // the old way: route the copy twice, before and after the cut + const old = ev(`(()=>{const save=S,live=R;try{S=JSON.parse(JSON.stringify(save));compute();const b=R;S.links[0].cut=S.day+1;compute(); + const g=c=>Math.max(0,(b.citySrv[c]||0)-(R.citySrv[c]||0)),l2=Math.max(0,(b.l2.circuit||0)-(R.l2.circuit||0)); + return Object.keys(S.pops).reduce((a,c)=>a+(g(c)>.01?g(c):0),0)+(l2>.01?l2:0);}finally{S=save;R=live;}})()`); + assert.equal(t.loss, old); + assert.deepEqual(errors, []); +});