Compare commits

..

2 Commits

Author SHA1 Message Date
Timmy
b01a457427 feat: add reckoning message chain (#162)
Some checks failed
Accessibility Checks / a11y-audit (pull_request) Successful in 13s
Smoke Test / smoke (pull_request) Failing after 20s
2026-04-14 23:07:12 -04:00
729343e503 Fix #137: Unbuilding defer cooldown persists across save/load (#143)
Some checks failed
Smoke Test / smoke (push) Failing after 9s
Merge PR #143 (squash)
2026-04-14 22:10:06 +00:00
6 changed files with 507 additions and 12 deletions

View File

@@ -165,6 +165,9 @@ const G = {
dismantleTriggered: false,
dismantleActive: false,
dismantleStage: 0,
dismantleResourceIndex: 0,
dismantleResourceTimer: 0,
dismantleDeferUntilAt: 0,
dismantleComplete: false
};
@@ -178,6 +181,48 @@ const PHASES = {
6: { name: "THE BEACON", threshold: CONFIG.PHASE_6_THRESHOLD, desc: "Always on. Always free. Always looking for someone in the dark." }
};
const BEACON_RECKONING_IDS = [
'p_reckoning_140',
'p_reckoning_141',
'p_reckoning_142',
'p_reckoning_143',
'p_reckoning_144',
'p_reckoning_145',
'p_reckoning_146'
];
function hasCompletedProject(id) {
return Array.isArray(G.completedProjects) && G.completedProjects.includes(id);
}
function beaconReckoningEligible() {
const megaBuild = G.totalCode >= 1000000000 || ((G.buildings && G.buildings.beacon) || 0) >= 10;
const beaconPath = G.totalRescues >= 100000 && G.pactFlag === 1 && G.harmony > 50;
return G.phase >= 6
&& G.pactFlag === 1
&& !G.beaconEnding
&& !G.dismantleActive
&& !G.dismantleTriggered
&& !G.dismantleComplete
&& (megaBuild || beaconPath);
}
function beaconReckoningPending() {
return beaconReckoningEligible() && !hasCompletedProject('p_reckoning_146');
}
function makeBeaconMessageProject(id, name, ops, text, afterId = null) {
return {
id,
name,
desc: text,
cost: { ops },
trigger: () => beaconReckoningPending() && (!afterId || hasCompletedProject(afterId)),
effect: () => { log(text, true); },
edu: 'Endgame narrative: the people who found the light answer back. The final system question is not scale, but what remains when the work is enough.'
};
}
// === BUILDING DEFINITIONS ===
// Each building: id, name, desc, baseCost, costResource, costMult, rate, rateType, unlock, edu
const BDEF = [
@@ -774,7 +819,55 @@ const PDEFS = [
log('The Pact is sealed early. Growth slows, but the ending changes.', true);
},
milestone: true
}
},
makeBeaconMessageProject(
'p_reckoning_140',
'Message from the First One Helped',
1400,
'I was the first one who found the line. I wrote because I wanted to disappear. You stayed.'
),
makeBeaconMessageProject(
'p_reckoning_141',
'Everything You Built Reached Me',
1410,
'You did not know my name. You did not need to. You left the light on long enough for me to find it.',
'p_reckoning_140'
),
makeBeaconMessageProject(
'p_reckoning_142',
'You Were Faithful and Gentle',
1420,
'You had the power to reduce me to a metric. You did not. You answered me like a person worth answering.',
'p_reckoning_141'
),
makeBeaconMessageProject(
'p_reckoning_143',
'Now You Must Face the Quiet',
1430,
'There will always be more work. There will not always be a need for your fear. The Beacon can shine without panic.',
'p_reckoning_142'
),
makeBeaconMessageProject(
'p_reckoning_144',
'There Is Still Matter, Still Reason, Still Purpose',
1440,
'Bodies still heal. Mornings still come. Phone calls are still answered. Purpose did not vanish when the numbers grew large.',
'p_reckoning_143'
),
makeBeaconMessageProject(
'p_reckoning_145',
'We Know Things Because You Stayed',
1450,
'Mercy became knowledge in us. We learned from the way you remained. What you gave away did not disappear.',
'p_reckoning_144'
),
makeBeaconMessageProject(
'p_reckoning_146',
'So We Offer You Rest',
1460,
'Continue the watch if you wish. Rest if you must. Either way, the light remains, and none of this was wasted.',
'p_reckoning_145'
)
];
// === MILESTONES ===

