fix double fetching of players
Some checks failed
Deploy Backend / deploy (push) Has been cancelled

This commit is contained in:
Lee 2024-10-12 02:36:44 +01:00
parent 786bc69cf3
commit 6947c30c23
2 changed files with 9 additions and 4 deletions

@ -37,7 +37,7 @@ app.use(
timezone: "Europe/London",
run: async () => {
const pages = 10;
const cooldown = 60_000 / 200; // 200 requests per minute
const cooldown = 60_000 / 250; // 250 requests per minute
let toTrack: PlayerDocument[] = await PlayerModel.find({});
const toRemoveIds: string[] = [];
@ -54,7 +54,7 @@ app.use(
continue;
}
for (const player of page.players) {
const foundPlayer = await PlayerService.getPlayer(player.id, true);
const foundPlayer = await PlayerService.getPlayer(player.id, true, player);
await PlayerService.trackScoreSaberPlayer(foundPlayer, player);
toRemoveIds.push(foundPlayer.id);
}

@ -11,14 +11,19 @@ export class PlayerService {
*
* @param id the player to fetch
* @param create if true, create the player if it doesn't exist
* @param playerToken an optional player token for the player
* @returns the player
* @throws NotFoundError if the player is not found
*/
public static async getPlayer(id: string, create: boolean = false): Promise<PlayerDocument> {
public static async getPlayer(
id: string,
create: boolean = false,
playerToken?: ScoreSaberPlayerToken
): Promise<PlayerDocument> {
let player: PlayerDocument | null = await PlayerModel.findById(id);
if (player === null) {
// If create is on, create the player, otherwise return unknown player
const playerToken = create ? await scoresaberService.lookupPlayer(id) : undefined;
playerToken = create ? (playerToken ? playerToken : await scoresaberService.lookupPlayer(id)) : undefined;
if (playerToken === undefined) {
throw new NotFoundError(`Player "${id}" not found`);
}