Puzzles: the best spend in the list is the cheapest build at your best stars

Progress is recorded on every change, not only when the stars go up: more stars
start the best spend again, and a cheaper build at the same stars replaces it.
This commit is contained in:
Frederic Jung 2026-10-28 19:41:10 +01:00
parent d3f550cc3e
commit 66652576e5
No known key found for this signature in database
2 changed files with 40 additions and 2 deletions

View file

@ -101,8 +101,11 @@ const pzNewPop=()=>({day:0,bb:null,boost:0,down:0,routers:1,gen:false});
function pzAfter(){if(UI.sel&&((UI.sel.type==='link'&&!S.links.some(l=>l.id===UI.sel.id))||(UI.sel.type==='city'&&!pzKind(UI.sel.id))))UI.sel=null;
compute();renderAll();pzCheck();}
// Stars count the moment they are earned: a better result than this attempt has had opens the results card.
function pzCheck(){const e=pzEval();if(e.stars<=(UI.pzTop||0))return e;const prev=UI.pzTop||0;UI.pzTop=e.stars;pzRecord(e);pzResults(e,prev);return e;}
// Every solved state is recorded, so a cheaper build at the same stars counts too.
function pzCheck(){const e=pzEval();if(e.stars)pzRecord(e);if(e.stars<=(UI.pzTop||0))return e;const prev=UI.pzTop||0;UI.pzTop=e.stars;pzResults(e,prev);return e;}
// The best spend belongs to the best star count: more stars start it again, the same stars keep the lower spend.
function pzRecord(e){const p=pzLoad(),P=S.pz,bag=P.id==='weekly'?p.wk:p.s,k=P.id==='weekly'?P.key:P.id,r=bag[k]||{st:0,sp:null};
r.st=Math.max(r.st,e.stars);r.sp=r.sp==null?e.spend:Math.min(r.sp,e.spend);bag[k]=r;if(P.id==='weekly')pzStreak(p,P.key);pzStore(p);}
if(e.stars<r.st||(e.stars===r.st&&r.sp!=null&&r.sp<=e.spend))return;
r.sp=e.spend;r.st=e.stars;bag[k]=r;if(P.id==='weekly')pzStreak(p,P.key);pzStore(p);}
// The next puzzle in the list whose pack is open.
function pzNextDef(){const p=pzLoad(),i=PZ.findIndex(x=>x.id===S.pz.id);for(let j=i+1;j<PZ.length;j++)if(pzPackOpen(PZ[j].pack,p))return PZ[j];return null;}

View file

@ -313,3 +313,38 @@ test('a POP in a thin market costs its higher monthly rent too, and the panel sa
assert.ok(ev("document.getElementById('panelBody').textContent").includes(ev("fmt$(popMo('suv'))") + '/mo'));
assert.deepEqual(errors, []);
});
test('best spend: kept for the best star count, and a cheaper build at the same stars counts', () => {
const { ev, errors } = load();
ev("localStorage.removeItem('sot-puzzles');pzBegin(pzDef('braces'))");
const rec = () => JSON.parse(ev("JSON.stringify(pzLoad().s.braces||null)"));
build(ev, REF.braces.one);
const two = evalP(ev).spend;
assert.deepEqual(rec(), { st: 2, sp: two });
// A gold-plated third link: N-1 but over budget is still two stars, and dearer, so the best stays.
ev("ACT.pzLinkTo({a:'dub',b:'nyc'});ACT.pzVia({v:'havf'});ACT.pzLink({tier:2});closeModal()");
assert.equal(evalP(ev).stars, 2);
assert.deepEqual(rec(), { st: 2, sp: two });
// Shrinking it to 10G earns the third star: the best spend starts again at the 3-star build.
ev('ACT.pzTier({id:S.links[2].id,tier:0})');
const three = evalP(ev);
assert.equal(three.stars, 3);
assert.deepEqual(rec(), { st: 3, sp: three.spend });
// With a looser budget, a dearer 3-star build does not replace it; going back to the cheaper one keeps it.
ev('S.pz.budget=1e7;ACT.pzTier({id:S.links[0].id,tier:1})');
assert.equal(evalP(ev).stars, 3);
assert.deepEqual(rec(), { st: 3, sp: three.spend });
// A cheaper 3-star build after a dearer first one is recorded (the old code only recorded star rises).
ev("localStorage.removeItem('sot-puzzles')");
ev('ACT.pzTier({id:S.links[1].id,tier:1})');
const dear = evalP(ev).spend;
assert.deepEqual(rec(), { st: 3, sp: dear });
ev('ACT.pzTier({id:S.links[1].id,tier:0});ACT.pzTier({id:S.links[0].id,tier:0})');
assert.deepEqual(rec(), { st: 3, sp: three.spend });
// Fewer stars never overwrite more.
ev('ACT.pzRmLink({id:S.links[2].id})');
assert.deepEqual(rec(), { st: 3, sp: three.spend });
ev('ACT.puzzleMenu({})');
assert.ok(ev("document.getElementById('modalCard').textContent").includes('best ' + ev(`fmt$(${three.spend})`)));
assert.deepEqual(errors, []);
});