From a3bc807fbf253ba7b97d8b47071b8d15612359dd Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 22 Oct 2023 09:12:19 +0100 Subject: [PATCH] fixed scores db not filling if it was deleted --- src/store/playerScoresStore.ts | 37 +++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/store/playerScoresStore.ts b/src/store/playerScoresStore.ts index 61e84a7..f9930ad 100644 --- a/src/store/playerScoresStore.ts +++ b/src/store/playerScoresStore.ts @@ -99,7 +99,7 @@ export const usePlayerScoresStore = create()( } // Get all of the players scores - const scores = await fetchAllScores( + let scores = await fetchAllScores( playerId, "recent", (page, totalPages) => { @@ -113,6 +113,18 @@ export const usePlayerScoresStore = create()( message: "Could not fetch scores for player", }; } + + // Remove scores that are already in the database + const player = usePlayerScoresStore.getState().get(playerId); + if (player) { + scores = scores.filter( + (score) => + player.scores.scoresaber.findIndex( + (s) => s.score.id == score.score.id, + ) == -1, + ); + } + set({ players: [ ...players, @@ -149,6 +161,15 @@ export const usePlayerScoresStore = create()( } const players = usePlayerScoresStore.getState().players; + if (players.length == 0) { + const userId = useSettingsStore.getState().userId; + if (userId) { + console.log( + `Your profile was not found in the scores database, adding it...`, + ); + await usePlayerScoresStore.getState().addPlayer(userId); + } + } const friends = useSettingsStore.getState().friends; for (const friend of friends) { players.push({ @@ -159,6 +180,15 @@ export const usePlayerScoresStore = create()( }); } + if (players.length == 0) { + for (const friend of friends) { + console.log( + `Friend ${friend.name} was not found in the scores database, adding them...`, + ); + await usePlayerScoresStore.getState().addPlayer(friend.id); + } + } + for (const player of players) { if (player == undefined) continue; console.log(`Updating scores for ${player.id}...`); @@ -172,10 +202,7 @@ export const usePlayerScoresStore = create()( return bDate.getTime() - aDate.getTime(); }); - if (!oldScores || oldScores.length == 0) continue; - const mostRecentScore = oldScores[0].score; - if (mostRecentScore == undefined) continue; let search = true; let page = 0; @@ -186,7 +213,7 @@ export const usePlayerScoresStore = create()( if (newScores == undefined) continue; for (const newScore of newScores.scores) { - if (newScore.score.id == mostRecentScore.id) { + if (mostRecentScore && newScore.score.id == mostRecentScore.id) { search = false; break; }