fix time formatting
All checks were successful
deploy / deploy (push) Successful in 57s

This commit is contained in:
Lee 2023-10-23 16:27:44 +01:00
parent b88c33e91c
commit cc1b6d9a63
3 changed files with 33 additions and 6 deletions

@ -3,6 +3,7 @@
import { ScoresaberPlayer } from "@/schemas/scoresaber/player";
import { ScoresaberSmallerPlayerScore } from "@/schemas/scoresaber/smaller/smallerPlayerScore";
import { ScoreSaberAPI } from "@/utils/scoresaber/api";
import { formatMsToTime } from "@/utils/timeUtils";
import { toast } from "react-toastify";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
@ -244,15 +245,16 @@ export const useScoresaberScoresStore = create<ScoreSaberScoresStore>()(
if (player.lastUpdated == undefined) {
player.lastUpdated = Date.now();
}
const playerData = useSettingsStore.getState().getProfile(player.id);
// 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,
`Waiting ${formatMsToTime(
timeUntilRefreshMs,
)} to refresh player: ${playerData?.name}`,
);
continue;
}

@ -3,6 +3,7 @@
import { ScoresaberPlayer } from "@/schemas/scoresaber/player";
import { SortType, SortTypes } from "@/types/SortTypes";
import { ScoreSaberAPI } from "@/utils/scoresaber/api";
import { formatMsToTime } from "@/utils/timeUtils";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import { IDBStorage } from "./IndexedDBStorage";
@ -22,6 +23,7 @@ interface SettingsStore {
setLastUsedSortType: (sortType: SortType) => void;
setProfilesLastUpdated: (profilesLastUpdated: number) => void;
refreshProfiles: () => Promise<void>;
getProfile(playerId: string): ScoresaberPlayer | undefined;
}
const UPDATE_INTERVAL = 1000 * 60 * 10; // 10 minutes
@ -82,9 +84,9 @@ export const useSettingsStore = create<SettingsStore>()(
UPDATE_INTERVAL - (Date.now() - get().profilesLastUpdated);
if (timeUntilRefreshMs > 0) {
console.log(
"Waiting",
timeUntilRefreshMs / 1000,
"to refresh profiles",
`Waiting ${formatMsToTime(
timeUntilRefreshMs,
)} to refresh player profiles`,
);
return;
}
@ -109,6 +111,11 @@ export const useSettingsStore = create<SettingsStore>()(
);
set({ profilesLastUpdated: Date.now(), friends: newFriends });
},
getProfile(playerId: string) {
const allProfiles = [get().player, ...get().friends];
return allProfiles.find((profile) => profile?.id == playerId);
},
}),
{
name: "settings",

@ -41,3 +41,21 @@ export function formatDate(timestamp: string) {
minute: "numeric",
});
}
/**
* Formats a time in milliseconds to a human readable format
*
* @param ms the time in milliseconds
* @returns the formatted time
*/
export function formatMsToTime(ms: number) {
const seconds = Math.floor((ms / 1000) % 60);
const minutes = Math.floor((ms / (1000 * 60)) % 60);
const hours = Math.floor((ms / (1000 * 60 * 60)) % 24);
const hoursStr = hours > 0 ? hours.toString() + ":" : "";
const minutesStr = minutes.toString().padStart(2, "0") + ":";
const secondsStr = seconds.toString().padStart(2, "0");
return hoursStr + minutesStr + secondsStr;
}