From 0a5d42f6ac04b1cf037c3efc25e7c3af466b7458 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 28 Oct 2024 12:12:44 +0000 Subject: [PATCH] make player score fetching much faster --- projects/backend/src/service/score.service.ts | 64 +++++++++---------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/projects/backend/src/service/score.service.ts b/projects/backend/src/service/score.service.ts index d32cca4..2e4cf22 100644 --- a/projects/backend/src/service/score.service.ts +++ b/projects/backend/src/service/score.service.ts @@ -343,16 +343,6 @@ export class ScoreService { }); } - /** - * Gets scores for a player. - * - * @param leaderboardName the leaderboard to get the scores from - * @param playerId the players id - * @param page the page to get - * @param sort the sort to use - * @param search the search to use - * @returns the scores - */ public static async getPlayerScores( leaderboardName: Leaderboards, playerId: string, @@ -364,16 +354,16 @@ export class ScoreService { playerScoresCache, `player-scores-${leaderboardName}-${playerId}-${page}-${sort}-${search}`, async () => { - const scores: PlayerScore[] | undefined = []; + const scores: PlayerScore[] = []; let metadata: Metadata = new Metadata(0, 0, 0, 0); // Default values switch (leaderboardName) { case "scoresaber": { const leaderboardScores = await scoresaberService.lookupPlayerScores({ - playerId: playerId, - page: page, + playerId, + page, sort: sort as ScoreSort, - search: search, + search, }); if (leaderboardScores == undefined) { break; @@ -386,37 +376,43 @@ export class ScoreService { leaderboardScores.metadata.itemsPerPage ); - for (const token of leaderboardScores.playerScores) { + const scorePromises = leaderboardScores.playerScores.map(async token => { const leaderboard = getScoreSaberLeaderboardFromToken(token.leaderboard); - if (leaderboard == undefined) { - continue; - } + if (!leaderboard) return undefined; const score = getScoreSaberScoreFromToken(token.score, leaderboard, playerId); - if (score == undefined) { - continue; - } + if (!score) return undefined; - const additionalData = await this.getAdditionalScoreData( - playerId, - leaderboard.songHash, - `${leaderboard.difficulty.difficulty}-${leaderboard.difficulty.characteristic}`, - score.score - ); - if (additionalData !== undefined) { + // Fetch additional data, previous score, and BeatSaver map concurrently + const [additionalData, previousScore, beatSaverMap] = await Promise.all([ + this.getAdditionalScoreData( + playerId, + leaderboard.songHash, + `${leaderboard.difficulty.difficulty}-${leaderboard.difficulty.characteristic}`, + score.score + ), + this.getPreviousScore(playerId, leaderboard.id + "", score.timestamp), + BeatSaverService.getMap(leaderboard.songHash), + ]); + + if (additionalData) { score.additionalData = additionalData; } - const previousScore = await this.getPreviousScore(playerId, leaderboard.id + "", score.timestamp); - if (previousScore !== undefined) { + if (previousScore) { score.previousScore = previousScore; } - scores.push({ + return { score: score, leaderboard: leaderboard, - beatSaver: await BeatSaverService.getMap(leaderboard.songHash), - }); - } + beatSaver: beatSaverMap, + } as PlayerScore; + }); + + const resolvedScores = (await Promise.all(scorePromises)).filter( + (s): s is PlayerScore => s !== undefined + ); + scores.push(...resolvedScores); break; } default: {