From 154c14177971b854692b7a368be0caccfd53a4f8 Mon Sep 17 00:00:00 2001 From: Quentin Santos Date: Sat, 30 Nov 2024 11:47:02 +0100 Subject: [PATCH] Fix last session stats update when importing --- src/lib.js | 22 +++++++++++++++------- src/types.d.ts | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/lib.js b/src/lib.js index 7e2d8dd..70fced3 100644 --- a/src/lib.js +++ b/src/lib.js @@ -42,6 +42,7 @@ const defaultSettings = { const defaultStats = { updated: new Date(), + lastSessionStarted: "", elapsed: { lastSession: 0, bestSession: 0, @@ -1351,12 +1352,15 @@ function refreshStatistics() { /** Increase a stat by a given amount * @param {import("./types").Stat} stat - The stat to be increased * @param {number} amount - The amount by which the stat should be increased + * @param {boolean} updateLastSession - Whether the last session should be updated */ -function updateStat(stat, amount) { +function updateStat(stat, amount, updateLastSession) { stat.total += amount; - stat.lastSession = amount; + if (updateLastSession) { + stat.lastSession = amount; + } stat.currentDay += amount; - stat.bestSession = Math.max(stat.bestSession, stat.lastSession); + stat.bestSession = Math.max(stat.bestSession, amount); stat.bestDay = Math.max(stat.bestDay, stat.currentDay); } @@ -1364,10 +1368,14 @@ function updateStat(stat, amount) { * @param {import("./types").HistoryEntry} session */ function updateStats(session) { - updateStat(stats.elapsed, session.elapsed); - updateStat(stats.copiedCharacters, session.copiedCharacters); - updateStat(stats.copiedGroups, session.copiedGroups); - updateStat(stats.score, session.score); + const updateLastSession = session.started > stats.lastSessionStarted; + if (updateLastSession) { + stats.lastSessionStarted = session.started; + } + updateStat(stats.elapsed, session.elapsed, updateLastSession); + updateStat(stats.copiedCharacters, session.copiedCharacters, updateLastSession); + updateStat(stats.copiedGroups, session.copiedGroups, updateLastSession); + updateStat(stats.score, session.score, updateLastSession); } /** Compute the duration of a character with the current settings diff --git a/src/types.d.ts b/src/types.d.ts index d258fc0..cfc47bf 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -8,6 +8,7 @@ export type Stat = { export type Stats = { updated: Date, + lastSessionStarted: string, elapsed: Stat, copiedCharacters: Stat, copiedGroups: Stat,