From e739e6842c48a9364cfb7286a7557f8e2b1f6b5a Mon Sep 17 00:00:00 2001 From: Quentin Santos Date: Sat, 30 Nov 2024 11:51:43 +0100 Subject: [PATCH] Fix today stats update when importing --- src/lib.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/lib.js b/src/lib.js index 70fced3..a1cd3a0 100644 --- a/src/lib.js +++ b/src/lib.js @@ -1353,15 +1353,20 @@ function refreshStatistics() { * @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 + * @param {boolean} updateToday - Whether the current day should be updated */ -function updateStat(stat, amount, updateLastSession) { +function updateStat(stat, amount, updateLastSession, updateToday) { stat.total += amount; if (updateLastSession) { stat.lastSession = amount; } - stat.currentDay += amount; + if (updateToday) { + stat.currentDay += amount; + // TODO: does not work when importing, since best day can be different + // from current day; also, order of sessions is not guaranteed + stat.bestDay = Math.max(stat.bestDay, stat.currentDay); + } stat.bestSession = Math.max(stat.bestSession, amount); - stat.bestDay = Math.max(stat.bestDay, stat.currentDay); } /** @@ -1372,10 +1377,11 @@ function updateStats(session) { 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); + const updateToday = session.started.slice(0, 10) === stats.updated.toISOString().slice(0, 10); + updateStat(stats.elapsed, session.elapsed, updateLastSession, updateToday); + updateStat(stats.copiedCharacters, session.copiedCharacters, updateLastSession, updateToday); + updateStat(stats.copiedGroups, session.copiedGroups, updateLastSession, updateToday); + updateStat(stats.score, session.score, updateLastSession, updateToday); } /** Compute the duration of a character with the current settings