Skip to content

Commit

Permalink
refactor(stats): races
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckySoLucky committed Nov 25, 2024
1 parent 7efefa9 commit 2efab66
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/lib/constants/NotEnoughUpdates-REPO
2 changes: 1 addition & 1 deletion src/lib/server/lib/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { getAccessories, getPets, getMainStats, getCollections } = stats;
export async function getStats(profile: Profile, player: Player): Promise<Stats> {
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);
}
Expand Down
30 changes: 15 additions & 15 deletions src/lib/stats/misc.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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<string, { name: string; time: number }>;
no_return: Record<string, { name: string; time: number }>;
}
>;
};
} = {};
const output: Misc["races"] = {};

const races = userProfile.player_stats.races;
for (let [id, data] of Object.entries(races)) {
Expand All @@ -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: {},
Expand All @@ -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;
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/types/processed/profile/misc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, { name: string; time: number }>;
no_return: Record<string, { name: string; time: number }>;
with_return: Record<string, { name: string; time: number }> | null;
no_return: Record<string, { name: string; time: number }> | null;
}
| null
>;
};
};
Expand Down

0 comments on commit 2efab66

Please sign in to comment.