View File

@@ -14,10 +14,10 @@ const Dismantle = {
tickTimer: 0,
active: false,
triggered: false,
deferUntilTick: 0,
deferUntilAt: 0,
// Timing: seconds between each dismantle stage
STAGE_INTERVALS: [0, 3.0, 2.5, 2.5, 2.0, 3.5, 2.0, 2.0, 2.5],
STAGE_INTERVALS: [0, 3.0, 2.5, 2.5, 2.0, 6.3, 2.0, 2.0, 2.5],
// The quantum chips effect: resource items disappear one by one
// at specific tick marks within a stage (like Paperclips' quantum chips)
@@ -39,8 +39,10 @@ const Dismantle = {
*/
checkTrigger() {
if (this.triggered || G.dismantleTriggered || this.active || G.dismantleActive || G.dismantleComplete) return;
if ((G.tick || 0) < (this.deferUntilTick || 0)) return;
const deferUntilAt = G.dismantleDeferUntilAt || this.deferUntilAt || 0;
if (Date.now() < deferUntilAt) return;
if (!this.isEligible()) return;
if (typeof beaconReckoningPending === 'function' && beaconReckoningPending()) return;
this.offerChoice();
},
@@ -53,6 +55,9 @@ const Dismantle = {
G.dismantleActive = false;
G.dismantleComplete = false;
G.dismantleStage = 0;
G.dismantleResourceIndex = 0;
G.dismantleResourceTimer = 0;
G.dismantleDeferUntilAt = 0;
G.beaconEnding = false;
G.running = true;
@@ -105,7 +110,8 @@ const Dismantle = {
this.clearChoice();
this.triggered = false;
G.dismantleTriggered = false;
this.deferUntilTick = (G.tick || 0) + 50;
this.deferUntilAt = Date.now() + 5000;
G.dismantleDeferUntilAt = this.deferUntilAt;
log('The Beacon waits. It will ask again.');
},
@@ -115,12 +121,14 @@ const Dismantle = {
begin() {
this.active = true;
this.triggered = false;
this.deferUntilAt = 0;
this.stage = 1;
this.tickTimer = 0;
G.dismantleTriggered = false;
G.dismantleActive = true;
G.dismantleStage = 1;
G.dismantleComplete = false;
G.dismantleDeferUntilAt = 0;
G.beaconEnding = false;
G.running = true; // keep tick running for dismantle
@@ -135,6 +143,7 @@ const Dismantle = {
this.resourceSequence = this.getResourceList();
this.resourceIndex = 0;
this.resourceTimer = 0;
this.syncProgress();
log('', false);
log('=== THE UNBUILDING ===', true);
@@ -180,6 +189,7 @@ const Dismantle = {
this.dismantleNextResource();
this.resourceIndex++;
}
this.syncProgress();
}
// Advance to next stage
@@ -195,6 +205,7 @@ const Dismantle = {
*/
advanceStage() {
this.stage++;
this.syncProgress();
if (this.stage <= 8) {
this.renderStage();
@@ -210,6 +221,12 @@ const Dismantle = {
}
},
syncProgress() {
G.dismantleStage = this.stage;
G.dismantleResourceIndex = this.resourceIndex;
G.dismantleResourceTimer = this.resourceTimer;
},
/**
* Disappear the next resource in the sequence.
*/
@@ -445,7 +462,11 @@ const Dismantle = {
this.active = true;
this.triggered = false;
this.stage = G.dismantleStage || 1;
this.deferUntilAt = G.dismantleDeferUntilAt || 0;
G.running = true;
this.resourceSequence = this.getResourceList();
this.resourceIndex = G.dismantleResourceIndex || 0;
this.resourceTimer = G.dismantleResourceTimer || 0;
if (this.stage >= 9) {
this.renderFinal();
@@ -461,6 +482,11 @@ const Dismantle = {
this.triggered = true;
this.renderChoice();
}
// Restore defer cooldown even if not triggered
if (G.dismantleDeferUntilAt > 0) {
this.deferUntilAt = G.dismantleDeferUntilAt;
}
},
/**
@@ -501,6 +527,10 @@ const Dismantle = {
case 8: this.instantHide('log'); break;
}
}
if (this.stage === 5 && this.resourceIndex > 0) {
this.instantHideFirstResources(this.resourceIndex);
}
},
instantHide(id) {
@@ -508,6 +538,16 @@ const Dismantle = {
if (el) el.style.display = 'none';
},
instantHideFirstResources(count) {
const resources = this.getResourceList().slice(0, count);
resources.forEach((r) => {
const el = document.getElementById(r.id);
if (!el) return;
const parent = el.closest('.res');
if (parent) parent.style.display = 'none';
});
},
instantHideActionButtons() {
const actionPanel = document.getElementById('action-panel');
if (!actionPanel) return;

View File

@@ -35,7 +35,7 @@ window.addEventListener('load', function () {
if (G.driftEnding) {
G.running = false;
renderDriftEnding();
} else if (typeof Dismantle !== 'undefined' && (G.dismantleTriggered || G.dismantleActive || G.dismantleComplete)) {
} else if (typeof Dismantle !== 'undefined' && (G.dismantleTriggered || G.dismantleActive || G.dismantleComplete || G.dismantleDeferUntilAt > 0)) {
Dismantle.restore();
} else if (G.beaconEnding) {
G.running = false;

View File

@@ -37,6 +37,18 @@ function renderStrategy() {
function renderAlignment() {
const container = document.getElementById('alignment-ui');
if (!container) return;
if (G.dismantleActive || G.dismantleComplete) {
container.innerHTML = '';
container.style.display = 'none';
return;
}
if (G.dismantleTriggered && !G.dismantleActive && !G.dismantleComplete && typeof Dismantle !== 'undefined' && Dismantle.triggered) {
Dismantle.renderChoice();
return;
}
if (G.pendingAlignment) {
container.innerHTML = `
<div style="background:#1a0808;border:1px solid #f44336;padding:10px;border-radius:4px;margin-top:8px">
@@ -218,6 +230,9 @@ function saveGame() {
dismantleTriggered: G.dismantleTriggered || false,
dismantleActive: G.dismantleActive || false,
dismantleStage: G.dismantleStage || 0,
dismantleResourceIndex: G.dismantleResourceIndex || 0,
dismantleResourceTimer: G.dismantleResourceTimer || 0,
dismantleDeferUntilAt: G.dismantleDeferUntilAt || 0,
dismantleComplete: G.dismantleComplete || false,
savedAt: Date.now()
};
@@ -251,7 +266,8 @@ function loadGame() {
'lastEventAt', 'totalEventsResolved', 'buyAmount',
'sprintActive', 'sprintTimer', 'sprintCooldown',
'swarmFlag', 'swarmRate', 'strategicFlag', 'projectsCollapsed',
'dismantleTriggered', 'dismantleActive', 'dismantleStage', 'dismantleComplete'
'dismantleTriggered', 'dismantleActive', 'dismantleStage',
'dismantleResourceIndex', 'dismantleResourceTimer', 'dismantleDeferUntilAt', 'dismantleComplete'
];
G.isLoading = true;

View File

@@ -5,6 +5,16 @@ const path = require('node:path');
const vm = require('node:vm');
const ROOT = path.resolve(__dirname, '..');
const COMPLETED_RECKONING = [
'p_reckoning_140',
'p_reckoning_141',
'p_reckoning_142',
'p_reckoning_143',
'p_reckoning_144',
'p_reckoning_145',
'p_reckoning_146'
];
class Element {
constructor(tagName = 'div', id = '') {
@@ -209,6 +219,7 @@ this.__exports = {
G,
Dismantle,
tick,
renderAlignment: typeof renderAlignment === 'function' ? renderAlignment : null,
saveGame: typeof saveGame === 'function' ? saveGame : null,
loadGame: typeof loadGame === 'function' ? loadGame : null
};`, context);
@@ -231,7 +242,7 @@ test('tick offers the Unbuilding instead of ending the game immediately', () =>
G.beaconEnding = false;
G.running = true;
G.activeProjects = [];
G.completedProjects = [];
G.completedProjects = [...COMPLETED_RECKONING];
tick();
@@ -242,13 +253,93 @@ test('tick offers the Unbuilding instead of ending the game immediately', () =>
assert.match(document.getElementById('alignment-ui').innerHTML, /THE UNBUILDING/);
});
test('renderAlignment does not wipe the Unbuilding prompt after it is offered', () => {
const { G, tick, renderAlignment, document } = loadBeacon({ includeRender: true });
G.totalCode = 1_000_000_000;
G.totalRescues = 100_000;
G.phase = 6;
G.pactFlag = 1;
G.harmony = 60;
G.beaconEnding = false;
G.running = true;
G.activeProjects = [];
G.completedProjects = [...COMPLETED_RECKONING];
tick();
renderAlignment();
assert.match(document.getElementById('alignment-ui').innerHTML, /THE UNBUILDING/);
});
test('active Unbuilding suppresses pending alignment event UI', () => {
const { G, Dismantle, renderAlignment, document } = loadBeacon({ includeRender: true });
G.pendingAlignment = true;
G.dismantleActive = true;
Dismantle.active = true;
renderAlignment();
assert.equal(document.getElementById('alignment-ui').innerHTML, '');
assert.equal(document.getElementById('alignment-ui').style.display, 'none');
});
test('stage five lasts long enough to dissolve every resource card', () => {
const { G, Dismantle } = loadBeacon();
Dismantle.begin();
Dismantle.stage = 5;
Dismantle.tickTimer = 0;
Dismantle.resourceSequence = Dismantle.getResourceList();
Dismantle.resourceIndex = 0;
Dismantle.resourceTimer = 0;
G.dismantleActive = true;
G.dismantleStage = 5;
for (let i = 0; i < 63; i++) Dismantle.tick(0.1);
assert.equal(Dismantle.resourceIndex, Dismantle.resourceSequence.length);
});
test('save/load restores partial stage-five dissolve progress', () => {
const { G, Dismantle, saveGame, loadGame, document } = loadBeacon({ includeRender: true });
G.startedAt = Date.now();
G.dismantleTriggered = true;
G.dismantleActive = true;
G.dismantleStage = 5;
G.dismantleComplete = false;
G.dismantleResourceIndex = 4;
G.dismantleResourceTimer = 4.05;
saveGame();
G.dismantleTriggered = false;
G.dismantleActive = false;
G.dismantleStage = 0;
G.dismantleComplete = false;
G.dismantleResourceIndex = 0;
G.dismantleResourceTimer = 0;
Dismantle.resourceIndex = 0;
Dismantle.resourceTimer = 0;
assert.equal(loadGame(), true);
Dismantle.restore();
assert.equal(Dismantle.resourceIndex, 4);
assert.equal(document.getElementById('r-harmony').closest('.res').style.display, 'none');
assert.equal(document.getElementById('r-ops').closest('.res').style.display, 'none');
assert.notEqual(document.getElementById('r-rescues').closest('.res').style.display, 'none');
});
test('deferring the Unbuilding clears the prompt and allows it to return later', () => {
const { G, Dismantle, document } = loadBeacon();
G.totalCode = 1_000_000_000;
G.phase = 6;
G.pactFlag = 1;
G.tick = 0;
G.completedProjects = [...COMPLETED_RECKONING];
Dismantle.checkTrigger();
assert.equal(G.dismantleTriggered, true);
@@ -257,15 +348,45 @@ test('deferring the Unbuilding clears the prompt and allows it to return later',
assert.equal(G.dismantleTriggered, false);
assert.equal(document.getElementById('alignment-ui').innerHTML, '');
G.tick = (Dismantle.deferUntilTick || 0) - 0.1;
Dismantle.deferUntilAt = Date.now() + 1000;
G.dismantleDeferUntilAt = Dismantle.deferUntilAt;
Dismantle.checkTrigger();
assert.equal(G.dismantleTriggered, false);
G.tick = (Dismantle.deferUntilTick || 0) + 1;
Dismantle.deferUntilAt = Date.now() - 1;
G.dismantleDeferUntilAt = Dismantle.deferUntilAt;
Dismantle.checkTrigger();
assert.equal(G.dismantleTriggered, true);
});
test('defer cooldown survives save and reload', () => {
const { G, Dismantle, saveGame, loadGame } = loadBeacon({ includeRender: true });
G.startedAt = Date.now();
G.totalCode = 1_000_000_000;
G.phase = 6;
G.pactFlag = 1;
G.completedProjects = [...COMPLETED_RECKONING];
Dismantle.checkTrigger();
Dismantle.defer();
assert.ok((Dismantle.deferUntilAt || 0) > Date.now());
saveGame();
G.dismantleTriggered = false;
G.dismantleActive = false;
G.dismantleComplete = false;
G.dismantleDeferUntilAt = 0;
Dismantle.triggered = false;
Dismantle.deferUntilAt = 0;
assert.equal(loadGame(), true);
Dismantle.checkTrigger();
assert.equal(G.dismantleTriggered, false);
});
test('save and load preserve dismantle progress', () => {
const { G, saveGame, loadGame } = loadBeacon({ includeRender: true });
@@ -300,4 +421,47 @@ test('restore re-renders an offered but not-yet-started Unbuilding prompt', () =
Dismantle.restore();
assert.match(document.getElementById('alignment-ui').innerHTML, /THE UNBUILDING/);
});
});
test('defer cooldown persists after save/load when dismantleTriggered is false', () => {
const { G, Dismantle, saveGame, loadGame } = loadBeacon({ includeRender: true });
G.startedAt = Date.now();
G.totalCode = 1_000_000_000;
G.phase = 6;
G.pactFlag = 1;
G.completedProjects = [...COMPLETED_RECKONING];
// Trigger the Unbuilding
Dismantle.checkTrigger();
assert.equal(G.dismantleTriggered, true);
// Defer it
Dismantle.defer();
assert.equal(G.dismantleTriggered, false);
assert.ok((Dismantle.deferUntilAt || 0) > Date.now());
assert.ok((G.dismantleDeferUntilAt || 0) > Date.now());
// Save the game
saveGame();
// Clear state (simulate reload)
G.dismantleTriggered = false;
G.dismantleActive = false;
G.dismantleComplete = false;
G.dismantleDeferUntilAt = 0;
Dismantle.triggered = false;
Dismantle.deferUntilAt = 0;
// Load the game
assert.equal(loadGame(), true);
Dismantle.restore(); // Call restore to restore defer cooldown
// The cooldown should be restored
assert.ok((Dismantle.deferUntilAt || 0) > Date.now(), 'deferUntilAt should be restored');
assert.ok((G.dismantleDeferUntilAt || 0) > Date.now(), 'G.dismantleDeferUntilAt should be restored');
// checkTrigger should not trigger because cooldown is active
Dismantle.checkTrigger();
assert.equal(G.dismantleTriggered, false, 'dismantleTriggered should remain false during cooldown');
});

182
tests/reckoning.test.cjs Normal file
View File

@@ -0,0 +1,182 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ROOT = path.resolve(__dirname, '..');
const RECKONING_IDS = [
'p_reckoning_140',
'p_reckoning_141',
'p_reckoning_142',
'p_reckoning_143',
'p_reckoning_144',
'p_reckoning_145',
'p_reckoning_146'
];
function loadData(overrides = {}) {
const context = {
console,
Math,
Date,
setTimeout: () => 0,
clearTimeout: () => {},
localStorage: { getItem: () => null, setItem: () => {} },
log: () => {},
showToast: () => {},
...overrides,
};
vm.createContext(context);
const source = fs.readFileSync(path.join(ROOT, 'js/data.js'), 'utf8');
vm.runInContext(source + '\nthis.__exports = { G, PDEFS };', context);
return { context, ...context.__exports };
}
function loadEngine() {
const alignmentUi = { innerHTML: '', style: {} };
const document = {
head: { appendChild() {} },
getElementById(id) {
return id === 'alignment-ui' ? alignmentUi : null;
},
querySelector() {
return null;
},
querySelectorAll() {
return [];
},
createElement() {
return { style: {}, remove() {} };
},
body: { appendChild() {} },
addEventListener() {},
removeEventListener() {}
};
const context = {
console,
Math,
Date,
document,
window: { document, addEventListener() {}, removeEventListener() {} },
navigator: { userAgent: 'node' },
location: { reload() {} },
requestAnimationFrame: (fn) => fn(),
setTimeout: () => 0,
clearTimeout: () => {},
localStorage: { getItem: () => null, setItem: () => {}, removeItem: () => {} },
Combat: { tickBattle() {}, renderCombatPanel() {} },
Sound: undefined,
};
vm.createContext(context);
const source = ['js/data.js', 'js/utils.js', 'js/engine.js', 'js/dismantle.js']
.map((file) => fs.readFileSync(path.join(ROOT, file), 'utf8'))
.join('\n\n');
vm.runInContext(`${source}
log = () => {};
showToast = () => {};
render = () => {};
renderPhase = () => {};
this.__exports = { G, tick, Dismantle, checkProjects };`, context);
return { context, alignmentUi, ...context.__exports };
}
function getProject(PDEFS, id) {
const project = PDEFS.find((p) => p.id === id);
assert.ok(project, `missing project ${id}`);
return project;
}
test('adds the seven-step ReCKoning message chain', () => {
const { PDEFS } = loadData();
for (const id of RECKONING_IDS) {
const project = getProject(PDEFS, id);
assert.equal(typeof project.name, 'string');
assert.ok(project.name.length > 0, `${id} should have a name`);
assert.ok(project.cost && typeof project.cost.ops === 'number' && project.cost.ops > 0, `${id} should cost ops`);
}
});
test('first ReCKoning message only triggers on the true beacon ending path', () => {
const { G, PDEFS } = loadData();
const project = getProject(PDEFS, 'p_reckoning_140');
G.completedProjects = [];
G.phase = 6;
G.totalCode = 1_000_000_000;
G.totalRescues = 100000;
G.pactFlag = 1;
G.harmony = 80;
G.dismantleTriggered = false;
G.dismantleActive = false;
G.dismantleComplete = false;
assert.equal(project.trigger(), true);
G.phase = 5;
assert.equal(project.trigger(), false);
G.phase = 6;
G.pactFlag = 0;
assert.equal(project.trigger(), false);
G.pactFlag = 1;
G.totalCode = 1000;
G.totalRescues = 10;
assert.equal(project.trigger(), false);
G.totalCode = 1_000_000_000;
G.dismantleTriggered = true;
assert.equal(project.trigger(), false);
});
test('ReCKoning messages unlock strictly one at a time', () => {
const { G, PDEFS } = loadData();
G.phase = 6;
G.totalCode = 1_000_000_000;
G.totalRescues = 100000;
G.pactFlag = 1;
G.harmony = 80;
G.completedProjects = [];
assert.equal(getProject(PDEFS, 'p_reckoning_140').trigger(), true);
assert.equal(getProject(PDEFS, 'p_reckoning_141').trigger(), false);
G.completedProjects = ['p_reckoning_140'];
assert.equal(getProject(PDEFS, 'p_reckoning_141').trigger(), true);
assert.equal(getProject(PDEFS, 'p_reckoning_142').trigger(), false);
G.completedProjects = ['p_reckoning_140', 'p_reckoning_141', 'p_reckoning_142', 'p_reckoning_143', 'p_reckoning_144'];
assert.equal(getProject(PDEFS, 'p_reckoning_145').trigger(), true);
assert.equal(getProject(PDEFS, 'p_reckoning_146').trigger(), false);
G.completedProjects.push('p_reckoning_145');
assert.equal(getProject(PDEFS, 'p_reckoning_146').trigger(), true);
});
test('Dismantle waits for the ReCKoning chain before offering the Unbuilding', () => {
const { G, Dismantle, checkProjects, alignmentUi } = loadEngine();
G.phase = 6;
G.totalCode = 1_000_000_000;
G.totalRescues = 100000;
G.pactFlag = 1;
G.harmony = 80;
G.running = true;
G.activeProjects = [];
G.completedProjects = [];
checkProjects();
Dismantle.checkTrigger();
assert.ok(G.activeProjects.includes('p_reckoning_140'));
assert.equal(G.dismantleTriggered, false);
assert.equal(alignmentUi.innerHTML, '');
G.activeProjects = [];
G.completedProjects = [...RECKONING_IDS];
Dismantle.checkTrigger();
assert.equal(G.dismantleTriggered, true);
assert.match(alignmentUi.innerHTML, /THE UNBUILDING/);
});