From fc287be481c7937cd5976efed6363988f2fbad5d Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 10 Oct 2024 01:40:21 +0100 Subject: [PATCH] fix player statistic tracking --- projects/backend/src/index.ts | 4 ++-- projects/backend/src/model/player.ts | 14 ++++++-------- projects/backend/src/service/player.service.ts | 3 ++- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/projects/backend/src/index.ts b/projects/backend/src/index.ts index 2c5ad17..236486b 100644 --- a/projects/backend/src/index.ts +++ b/projects/backend/src/index.ts @@ -32,13 +32,13 @@ export const app = new Elysia(); app.use( cron({ name: "player-statistics-tracker-cron", - pattern: "0 1 * * *", // Every day at 00:01 (midnight) + pattern: "*/1 * * * *", // Every day at 00:01 (midnight) timezone: "Europe/London", run: async () => { console.log("Tracking player statistics..."); const players: PlayerDocument[] = await PlayerModel.find({}); for (const player of players) { - await PlayerService.trackScoreSaberPlayer(getMidnightAlignedDate(new Date()), player); + await PlayerService.trackScoreSaberPlayer(player); } console.log("Finished tracking player statistics."); }, diff --git a/projects/backend/src/model/player.ts b/projects/backend/src/model/player.ts index e3f944e..a4dc7cc 100644 --- a/projects/backend/src/model/player.ts +++ b/projects/backend/src/model/player.ts @@ -60,13 +60,11 @@ export class Player { * @param days the number of days to get the history for. */ public getHistoryPreviousDays(days: number): Record { - if (this.statisticHistory === undefined) { - this.statisticHistory = {}; - } + const statisticHistory = this.getStatisticHistory(); const history: Record = {}; for (let i = 0; i < days; i++) { const date = formatDateMinimal(getMidnightAlignedDate(getDaysAgoDate(i))); - const playerHistory = this.getStatisticHistory()[date]; + const playerHistory = statisticHistory[date]; if (playerHistory === undefined || Object.keys(playerHistory).length === 0) { continue; } @@ -85,7 +83,7 @@ export class Player { if (this.statisticHistory === undefined) { this.statisticHistory = {}; } - this.getStatisticHistory()[formatDateMinimal(getMidnightAlignedDate(date))] = history; + this.statisticHistory[formatDateMinimal(getMidnightAlignedDate(date))] = history; } /** @@ -96,9 +94,9 @@ export class Player { if (this.statisticHistory === undefined) { this.statisticHistory = {}; } - return Object.entries(this.getStatisticHistory()) - .sort((a, b) => Date.parse(b[0]) - Date.parse(a[0])) - .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}); + this.statisticHistory = Object.fromEntries( + Object.entries(this.statisticHistory).sort((a, b) => new Date(b[0]).getTime() - new Date(a[0]).getTime()) + ); } /** diff --git a/projects/backend/src/service/player.service.ts b/projects/backend/src/service/player.service.ts index cdc0051..8c23956 100644 --- a/projects/backend/src/service/player.service.ts +++ b/projects/backend/src/service/player.service.ts @@ -68,7 +68,8 @@ export class PlayerService { * @param dateToday the date to track * @param foundPlayer the player to track */ - public static async trackScoreSaberPlayer(dateToday: Date, foundPlayer: PlayerDocument) { + public static async trackScoreSaberPlayer(foundPlayer: PlayerDocument) { + const dateToday = getMidnightAlignedDate(new Date()); const player = await scoresaberService.lookupPlayer(foundPlayer.id); if (player == undefined) { console.log(`Player "${foundPlayer.id}" not found on ScoreSaber`);