cache unknown beatsaver maps
All checks were successful
Deploy Backend / deploy (push) Successful in 2m53s
Deploy Website / deploy (push) Successful in 4m46s

This commit is contained in:
Lee 2024-10-17 16:24:10 +01:00
parent 73b7d17597
commit c72230a98d
4 changed files with 57 additions and 43 deletions

@ -11,20 +11,28 @@ export default class BeatSaverService {
public static async getMap(hash: string): Promise<BeatSaverMap | undefined> { public static async getMap(hash: string): Promise<BeatSaverMap | undefined> {
let map = await BeatSaverMapModel.findById(hash); let map = await BeatSaverMapModel.findById(hash);
if (map != undefined) { if (map != undefined) {
return map.toObject() as BeatSaverMap; const toObject = map.toObject() as BeatSaverMap;
if (toObject.unknownMap) {
return undefined;
}
return toObject;
} }
const token = await beatsaverService.lookupMap(hash); const token = await beatsaverService.lookupMap(hash);
if (token == undefined) { map = await BeatSaverMapModel.create(
return undefined; token
} ? {
map = await BeatSaverMapModel.create({ _id: hash,
_id: hash, bsr: token.id,
bsr: token.id, author: {
author: { id: token.uploader.id,
id: token.uploader.id, },
}, }
}); : {
_id: hash,
unknownMap: true,
}
);
return map.toObject() as BeatSaverMap; return map.toObject() as BeatSaverMap;
} }
} }

@ -91,11 +91,16 @@ export class ScoreService {
search: search, search: search,
}); });
if (leaderboardScores == undefined) { if (leaderboardScores == undefined) {
throw new NotFoundError( break;
`No scores found for "${id}", leaderboard "${leaderboardName}", page "${page}", sort "${sort}", search "${search}"`
);
} }
metadata = new Metadata(
Math.ceil(leaderboardScores.metadata.total / leaderboardScores.metadata.itemsPerPage),
leaderboardScores.metadata.total,
leaderboardScores.metadata.page,
leaderboardScores.metadata.itemsPerPage
);
for (const token of leaderboardScores.playerScores) { for (const token of leaderboardScores.playerScores) {
const score = getScoreSaberScoreFromToken(token.score); const score = getScoreSaberScoreFromToken(token.score);
if (score == undefined) { if (score == undefined) {
@ -113,13 +118,6 @@ export class ScoreService {
beatSaver: beatSaverMap, beatSaver: beatSaverMap,
}); });
} }
metadata = new Metadata(
Math.ceil(leaderboardScores.metadata.total / leaderboardScores.metadata.itemsPerPage),
leaderboardScores.metadata.total,
leaderboardScores.metadata.page,
leaderboardScores.metadata.itemsPerPage
);
break; break;
} }
default: { default: {
@ -145,7 +143,7 @@ export class ScoreService {
leaderboardName: Leaderboards, leaderboardName: Leaderboards,
id: string, id: string,
page: number page: number
): Promise<LeaderboardScoresResponse<unknown>> { ): Promise<LeaderboardScoresResponse<unknown, unknown>> {
const scores: Score[] = []; const scores: Score[] = [];
let leaderboard: Leaderboard | undefined; let leaderboard: Leaderboard | undefined;
let beatSaverMap: BeatSaverMap | undefined; let beatSaverMap: BeatSaverMap | undefined;
@ -153,11 +151,6 @@ export class ScoreService {
switch (leaderboardName) { switch (leaderboardName) {
case "scoresaber": { case "scoresaber": {
const leaderboardScores = await scoresaberService.lookupLeaderboardScores(id, page);
if (leaderboardScores == undefined) {
throw new NotFoundError(`No scores found for "${id}", leaderboard "${leaderboardName}", page "${page}""`);
}
const leaderboardResponse = await LeaderboardService.getLeaderboard(leaderboardName, id); const leaderboardResponse = await LeaderboardService.getLeaderboard(leaderboardName, id);
if (leaderboardResponse == undefined) { if (leaderboardResponse == undefined) {
throw new NotFoundError(`Leaderboard "${leaderboardName}" not found`); throw new NotFoundError(`Leaderboard "${leaderboardName}" not found`);
@ -165,6 +158,11 @@ export class ScoreService {
leaderboard = leaderboardResponse.leaderboard; leaderboard = leaderboardResponse.leaderboard;
beatSaverMap = leaderboardResponse.beatsaver; beatSaverMap = leaderboardResponse.beatsaver;
const leaderboardScores = await scoresaberService.lookupLeaderboardScores(id, page);
if (leaderboardScores == undefined) {
break;
}
for (const token of leaderboardScores.scores) { for (const token of leaderboardScores.scores) {
const score = getScoreSaberScoreFromToken(token); const score = getScoreSaberScoreFromToken(token);
if (score == undefined) { if (score == undefined) {

@ -30,15 +30,21 @@ export class BeatSaverMap {
* The bsr code for the map. * The bsr code for the map.
* @private * @private
*/ */
@prop({ required: true }) @prop({ required: false })
public bsr!: string; public bsr!: string;
/** /**
* The author of the map. * The author of the map.
*/ */
@prop({ required: true, _id: false, type: () => BeatsaverAuthor }) @prop({ required: false, _id: false, type: () => BeatsaverAuthor })
public author!: BeatsaverAuthor; public author!: BeatsaverAuthor;
/**
* True if the map is unknown on beatsaver.
*/
@prop({ required: false })
public unknownMap?: boolean;
/** /**
* Exposes `id` as a virtual field mapped from `_id`. * Exposes `id` as a virtual field mapped from `_id`.
*/ */

@ -229,20 +229,22 @@ export default function PlayerScores({ initialScoreData, initialSearch, player,
))} ))}
</motion.div> </motion.div>
<Pagination {scores.metadata.totalPages > 1 && (
mobilePagination={width < 768} <Pagination
page={pageState.page} mobilePagination={width < 768}
totalPages={scores.metadata.totalPages} page={pageState.page}
loadingPage={isLoading ? pageState.page : undefined} totalPages={scores.metadata.totalPages}
generatePageUrl={page => { loadingPage={isLoading ? pageState.page : undefined}
return getUrl(page); generatePageUrl={page => {
}} return getUrl(page);
onPageChange={newPage => { }}
setPreviousPage(pageState.page); onPageChange={newPage => {
setPageState({ ...pageState, page: newPage }); setPreviousPage(pageState.page);
setShouldFetch(true); // Set to true to trigger fetch on page change setPageState({ ...pageState, page: newPage });
}} setShouldFetch(true); // Set to true to trigger fetch on page change
/> }}
/>
)}
</> </>
)} )}
</Card> </Card>