From 537ec4efd0d7a60f830635ab34457b9fa5f98926 Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Thu, 29 Oct 2026 07:01:18 +0100 Subject: [PATCH] Saves get the rules of their mode, whatever their rules say sanitize() trusted any preset and override in a save, so an edited or imported save could run a Normal game on puzzle rules (no clock, ever), On-call with infinite money and open gates, sandbox powers without the sandbox tag, or a scenario off On-call. Rules now follow the mode: scenarios and the weekly are On-call, a sandbox keeps only its chaos, a Normal game one of the three Normal presets unchanged. --- src/game/80-simulation/50-sanitize.js | 7 +++-- tests/rules-locked.test.js | 45 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/rules-locked.test.js diff --git a/src/game/80-simulation/50-sanitize.js b/src/game/80-simulation/50-sanitize.js index 44e8205..bdb268a 100644 --- a/src/game/80-simulation/50-sanitize.js +++ b/src/game/80-simulation/50-sanitize.js @@ -77,8 +77,11 @@ function sanitize(s){ for(const k of Object.keys(s.ach))s.ach[k]=num(s.ach[k],0); {const p=s.achP&&typeof s.achP==='object'&&!Array.isArray(s.achP)?s.achP:{};s.achP={};for(const k of Object.keys(s.ach))s.achP[k]=BVAR[p[k]]?p[k]:'oncall';} if(s.merged){const o={};if(typeof s.merged==='object')for(const m of MERGERS)if(isFinite(s.merged[m.id]))o[m.id]=+s.merged[m.id];s.merged=Object.keys(o).length?o:undefined;} - // The rules (2.0): a save without them is the classic game, On-call (or a sandbox or puzzle, if it was one). Locked after the start. - {const r=s.rules&&typeof s.rules==='object'&&!Array.isArray(s.rules)?s.rules:{},p=s.mode==='sandbox'?'sandbox':s.mode==='puzzle'?'puzzle':own(PRESETS,r.preset)?r.preset:'oncall';s.rules=makeRules(p,own(PRESETS,r.preset)?r:{});} + /* The rules (2.0) follow from the mode, as the doors set them, and nothing else: a save without them is the classic + game, On-call. Scenarios and the weekly run on On-call; a sandbox keeps only its chaos; a Normal game one of the + three Normal presets, unchanged (its validated mods kept). So an edited or imported save cannot stop the clock or print money. */ + {const r=s.rules&&typeof s.rules==='object'&&!Array.isArray(s.rules)?s.rules:{}; + const m={mods:r.mods};s.rules=s.scn&&typeof s.scn==='object'?makeRules('oncall',m):s.mode==='sandbox'?makeRules('sandbox',{...m,chaos:r.chaos}):makeRules(NORMAL_PRESETS.includes(r.preset)?r.preset:'oncall',m);} if(s.te){if(typeof s.te!=='object')s.te=null;else{s.te.on=!!s.te.on;s.te.script=typeof s.te.script==='string'?s.te.script.slice(0,4000):'';}} return s;} function migrate(s){sanitize(s); diff --git a/tests/rules-locked.test.js b/tests/rules-locked.test.js new file mode 100644 index 0000000..61d3941 --- /dev/null +++ b/tests/rules-locked.test.js @@ -0,0 +1,45 @@ +// The rules follow from the mode when a save loads: an edited or imported save cannot change them (2.0). +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { load } = require('./harness'); + +test('a loaded save gets the rules of its mode, whatever its rules say', () => { + const { ev, errors } = load(); + ev("beginGame('T','fra',{mode:'normal'});if(UI.pendGame)ACT.pitchSkip()"); + const base = JSON.parse(ev('JSON.stringify(packSave(S))')); + const loadAs = (mut) => { const s = JSON.parse(JSON.stringify(base)); mut(s); ev(`S=migrate(JSON.parse(${JSON.stringify(JSON.stringify(s))}))`); return JSON.parse(ev('JSON.stringify(S.rules)')); }; + const preset = (p, o) => JSON.parse(ev(`JSON.stringify(makeRules('${p}'${o ? ',' + JSON.stringify(o) : ''}))`)); + // Normal games keep one of the three Normal presets, with nothing changed. + for (const p of ['freeze', 'oncall', 'bgp']) assert.deepEqual(loadAs(s => { s.rules = { preset: p }; }), preset(p)); + assert.deepEqual(loadAs(s => { s.rules = { preset: 'oncall', money: 'infinite', gates: 'open', chaos: 'none', lead: 'quick' }; }), preset('oncall'), 'no free money or open gates in On-call'); + assert.deepEqual(loadAs(s => { s.rules = { preset: 'freeze', clock: false }; }), preset('freeze'), 'the clock always runs'); + assert.deepEqual(loadAs(s => { s.rules = { preset: 'puzzle' }; }), preset('oncall'), 'no puzzle rules (clock off for ever) in a Normal game'); + assert.deepEqual(loadAs(s => { s.rules = { preset: 'sandbox' }; }), preset('oncall'), 'no sandbox powers without the sandbox tag'); + assert.deepEqual(loadAs(s => { delete s.rules; }), preset('oncall'), 'a save from before 2.0 is On-call'); + // Sandboxes keep only their chaos. + assert.deepEqual(loadAs(s => { s.mode = 'sandbox'; s.rules = { preset: 'sandbox', chaos: 'spicy', clock: false, money: 'normal' }; }), preset('sandbox', { chaos: 'spicy' })); + assert.deepEqual(loadAs(s => { s.mode = 'sandbox'; s.rules = { preset: 'bgp' }; }), preset('sandbox')); + // Scenarios and the weekly run on On-call. + const scn = { id: 'pac', rules: {}, days: 1095, name: 'x', mods: [] }; + for (const r of [{ preset: 'bgp' }, { preset: 'oncall', chaos: 'none' }, { preset: 'sandbox' }, { preset: 'freeze' }]) + assert.deepEqual(loadAs(s => { s.mode = 'scenario'; s.scn = scn; s.rules = r; }), preset('oncall'), JSON.stringify(r)); + assert.deepEqual(loadAs(s => { s.mode = 'scenario'; s.scn = { ...scn, id: 'weekly', key: '2026-W44' }; s.rules = { preset: 'freeze' }; }), preset('oncall'), 'the weekly too'); + assert.deepEqual(errors, []); +}); + +test('a save that claims to be a puzzle loads as a Normal On-call game with the clock running', () => { + const { ev, errors } = load(); + ev("beginGame('T','fra',{mode:'normal'});if(UI.pendGame)ACT.pitchSkip()"); + const s = JSON.parse(ev('JSON.stringify(packSave(S))')); + s.mode = 'puzzle'; s.rules = { preset: 'puzzle' }; + ev(`S=migrate(JSON.parse(${JSON.stringify(JSON.stringify(s))}));UI.started=true;compute()`); + assert.equal(ev('S.mode'), 'normal'); + assert.equal(ev('pzOn()'), false); + assert.equal(ev("rule('clock')"), true); + ev('for(let i=0;i<3;i++)dayTick()'); + assert.equal(ev('S.day'), 3, 'days pass'); + ev(`UI.importing=JSON.parse(${JSON.stringify(JSON.stringify(s))});ACT.importGo()`); + assert.equal(ev('S.mode'), 'normal'); + assert.equal(ev('rulePreset()'), 'oncall'); + assert.deepEqual(errors, []); +});