generated from nhcarrigan/template
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:
@@ -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
|
||||
? {}
|
||||
|
||||
Reference in New Issue
Block a user