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.
This commit is contained in:
parent
ecb862e70d
commit
d7301747f7
5 changed files with 36 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ Object.assign(ACT,{
|
|||
vpSet:({v})=>{vpOn(v==='on');refreshSettings();},
|
||||
resetAsk:()=>openModal(`<h2 style="font-size:26px">${tr('game_and_settings.start_a_new_network')}</h2><p class="tagline">${tr('game_and_settings.name_and_everything_it_owns_will',{name:esc(S?S.name:tr('common.your_network'))})}</p><div class="btns">${btn(tr('game_and_settings.delete_and_start_over'),'newgame',{},'red big')}${btn(tr('common.keep_playing'),'closeModal',{},'ghost big')}</div>`),
|
||||
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();},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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.at<f.wait){UI.lastSave=performance.now();return;}
|
||||
try{localStorage.setItem('sot-save-v1',JSON.stringify(S));UI.saveFail=null;}
|
||||
catch(e){UI.saveFail={s:S,at:performance.now(),wait:f&&f.s===S?Math.min(300000,f.wait*2):60000};}
|
||||
UI.lastSave=performance.now();}
|
||||
// Save whenever the player might be leaving: tab hidden, page closing or reloading.
|
||||
addEventListener('pagehide',()=>save());
|
||||
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')};}
|
||||
|
|
|
|||
|
|
@ -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' } });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue