From 999aaca0e804be264511f0434117b2f8a7706cc8 Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Sun, 1 Nov 2026 11:03:37 +0100 Subject: [PATCH] Cache boxes share router ports: as many 100G boxes per port as its speed holds (closes #252) --- CHANGELOG.md | 1 + src/game/10-catalog/40-ports.js | 13 ++++-- src/game/60-features/95-standing-orders.js | 4 +- src/game/70-rules/95-utilities.js | 2 +- .../90-actions/40-sites-peering-caches.js | 4 +- src/game/90-actions/88-utilities.js | 2 +- src/game/93-ui/55-utilities.js | 4 +- tests/cache-ports.test.js | 43 +++++++++++++++++++ 8 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 tests/cache-ports.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index b80c62d..0f1dbfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project are documented here. The project follows ## [Unreleased] +- Cache boxes now share router ports through an aggregation switch: one port carries as many 100G boxes as its speed holds, so 32 fit on a 3.2T port (#252). Each site gets its own. - Opening a link from a list now moves the map to it, zoomed so the whole line fits beside the open panel. - Traffic for a carrier now leaves through that carrier's own transit port at the same POP, instead of being shared with every other transit port there. Networks that buy transit from the carrier get the same treatment, and a new switch on each transit port, Carry here, pulls that traffic across your network from POPs that have no port of that carrier. Prefer, Backup only and RouteScript still win, and Prefer only does so at the POP where you set it. - Hurrying anything at sea now takes a cable ship, and there are only a few dozen in the world, so it costs millions. Speeding up your own subsea cable charters another ship for $3M plus $500 per km for every 30 days saved, and a rush repair on a subsea cut costs $1M plus $200 per km, where a cut on land still costs a few thousand. diff --git a/src/game/10-catalog/40-ports.js b/src/game/10-catalog/40-ports.js index 435cd37..b483e5f 100644 --- a/src/game/10-catalog/40-ports.js +++ b/src/game/10-catalog/40-ports.js @@ -1,14 +1,21 @@ /* Ports are finite. Every transit, IX, PNI and cache port, every wave and every fibre channel takes a router port. 10G ports ride a breakout cable, four to a port. */ const PORT_SLOT=i=>i===0?0.25:1; -function portsUsed(c){let u=0; +// Cache boxes sit behind an aggregation switch: one router port carries as many 100G boxes as its speed holds +// (a 3.2T port takes 32), per site (#252). +const portG=g=>{const s=RG[g].pn;return parseFloat(s)*(s.endsWith('T')?1000:1);}; +const cachePerPort=(c,g)=>{const p=S.pops[c];return p?Math.max(1,Math.floor(portG(g===undefined?(p.rgen||0):g)/CACHE_CAP)):1;}; +function cachePorts(c,extra=[],g){const n={a:0,b:0};for(const x of S.caches)if(x.city===c)n[x.site==='b'?'b':'a']++;for(const x of extra)n[x.site==='b'?'b':'a']++; + const k=cachePerPort(c,g);return Math.ceil(n.a/k)+Math.ceil(n.b/k);} +const cachePortsAdd=(c,adds)=>cachePorts(c,adds)-cachePorts(c); +function portsUsed(c,g){let u=0; for(const l of S.links){if(l.a!==c&&l.b!==c||l.kind==='vxc')continue;u+=l.kind==='df'?(l.ch||[]).length:1;} // A puzzle's content origin (t.pz) hands off on a port of its own: it takes none of yours. for(const t of S.transit)if(t.city===c&&!t.pz)u+=PORT_SLOT(t.tier)*(t.n||1); for(const x of S.pni)if(x.city===c)u+=PORT_SLOT(x.tier)*(x.n||1); for(const x of ixIdsIn(c))if(S.ixp[x]!==undefined&&!S.myIx[x])u+=PORT_SLOT(S.ixp[x])*((S.ixn||{})[x]||1); if(S.myIx[c])u+=1; - u+=S.caches.filter(x=>x.city===c).length+(S.nni||[]).filter(x=>x.city===c).length; + u+=cachePorts(c,[],g)+(S.nni||[]).filter(x=>x.city===c).length; return u;} function portsMax(c,g,rt){const p=S.pops[c];if(!p)return 0;return RG[g===undefined?(p.rgen||0):g].ports*(rt||p.routers||1);} function portGate(c,add){if(!S.pops[c]||add<=0)return true;const u=portsUsed(c),m=portsMax(c);if(u+add<=m+1e-9)return true; @@ -20,4 +27,4 @@ function modelFits(c,g){const G=RG[g],p=S.pops[c];if(!p)return ''; if(S.links.some(l=>(l.a===c||l.b===c)&&l.kind==='wave'&&l.tier>G.maxLT))return tr('ports.a_wave_here_is_faster_than',{pn:G.pn}); if([...S.transit,...S.pni].some(t=>t.city===c&&!t.pz&&t.tier>G.maxP)||ixIdsIn(c).some(x=>S.ixp[x]!==undefined&&!S.myIx[x]&&S.ixp[x]>G.maxP))return tr('ports.a_port_here_is_bigger_than',{pn:G.pn}); if(S.links.some(l=>(l.a===c||l.b===c)&&l.kind==='df'&&(l.ch||[]).some(o=>OPT[o]&&OPT[o].rg>G.cls)))return tr('ports.a_fibre_here_uses_optics_this'); - if(portsUsed(c)>portsMax(c,g))return tr('ports.you_use_u_ports_here_and',{u:fmtPorts(portsUsed(c)),r:p.routers||1,model:G.n,max:portsMax(c,g)});return '';} + if(portsUsed(c,g)>portsMax(c,g))return tr('ports.you_use_u_ports_here_and',{u:fmtPorts(portsUsed(c,g)),r:p.routers||1,model:G.n,max:portsMax(c,g)});return '';} diff --git a/src/game/60-features/95-standing-orders.js b/src/game/60-features/95-standing-orders.js index 81336ec..6f5b4a1 100644 --- a/src/game/60-features/95-standing-orders.js +++ b/src/game/60-features/95-standing-orders.js @@ -166,8 +166,8 @@ const OPS_RUN={ cap+=optG(o,l);X.did('optics',l.a+'|'+l.b);}}}, caches(X){const g=opsP('caches','g'); for(const c of opsPops('caches')){const q=utilityCaches(c);if(!q||!q.add.length)continue; - const want=q.add.filter(x=>(R.cityDem[c]||0)*(NSH[x.net]||0)>=g),free=Math.floor(portsMax(c)-portsUsed(c)+1e-9); - for(const x of want.slice(0,Math.max(0,free))){if(!X.can('caches',0,CACHE_MO))return;S.caches.push({id:uid(),city:c,net:x.net,site:x.site});X.book('caches',0,CACHE_MO);X.did('caches',c);}}}, + const want=q.add.filter(x=>(R.cityDem[c]||0)*(NSH[x.net]||0)>=g),free=()=>portsMax(c)-portsUsed(c)+1e-9; + for(const x of want){if(cachePortsAdd(c,[x])>free())continue;if(!X.can('caches',0,CACHE_MO))return;S.caches.push({id:uid(),city:c,net:x.net,site:x.site});X.book('caches',0,CACHE_MO);X.did('caches',c);}}}, // Weekly, like a person going through PeeringDB on a Monday: route servers, then every willing member (#192). ix(X){if(S.day%7)return;for(const ix of utilityRouteServers()){S.rs[ix]=true;X.did('rs');} for(const row of utilityPeers())for(const id of row.requests){S.peers[id+'@'+row.ix]=true;X.did('peer',row.ix);}}, diff --git a/src/game/70-rules/95-utilities.js b/src/game/70-rules/95-utilities.js index 65385b3..2084ef9 100644 --- a/src/game/70-rules/95-utilities.js +++ b/src/game/70-rules/95-utilities.js @@ -17,7 +17,7 @@ function utilityCaches(city){const p=S&&S.pops[city];if(!p)return null;const sit sites.forEach((site,i)=>{if(!box[i]){add.push({net:n.id,site});box[i]=1;}}); const need=Math.ceil(((R.cityDem&&R.cityDem[city])||0)*(NSH[n.id]||0)/CACHE_CAP-1e-9); for(let t=box.reduce((a,b)=>a+b,0);tc.net+'@'+c.site).join(','),mo:add.length*CACHE_MO,free:Math.max(0,portsMax(city)-portsUsed(city))};} + return {add,ports:cachePortsAdd(city,add),key:add.map(c=>c.net+'@'+c.site).join(','),mo:add.length*CACHE_MO,free:Math.max(0,portsMax(city)-portsUsed(city))};} function utilityKit(city){const p=S&&S.pops[city];if(!p)return null; const gen=!p.gen,site=!p.site2,router=(p.routers||1)<2,g=p.rgen||0; return {gen,site,router,moves:utilityCacheMoves(city),cost:(gen?GEN_COST:0)+(site?SITE2_COST:0)+(router?routerCost(g,1):0), diff --git a/src/game/90-actions/40-sites-peering-caches.js b/src/game/90-actions/40-sites-peering-caches.js index ffe883c..cd78906 100644 --- a/src/game/90-actions/40-sites-peering-caches.js +++ b/src/game/90-actions/40-sites-peering-caches.js @@ -28,8 +28,8 @@ Object.assign(ACT,{ else toast(pick([tr('sites_peering_caches.it_is_with_the_provisioning_team'),tr('sites_peering_caches.we_are_waiting_on_the_colo'),tr('sites_peering_caches.your_ticket_was_merged_into_another'),tr('sites_peering_caches.could_you_resend_the_loa_you'),tr('sites_peering_caches.your_account_manager_is_on_paternity')]));after();}, peerPol:({p})=>{if(!PPOL.includes(p))return;S.ppol=p;after();}, cancelPni:({id,ok})=>{if(!ok&&UI.confirm!==id){UI.confirm=id;renderPanel();return;}S.pni=S.pni.filter(p=>p.id!==id);UI.confirm=null;after();}, - cache:({city,net})=>{const n=NET[net];if(!n||!n.cache||!S.pops[city])return;if((R.demTo[net]||0)x.city===city&&x.net===net),nb=hv.filter(x=>x.site==='b').length;S.caches.push({id:uid(),city,net,site:S.pops[city].site2&&nb{const n=NET[net];if(!n||!n.cache||!S.pops[city])return;if((R.demTo[net]||0)x.city===city&&x.net===net),nb=hv.filter(x=>x.site==='b').length,site=S.pops[city].site2&&nb{S.caches=S.caches.filter(c=>c.id!==id);after();}, // Move one of a CDN's boxes to the other building (#69), so a dark building only takes out part of the cache. cacheSite:({city,net,s})=>{if(s!=='a'&&s!=='b'||!S.pops[city]||!S.pops[city].site2)return;const x=S.caches.find(c=>c.city===city&&c.net===net&&(c.site==='b'?'b':'a')!==s);if(!x)return;x.site=s;sfx('click');after();}, diff --git a/src/game/90-actions/88-utilities.js b/src/game/90-actions/88-utilities.js index 57f818d..5721f8b 100644 --- a/src/game/90-actions/88-utilities.js +++ b/src/game/90-actions/88-utilities.js @@ -12,7 +12,7 @@ Object.assign(ACT,{ after();const msg=tr('utilities.kit_applied',{city:CITY[city].name});if(modal)utilitiesModal(city,msg);else toast(esc(msg),'good');}, utilityCaches:({city,quote,modal})=>{const q=utilityCaches(city);if(!q||!q.add.length)return; if(q.key!==quote){if(modal)utilitiesModal(city,tr('utilities.quote_changed'));else{renderPanel();toast(tr('utilities.quote_changed'));}return;} - if(!portGate(city,q.add.length))return; + if(!portGate(city,q.ports))return; for(const c of q.add)S.caches.push({id:uid(),city,net:c.net,site:c.site}); sfx('build');after();const msg=tr('utilities.caches_requested',{n:q.add.length,city:CITY[city].name});if(modal)utilitiesModal(city,msg);else toast(esc(msg),'good');}, utilityPeers:({city}={})=>{if(!S)return;const rows=utilityPeers();let n=0; diff --git a/src/game/93-ui/55-utilities.js b/src/game/93-ui/55-utilities.js index 9eb55b0..f32be67 100644 --- a/src/game/93-ui/55-utilities.js +++ b/src/game/93-ui/55-utilities.js @@ -13,8 +13,8 @@ function utilityKitLine(city){const q=utilityKit(city);if(!q)return '';const tod function utilityCachesHtml(city,modal=false){const q=utilityCaches(city);if(!q)return ''; return `

${tr('common.embedded_caches')}

${tr('utilities.cache_note')}

${tr('utilities.cache_quote',{n:q.add.length,mo:fmt$(q.mo)})}

- ${q.free${tr('utilities.cache_ports',{need:q.add.length,free:fmtPorts(q.free)})}

`:''} -
${btn(tr('utilities.request_caches',{n:q.add.length}),'utilityCaches',{city,quote:q.key,modal},'pink',!q.add.length||q.free+1e-9
`;} + ${q.free+1e-9${tr('utilities.cache_ports',{need:q.ports,free:fmtPorts(q.free)})}

`:''} +
${btn(tr('utilities.request_caches',{n:q.add.length}),'utilityCaches',{city,quote:q.key,modal},'pink',!q.add.length||q.free+1e-9`;} function utilitiesModal(city,message){if(!S)return;const cities=Object.keys(S.pops).sort((a,b)=>CITY[a].name.localeCompare(CITY[b].name)); city=S.pops[city]?city:UI.sel&&UI.sel.type==='city'&&S.pops[UI.sel.id]?UI.sel.id:S.pops[S.home]?S.home:cities[0]; const rows=utilityPeers(),n=rows.reduce((v,r)=>v+r.requests.length,0),rs=utilityRouteServers().length; diff --git a/tests/cache-ports.test.js b/tests/cache-ports.test.js new file mode 100644 index 0000000..974716c --- /dev/null +++ b/tests/cache-ports.test.js @@ -0,0 +1,43 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { load } = require('./harness'); + +const g = n => `RG.findIndex(r=>r.n===${JSON.stringify(n)})`; +const setup = (ev, router) => ev(`ACT.start({});S.cash=1e8;S.nextFail=1e9;S.nextEvent=1e9;S.pops.fra.rgen=${g(router)};S.caches=[];compute()`); +const boxes = (n, site = 'a') => `for(let i=0;i<${n};i++)S.caches.push({id:'c${site}'+i,city:'fra',net:'cfl',site:'${site}'});`; + +test('cache boxes share a router port, as many as its speed holds (#252)', () => { + const { ev, errors } = load(); + setup(ev, 'Absolute Unit 3.2T'); + const base = ev("portsUsed('fra')"); + ev(boxes(32)); + assert.equal(ev("portsUsed('fra')") - base, 1, '32 boxes behind one 3.2T port'); + ev(boxes(1).replace("'ca'+i", "'cx'+i")); + assert.equal(ev("portsUsed('fra')") - base, 2, 'the 33rd takes a second port'); + ev(boxes(1, 'b')); + assert.equal(ev("portsUsed('fra')") - base, 3, 'the second site has its own'); + assert.deepEqual(errors, []); +}); + +test('the share follows the router: 16 on 1.6T, one each on 100G, and a downgrade counts its ports', () => { + const { ev, errors } = load(); + setup(ev, 'Absolute Unit 1.6T'); + const base = ev("portsUsed('fra')"); + ev(boxes(17)); + assert.equal(ev("portsUsed('fra')") - base, 2); + assert.equal(ev(`portsUsed('fra',${g('Rusty Router 9000')})`) - base, 17, 'on 100G ports every box takes its own'); + assert.equal(ev("cachePortsAdd('fra',[{site:'a'}])"), 0, 'the 18th still fits on the second port'); + assert.deepEqual(errors, []); +}); + +test('requesting caches only needs the ports the boxes really take', () => { + const { ev, errors } = load(); + setup(ev, 'Absolute Unit 3.2T'); + ev("R.demTo={cfl:NET.cfl.cache.min};R.cityDem.fra=900/NSH.cfl"); + const q = JSON.parse(ev("JSON.stringify(utilityCaches('fra'))")); + assert.equal(q.add.length, 9); + assert.equal(q.ports, 1, 'nine boxes, one port'); + ev("ACT.utilityCaches({city:'fra',quote:utilityCaches('fra').key})"); + assert.equal(ev("S.caches.length"), 9); + assert.deepEqual(errors, []); +});