Compress huge saves with the browser's gzip (#176)
This commit is contained in:
parent
8c3ac4e9df
commit
146fae0faf
5 changed files with 86 additions and 7 deletions
|
|
@ -19,6 +19,7 @@ All notable changes to this project are documented here. The project follows
|
|||
- 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 take about 40% less space to save: saves leave out customer details the game can work out again when loading (#176).
|
||||
- Huge networks fit in the browser again: saves over a million characters are compressed, about ten times smaller (#176).
|
||||
|
||||
## [1.34.1] — 2026-10-27
|
||||
|
||||
|
|
|
|||
|
|
@ -4,11 +4,35 @@
|
|||
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(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:{}}]);}
|
||||
const txt=JSON.stringify(packSave(S));
|
||||
const seq=UI.saveSeq=(UI.saveSeq||0)+1;
|
||||
if(txt.length>=GZ_MIN&&gzOk())gzSave(S,txt,seq);
|
||||
else try{localStorage.setItem('sot-save-v1',txt);UI.saveFail=null;}catch(e){saveErr(e,S);}
|
||||
UI.lastSave=performance.now();}
|
||||
function saveErr(e,s){const f=UI.saveFail,again=f&&f.s===s;UI.saveFail={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&&s===S)toast(saveFailMsg(),'bad',[{l:tr('common.export_save'),act:'exportSave',args:{}}]);}
|
||||
/* Saves of huge networks are gzipped by the browser (#176): about ten times smaller, so they fit in its storage.
|
||||
Compressing runs in the background, one at a time, and the newest save wins; one that a newer save or another
|
||||
game has overtaken meanwhile is dropped. Small saves stay plain JSON, written at once, as before. */
|
||||
const GZ='gz:',GZ_MIN=1e6,gzOk=()=>typeof CompressionStream==='function'&&typeof DecompressionStream==='function';
|
||||
async function gzPipe(bytes,stream){const w=stream.writable.getWriter(),no=()=>{};w.write(bytes).catch(no);w.close().catch(no);// a bad stream is reported by read() below
|
||||
const r=stream.readable.getReader(),parts=[];let n=0;
|
||||
for(;;){const {done,value}=await r.read();if(done)break;parts.push(value);n+=value.length;}
|
||||
const out=new Uint8Array(n);let o=0;for(const p of parts){out.set(p,o);o+=p.length;}return out;}
|
||||
const b64=u=>{let s='';for(let i=0;i<u.length;i+=0x8000)s+=String.fromCharCode.apply(null,u.subarray(i,i+0x8000));return btoa(s);};
|
||||
const unb64=t=>{const s=atob(t),u=new Uint8Array(s.length);for(let i=0;i<s.length;i++)u[i]=s.charCodeAt(i);return u;};
|
||||
const gzip=async txt=>GZ+b64(await gzPipe(new TextEncoder().encode(txt),new CompressionStream('gzip')));
|
||||
const gunzip=async raw=>new TextDecoder().decode(await gzPipe(unb64(raw.slice(GZ.length)),new DecompressionStream('gzip')));
|
||||
function gzSave(s,txt,seq){if(UI.gzBusy){UI.gzNext={s,txt,seq};return;}UI.gzBusy=true;
|
||||
gzip(txt).then(v=>{if(s!==S||seq!==UI.saveSeq)return;try{localStorage.setItem('sot-save-v1',v);UI.saveFail=null;}catch(e){saveErr(e,s);}})
|
||||
.catch(e=>{console.error('[tubes] compressing the save failed',e);})
|
||||
.finally(()=>{UI.gzBusy=false;const n=UI.gzNext;UI.gzNext=null;if(n&&n.s===S)gzSave(n.s,n.txt,n.seq);});}
|
||||
// A compressed save is unpacked before the game boots, so loadSave() stays synchronous for everything else.
|
||||
async function gzBoot(go){let raw=null;try{raw=localStorage.getItem('sot-save-v1');}catch(e){}
|
||||
if(raw&&raw.startsWith(GZ)){try{UI.gzRaw=raw;UI.gzText=await gunzip(raw);}catch(e){console.error('[tubes] unpacking the save failed',e);UI.gzRaw=UI.gzText=null;
|
||||
try{localStorage.setItem('sot-save-broken',raw);localStorage.removeItem('sot-save-v1');}catch(e2){}UI.gzBroken=true;}}
|
||||
go();}
|
||||
/* 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
|
||||
|
|
@ -41,5 +65,5 @@ function importFile(file){if(!file)return;if(file.size>8e6){toast(tr('save.that_
|
|||
const rd=new FileReader();rd.onload=()=>{const r=parseSave(String(rd.result));if(r.err){toast(tr('save.import_failed_err',{err:esc(r.err)}),'bad');sfx('err');return;}
|
||||
UI.importing=r.s;openModal(`<div class="eyebrow">${tr('common.import_a_save')}</div><h2 style="font-size:26px">${tr('save.load_name',{name:esc(r.name)})}</h2><p class="tagline">${esc(dateStr(r.s.day))} · ${tr('save.cash_in_the_bank',{cash:fmt$(r.s.cash)})} · ${trn('save.n_pop',Object.keys(r.s.pops).length)} · ${tr('save.saved_by_v_ver',{ver:esc(r.ver)})}</p>${S&&UI.started&&!S.lost?`<p class="note" style="color:var(--bad)">${tr('save.this_replaces_name_export_it_first',{name:esc(S.name)})}</p>`:''}<div class="btns">${btn(tr('save.load_this_network'),'importGo',{},'pink big')}${btn(tr('common.cancel'),UI.started?'closeModal':'helpClose',{},'ghost big')}</div>`);};
|
||||
rd.readAsText(file);}
|
||||
function loadSave(){try{const t=localStorage.getItem('sot-save-v1');if(t){const s=JSON.parse(t);if(s&&s.v===1&&!s.lost)return s;}}catch(e){}return null;}
|
||||
function loadSave(){try{let t=localStorage.getItem('sot-save-v1');if(t&&t.startsWith(GZ))t=t===UI.gzRaw?UI.gzText:null;if(t){const s=JSON.parse(t);if(s&&s.v===1&&!s.lost)return s;}}catch(e){}return null;}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,9 +40,9 @@ function boot(data){
|
|||
renderAll();if(S.grace&&S.grace.until>S.day&&R.graceNets&&!S.grace.told){S.grace.told=1;toast(tr('loop.new_networks_on_the_map_this',{n:S.grace.until-S.day}),'goal',[{l:tr('common.open_noc'),act:'leftTab',args:{t:'noc'}}]);}
|
||||
if(S.auNews){delete S.auNews;const c=Object.keys(S.pops).find(x=>CITY[x].au);toast(tr('loop.australia_got_more_australian'),'bad',[{l:tr('common.open'),act:'focusCity',args:{city:c}}],{a:'city',v:c});}
|
||||
if(!whatsNew(true))toast(tr('loop.welcome_back_name_your_network_is',{name:esc(S.name),date:dateStr(S.day)}),'good');}
|
||||
else{showTitle();whatsNew(false);if(sv)toast(tr('loop.your_saved_game_could_not_be'),'bad');}}
|
||||
else{showTitle();whatsNew(false);if(sv||UI.gzBroken)toast(tr('loop.your_saved_game_could_not_be'),'bad');}}
|
||||
renderHUD();renderLeft();
|
||||
window.claude?.hot?.snapshot?.(()=>({S:S&&JSON.parse(JSON.stringify(S)),cam:{...cam}}));
|
||||
if(!window.__TUBES_TEST__){requestAnimationFrame(frame);setTimeout(checkUpdate,4000);setInterval(checkUpdate,10*60*1000);}
|
||||
}
|
||||
window.claude?.hot?.ready ? window.claude.hot.ready(boot) : boot(window.claude?.hot?.data ?? {});
|
||||
gzBoot(()=>window.claude?.hot?.ready ? window.claude.hot.ready(boot) : boot(window.claude?.hot?.data ?? {}));
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@ function load(opts = {}) {
|
|||
// A non-English game: jsdom does not fetch the dictionary script, so register it up front.
|
||||
// (opts.dir: the folder the dictionary comes from, e.g. a build's _site/i18n.)
|
||||
if (opts.lang) { window.localStorage.setItem('sot-lang', opts.lang); window.eval(fs.readFileSync(path.join(opts.dir || path.join(__dirname, '..', 'site', 'i18n'), opts.lang + '.js'), 'utf8')); }
|
||||
// jsdom has no CompressionStream; {gzip:true} lends the page Node's, the same API browsers have (#176).
|
||||
if (opts.gzip) for (const k of ['CompressionStream', 'DecompressionStream', 'TextEncoder', 'TextDecoder']) window[k] = globalThis[k];
|
||||
window.Path2D = class { moveTo() {} lineTo() {} quadraticCurveTo() {} bezierCurveTo() {} arc() {} rect() {} closePath() {} };
|
||||
// A recording 2D context, so draw code really runs under test (jsdom has no canvas).
|
||||
const fake = () => { const calls = { n: 0 };
|
||||
|
|
|
|||
|
|
@ -69,3 +69,55 @@ test('#176 an older, unpacked save still loads the same', () => {
|
|||
assert.deepEqual(norm(JSON.parse(ev(sorted))), norm(JSON.parse(before)));
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
// Compressing runs in the background: wait for it, without depending on how long it takes.
|
||||
const settle = async (ev, cond) => { for (let i = 0; i < 400 && !ev(cond); i++) await new Promise(r => setTimeout(r, 5)); assert.ok(ev(cond), cond); };
|
||||
const huge = "for(let i=0;i<12000;i++){const slots=1+i%4;S.biz.push({id:'h'+i,city:'fra',city2:'ams',name:'Huge Carrier '+i,type:'wwave',bw:1,rate:4000+i,mood:100,since:i,link:'df1',slots,what:wwWhat(S.links.find(l=>l.id==='df1'),slots)});}compute()";
|
||||
|
||||
test('#176 small saves stay plain JSON, written at once', () => {
|
||||
const { ev, errors } = load({ gzip: true });
|
||||
ev("ACT.start({});save(true)");
|
||||
assert.equal(ev("localStorage.getItem('sot-save-v1')[0]"), '{');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('#176 a huge save is gzipped into storage and the next visit loads it', async () => {
|
||||
const a = load({ gzip: true });
|
||||
a.ev(bigBiz + ';' + huge);
|
||||
assert.ok(a.ev('JSON.stringify(packSave(S)).length') >= a.ev('GZ_MIN'), 'big enough to compress');
|
||||
a.ev('save(true)');
|
||||
await settle(a.ev, "(localStorage.getItem('sot-save-v1')||'').startsWith(GZ)");
|
||||
const raw = a.ev("localStorage.getItem('sot-save-v1')"), full = a.ev('JSON.stringify(packSave(S)).length');
|
||||
assert.ok(raw.length < full / 4, `much smaller in storage (${raw.length} vs ${full})`);
|
||||
const before = a.ev("JSON.stringify([S.name,S.day,S.biz.length,S.biz.find(b=>b.id==='h77').what])");
|
||||
// The next visit: the page unpacks it before booting and continues the same network.
|
||||
const b = load({ gzip: true, storage: { 'sot-save-v1': raw } });
|
||||
await settle(b.ev, 'UI.started===true');
|
||||
assert.equal(b.ev("JSON.stringify([S.name,S.day,S.biz.length,S.biz.find(b=>b.id==='h77').what])"), before);
|
||||
assert.deepEqual(a.errors, []);
|
||||
assert.deepEqual(b.errors, []);
|
||||
});
|
||||
|
||||
test('#176 a newer save is never overwritten by an older one still compressing', async () => {
|
||||
const { ev, errors } = load({ gzip: true });
|
||||
ev(bigBiz + ';' + huge);
|
||||
ev("save(true);S.name='Newer';save(true);S.name='Newest';save(true)");
|
||||
await settle(ev, "!UI.gzBusy&&(localStorage.getItem('sot-save-v1')||'').startsWith(GZ)");
|
||||
ev("gunzip(localStorage.getItem('sot-save-v1')).then(t=>window.__t=JSON.parse(t).name)");
|
||||
await settle(ev, "!!window.__t");
|
||||
assert.equal(ev('__t'), 'Newest');
|
||||
// A new game started while a big save compresses: the old network does not come back.
|
||||
ev("save(true);ACT.newgame();ACT.start({});window.__new=S.name");
|
||||
await settle(ev, '!UI.gzBusy');
|
||||
assert.equal(ev("JSON.parse(localStorage.getItem('sot-save-v1')).name"), ev('__new'), 'the new game stays saved');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('#176 a damaged compressed save is kept aside and the player is told', async () => {
|
||||
const { ev, dom, errors } = load({ gzip: true, storage: { 'sot-save-v1': 'gz:bm90IGd6aXA=' } });
|
||||
await settle(ev, "localStorage.getItem('sot-save-broken')!==null");
|
||||
assert.match(dom.window.document.body.textContent, /could not be/);
|
||||
assert.equal(ev("localStorage.getItem('sot-save-broken')"), 'gz:bm90IGd6aXA=');
|
||||
assert.equal(ev("localStorage.getItem('sot-save-v1')"), null);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue