From e80fd0e453bb0f521bc549f0c4865756697b0ca9 Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Wed, 28 Oct 2026 20:19:41 +0100 Subject: [PATCH] Spicy chaos stretches exactly what an incident holds down stretchInc() stretched anything ending on the same day: always the primary attack (even when the incident was about another one, never the overlapping ones) and any unrelated outage that happened to end then. It now finds the attack by i.atk and everything else by the incident's ref, as clearInc() does, and saves allow the longest stretched attack (11 days). --- src/game/40-progress/30-rules.js | 22 ++++++++++++++-------- src/game/80-simulation/50-sanitize.js | 2 +- tests/ddos-floods.test.js | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/game/40-progress/30-rules.js b/src/game/40-progress/30-rules.js index 928a462..c61421b 100644 --- a/src/game/40-progress/30-rules.js +++ b/src/game/40-progress/30-rules.js @@ -31,12 +31,18 @@ function makeRules(preset,o){const p=Object.prototype.hasOwnProperty.call(PRESET // What the HUD and the share card say about the rules. On-call says nothing: it is the game. function ruleTag(){const p=rulePreset();if(S&&S.mode==='sandbox'){const c=CHAOS[rule('chaos')];return tr('common.sandbox')+(c&&rule('chaos')!=='none'?' '+c.i:'');} return p==='oncall'||p==='sandbox'?'':PRESETS[p].i+' '+PRESETS[p].n;} -/* Spicy outages last longer: stretch a fresh incident, and whatever it holds down until the same day, by half. */ +/* Spicy outages last longer: stretch a fresh incident by half, and exactly what it holds down (found by its ref, as + clearInc() finds it), never something else that happens to end the same day. */ function stretchInc(i){if(rule('chaos')!=='spicy'||!i||!(i.until>S.day)||i.start!==S.day)return;const U=i.until,V=S.day+Math.ceil((U-S.day)*SPICY_LONG);if(V===U)return;i.until=V; - const f=(o,k)=>{if(o&&o[k]===U)o[k]=V;},E=S.ev; - for(const l of S.links){f(l,'cut');f(l,'eqDown');f(l,'deg');f(l.loss,'until');f(l.chFail,'until');} - for(const c in S.pops){const p=S.pops[c];f(p,'down');f(p,'ringcut');f(p,'hot');} - for(const t of S.transit){f(t,'down');f(t.lacp,'until');}for(const p of S.pni)f(p.lacp,'until'); - for(const k of ['ixdown','fab'])if(E[k])for(const x in E[k])if(E[k][x]===U)E[k][x]=V; - if(E.ixlacp)for(const x in E.ixlacp)f(E.ixlacp[x],'until'); - for(const k of ['leak','rpki','pmtud','ddos'])f(E[k],'until');} + const f=(o,k)=>{if(o&&o[k]===U)o[k]=V;},E=S.ev,T=i.type,r=i.ref,L=()=>S.links.find(l=>l.id===r); + if(T==='cut'||T==='redsea'){for(const l of S.links)if((l.trench||l.id)===r)f(l,'cut');} + else if(T==='fatwave'||T==='label')f(L(),'cut'); + else if(T==='ctrlplane'){for(const id of Array.isArray(i.links)?i.links:[])f(S.links.find(l=>l.id===id),'cut');} + else if(T==='amp'||T==='roadmff')f(L(),'eqDown');else if(T==='mtu')f(L(),'deg');else if(T==='loss')f(L()?.loss,'until');else if(T==='optic')f(L()?.chFail,'until'); + else if(['crash','power','oops','cleaner','maint','shutdown'].includes(T))f(S.pops[r],'down'); + else if(T==='ring')f(S.pops[r],'ringcut');else if(T==='ac')f(S.pops[r],'hot'); + else if(T==='transit'||T==='prepend')f(S.transit.find(t=>t.id===r),'down'); + else if((T==='ixloop'||T==='maxpfx')&&E.ixdown)f(E.ixdown,r);else if(T==='fabric'&&E.fab)f(E.fab,r); + else if(T==='lacp'){const [k,...x]=String(r).split(':'),id=x.join(':');f(k==='ix'?E.ixlacp&&E.ixlacp[id]:(k==='t'?S.transit:S.pni).find(o=>o.id===id)?.lacp,'until');} + else if(T==='rpki'||T==='hijack')f(E.rpki,'until');else if(T==='leak'||T==='pmtud')f(E[T],'until');else if(T==='krroute'||T==='depeer')f(E,T); + else if(T==='ddos')f(ddosAll(S,false).find(a=>a.id===i.atk),'until');} diff --git a/src/game/80-simulation/50-sanitize.js b/src/game/80-simulation/50-sanitize.js index 0222376..48c6e55 100644 --- a/src/game/80-simulation/50-sanitize.js +++ b/src/game/80-simulation/50-sanitize.js @@ -65,7 +65,7 @@ function sanitize(s){ s.scrubMode=s.scrubMode==='ondemand'?'ondemand':'always'; // Keep the legacy primary attack so old saves and imports remain compatible; additional floods overlap it. const repairAttack=a=>{if(!a||typeof a!=='object'||!isCity(a.city)||!s.pops[a.city]||!own(DDOS_VEC,a.vec))return null; - a.id=sid(a.id);a.gbps=Math.max(0,num(a.gbps,2));a.until=num(a.until,s.day);a.bh=!!a.bh;a.ixbh=!!a.ixbh;a.start=Math.max(0,Math.min(s.day,num(a.start,s.day)));a.until=Math.min(a.until,a.start+7); + a.id=sid(a.id);a.gbps=Math.max(0,num(a.gbps,2));a.until=num(a.until,s.day);a.bh=!!a.bh;a.ixbh=!!a.ixbh;a.start=Math.max(0,Math.min(s.day,num(a.start,s.day)));a.until=Math.min(a.until,a.start+Math.ceil(7*SPICY_LONG)); if(Array.isArray(a.entries)){a.entries=a.entries.slice(0,3000).filter(en=>en&&isCity(en.city)&&Number.isFinite(+en.share)&&+en.share>0&&(en.k==='rest'||['t','ix','p'].includes(en.k)&&typeof en.id==='string')).map(en=>en.k==='rest'?{k:'rest',city:en.city,share:+en.share}:{k:en.k,id:en.id.slice(0,40),city:en.city,lab:String(en.lab||'').slice(0,60),share:+en.share});const sum=a.entries.reduce((n,en)=>n+en.share,0);if(Number.isFinite(sum)&&sum>0){for(const en of a.entries)en.share/=sum;a.entries=ddosFold(a.entries,a.city);}else delete a.entries;}else delete a.entries; a.sla=Array.isArray(a.sla)?a.sla.filter(x=>typeof x==='string').slice(0,s.biz.length):[]; if(!a.entry||typeof a.entry!=='object')a.entry={k:'edge',lab:''};else{a.entry.lab=String(a.entry.lab||'').slice(0,60);a.entry.id=a.entry.id==null?undefined:String(a.entry.id).slice(0,40);}return a;}; diff --git a/tests/ddos-floods.test.js b/tests/ddos-floods.test.js index f11eb02..33bc30a 100644 --- a/tests/ddos-floods.test.js +++ b/tests/ddos-floods.test.js @@ -84,3 +84,25 @@ test('an attack keeps its biggest ingress ports and folds the long tail, so save assert.equal(ev("S.ev.ddos.entries.find(e=>e.k==='rest').share.toFixed(4)"), (288/300).toFixed(4)); assert.deepEqual(errors, []); }); + +test('spicy chaos stretches exactly the attack (or outage) an incident is about, and saves keep it', () => { + const { ev, errors } = load({ seed: 99 }); + ev("beginGame('R','fra',{mode:'normal',preset:'bgp'});if(UI.pendGame)ACT.pitchSkip();S.cash=1e12;ACT.launchBB({city:'fra'});ACT.buyTransit({city:'fra',net:'cog',tier:1});ACT.buildPop({city:'ams'});S.links.push({id:'L',a:'fra',b:'ams',kind:'wave',tier:1,km:400,mo:1,cut:0});S.day=50"); + // A link that happens to be cut until the same day as the next attacks must stay as it is. + ev("S.links[0].cut=S.day+2"); + for (let k = 0; k < 4; k++) ev("const old=Math.random;let n=0;Math.random=()=>n++===3?0:old();ddosAttack();Math.random=old"); + const rows = JSON.parse(ev("JSON.stringify(S.inc.filter(i=>i.type==='ddos').map(i=>[i.until,ddosAll(S,false).find(a=>a.id===i.atk).until]))")); + assert.equal(rows.length, 4); + for (const [inc, atk] of rows) { assert.equal(atk, inc, 'the attack lasts as long as its incident'); assert.equal(inc, 52, 'one day, stretched by half'); } + assert.equal(ev('S.links[0].cut'), 52, 'an unrelated cut ending the same day is not stretched'); + // The longest spicy attack (7 days, stretched to 11) survives a reload. + ev("S.ev.ddos.until=S.day+11;S=migrate(JSON.parse(JSON.stringify(S)))"); + assert.equal(ev('S.ev.ddos.until-S.day'), 11); + ev("S.ev.ddos.until=S.day+40;S=migrate(JSON.parse(JSON.stringify(S)))"); + assert.equal(ev('S.ev.ddos.until-S.ev.ddos.start'), 11, 'but not longer'); + // Outages are found by their ref: a fiber cut stretches its own link only. + ev("S.links.push({id:'M',a:'fra',b:'ams',kind:'wave',tier:1,km:400,mo:1,cut:S.day+4});S.links[0].cut=S.day+4;S.links[0].trench='T';addInc({type:'cut',ref:'T',until:S.day+4,msg:'x'})"); + assert.equal(ev("S.links[0].cut-S.day"), 6); + assert.equal(ev("S.links.find(l=>l.id==='M').cut-S.day"), 4); + assert.deepEqual(errors, []); +});