Skip to content

Commit

Permalink
refactor(/item/): small perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckySoLucky committed Jan 16, 2025
1 parent ef44863 commit 2ab8bd4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/lib/server/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export async function applyResourcePack(item: ProcessedItem, packs: string[]) {
return item;
}

// ? NOTE: we're ignoring enchanted books because they're quite expensive to render and not really useful the performance hit
// ? NOTE: we're ignoring enchanted books because they're quite expensive to render and not really worth the performance hit
if (item.tag?.ExtraAttributes?.id === "ENCHANTED_BOOK") {
const enchantedBookItem = getItemData({ id: 403 }) as ProcessedItem;
const texture = getTexture(enchantedBookItem);
Expand Down
32 changes: 6 additions & 26 deletions src/lib/server/helper/item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { DatabaseItem, Item, ItemQuery } from "$types/stats";
import sanitize from "mongo-sanitize";
import type { Item, ItemQuery } from "$types/stats";
import * as constants from "../constants/constants";

/**
Expand All @@ -24,31 +23,8 @@ function rgbToHex(rgb: string) {
*/
export function getItemData(query: ItemQuery) {
query = Object.assign({ skyblockId: undefined, id: undefined, name: undefined, damage: undefined }, query);
const dbItem = query.skyblockId ? (constants.ITEMS.get(query.skyblockId) ?? {}) : {};
const item: Item = { id: -1, Damage: 0, Count: 1, tag: { ExtraAttributes: {} } };
let dbItem: DatabaseItem = {};

if (query.skyblockId) {
query.skyblockId = sanitize(query.skyblockId);

if (query.skyblockId !== undefined && query.skyblockId.includes(":")) {
const split = query.skyblockId.split(":");

query.skyblockId = split[0];
query.damage = Number(split[1]);
}

dbItem = { ...(item as unknown as DatabaseItem), ...constants.ITEMS.get(query.skyblockId) };
}

if (query && query.name !== undefined) {
const results = Object.values(constants.ITEMS) as DatabaseItem[];

const filteredResults = results.filter((a) => a.name && a.name.toLowerCase() == (query.name ?? "").toLowerCase());

if (filteredResults.length > 0) {
dbItem = filteredResults[0] ?? {};
}
}

if (query.id !== undefined) {
item.id = query.id;
Expand Down Expand Up @@ -78,6 +54,10 @@ export function getItemData(query: ItemQuery) {
item.texture = dbItem.texture as string;
}

if ("glowing" in dbItem || "shiny" in dbItem) {
item.glowing = true;
}

if (dbItem.item_id && dbItem.item_id >= 298 && dbItem.item_id <= 301) {
const type = ["helmet", "chestplate", "leggings", "boots"][dbItem.item_id - 298];
if (dbItem.color !== undefined) {
Expand Down
7 changes: 3 additions & 4 deletions src/lib/server/stats/items/processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function getCategories(type: string, item: Item) {
// Process items returned by API
export async function processItems(items: ProcessedItem[], source: string, packs: string[]): Promise<ProcessedItem[]> {
for (const item of items) {
if (!item.tag?.ExtraAttributes?.id) {
// continue;
if (!item.tag?.ExtraAttributes?.id && item.exp === undefined) {
continue;
}

// POTIONS
Expand Down Expand Up @@ -116,7 +116,6 @@ export async function processItems(items: ProcessedItem[], source: string, packs
}
}

// Set HTML lore to be displayed on the website
if (itemLore.length > 0 && item.tag.ExtraAttributes) {
if (item.tag.ExtraAttributes.rarity_upgrades) {
itemLore.push("§8(Recombobulated)");
Expand Down Expand Up @@ -147,7 +146,7 @@ export async function processItems(items: ProcessedItem[], source: string, packs
}
}

if (item?.tag || item?.exp) {
if (item.tag || item.exp !== undefined) {
if (source.startsWith("storage_icons") === false) {
try {
const ITEM_PRICE = await getItemNetworth(item, { cache: true });
Expand Down

0 comments on commit 2ab8bd4

Please sign in to comment.