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:
Hugues 2026-10-27 13:44:57 +01:00
parent ecb862e70d
commit d7301747f7
5 changed files with 36 additions and 6 deletions

View file

@ -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' } });