fixed scores db not filling if it was deleted
All checks were successful
deploy / deploy (push) Successful in 59s

This commit is contained in:
Lee 2023-10-22 09:12:19 +01:00
parent b475ae1d66
commit a3bc807fbf

@ -99,7 +99,7 @@ export const usePlayerScoresStore = create<PlayerScoresStore>()(
} }
// Get all of the players scores // Get all of the players scores
const scores = await fetchAllScores( let scores = await fetchAllScores(
playerId, playerId,
"recent", "recent",
(page, totalPages) => { (page, totalPages) => {
@ -113,6 +113,18 @@ export const usePlayerScoresStore = create<PlayerScoresStore>()(
message: "Could not fetch scores for player", 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({ set({
players: [ players: [
...players, ...players,
@ -149,6 +161,15 @@ export const usePlayerScoresStore = create<PlayerScoresStore>()(
} }
const players = usePlayerScoresStore.getState().players; 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; const friends = useSettingsStore.getState().friends;
for (const friend of friends) { for (const friend of friends) {
players.push({ players.push({
@ -159,6 +180,15 @@ export const usePlayerScoresStore = create<PlayerScoresStore>()(
}); });
} }
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) { for (const player of players) {
if (player == undefined) continue; if (player == undefined) continue;
console.log(`Updating scores for ${player.id}...`); console.log(`Updating scores for ${player.id}...`);
@ -172,10 +202,7 @@ export const usePlayerScoresStore = create<PlayerScoresStore>()(
return bDate.getTime() - aDate.getTime(); return bDate.getTime() - aDate.getTime();
}); });
if (!oldScores || oldScores.length == 0) continue;
const mostRecentScore = oldScores[0].score; const mostRecentScore = oldScores[0].score;
if (mostRecentScore == undefined) continue;
let search = true; let search = true;
let page = 0; let page = 0;
@ -186,7 +213,7 @@ export const usePlayerScoresStore = create<PlayerScoresStore>()(
if (newScores == undefined) continue; if (newScores == undefined) continue;
for (const newScore of newScores.scores) { for (const newScore of newScores.scores) {
if (newScore.score.id == mostRecentScore.id) { if (mostRecentScore && newScore.score.id == mostRecentScore.id) {
search = false; search = false;
break; break;
} }