Skip to content

Commit

Permalink
Fix last session 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 213ec25 commit 154c141
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const defaultSettings = {

const defaultStats = {
updated: new Date(),
lastSessionStarted: "",
elapsed: {
lastSession: 0,
bestSession: 0,
Expand Down Expand Up @@ -1351,23 +1352,30 @@ 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);
}

/**
* @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
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Stat = {

export type Stats = {
updated: Date,
lastSessionStarted: string,
elapsed: Stat,
copiedCharacters: Stat,
copiedGroups: Stat,
Expand Down

0 comments on commit 154c141

Please sign in to comment.