fix: preserve lifetime player stats across prestige

Fixes #37. After prestige, the GameState's player.lifetime* fields were
stale — they did not include the current run's contributions. The Prisma
Player record was updated correctly, but the saved GameState had old values,
so the UI showed stale all-time totals on reload.

buildPostPrestigeState now computes run-stat contributions and folds them
into the fresh player object before writing the prestige state, ensuring
the GameState is always consistent with the DB Player record.
This commit is contained in:
2026-03-09 21:28:16 -07:00
committed by Naomi Carrigan
parent 4d7e624358
commit 062e5b59a6
2 changed files with 141 additions and 9 deletions
+42 -1
View File
@@ -205,10 +205,51 @@ const buildPostPrestigeState = (
};
const freshState = initialGameState(currentState.player, characterName);
// Compute current-run contributions to accumulate into lifetime totals
const runBossesDefeated = currentState.bosses.filter((boss) => {
return boss.status === "defeated";
}).length;
const runQuestsCompleted = currentState.quests.filter((quest) => {
return quest.status === "completed";
}).length;
let runAdventurersRecruited = 0;
for (const adventurer of currentState.adventurers) {
runAdventurersRecruited = runAdventurersRecruited + adventurer.count;
}
const runAchievementsUnlocked = currentState.achievements.filter(
(achievement) => {
return achievement.unlockedAt !== null;
},
).length;
const prestigeState: GameState = {
...freshState,
lastTickAt: Date.now(),
prestige: prestigeData,
/*
* Fold current-run totals into lifetime stats so the GameState reflects
* the true all-time values immediately after prestige.
*/
player: {
...freshState.player,
lifetimeAchievementsUnlocked:
freshState.player.lifetimeAchievementsUnlocked
+ runAchievementsUnlocked,
lifetimeAdventurersRecruited:
freshState.player.lifetimeAdventurersRecruited
+ runAdventurersRecruited,
lifetimeBossesDefeated:
freshState.player.lifetimeBossesDefeated + runBossesDefeated,
lifetimeClicks:
freshState.player.lifetimeClicks + currentState.player.totalClicks,
lifetimeGoldEarned:
freshState.player.lifetimeGoldEarned
+ currentState.player.totalGoldEarned,
lifetimeQuestsCompleted:
freshState.player.lifetimeQuestsCompleted + runQuestsCompleted,
},
prestige: prestigeData,
// Codex lore persists across prestiges — players keep their discovered entries
...currentState.codex === undefined
? {}