From a8139676b8a10f2344d11dd584749fcf91bb3044 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 22 Oct 2023 08:48:32 +0100 Subject: [PATCH] updated store --- src/store/playerScoresStore.ts | 31 ++++++++++++++++++++++++++----- src/utils/scoresaber/scores.ts | 6 +++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/store/playerScoresStore.ts b/src/store/playerScoresStore.ts index 50774b3..7d3eefb 100644 --- a/src/store/playerScoresStore.ts +++ b/src/store/playerScoresStore.ts @@ -9,7 +9,9 @@ import { useSettingsStore } from "./settingsStore"; type Player = { id: string; - scores: ScoresaberPlayerScore[]; + scores: { + scoresaber: ScoresaberPlayerScore[]; + }; }; interface PlayerScoresStore { @@ -116,7 +118,9 @@ export const usePlayerScoresStore = create()( ...players, { id: playerId, - scores: scores, + scores: { + scoresaber: scores, + }, }, ], }); @@ -149,7 +153,9 @@ export const usePlayerScoresStore = create()( for (const friend of friends) { players.push({ id: friend.id, - scores: [], + scores: { + scoresaber: [], + }, }); } @@ -157,7 +163,7 @@ export const usePlayerScoresStore = create()( if (player == undefined) continue; console.log(`Updating scores for ${player.id}...`); - let oldScores = player.scores; + let oldScores = player.scores.scoresaber; // Sort the scores by date (newset to oldest), so we know when to stop searching for new scores oldScores = oldScores.sort((a, b) => { @@ -203,7 +209,9 @@ export const usePlayerScoresStore = create()( // Add the player newPlayers.push({ id: player.id, - scores: oldScores, + scores: { + scoresaber: oldScores, + }, }); if (newScoresCount > 0) { @@ -220,6 +228,19 @@ export const usePlayerScoresStore = create()( { name: "playerScores", storage: createJSONStorage(() => localStorage), + version: 1, + + migrate: (state: any, version: number) => { + if (version == 1) { + console.log("Migrating player scores..."); + const players = state.players; + for (const player of players) { + player.scores = player.scores.scoresaber; + } + + return state; + } + }, }, ), ); diff --git a/src/utils/scoresaber/scores.ts b/src/utils/scoresaber/scores.ts index db2cf39..4919ae5 100644 --- a/src/utils/scoresaber/scores.ts +++ b/src/utils/scoresaber/scores.ts @@ -118,7 +118,7 @@ export function calcPpBoundary(playerId: string, expectedPp = 1) { const rankedScores = usePlayerScoresStore .getState() .players.find((p) => p.id === playerId) - ?.scores?.filter((s) => s.score.pp !== undefined); + ?.scores?.scoresaber.filter((s) => s.score.pp !== undefined); if (!rankedScores) return null; const rankedScorePps = rankedScores @@ -159,7 +159,7 @@ export function getHighestPpPlay(playerId: string) { const rankedScores = usePlayerScoresStore .getState() .players.find((p) => p.id === playerId) - ?.scores?.filter((s) => s.score.pp !== undefined); + ?.scores?.scoresaber.filter((s) => s.score.pp !== undefined); if (!rankedScores) return null; const rankedScorePps = rankedScores @@ -179,7 +179,7 @@ export function getAveragePp(playerId: string, limit: number = 20) { const rankedScores = usePlayerScoresStore .getState() .players.find((p) => p.id === playerId) - ?.scores?.filter((s) => s.score.pp !== undefined); + ?.scores?.scoresaber.filter((s) => s.score.pp !== undefined); if (!rankedScores) return null; const rankedScorePps = rankedScores