From 22817c83c59cf1f1867a6433fe31ec460681f08f Mon Sep 17 00:00:00 2001 From: Hugues <16280457+huguesdelamure@users.noreply.github.com> Date: Tue, 27 Oct 2026 13:44:57 +0100 Subject: [PATCH] Remember each used customer name once S.usedNames only answers "was this name used?", but every offer pushed its name again, also once all names were taken: a 5,500-day save holds 560,345 entries for 129 names, 11.8 of its 22.4 million characters. Names are now added once (useName), and sanitize() drops duplicates from older saves. Player save, 30 days from load, against main: routing and the whole state except usedNames identical, usedNames identical as a set; save 22.4 -> 10.6 million characters, first save in Firefox 95 -> 55 ms, JSON.stringify 68 -> 42 ms. Still over Firefox's quota on this save. --- CHANGELOG.md | 1 + src/game/70-rules/10-helpers.js | 3 +++ src/game/80-simulation/50-sanitize.js | 2 +- src/game/80-simulation/70-customers-and-events.js | 2 +- src/game/88-days/10-day-tick.js | 4 ++-- src/game/90-actions/50-customers.js | 2 +- tests/persist.test.js | 10 ++++++++++ 7 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03a3406..e891308 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project are documented here. The project follows ## [Unreleased] - Huge networks stop stuttering every few seconds when their save is too big for the browser to keep: after a failed save the game waits a minute before trying again, then longer, instead of retrying every 5 seconds and after every click. Leaving the page, exporting and new games still save at once. +- Saves of long games are much smaller: the list of customer names already used kept growing by the same names every day (half of a late-game save). Older saves shrink when loaded. - The map of a huge network spends less time working out fiber capacities on every frame, and skips links above or below the screen. ## [1.34.1] — 2026-10-27 diff --git a/src/game/70-rules/10-helpers.js b/src/game/70-rules/10-helpers.js index c082d5e..29d14ec 100644 --- a/src/game/70-rules/10-helpers.js +++ b/src/game/70-rules/10-helpers.js @@ -6,6 +6,9 @@ const pct=x=>Math.round(x*100)+'%'; const reachPct=x=>x>0.999?'100%':x>=0.99?(Math.floor(x*1000)/10).toFixed(1)+'%':pct(x); const clamp=(v,a,b)=>Math.max(a,Math.min(b,v)); const pick=a=>a[Math.floor(Math.random()*a.length)]; +// Names already given to customers, each once: only whether a name is in the list counts. Once every name had been +// used, the same names were added again every day (half a million entries, half of a late-game save). +const useName=n=>{if(!S.usedNames.includes(n))S.usedNames.push(n);}; const esc=s=>String(s).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); function dateStr(d){const y=1+Math.floor(d/360),m=Math.floor(d/30)%12,dd=d%30+1;return tr('helpers.y_y_mon_d',{y,mon:MONTHS[m],d:dd});} function wrapDx(a,b){let dx=b-a;if(dx>W/2)dx-=W;if(dx<-W/2)dx+=W;return dx;} diff --git a/src/game/80-simulation/50-sanitize.js b/src/game/80-simulation/50-sanitize.js index 318903e..fd6ffed 100644 --- a/src/game/80-simulation/50-sanitize.js +++ b/src/game/80-simulation/50-sanitize.js @@ -43,7 +43,7 @@ function sanitize(s){ s.biz=s.biz.filter(okBiz).map(fixBiz);s.offers=s.offers.filter(okBiz).map(fixBiz); s.inc=s.inc.filter(i=>i&&typeof i.type==='string').map(i=>(i.id=sid(i.id),i.until=num(i.until,s.day),i.cost=num(i.cost,0),i.msg=String(i.msg||'').slice(0,400),i.fix=i.fix==null?undefined:String(i.fix).slice(0,60),i)); s.loans=s.loans.filter(l=>l&&own(BANKS,l.bank));for(const l of s.loans){if(l.model!==undefined&&!['balloon','storm','transit','astro'].includes(l.model))delete l.model;for(const k of ['apr0','cut','share'])if(l[k]!==undefined&&!isFinite(+l[k]))delete l[k];if(!Array.isArray(l.covs))l.covs=[];l.covs=l.covs.filter(c=>own(COVS,c));}s.loanOffers=s.loanOffers.filter(l=>l&&own(BANKS,l.bank)); - s.usedNames=s.usedNames.filter(x=>typeof x==='string');s.log=s.log.filter(e=>e&&typeof e.t==='string').slice(0,LOG_KEEP).map(e=>{const o=e.o&&typeof e.o==='object'&&LOG_OPEN.includes(e.o.a)?(e.o.a==='bank'||e.o.a==='inbox'?{a:e.o.a}:typeof e.o.v==='string'&&e.o.v.length<=64&&(e.o.a!=='city'||isCity(e.o.v))&&(e.o.a!=='tab'||LOG_TABS.includes(e.o.v))?{a:e.o.a,v:e.o.v}:null):null; + s.usedNames=[...new Set(s.usedNames.filter(x=>typeof x==='string'))];s.log=s.log.filter(e=>e&&typeof e.t==='string').slice(0,LOG_KEEP).map(e=>{const o=e.o&&typeof e.o==='object'&&LOG_OPEN.includes(e.o.a)?(e.o.a==='bank'||e.o.a==='inbox'?{a:e.o.a}:typeof e.o.v==='string'&&e.o.v.length<=64&&(e.o.a!=='city'||isCity(e.o.v))&&(e.o.a!=='tab'||LOG_TABS.includes(e.o.v))?{a:e.o.a,v:e.o.v}:null):null; return {id:typeof e.id==='string'&&e.id.length<=40?e.id:uid(),d:num(e.d,0),t:e.t.slice(0,600),h:typeof e.h==='string'?e.h.slice(0,200):'',k:LOG_KINDS.includes(e.k)?e.k:'',at:Number.isFinite(+e.at)?+e.at:0,o,n:int(e.n,1,9999,1)};}); // One scrubbing center became a list (#117): older saves carry theirs over. if(!Array.isArray(s.scrubs))s.scrubs=s.scrubAt&&isCity(s.scrubAt)?[{city:s.scrubAt,tier:s.scrubTier}]:[];delete s.scrubAt;delete s.scrubTier; diff --git a/src/game/80-simulation/70-customers-and-events.js b/src/game/80-simulation/70-customers-and-events.js index 27142ff..1b90c6e 100644 --- a/src/game/80-simulation/70-customers-and-events.js +++ b/src/game/80-simulation/70-customers-and-events.js @@ -31,7 +31,7 @@ function spawnMega(t,T,nm,mine){ S.offers.push({id:uid(),city:l.a,city2:l.b,link:l.id,what:tr('customers_and_events.n_fiber_pairs_in_your_a',{n,a:CITY[l.a].name,b:CITY[l.b].name}),name:nm(),type:t,bw:1,rate:Math.round(dfLeaseMo(l.km)*n*0.2),reqs:[],exp:S.day+30,mega:true});} } function spawnSpecial(t){const T=BIZ_T[t],mine=Object.keys(S.pops),lv=S.lvl||1; - const nm=()=>{const list=T.names.filter(n=>!S.usedNames.includes(n));const n=list.length?pick(list):pick(T.names);S.usedNames.push(n);return n;}; + const nm=()=>{const list=T.names.filter(n=>!S.usedNames.includes(n));const n=list.length?pick(list):pick(T.names);useName(n);return n;}; if(T.mega){spawnMega(t,T,nm,mine);return;} if(T.event){const city=Math.random()<0.85&&mine.length?pick(mine):pick(OFFC);const bw=pick(T.opts.slice(0,Math.min(T.opts.length,lv+2)));const dur=3+Math.floor(Math.random()*5),start=S.day+12+Math.floor(Math.random()*14); S.offers.push({id:uid(),city,city2:null,name:tr('customers_and_events.name_in_city',{name:nm(),city:CITY[city].name}),type:t,bw,rate:0,reqs:[],start,dur,fee:Math.round(bw*dur*70),exp:start-2});return;} diff --git a/src/game/88-days/10-day-tick.js b/src/game/88-days/10-day-tick.js index a7213b4..4fcd4d6 100644 --- a/src/game/88-days/10-day-tick.js +++ b/src/game/88-days/10-day-tick.js @@ -86,7 +86,7 @@ function spawnOffer(){ const t=pick(types),T=BIZ_T[t]; if(T.event||T.sdwan||T.fiber||T.mega){spawnSpecial(t);return;} if(T.nni){const city=pick(fabC),prov=pick(FABS.filter(f=>f.cities.includes(city))).id,bw=pick(T.opts),others=mine.filter(c=>c!==city),city2=T.l2&&others.length?pick(others):null;if(T.l2&&!city2)return; - const base=pick(BIZ_N.filter(n=>!S.usedNames.includes(n)).concat(BIZ_N)).replace(/ (Inc\.|Co\.|Ltd)$/,'');const name=tr('day_tick.name_via_fab',{name:base,fab:FAB[prov].name});S.usedNames.push(base); + const base=pick(BIZ_N.filter(n=>!S.usedNames.includes(n)).concat(BIZ_N)).replace(/ (Inc\.|Co\.|Ltd)$/,'');const name=tr('day_tick.name_via_fab',{name:base,fab:FAB[prov].name});useName(base); const rate=T.l2?Math.round((150+kmBetween(city,city2)*0.08)*T.km):T.rate; S.offers.push({id:uid(),city,city2,prov,name,type:t,bw,rate,reqs:['nni'],exp:S.day+40});return;} let city=(Math.random()<0.65&&mine.length)?pick(mine):pick(OFFC.concat(cnOk()?['pek','sha']:[])),city2=null; @@ -94,7 +94,7 @@ function spawnOffer(){ let bw=T.lv>=2?pick(T.opts):pick(T.opts.slice(0,mine.length>=4?4:3)); const list=(T.names||BIZ_N).filter(n=>!S.usedNames.includes(n)); let name=list.length?pick(list):(T.names?pick(T.names):'Startup #'+Math.floor(Math.random()*999)); - S.usedNames.push(name); + useName(name); if(t==='provedor')city=pick(br); if(t==='hft'){const fin=['nyc','chi','lon','fra','tok','sin','hkg','ams','ash'],mf=mine.filter(c=>fin.includes(c));city=mf.length?pick(mf):pick(mine);city2=pick(fin.filter(c=>c!==city));} let rate=T.l2?Math.round((150+kmBetween(city,city2)*0.08)*T.km):T.rate; diff --git a/src/game/90-actions/50-customers.js b/src/game/90-actions/50-customers.js index 0ffd772..d83cb3a 100644 --- a/src/game/90-actions/50-customers.js +++ b/src/game/90-actions/50-customers.js @@ -1,7 +1,7 @@ Object.assign(ACT,{ advertise:({id})=>{const l=S.links.find(x=>x.id===id);if(!l||l.kind!=='df'||l.ready>S.day)return;if(S.offers.some(o=>o.link===l.id))return; const t=freeCh(l)>=1?'wwave':pairFree(l)?'dfpair':null;if(!t){toast(tr('customers.nothing_spare_to_sell_on_this'),'bad');sfx('err');return;} - const T=BIZ_T[t],nm=()=>{const list=T.names.filter(n=>!S.usedNames.includes(n)),n=list.length?pick(list):pick(T.names);S.usedNames.push(n);return n;}; + const T=BIZ_T[t],nm=()=>{const list=T.names.filter(n=>!S.usedNames.includes(n)),n=list.length?pick(list):pick(T.names);useName(n);return n;}; // A capacity broker finds a buyer at once and keeps 10% of the monthly price. const o=fiberOffer(t,l,nm,0.9);o.broker=true;S.offers.push(o);toast(tr('customers.listed_with_a_capacity_broker_name',{name:esc(o.name),what:esc(o.what)}),'good');sfx('click');after();}, accept:({id})=>{const o=S.offers.find(x=>x.id===id);if(!o)return;if(!bHas(o)){toast(tr('customers.you_need_a_pop_in_every'),'bad');return;} diff --git a/tests/persist.test.js b/tests/persist.test.js index 73cd166..dae58db 100644 --- a/tests/persist.test.js +++ b/tests/persist.test.js @@ -52,6 +52,16 @@ test('a save too big for the browser is not retried every few seconds, but leavi assert.equal(a.ev('__tries'), 6, 'another game (a new state) tries at once'); }); +test('customer names already used are remembered once each, and old saves shrink on load', () => { + const { ev, errors } = load(); + ev("ACT.start({});S.usedNames=['A','B','A','A',7,'B'];S=migrate(JSON.parse(JSON.stringify(S)))"); + assert.deepEqual(JSON.parse(ev('JSON.stringify(S.usedNames)')), ['A', 'B']); + ev("for(let i=0;i<400;i++)dayTick()"); + const u = JSON.parse(ev('JSON.stringify(S.usedNames)')); + assert.equal(new Set(u).size, u.length, 'no name twice'); + assert.deepEqual(errors, []); +}); + test('returning players see what is new after an update, once', () => { const { saved } = playedGame(); execFileSync(process.execPath, ['scripts/build.js'], { cwd: root, env: { ...process.env, OUT, VERSION: '1.1.0', BUILD_DATE: '2026-10-23' } });