fix player statistic tracking
Some checks failed
Deploy Backend / deploy (push) Has been cancelled

This commit is contained in:
Lee 2024-10-10 01:40:21 +01:00
parent 0f1c101acc
commit fc287be481
3 changed files with 10 additions and 11 deletions

@ -32,13 +32,13 @@ export const app = new Elysia();
app.use( app.use(
cron({ cron({
name: "player-statistics-tracker-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", timezone: "Europe/London",
run: async () => { run: async () => {
console.log("Tracking player statistics..."); console.log("Tracking player statistics...");
const players: PlayerDocument[] = await PlayerModel.find({}); const players: PlayerDocument[] = await PlayerModel.find({});
for (const player of players) { for (const player of players) {
await PlayerService.trackScoreSaberPlayer(getMidnightAlignedDate(new Date()), player); await PlayerService.trackScoreSaberPlayer(player);
} }
console.log("Finished tracking player statistics."); console.log("Finished tracking player statistics.");
}, },

@ -60,13 +60,11 @@ export class Player {
* @param days the number of days to get the history for. * @param days the number of days to get the history for.
*/ */
public getHistoryPreviousDays(days: number): Record<string, PlayerHistory> { public getHistoryPreviousDays(days: number): Record<string, PlayerHistory> {
if (this.statisticHistory === undefined) { const statisticHistory = this.getStatisticHistory();
this.statisticHistory = {};
}
const history: Record<string, PlayerHistory> = {}; const history: Record<string, PlayerHistory> = {};
for (let i = 0; i < days; i++) { for (let i = 0; i < days; i++) {
const date = formatDateMinimal(getMidnightAlignedDate(getDaysAgoDate(i))); const date = formatDateMinimal(getMidnightAlignedDate(getDaysAgoDate(i)));
const playerHistory = this.getStatisticHistory()[date]; const playerHistory = statisticHistory[date];
if (playerHistory === undefined || Object.keys(playerHistory).length === 0) { if (playerHistory === undefined || Object.keys(playerHistory).length === 0) {
continue; continue;
} }
@ -85,7 +83,7 @@ export class Player {
if (this.statisticHistory === undefined) { if (this.statisticHistory === undefined) {
this.statisticHistory = {}; 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) { if (this.statisticHistory === undefined) {
this.statisticHistory = {}; this.statisticHistory = {};
} }
return Object.entries(this.getStatisticHistory()) this.statisticHistory = Object.fromEntries(
.sort((a, b) => Date.parse(b[0]) - Date.parse(a[0])) Object.entries(this.statisticHistory).sort((a, b) => new Date(b[0]).getTime() - new Date(a[0]).getTime())
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}); );
} }
/** /**

@ -68,7 +68,8 @@ export class PlayerService {
* @param dateToday the date to track * @param dateToday the date to track
* @param foundPlayer the player 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); const player = await scoresaberService.lookupPlayer(foundPlayer.id);
if (player == undefined) { if (player == undefined) {
console.log(`Player "${foundPlayer.id}" not found on ScoreSaber`); console.log(`Player "${foundPlayer.id}" not found on ScoreSaber`);