Save slots: several networks per browser, one per window, with a cross-window guard
This commit is contained in:
parent
39ef7c13bb
commit
da8150b7c4
29 changed files with 595 additions and 124 deletions
|
|
@ -100,7 +100,7 @@ test('networks in its own regions buy their transit from it, so a PNI with it re
|
|||
test('a game saved before it existed gets the courtesy route to it', () => {
|
||||
const a = load();
|
||||
a.ev("ACT.start({});S.day=40;S.seen=S.seen.filter(x=>x!=='gsl');save(true)");
|
||||
const b = load({ storage: { 'sot-save-v1': a.ev("localStorage.getItem('sot-save-v1')") } });
|
||||
const b = load({ storage: { 'sot-save-v1': a.ev("slotRaw(UI.slot)") } });
|
||||
assert.equal(b.ev("S.grace.nets.includes('gsl')"), true);
|
||||
assert.equal(b.ev("S.seen.includes('gsl')"), true);
|
||||
assert.equal(b.ev("R.reachable.gsl"), true, 'reachable through the courtesy route until you find your own way');
|
||||
|
|
|
|||
|
|
@ -122,7 +122,7 @@ test('picking a language saves the choice and the game, then reloads', () => {
|
|||
ev('ACT.start({});dayTick()');
|
||||
ev("ACT.langSet({v:'it'})");
|
||||
assert.equal(ev("localStorage.getItem('sot-lang')"), 'it');
|
||||
assert.ok(ev("localStorage.getItem('sot-save-v1')"));
|
||||
assert.ok(ev("slotRaw(UI.slot)"));
|
||||
ev("ACT.langSet({v:'klingon'})");
|
||||
assert.equal(ev("localStorage.getItem('sot-lang')"), 'it');
|
||||
});
|
||||
|
|
@ -130,7 +130,7 @@ test('picking a language saves the choice and the game, then reloads', () => {
|
|||
test('a scenario started in English shows its Italian name after switching', () => {
|
||||
const en = load();
|
||||
en.ev("ACT.startScn({id:'stg'});save()");
|
||||
const { ev } = load({ lang: 'it', storage: { 'sot-save-v1': en.ev("localStorage.getItem('sot-save-v1')") } });
|
||||
const { ev } = load({ lang: 'it', storage: { 'sot-save-v1': en.ev("slotRaw(UI.slot)") } });
|
||||
const name = ev("SCN.find(x=>x.id==='stg').n");
|
||||
assert.notEqual(name, en.ev("SCN.find(x=>x.id==='stg').n"));
|
||||
assert.ok(ev("document.getElementById('hAsn').textContent").includes(name));
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ test('#5 export writes a file and import restores it, with checks', () => {
|
|||
ev(`UI.importing=parseSave(${JSON.stringify(txt)}).s;ACT.importGo()`);
|
||||
assert.equal(ev("S.name"), 'Export Me');
|
||||
assert.equal(ev("S.cash"), 1234567);
|
||||
assert.equal(ev("JSON.parse(localStorage.getItem('sot-save-v1')).name"), 'Export Me', 'saved on this device');
|
||||
assert.equal(ev("JSON.parse(slotRaw(UI.slot)).name"), 'Export Me', 'saved on this device');
|
||||
ev("showHelp()");
|
||||
assert.match(ev("document.getElementById('modalCard').textContent"), /Export save.*Import a save/);
|
||||
assert.deepEqual(errors, []);
|
||||
|
|
@ -95,7 +95,7 @@ test('the network name opens a menu with save, load and start over', () => {
|
|||
ev("document.querySelector('#modalCard [data-act=resetAsk]').click()");
|
||||
assert.match(ev("document.getElementById('modalCard').textContent"), /cannot be undone/);
|
||||
ev("document.querySelector('#modalCard [data-act=newgame]').click()");
|
||||
assert.equal(ev("!!localStorage.getItem('sot-save-v1')&&JSON.parse(localStorage.getItem('sot-save-v1')).day>0"), false);
|
||||
assert.equal(ev("!!slotRaw(UI.slot)&&JSON.parse(slotRaw(UI.slot)).day>0"), false);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
|
|
@ -1052,7 +1052,7 @@ test('#104 declining a loan offer clears the bank badge while paused', () => {
|
|||
assert.equal(ev("$('hBank').hidden"), false);
|
||||
ev("ACT.declineLoan({id:S.loanOffers[0].id})");
|
||||
assert.equal(ev("$('hBank').hidden"), true, 'badge gone without a new day');
|
||||
assert.equal(ev("JSON.parse(localStorage.getItem('sot-save-v1')).loanOffers.length"), 0, 'saved right away');
|
||||
assert.equal(ev("JSON.parse(slotRaw(UI.slot)).loanOffers.length"), 0, 'saved right away');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ test('changing the dropdown in Settings switches language and keeps the game', (
|
|||
const { ev, errors } = load();
|
||||
ev("ACT.start({});dayTick();ACT.settings();const s=document.getElementById('langSel');s.value='fr';s.dispatchEvent(new Event('change',{bubbles:true}))");
|
||||
assert.equal(ev("localStorage.getItem('sot-lang')"), 'fr');
|
||||
assert.ok(ev("localStorage.getItem('sot-save-v1')"), 'saved before reloading');
|
||||
assert.ok(ev("slotRaw(UI.slot)"), 'saved before reloading');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ test('#186 on a phone, play after pause resumes at half speed, not the old game\
|
|||
test('#186 a saved game still continues at the speed it was saved at', () => {
|
||||
const a = load();
|
||||
a.ev("ACT.start({});ACT.speed({v:4});save(true)");
|
||||
const b = load({ storage: { 'sot-save-v1': a.ev("localStorage.getItem('sot-save-v1')") } });
|
||||
const b = load({ storage: { 'sot-save-v1': a.ev("slotRaw(UI.slot)") } });
|
||||
assert.equal(b.ev('UI.speed'), 4);
|
||||
assert.deepEqual(a.errors, []);
|
||||
assert.deepEqual(b.errors, []);
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ test('Rackstar boxes survive save migration and appear on phone peering panels i
|
|||
const { dom, ev, errors } = load(lang === 'en' ? {} : { lang });
|
||||
setup(ev);
|
||||
ev('window.matchMedia=()=>({matches:true})');
|
||||
ev("S.pops.fra.bb={d:10/NSH.cfl};compute();ACT.cache({city:'fra',net:'cfl'});save();S=migrate(JSON.parse(localStorage.getItem('sot-save-v1')));compute()");
|
||||
ev("S.pops.fra.bb={d:10/NSH.cfl};compute();ACT.cache({city:'fra',net:'cfl'});save();S=migrate(JSON.parse(slotRaw(UI.slot)));compute()");
|
||||
assert.equal(ev("S.caches.filter(x=>x.net==='cfl').length"), 1, lang);
|
||||
assert.ok(ev('R.useCache[S.caches[0].id]') > 0, lang);
|
||||
for (const [width, height] of [[360, 800], [800, 360]]) {
|
||||
|
|
@ -221,7 +221,7 @@ test('Amsterdam has FRIES-IX, with free 10G and 100G ports and paid larger ports
|
|||
const txt = ev("document.getElementById('panelBody').textContent");
|
||||
assert.match(txt, /FRIES-IX/); assert.match(txt, /HAMS-IX/);
|
||||
assert.match(ev("ixBlock('ams~3',true)"), /LAG member <small>free/);
|
||||
ev("save();restored=migrate(JSON.parse(localStorage.getItem('sot-save-v1')));");
|
||||
ev("save();restored=migrate(JSON.parse(slotRaw(UI.slot)));");
|
||||
assert.equal(ev("restored.ixp['ams~3']"), 1, 'free port survives save migration');
|
||||
assert.equal(ev("restored.ixn['ams~3']"), 2, 'free LAG survives save migration');
|
||||
ev("S.ev.ixdown={'ams~3':S.day+2};compute()");
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ const root = path.join(__dirname, '..');
|
|||
function playedGame() {
|
||||
const a = load();
|
||||
a.ev("ACT.start({});ACT.launchBB({city:'fra'});for(let i=0;i<25;i++)dayTick();UI.speed=2;save()");
|
||||
return { a, saved: a.ev("localStorage.getItem('sot-save-v1')"), day: a.ev('S.day') };
|
||||
return { a, saved: a.ev("slotRaw(UI.slot)"), day: a.ev('S.day') };
|
||||
}
|
||||
|
||||
test('a reload drops you straight back into your saved network', () => {
|
||||
|
|
@ -25,16 +25,16 @@ test('a reload drops you straight back into your saved network', () => {
|
|||
|
||||
test('leaving the page saves the game', () => {
|
||||
const a = load();
|
||||
a.ev("ACT.start({});for(let i=0;i<7;i++)dayTick();localStorage.removeItem('sot-save-v1')");
|
||||
a.ev("ACT.start({});for(let i=0;i<7;i++)dayTick();localStorage.removeItem(SLOT_K+UI.slot)");
|
||||
a.ev("dispatchEvent(new Event('pagehide'))");
|
||||
assert.equal(JSON.parse(a.ev("localStorage.getItem('sot-save-v1')")).day, a.ev('S.day'));
|
||||
assert.equal(JSON.parse(a.ev("slotRaw(UI.slot)")).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);};
|
||||
P.setItem=function(k,v){if(k.startsWith('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()');
|
||||
|
|
@ -44,7 +44,7 @@ test('a save too big for the browser is not retried every few seconds, but leavi
|
|||
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(JSON.parse(a.ev("slotRaw(UI.slot)")).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');
|
||||
|
|
|
|||
|
|
@ -120,8 +120,8 @@ test('no clock in a puzzle: days never pass, nothing breaks, no bills', () => {
|
|||
|
||||
test('the main save is byte-identical before and after playing and solving a puzzle', () => {
|
||||
const { ev, errors } = load();
|
||||
ev("ACT.start({});S.cash=1e7;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});ACT.buildPop({city:'ams'});save(true)");
|
||||
const before = ev("localStorage.getItem('sot-save-v1')");
|
||||
ev("ACT.start({});S.cash=1e7;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});ACT.buildPop({city:'ams'});save(true);window.__slot=UI.slot");
|
||||
const before = ev("slotRaw(UI.slot||window.__slot)");
|
||||
const name = ev('S.name'), day = ev('S.day');
|
||||
ev('ACT.puzzleMenu({})');
|
||||
assert.match(ev("document.getElementById('modalCard').textContent"), /Hello, World/);
|
||||
|
|
@ -129,7 +129,7 @@ test('the main save is byte-identical before and after playing and solving a puz
|
|||
assert.equal(ev('S.mode'), 'puzzle');
|
||||
build(ev, REF.hello.three);
|
||||
ev("save(true);for(let i=0;i<5;i++)frame(performance.now()+6000*i);window.dispatchEvent(new Event('pagehide'));ACT.exportSave({})");
|
||||
assert.equal(ev("localStorage.getItem('sot-save-v1')"), before, 'untouched while in the puzzle');
|
||||
assert.equal(ev("slotRaw(UI.slot||window.__slot)"), before, 'untouched while in the puzzle');
|
||||
assert.equal(ev("JSON.parse(localStorage.getItem('sot-puzzles')).s.hello.st"), 3, 'the stars are kept');
|
||||
ev('ACT.pzLeave({})');
|
||||
assert.equal(ev('S.mode'), 'normal');
|
||||
|
|
@ -137,7 +137,7 @@ test('the main save is byte-identical before and after playing and solving a puz
|
|||
assert.equal(ev('S.day'), day);
|
||||
assert.ok(ev('!!S.pops.ams'));
|
||||
assert.equal(ev('UI.started'), true);
|
||||
assert.equal(ev("localStorage.getItem('sot-save-v1')"), before, 'and after');
|
||||
assert.equal(ev("slotRaw(UI.slot||window.__slot)"), before, 'and after');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
|
|
@ -149,7 +149,7 @@ test('leaving a puzzle with no game goes back to the title screen', () => {
|
|||
assert.equal(ev('S.mode'), 'puzzle');
|
||||
ev('ACT.pzLeave({})');
|
||||
assert.equal(ev('S'), null);
|
||||
assert.equal(ev("localStorage.getItem('sot-save-v1')"), null, 'no main save appeared');
|
||||
assert.equal(ev("slotIdx(true).length"), 0, 'no network was saved');
|
||||
assert.match(ev("document.getElementById('modalCard').textContent"), /Series/);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -268,7 +268,7 @@ test('several exchanges per city: overlapping members, cheaper challengers, sepa
|
|||
const t = ev("document.getElementById('panelBody').textContent");
|
||||
assert.match(t, /3 exchanges compete/);
|
||||
assert.match(t, /NL-Nyx/);
|
||||
ev("save();load2=JSON.parse(localStorage.getItem('sot-save-v1'));sanitize(load2)");
|
||||
ev("save();load2=JSON.parse(slotRaw(UI.slot));sanitize(load2)");
|
||||
assert.equal(ev("load2.ixp['ams~2']"), 1, 'saves keep challenger ports');
|
||||
ev("ACT.cancelIx({city:'ams~2'});ACT.cancelIx({city:'ams~2'})");
|
||||
assert.equal(ev("S.ixp['ams~2']"), undefined);
|
||||
|
|
@ -1034,16 +1034,16 @@ test('an old save with Australian POPs is told once that Australia changed', ()
|
|||
const a = load();
|
||||
start(a.ev);
|
||||
a.ev("ACT.buildPop({city:'syd'});for(let d=0;d<3;d++)dayTick();delete S.auV;save()");
|
||||
const sv = a.ev("localStorage.getItem('sot-save-v1')");
|
||||
const sv = a.ev("slotRaw(UI.slot)");
|
||||
const { ev, errors } = load({ storage: { 'sot-save-v1': sv } });
|
||||
assert.match(ev("S.log.map(e=>e.t).join(' | ')"), /Australia got more Australian/);
|
||||
assert.equal(ev("S.auV"), 1);
|
||||
assert.equal(ev("S.auNews"), undefined, 'only once');
|
||||
const again = load({ storage: { 'sot-save-v1': ev("save();localStorage.getItem('sot-save-v1')") } });
|
||||
const again = load({ storage: { 'sot-save-v1': ev("save();slotRaw(UI.slot)") } });
|
||||
assert.equal(again.ev("S.log.filter(e=>/Australia got more Australian/.test(e.t)).length"), 1, 'not told twice');
|
||||
// a new game never sees it, and neither does a save without Australia
|
||||
const b = load(); start(b.ev); b.ev("for(let d=0;d<3;d++)dayTick();delete S.auV;save()");
|
||||
const c = load({ storage: { 'sot-save-v1': b.ev("localStorage.getItem('sot-save-v1')") } });
|
||||
const c = load({ storage: { 'sot-save-v1': b.ev("slotRaw(UI.slot)") } });
|
||||
assert.doesNotMatch(c.ev("S.log.map(e=>e.t).join(' | ')"), /Australia got more/);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ const { load } = require('./harness');
|
|||
|
||||
// Make the browser's storage refuse the save: 'QuotaExceededError' (too big) or 'SecurityError' (not allowed).
|
||||
const refuse = name => `window.__err=${JSON.stringify(name)};{const P=Object.getPrototypeOf(localStorage),set=P.setItem;
|
||||
P.setItem=function(k,v){if(k==='sot-save-v1'&&__err){const e=new Error('no');e.name=__err;throw e;}return set.call(this,k,v);};}`;
|
||||
P.setItem=function(k,v){if(k.startsWith('sot-save-v1')&&__err){const e=new Error('no');e.name=__err;throw e;}return set.call(this,k,v);};}`;
|
||||
|
||||
test('#176 a save that does not fit tells the player once, with Export, and the menu says so', () => {
|
||||
const { ev, dom, errors } = load();
|
||||
|
|
@ -55,8 +55,8 @@ test('#176 saves leave customer defaults out, and loading puts them back exactly
|
|||
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.match(ev("slotRaw(UI.slot)"), /"Carrier 1"/);
|
||||
assert.doesNotMatch(ev("slotRaw(UI.slot)"), /alien wave on your/, 'descriptions are not stored');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
|
|
@ -77,7 +77,7 @@ const huge = "for(let i=0;i<12000;i++){const slots=1+i%4;S.biz.push({id:'h'+i,ci
|
|||
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.equal(ev("slotRaw(UI.slot)[0]"), '{');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
|
|
@ -86,8 +86,8 @@ test('#176 a huge save is gzipped into storage and the next visit loads it', asy
|
|||
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');
|
||||
await settle(a.ev, "(slotRaw(UI.slot)||'').startsWith(GZ)");
|
||||
const raw = a.ev("slotRaw(UI.slot)"), 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.
|
||||
|
|
@ -102,22 +102,22 @@ test('#176 a newer save is never overwritten by an older one still compressing',
|
|||
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, "!UI.gzBusy&&(slotRaw(UI.slot)||'').startsWith(GZ)");
|
||||
ev("gunzip(slotRaw(UI.slot)).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.equal(ev("JSON.parse(slotRaw(UI.slot)).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 () => {
|
||||
test('#176 a damaged compressed save is kept, marked damaged, 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);
|
||||
await settle(ev, "slotIdx().length===1&&/could not be/.test(document.getElementById('toasts').textContent)");
|
||||
assert.equal(ev("slotIdx()[0].broken"), true, 'listed as damaged, so it can still be exported or deleted');
|
||||
assert.equal(ev("slotRaw('main')"), 'gz:bm90IGd6aXA=', 'the damaged save is kept as it was');
|
||||
assert.equal(ev('!!UI.started'), false);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue