Skip to content

Commit

Permalink
Fix today stats update when importing
Browse files Browse the repository at this point in the history
  • Loading branch information
qsantos committed Nov 30, 2024
1 parent 154c141 commit e739e68
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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
Expand Down

0 comments on commit e739e68

Please sign in to comment.