From d7301747f734a359291e732896440b5e37e2a5f9 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] Saves too big for the browser: wait before retrying A late-game save of ~22 million characters is over Firefox's localStorage quota, so every save failed, yet the game rebuilt it with JSON.stringify(S) every 5 seconds, every 10 days and after every action: ~95 ms each in headless Firefox, 300-570 ms in a player's browser, all thrown away. After a failed save the autosave now waits 1 minute before trying again, then 2, 4, at most 5. A save that works resets this, so normal games still save every time. Leaving the page, exporting, reloading for an update or a language and a new game (a new S) still try at once. Player save in Firefox: first save 95 ms as before, the next autosaves 0 ms. --- CHANGELOG.md | 1 + src/game/90-actions/10-interface.js | 2 +- src/game/90-actions/89-game-and-settings.js | 2 +- src/game/91-save.js | 15 ++++++++++---- tests/persist.test.js | 22 +++++++++++++++++++++ 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5971143..03a3406 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,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. - 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/90-actions/10-interface.js b/src/game/90-actions/10-interface.js index af3bcfa..daedd0f 100644 --- a/src/game/90-actions/10-interface.js +++ b/src/game/90-actions/10-interface.js @@ -22,7 +22,7 @@ const ACT={ dashLink:({id})=>{$('dash').hidden=true;selectLink(id);}, animSet:({v})=>{if(!['auto','on','off'].includes(v))return;UI.anim=v;try{localStorage.setItem('sot-anim',v);}catch(e){}applyAnim();refreshSettings();}, soundSet:({v})=>{UI.muted=v==='off';try{localStorage.setItem('sot-mute',UI.muted?'1':'0');}catch(e){}renderHUD();refreshSettings();}, - langSet:({v})=>{if(!Object.hasOwn(LANGS,v)||v===LANG)return;try{localStorage.setItem('sot-lang',v);}catch(e){}save();location.reload();}, + langSet:({v})=>{if(!Object.hasOwn(LANGS,v)||v===LANG)return;try{localStorage.setItem('sot-lang',v);}catch(e){}save(true);location.reload();}, mute:()=>{UI.muted=!UI.muted;try{localStorage.setItem('sot-mute',UI.muted?'1':'0');}catch(e){}renderHUD();}, closePanel:()=>{UI.sel=null;UI.confirm=null;renderPanel();renderLeft();}, closeModal:()=>closeModal(), diff --git a/src/game/90-actions/89-game-and-settings.js b/src/game/90-actions/89-game-and-settings.js index ed9a284..82f3adc 100644 --- a/src/game/90-actions/89-game-and-settings.js +++ b/src/game/90-actions/89-game-and-settings.js @@ -31,7 +31,7 @@ Object.assign(ACT,{ vpSet:({v})=>{vpOn(v==='on');refreshSettings();}, resetAsk:()=>openModal(`

${tr('game_and_settings.start_a_new_network')}

${tr('game_and_settings.name_and_everything_it_owns_will',{name:esc(S?S.name:tr('common.your_network'))})}

${btn(tr('game_and_settings.delete_and_start_over'),'newgame',{},'red big')}${btn(tr('common.keep_playing'),'closeModal',{},'ghost big')}
`), whatsNewShow:()=>whatsNew(true,true), - updateNow:()=>{save();location.reload();}, + updateNow:()=>{save(true);location.reload();}, updateLater:()=>{$('update').hidden=true;UI.updLater=Date.now();}, helpClose:()=>{if(UI.started)closeModal();else showTitle();}, }); diff --git a/src/game/91-save.js b/src/game/91-save.js index e80a76c..9b0c3f6 100644 --- a/src/game/91-save.js +++ b/src/game/91-save.js @@ -1,12 +1,19 @@ /* ================= SAVE ================= */ -function save(){if(!S||S.lost)return;S.uiSpeed=UI.speed;S.cam={x:cam.x,y:cam.y,z:cam.z};try{localStorage.setItem('sot-save-v1',JSON.stringify(S));}catch(e){}UI.lastSave=performance.now();} +/* A network too big for the browser's storage fails to save every time, and turning it into text takes a + moment on huge saves, every 5 seconds and after every click. After a failure the autosave waits before trying + 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.atsave()); -document.addEventListener('visibilitychange',()=>{if(document.visibilityState==='hidden')save();else checkUpdate();}); +addEventListener('pagehide',()=>save(true)); +document.addEventListener('visibilitychange',()=>{if(document.visibilityState==='hidden')save(true);else checkUpdate();}); /* 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();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},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')};} diff --git a/tests/persist.test.js b/tests/persist.test.js index 733b1ef..73cd166 100644 --- a/tests/persist.test.js +++ b/tests/persist.test.js @@ -30,6 +30,28 @@ test('leaving the page saves the game', () => { assert.equal(JSON.parse(a.ev("localStorage.getItem('sot-save-v1')")).day, a.ev('S.day')); }); +test('a save too big for the browser is not retried every few seconds, but leaving still tries', () => { + const a = load(); + // A storage that counts tries and refuses the game save, like a full quota. + a.ev(`window.__tries=0;window.__full=true;const P=Object.getPrototypeOf(localStorage),set=P.setItem; + P.setItem=function(k,v){if(k==='sot-save-v1'){__tries++;if(__full){const e=new Error('full');e.name='QuotaExceededError';throw e;}}return set.call(this,k,v);}; + ACT.start({});for(let i=0;i<7;i++)dayTick();__tries=0;UI.saveFail=null;save()`); + assert.equal(a.ev('__tries'), 1); + a.ev('save();save();after()'); + assert.equal(a.ev('__tries'), 1, 'autosaves wait after a failure'); + a.ev("dispatchEvent(new Event('pagehide'))"); + assert.equal(a.ev('__tries'), 2, 'leaving the page still tries'); + assert.equal(a.ev('UI.saveFail.wait'), 120000, 'and each failure waits longer'); + a.ev('__full=false;UI.saveFail.at-=120001;save()'); + assert.equal(a.ev('__tries'), 3, 'tried again once the wait is over'); + assert.equal(JSON.parse(a.ev("localStorage.getItem('sot-save-v1')")).day, a.ev('S.day')); + assert.equal(a.ev('UI.saveFail'), null); + a.ev('save()'); + assert.equal(a.ev('__tries'), 4, 'a save that works keeps saving every time'); + a.ev('__full=true;save();S=migrate(JSON.parse(JSON.stringify(S)));save()'); + assert.equal(a.ev('__tries'), 6, 'another game (a new state) tries at once'); +}); + 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' } });