Smaller saves: leave customer defaults out and rebuild them on load (#176)
This commit is contained in:
parent
cbe11339a7
commit
d36426a8ae
4 changed files with 49 additions and 3 deletions
|
|
@ -18,6 +18,7 @@ All notable changes to this project are documented here. The project follows
|
|||
- The map of a huge network spends less time working out fiber capacities on every frame, and skips links above or below the screen.
|
||||
- Big networks with thousands of fiber customers no longer stall: each game day and each action is about two to three times faster, and new alien-wave offers no longer freeze the game for a moment.
|
||||
- The game now tells you when it cannot save: a network too big for the browser's storage, or a browser that blocks it, used to stop saving without a word (#176). Export a save from the warning to keep a backup.
|
||||
- Big networks save in about half the space and a little faster: saves leave out customer details the game can work out again when loading (#176).
|
||||
|
||||
## [1.34.1] — 2026-10-27
|
||||
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ function sanitize(s){
|
|||
for(const c of Object.keys(s.myIx)){const m=s.myIx[c];if(!isCity(c)||!m||typeof m!=='object'){delete s.myIx[c];continue;}m.members=Array.isArray(m.members)?m.members.filter(isNet):[];m.name=String(m.name||tr('common.your_ix')).slice(0,40);m.next=num(m.next,s.day);m.fab=int(m.fab,0,MYIX_FAB.length-1,0);}
|
||||
const okBiz=b=>b&&own(BIZ_T,b.type)&&(!BIZ_T[b.type].nni||own(FAB,b.prov))&&isCity(b.city)&&(!b.city2||isCity(b.city2))&&(!b.sites||Array.isArray(b.sites)&&b.sites.every(isCity));
|
||||
const fixBiz=b=>{b.id=sid(b.id);b.name=String(b.name||tr('common.customer')).slice(0,80);b.bw=num(b.bw,1);b.rate=num(b.rate,0);if(b.fee!==undefined)b.fee=num(b.fee,0);if(b.start!==undefined)b.start=num(b.start,0);if(b.dur!==undefined)b.dur=num(b.dur,1);if(b.slots!==undefined)b.slots=num(b.slots,0);if(b.what!==undefined)b.what=String(b.what).slice(0,200);if(b.broker!==undefined)b.broker=!!b.broker;if(!Array.isArray(b.reqs))b.reqs=[];b.reqs=b.reqs.filter(r=>own(REQS,r));if(b.mood!==undefined)b.mood=num(b.mood,100);if(b.pm!==undefined)b.pm=num(b.pm,b.mood||100);if(b.exp!==undefined)b.exp=num(b.exp,0);if(b.latMax!=null)b.latMax=num(b.latMax,10);if(b.route&&!own(RT,b.route))b.route='normal';if(b.due!==undefined)b.due=num(b.due,0);if(b.win!==undefined)b.win=num(b.win,180);if(b.live!==undefined)b.live=!!b.live;if(b.link!==undefined&&typeof b.link!=='string')delete b.link;return b;};
|
||||
s.biz=s.biz.filter(okBiz).map(fixBiz);s.offers=s.offers.filter(okBiz).map(fixBiz);
|
||||
{const ids=new Map(s.links.map(l=>[l.id,l]));s.biz=s.biz.filter(okBiz).map(b=>unpackBiz(fixBiz(b),ids));}s.offers=s.offers.filter(okBiz).map(fixBiz);
|
||||
// Sponsorships and capacity conferences (#168): registrations are true or 'sp', the glow and the pending deals are checked.
|
||||
for(const k of Object.keys(s.confReg))s.confReg[k]=s.confReg[k]==='sp'?'sp':!!s.confReg[k];
|
||||
if(s.conf){const c=s.conf;if(typeof c!=='object'||!own(REG,c.reg))s.conf=null;else s.conf={until:num(c.until,0),reg:c.reg,name:String(c.name||'').slice(0,40),sp:!!c.sp};}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,20 @@
|
|||
again (1 minute, then 2, 4, at most 5); leaving the page, exporting or another game (a new S) still try at once. */
|
||||
function save(force){if(!S||S.lost)return;S.uiSpeed=UI.speed;S.cam={x:cam.x,y:cam.y,z:cam.z};const f=UI.saveFail;
|
||||
if(!force&&f&&f.s===S&&performance.now()-f.at<f.wait){UI.lastSave=performance.now();return;}
|
||||
try{localStorage.setItem('sot-save-v1',JSON.stringify(S));UI.saveFail=null;}
|
||||
try{localStorage.setItem('sot-save-v1',JSON.stringify(packSave(S)));UI.saveFail=null;}
|
||||
catch(e){const again=f&&f.s===S;UI.saveFail={s:S,at:performance.now(),wait:again?Math.min(300000,f.wait*2):60000,full:saveFull(e)};
|
||||
// Say so once per game (#176): a save that silently fails loses everything since the last one that worked.
|
||||
if(!again)toast(saveFailMsg(),'bad',[{l:tr('common.export_save'),act:'exportSave',args:{}}]);}
|
||||
UI.lastSave=performance.now();}
|
||||
/* Big networks are mostly customers, and most of a customer record was defaults (#176): no second city, no
|
||||
requirements, the normal route, no warnings yet. The save leaves defaults out and sanitize() puts them back, so
|
||||
the game after loading is the same; older versions read a missing field as its default too. An alien wave's
|
||||
description is rebuilt from its fiber. */
|
||||
const BIZ_DEF={city2:null,latMax:null,route:'normal',sites:null,slots:0,warned:false,warn60:false};
|
||||
function packBiz(b){const o={};for(const k in b){const v=b[k];if(v===undefined||BIZ_DEF[k]===v||(k==='reqs'&&Array.isArray(v)&&!v.length)||(k==='pm'&&v===b.mood)||(k==='what'&&b.type==='wwave'&&b.link))continue;o[k]=v;}return o;}
|
||||
function unpackBiz(b,links){for(const k in BIZ_DEF)if(!(k in b))b[k]=BIZ_DEF[k];if(!('pm' in b)&&b.mood!==undefined)b.pm=b.mood;if(!Array.isArray(b.reqs))b.reqs=[];
|
||||
if(b.type==='wwave'&&b.what===undefined){const l=links.get(b.link);b.what=l?wwWhat(l,b.slots||1):'';}return b;}
|
||||
const packSave=s=>({...s,biz:s.biz.map(packBiz)});
|
||||
// Out of room (quota) or not allowed to store at all (private window, blocked site data): different advice.
|
||||
const saveFull=e=>!!e&&(e.name==='QuotaExceededError'||e.name==='NS_ERROR_DOM_QUOTA_REACHED'||e.code===22||e.code===1014);
|
||||
const saveFailing=()=>!!(UI.saveFail&&UI.saveFail.s===S);
|
||||
|
|
@ -19,7 +28,7 @@ document.addEventListener('visibilitychange',()=>{if(document.visibilityState===
|
|||
/* Export / import: move a network to another device or keep a backup. The file is the same
|
||||
object the autosave writes, wrapped with a little metadata. */
|
||||
function saveFileName(){const d=new Date().toISOString().slice(0,10),n=(S&&S.name||'network').toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/^-|-$/g,'')||'network';return `series-of-tubes-${n}-${d}.json`;}
|
||||
function exportSave(){if(!S)return;save(true);const blob=new Blob([JSON.stringify({app:'series-of-tubes',version:APP_VERSION,exported:new Date().toISOString(),S},null,1)],{type:'application/json'});
|
||||
function exportSave(){if(!S)return;save(true);const blob=new Blob([JSON.stringify({app:'series-of-tubes',version:APP_VERSION,exported:new Date().toISOString(),S:packSave(S)},null,1)],{type:'application/json'});
|
||||
const a=document.createElement('a');a.href=URL.createObjectURL(blob);a.download=saveFileName();document.body.appendChild(a);a.click();setTimeout(()=>{URL.revokeObjectURL(a.href);a.remove();},1000);
|
||||
toast(tr('save.save_exported_file_is_in_your',{file:esc(saveFileName())}),'good');}
|
||||
function parseSave(txt){if(typeof txt!=='string')return {err:tr('save.that_file_is_not_valid_json')};let j;try{j=JSON.parse(txt);}catch(e){return {err:tr('save.that_file_is_not_valid_json')};}
|
||||
|
|
|
|||
|
|
@ -33,3 +33,39 @@ test('#176 storage that is blocked altogether gets its own advice', () => {
|
|||
assert.match(dom.window.document.body.textContent, /private window/);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
// A network with plenty of alien waves and ordinary customers, the shape of big late-game saves.
|
||||
const bigBiz = "ACT.start({});S.cash=1e9;ACT.launchBB({city:'fra'});ACT.buildPop({city:'ams'});" +
|
||||
"S.links.push({id:'df1',a:'fra',b:'ams',kind:'df',own:true,km:450,sys:'roadm',ch:[],ready:0,mo:500,day:0,cut:0});" +
|
||||
"for(let i=0;i<300;i++){const slots=1+i%4;S.biz.push({id:'w'+i,city:'fra',city2:'ams',reqs:[],latMax:null,route:'normal',name:'Carrier '+i,type:'wwave',bw:1,rate:4000,mood:100-i%9*1.37,since:i,sites:null,link:'df1',slots,what:wwWhat(S.links.find(l=>l.id==='df1'),slots),pm:i%2?100:99,warned:i%5===0,warn60:false});}" +
|
||||
"S.biz.push({id:'l2',city:'fra',city2:'ams',reqs:['diverse'],latMax:3.2,route:'fast',name:'Fast Co',type:'hft',bw:5,rate:900,mood:80,since:0,sites:null,slots:0,pm:81,warned:false,warn60:true});" +
|
||||
"S.biz.push({id:'d1',city:'fra',city2:null,reqs:[],latMax:null,route:'normal',name:'Plain Co',type:'dia',bw:5,rate:600,mood:100,since:0,sites:null,slots:0});compute()";
|
||||
const sorted = "JSON.stringify(S.biz.map(b=>Object.keys(b).sort().map(k=>[k,b[k]])))";
|
||||
|
||||
test('#176 saves leave customer defaults out, and loading puts them back exactly', () => {
|
||||
const { ev, errors } = load();
|
||||
ev(bigBiz);
|
||||
const full = ev('JSON.stringify(S).length'), packed = ev('JSON.stringify(packSave(S)).length');
|
||||
assert.ok(packed < full * 0.75, `the save is much smaller (${packed} vs ${full})`);
|
||||
const before = ev(sorted);
|
||||
ev("save(true);S=migrate(loadSave());compute()");
|
||||
const after = JSON.parse(ev(sorted)), want = JSON.parse(before);
|
||||
// Customers that never got warnings or a previous mood come back with the defaults; everything else is identical.
|
||||
const norm = list => list.map(b => Object.fromEntries(b)).map(b => ({ pm: b.mood, warned: false, warn60: false, ...b }));
|
||||
assert.deepEqual(norm(after), norm(want));
|
||||
assert.equal(ev("S.biz.find(b=>b.id==='l2').route"), 'fast', 'a chosen route is kept');
|
||||
assert.equal(ev("S.biz.find(b=>b.id==='w7').what"), ev("wwWhat(S.links.find(l=>l.id==='df1'),4)"), 'alien waves get their description back');
|
||||
assert.match(ev("localStorage.getItem('sot-save-v1')"), /"Carrier 1"/);
|
||||
assert.doesNotMatch(ev("localStorage.getItem('sot-save-v1')"), /alien wave on your/, 'descriptions are not stored');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('#176 an older, unpacked save still loads the same', () => {
|
||||
const { ev, errors } = load();
|
||||
ev(bigBiz);
|
||||
const before = ev(sorted);
|
||||
ev("S=migrate(JSON.parse(JSON.stringify(S)));compute()");
|
||||
const norm = list => list.map(b => Object.fromEntries(b)).map(b => ({ pm: b.mood, warned: false, warn60: false, ...b }));
|
||||
assert.deepEqual(norm(JSON.parse(ev(sorted))), norm(JSON.parse(before)));
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue