From 85495bfc18952914faa5ce5918eb752f013d23e5 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 22 Oct 2023 10:00:03 +0100 Subject: [PATCH] made the missing from score db check not awful --- src/store/playerScoresStore.ts | 72 ++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/src/store/playerScoresStore.ts b/src/store/playerScoresStore.ts index b9ddae7..a18d4d0 100644 --- a/src/store/playerScoresStore.ts +++ b/src/store/playerScoresStore.ts @@ -144,6 +144,43 @@ export const usePlayerScoresStore = create()( }, updatePlayerScores: async () => { + const players = usePlayerScoresStore.getState().players; + const friends = useSettingsStore.getState().friends; + + const localPlayer = { + id: useSettingsStore.getState().userId, + scores: { + scoresaber: [], + }, + }; + let allPlayers: any = friends; + if (players.findIndex((player) => player.id == localPlayer.id) == -1) { + allPlayers.push(localPlayer); + } + + // add local player and friends if they don't exist + for (const player of allPlayers) { + if (usePlayerScoresStore.getState().get(player.id) == undefined) { + toast.success( + `${ + player.id == localPlayer.id + ? `You were` + : `Friend ${player.name} was` + } missing from the scores database, adding...`, + ); + console.log( + await usePlayerScoresStore.getState().addPlayer(player.id), + ); + toast.success( + `${ + player.id == useSettingsStore.getState().userId + ? `You were` + : `Friend ${player.name} was` + } added to the scores database`, + ); + } + } + // Skip if we refreshed the scores recently const timeUntilRefreshMs = UPDATE_INTERVAL - @@ -161,39 +198,6 @@ export const usePlayerScoresStore = create()( return; } - const players = usePlayerScoresStore.getState().players; - const friends = useSettingsStore.getState().friends; - if (players.length == 0) { - for (const friend of friends) { - if (usePlayerScoresStore.getState().exists(friend.id)) continue; - players.push({ - id: friend.id, - scores: { - scoresaber: [], - }, - }); - } - toast.info( - "Scores database was empty, adding your profile and friends to it", - ); - - 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); - } - for (const friend of friends) { - if (usePlayerScoresStore.getState().exists(friend.id)) continue; - - 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}...`); @@ -206,9 +210,9 @@ export const usePlayerScoresStore = create()( const bDate = new Date(b.score.timeSet); return bDate.getTime() - aDate.getTime(); }); + if (!oldScores.length) continue; const mostRecentScore = oldScores[0].score; - if (mostRecentScore == undefined) continue; let search = true; let page = 0;