From d6b08dded3dd5ff317e012f52f70d1d8dded22b4 Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Mon, 9 Nov 2026 10:40:39 +0000 Subject: [PATCH] Kept exit orders also check each exit's kind An exit that turned from an exchange into a PNI at the same place in the list, city and preference kept the order made for the exchange, which sorts behind a PNI at the same distance, so the cached routing could differ from routing without the cache (#320 review). The exit signature now includes the kind. Also drops the unused e.vis. --- src/game/80-simulation/29-route-cache.js | 3 ++- src/game/80-simulation/30-compute.js | 4 ++-- tests/route-cache.test.js | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/game/80-simulation/29-route-cache.js b/src/game/80-simulation/29-route-cache.js index 15dc982..a7340e6 100644 --- a/src/game/80-simulation/29-route-cache.js +++ b/src/game/80-simulation/29-route-cache.js @@ -44,7 +44,8 @@ function rcReachPut(X,R2){const r=X.reach;Object.assign(R2.reachable,r.reachable exits, cities and preferences on the same graph keeps the orders it had, and uses them today already. Only the lists that changed start again (scanned today, sorted from tomorrow). */ function rcExitKeep(key,E,okBy,reach,LIST,BRJ,DSEL,RSET,bk=''){const ok=[];for(let j=0;j0?1:0); const P=RC.x&&RC.x.gk===RC.gk&&RC.x.bk===bk&&RC.x.sig?RC.x:null,old=new Map(); if(P)for(const d of P.lists){if(d.cs){for(let k=0;k{for(let i=0;i=P.sig.length||P.sig[x]!==sig[x])return false;}return true;}; diff --git a/src/game/80-simulation/30-compute.js b/src/game/80-simulation/30-compute.js index 1bae9a6..3438f48 100644 --- a/src/game/80-simulation/30-compute.js +++ b/src/game/80-simulation/30-compute.js @@ -22,7 +22,7 @@ function flDefine(R2,c){const {NF,FREM,FCI,FNJ,FCA,FBZ,FGT,FGI,FGP,FGH,FTI,FII,E /* Every exit has the same fields in the same order, whatever its kind, so the routing loop reads them from one object shape (exits of six shapes made each property read there a slow lookup). */ function mkEx(k,id,city,cap,rank,pref,lab,ok,nets,net){return {k,net,id,city,lab,cap,used:0,rank,pref,ok,nets,dir:null,clean:false,prem:false,carry:false,prep:false,sac:false,pnc:false, - ek:'',J:0,byC:null,desc:null,dup:false,told:false,hp:1,dt:0,ci:0,cn:false,vis:0,ts:-1,xi:0,kx:EXK.indexOf(k),fac:undefined};} + ek:'',J:0,byC:null,desc:null,dup:false,told:false,hp:1,dt:0,ci:0,cn:false,ts:-1,xi:0,kx:EXK.indexOf(k),fac:undefined};} // The exit kinds by number (mkEx's kx), for what routing adds up per kind in typed arrays. const EXK=['t','ix','p','c']; function compute(){RW.gen++; @@ -79,7 +79,7 @@ function compute(){RW.gen++; // TSL: a slot per transit port id, for what it carries to each network (R.tNet, added up in TNA). const ekN={},TSL=new Map();(()=>{for(const e of E){e.ek=e.k+':'+e.id;ekN[e.ek]=(ekN[e.ek]||0)+1;} // dup: shares its key with another exit (a hand-edited save), so R.egDesc shows whichever pushed last. - for(let i=0;i1;e.told=false;e.hp=e.pref==='high'?0:1;e.dt=e.k==='ix'?PNI_TIE:0;e.ci=CIDX[e.city];e.cn=cnCity(e.city);e.vis=0;e.ts=-1;if(e.k==='t'){let t=TSL.get(e.id);if(t===undefined)TSL.set(e.id,t=TSL.size);e.ts=t;} + for(let i=0;i1;e.told=false;e.hp=e.pref==='high'?0:1;e.dt=e.k==='ix'?PNI_TIE:0;e.ci=CIDX[e.city];e.cn=cnCity(e.city);e.ts=-1;if(e.k==='t'){let t=TSL.get(e.id);if(t===undefined)TSL.set(e.id,t=TSL.size);e.ts=t;} if(XC)continue; // Index loops or forEach (same order): for…of here made an iterator result per exit and network. const ns=e.nets||NET_IDS;if(Array.isArray(ns)){for(let q=0;qokAdd(e,n));} diff --git a/tests/route-cache.test.js b/tests/route-cache.test.js index 4a0af1d..b7ad481 100644 --- a/tests/route-cache.test.js +++ b/tests/route-cache.test.js @@ -128,3 +128,19 @@ test('lists an exit change leaves alone keep their orders; routing stays the sam for (let d = 0; d < 3; d++) assert.equal(step(ev, 'for(const c in S.pops)if(S.pops[c].bb)S.pops[c].bb.d*=1.03').same, true, `day ${d + 1} after`); assert.deepEqual(errors, []); }); + +test('an exit that turns from an exchange into a PNI at the same place in the list does not keep its old order', () => { + const { ev, errors } = open(); + // The last exchange with a few networks on it, its city A, and a network m there that also gets a PNI at A. + ev(`(()=>{const ixs=Object.keys(S.ixp).filter(c=>S.pops[ixCity(c)]);let p=null; + for(let k=ixs.length-1;k>=0&&!p;k--){const c=ixs[k],ss=[...ixSessions(c)];if(ss.length>2)p={c,A:ixCity(c),m:ss[0]};} + window.__P=p;for(const c of Object.keys(S.ixp).slice(Object.keys(S.ixp).indexOf(p.c)+1))delete S.ixp[c]; + S.pni.push({id:'pz0',net:p.m,city:p.A,tier:1,n:1,pref:'normal'});})();compute();compute();compute();`); + assert.ok(ev('!!__P'), 'an exchange to drop'); + // The exchange goes and a PNI with m at A comes first: an exit of the same city and preference, but now a PNI. + const r = step(ev, `delete S.ixp[__P.c];S.pni.unshift({id:'pz1',net:__P.m,city:__P.A,tier:1,n:1,pref:'normal'})`); + assert.equal(r.g, false, 'same graph'); + assert.equal(r.x, true, 'the exits changed'); + assert.equal(r.same, true, 'the same routing as without the kept tables'); + assert.deepEqual(errors, []); +});