Fix data parsing with CSVs

This commit is contained in:
KingRainbow44 2023-04-06 18:55:10 -04:00
parent 5fe304d2e8
commit 44b90612f2
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
2 changed files with 31 additions and 14 deletions

View File

@ -2,8 +2,11 @@ import commands from "@data/commands.json";
import avatars from "@data/avatars.csv"; import avatars from "@data/avatars.csv";
import items from "@data/items.csv"; import items from "@data/items.csv";
import { Quality, ItemType } from "@backend/types";
import type { Command, Avatar, Item } from "@backend/types"; import type { Command, Avatar, Item } from "@backend/types";
type AvatarDump = { [key: number]: Avatar };
/** /**
* Fetches and casts all commands in the file. * Fetches and casts all commands in the file.
*/ */
@ -14,8 +17,19 @@ export function getCommands(): { [key: string]: Command } {
/** /**
* Fetches and casts all avatars in the file. * Fetches and casts all avatars in the file.
*/ */
export function getAvatars(): { [key: number]: Avatar } { export function getAvatars(): AvatarDump {
return avatars as { [key: number] : Avatar }; const map: AvatarDump = {}; avatars.forEach(avatar => {
const values = Object.values(avatar) as
[string, string, string];
const id = parseInt(values[0]);
map[id] = {
id,
name: values[1],
quality: values[2] as Quality
};
});
return map;
} }
/** /**
@ -23,11 +37,14 @@ export function getAvatars(): { [key: number]: Avatar } {
*/ */
export function getItems(): Item[] { export function getItems(): Item[] {
return items.map(item => { return items.map(item => {
const values = Object.values(item) as
[string, string, string, string];
const id = parseInt(values[0]);
return { return {
id: item[0], id,
name: item[1], name: values[1],
quality: item[2], type: values[2] as ItemType,
type: item[3] quality: values[3] as Quality
}; }
}); });
} }

View File

@ -38,13 +38,13 @@ export enum Quality {
} }
export enum ItemType { export enum ItemType {
None = 0, None = "ITEM_NONE",
Virtual = 1, Virtual = "ITEM_VIRTUAL",
Material = 2, Material = "ITEM_MATERIAL",
Reliquary = 3, Reliquary = "ITEM_RELIQUARY",
Weapon = 4, Weapon = "ITEM_WEAPON",
Display = 5, Display = "ITEM_DISPLAY",
Furniture = 6 Furniture = "ITEM_FURNITURE"
} }
/** /**