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(
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.");
},

@ -60,13 +60,11 @@ export class Player {
* @param days the number of days to get the history for.
*/
public getHistoryPreviousDays(days: number): Record<string, PlayerHistory> {
if (this.statisticHistory === undefined) {
this.statisticHistory = {};
}
const statisticHistory = this.getStatisticHistory();
const history: Record<string, PlayerHistory> = {};
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())
);
}
/**

@ -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`);