From 2efab668b68e8bd18add44ef87705d8a228c06ac Mon Sep 17 00:00:00 2001 From: DuckySoLucky Date: Mon, 25 Nov 2024 21:16:19 +0100 Subject: [PATCH] refactor(stats): races --- src/lib/constants/NotEnoughUpdates-REPO | 2 +- src/lib/server/lib/stats.ts | 2 +- src/lib/stats/misc.ts | 30 ++++++++++++------------- src/types/processed/profile/misc.d.ts | 7 +++--- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/lib/constants/NotEnoughUpdates-REPO b/src/lib/constants/NotEnoughUpdates-REPO index f4aa51bf..5ad539f7 160000 --- a/src/lib/constants/NotEnoughUpdates-REPO +++ b/src/lib/constants/NotEnoughUpdates-REPO @@ -1 +1 @@ -Subproject commit f4aa51bfce15f3fbc4c58b07db979714233be43d +Subproject commit 5ad539f7d6b791e9fa2695a32b446d349829c760 diff --git a/src/lib/server/lib/stats.ts b/src/lib/server/lib/stats.ts index 2bf6ad7d..9f7eb014 100644 --- a/src/lib/server/lib/stats.ts +++ b/src/lib/server/lib/stats.ts @@ -9,7 +9,7 @@ const { getAccessories, getPets, getMainStats, getCollections } = stats; export async function getStats(profile: Profile, player: Player): Promise { const timeNow = Date.now(); const cache = await REDIS.get(`STATS:${profile.uuid}`); - if (cache) { + if (cache && process.env.NODE_ENV !== "development") { console.log(`[CACHE] Found cache for ${profile.uuid} in ${Date.now() - timeNow}ms`); return JSON.parse(cache); } diff --git a/src/lib/stats/misc.ts b/src/lib/stats/misc.ts index 27fe9827..cc329108 100644 --- a/src/lib/stats/misc.ts +++ b/src/lib/stats/misc.ts @@ -1,6 +1,6 @@ import * as constants from "$constants/constants"; import * as helper from "$lib/helper"; -import type { Member, Profile } from "$types/global"; +import type { Member, Misc, Profile } from "$types/global"; import type { Player } from "$types/raw/player/lib"; function getEssence(userProfile: Member) { @@ -53,19 +53,7 @@ function formatKillsAndDeaths(userProfile: Member) { } function getRaces(userProfile: Member) { - const output: { - [id: string]: { - name: string; - races: Record< - string, - | { name: string; time: number } - | { - with_return: Record; - no_return: Record; - } - >; - }; - } = {}; + const output: Misc["races"] = {}; const races = userProfile.player_stats.races; for (let [id, data] of Object.entries(races)) { @@ -82,13 +70,17 @@ function getRaces(userProfile: Member) { name: raceName, time: data }; + + if (Object.keys(output.other.races).length === 0) { + output.other.races[raceId] = null; + } } else { for ([id, data] of Object.entries(data)) { const shortId = id.split("_").slice(0, 2).join("_"); const raceId = id.replace(`${shortId}_`, "").replace("_best_time", ""); const raceName = constants.RACE_NAMES[shortId] ?? helper.titleCase(shortId.replace("_", " ")); - output[shortId] = output[shortId] || { + output[shortId] = output[shortId] ?? { name: raceName, races: { with_return: {}, @@ -105,6 +97,14 @@ function getRaces(userProfile: Member) { name: constants.RACE_NAMES[dungeonRaceId] ?? dungeonRaceId.split("_").map(helper.titleCase).join(" "), time: data }; + + if (output[shortId].races.with_return && Object.keys(output[shortId].races.with_return).length === 0) { + output[shortId].races.with_return = null; + } + + if (output[shortId].races.no_return && Object.keys(output[shortId].races.no_return).length === 0) { + output[shortId].races.no_return = null; + } } } } diff --git a/src/types/processed/profile/misc.d.ts b/src/types/processed/profile/misc.d.ts index 3481e65e..f3897d63 100644 --- a/src/types/processed/profile/misc.d.ts +++ b/src/types/processed/profile/misc.d.ts @@ -12,15 +12,16 @@ export type Misc = { deaths: { id: string; name: string; amount: number }[]; }; races: { - [string: string]: { + [id: string]: { name: string; races: Record< string, | { name: string; time: number } | { - with_return: Record; - no_return: Record; + with_return: Record | null; + no_return: Record | null; } + | null >; }; };