make player score fetching much faster
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 41s

This commit is contained in:
Lee 2024-10-28 12:12:44 +00:00
parent ce65116db4
commit 0a5d42f6ac

@ -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( public static async getPlayerScores(
leaderboardName: Leaderboards, leaderboardName: Leaderboards,
playerId: string, playerId: string,
@ -364,16 +354,16 @@ export class ScoreService {
playerScoresCache, playerScoresCache,
`player-scores-${leaderboardName}-${playerId}-${page}-${sort}-${search}`, `player-scores-${leaderboardName}-${playerId}-${page}-${sort}-${search}`,
async () => { async () => {
const scores: PlayerScore<unknown, unknown>[] | undefined = []; const scores: PlayerScore<unknown, unknown>[] = [];
let metadata: Metadata = new Metadata(0, 0, 0, 0); // Default values let metadata: Metadata = new Metadata(0, 0, 0, 0); // Default values
switch (leaderboardName) { switch (leaderboardName) {
case "scoresaber": { case "scoresaber": {
const leaderboardScores = await scoresaberService.lookupPlayerScores({ const leaderboardScores = await scoresaberService.lookupPlayerScores({
playerId: playerId, playerId,
page: page, page,
sort: sort as ScoreSort, sort: sort as ScoreSort,
search: search, search,
}); });
if (leaderboardScores == undefined) { if (leaderboardScores == undefined) {
break; break;
@ -386,37 +376,43 @@ export class ScoreService {
leaderboardScores.metadata.itemsPerPage leaderboardScores.metadata.itemsPerPage
); );
for (const token of leaderboardScores.playerScores) { const scorePromises = leaderboardScores.playerScores.map(async token => {
const leaderboard = getScoreSaberLeaderboardFromToken(token.leaderboard); const leaderboard = getScoreSaberLeaderboardFromToken(token.leaderboard);
if (leaderboard == undefined) { if (!leaderboard) return undefined;
continue;
}
const score = getScoreSaberScoreFromToken(token.score, leaderboard, playerId); const score = getScoreSaberScoreFromToken(token.score, leaderboard, playerId);
if (score == undefined) { if (!score) return undefined;
continue;
}
const additionalData = await this.getAdditionalScoreData( // Fetch additional data, previous score, and BeatSaver map concurrently
playerId, const [additionalData, previousScore, beatSaverMap] = await Promise.all([
leaderboard.songHash, this.getAdditionalScoreData(
`${leaderboard.difficulty.difficulty}-${leaderboard.difficulty.characteristic}`, playerId,
score.score leaderboard.songHash,
); `${leaderboard.difficulty.difficulty}-${leaderboard.difficulty.characteristic}`,
if (additionalData !== undefined) { score.score
),
this.getPreviousScore(playerId, leaderboard.id + "", score.timestamp),
BeatSaverService.getMap(leaderboard.songHash),
]);
if (additionalData) {
score.additionalData = additionalData; score.additionalData = additionalData;
} }
const previousScore = await this.getPreviousScore(playerId, leaderboard.id + "", score.timestamp); if (previousScore) {
if (previousScore !== undefined) {
score.previousScore = previousScore; score.previousScore = previousScore;
} }
scores.push({ return {
score: score, score: score,
leaderboard: leaderboard, leaderboard: leaderboard,
beatSaver: await BeatSaverService.getMap(leaderboard.songHash), beatSaver: beatSaverMap,
}); } as PlayerScore<ScoreSaberScore, ScoreSaberLeaderboard>;
} });
const resolvedScores = (await Promise.all(scorePromises)).filter(
(s): s is PlayerScore<ScoreSaberScore, ScoreSaberLeaderboard> => s !== undefined
);
scores.push(...resolvedScores);
break; break;
} }
default: { default: {