From 7e2c4cf972e9992875e6e1ad382f7df156204175 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 23 Oct 2023 16:12:09 +0100 Subject: [PATCH] fix score fetching --- src/store/scoresaberScoresStore.ts | 40 +++++++++--------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/src/store/scoresaberScoresStore.ts b/src/store/scoresaberScoresStore.ts index 24bec0f..f83fd69 100644 --- a/src/store/scoresaberScoresStore.ts +++ b/src/store/scoresaberScoresStore.ts @@ -16,16 +16,8 @@ type Player = { }; interface ScoreSaberScoresStore { - lastUpdated: number; players: Player[]; - /** - * Sets when the player scores were last updated - * - * @param lastUpdated when the player scores were last updated - */ - setLastUpdated: (lastUpdated: number) => void; - /** * Checks if the player exists * @@ -78,10 +70,6 @@ export const useScoresaberScoresStore = create()( lastUpdated: 0, players: [], - setLastUpdated: (lastUpdated: number) => { - set({ lastUpdated }); - }, - exists: (playerId: string) => { const players: Player[] = get().players; return players.some((player) => player.id == playerId); @@ -238,10 +226,6 @@ export const useScoresaberScoresStore = create()( // add local player and friends if they don't exist for (const player of allPlayers) { - if (get().lastUpdated == 0) { - set({ lastUpdated: Date.now() }); - } - if (get().get(player.id) == undefined) { toast.info( `${ @@ -261,20 +245,20 @@ export const useScoresaberScoresStore = create()( } } - // Skip if we refreshed the scores recently - const timeUntilRefreshMs = - UPDATE_INTERVAL - (Date.now() - get().lastUpdated); - if (timeUntilRefreshMs > 0) { - console.log( - "Waiting", - timeUntilRefreshMs / 1000, - "seconds to refresh scores for players", - ); - return; - } - // loop through all of the players and update their scores for (const player of players) { + // Skip if we refreshed the scores recently + const timeUntilRefreshMs = + UPDATE_INTERVAL - (Date.now() - player.lastUpdated); + if (timeUntilRefreshMs > 0) { + console.log( + "Waiting", + timeUntilRefreshMs / 1000, + "seconds to refresh scores for " + player.id, + ); + continue; + } + get().addOrUpdatePlayer(player.id); } },