From 181eb564719ffa3ef193673c70e52c9c18db4a22 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Sat, 8 Apr 2023 00:18:06 -0400 Subject: [PATCH] Sort item data --- src/handbook/src/backend/data.ts | 39 ++++++++++++++++++++++++++++++- src/handbook/src/backend/types.ts | 9 +++++++ src/handbook/src/main.tsx | 4 +++- src/handbook/src/utils.ts | 11 +++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/handbook/src/backend/data.ts b/src/handbook/src/backend/data.ts index 2589e5d3b..de48d6d2b 100644 --- a/src/handbook/src/backend/data.ts +++ b/src/handbook/src/backend/data.ts @@ -2,11 +2,48 @@ import commands from "@data/commands.json"; import avatars from "@data/avatars.csv"; import items from "@data/items.csv"; -import { Quality, ItemType } from "@backend/types"; +import { Quality, ItemType, ItemCategory } from "@backend/types"; import type { Command, Avatar, Item } from "@backend/types"; +import { inRange } from "@app/utils"; + type AvatarDump = { [key: number]: Avatar }; type CommandDump = { [key: string]: Command }; +type TaggedItems = { [key: number]: Item[] } + +/* + * Notes on artifacts: + * TODO: Figure out what suffix is for which artifact type. + */ + +const sortedItems: TaggedItems = { + [ItemCategory.Constellation]: [], // Range: 1102 - 11xx + [ItemCategory.Weapon]: [], + [ItemCategory.Artifact]: [], + [ItemCategory.Furniture]: [], + [ItemCategory.Material]: [], + [ItemCategory.Miscellaneous]: [] +}; + +/** + * Setup function for this file. + * Sorts all items into their respective categories. + */ +export function setup(): void { + getItems().forEach(item => { + switch (item.type) { + case ItemType.Weapon: sortedItems[ItemCategory.Weapon].push(item); break; + case ItemType.Material: sortedItems[ItemCategory.Material].push(item); break; + case ItemType.Furniture: sortedItems[ItemCategory.Furniture].push(item); break; + case ItemType.Reliquary: sortedItems[ItemCategory.Artifact].push(item); break; + } + + // Sort constellations. + if (inRange(item.id, 1102, 1199)) { + sortedItems[ItemCategory.Constellation].push(item); + } + }); +} /** * Fetches and casts all commands in the file. diff --git a/src/handbook/src/backend/types.ts b/src/handbook/src/backend/types.ts index d66f9f53a..71251a9b6 100644 --- a/src/handbook/src/backend/types.ts +++ b/src/handbook/src/backend/types.ts @@ -47,6 +47,15 @@ export enum ItemType { Furniture = "ITEM_FURNITURE" } +export enum ItemCategory { + Constellation, + Weapon, + Artifact, + Furniture, + Material, + Miscellaneous +} + /** * Checks if a string is a page. * diff --git a/src/handbook/src/main.tsx b/src/handbook/src/main.tsx index 16159207d..f80fd024a 100644 --- a/src/handbook/src/main.tsx +++ b/src/handbook/src/main.tsx @@ -1,11 +1,13 @@ import React from "react"; import { createRoot } from "react-dom/client"; +import * as data from "@backend/data"; import * as events from "@backend/events"; -import App from "@components/App"; +import App from "@ui/App"; // Call initial setup functions. +data.setup(); events.setup(); // Render the application. diff --git a/src/handbook/src/utils.ts b/src/handbook/src/utils.ts index 089e2edf2..d68e9243a 100644 --- a/src/handbook/src/utils.ts +++ b/src/handbook/src/utils.ts @@ -21,3 +21,14 @@ export function colorFor(quality: Quality): string { return "--unknown-color"; } } + +/** + * Checks if a value is between two numbers. + * + * @param value The value to check. + * @param min The minimum value. + * @param max The maximum value. + */ +export function inRange(value: number, min: number, max: number): boolean { + return value >= min && value <= max; +}