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 items from "@data/items.csv";
import { Quality, ItemType } from "@backend/types";
import type { Command, Avatar, Item } from "@backend/types";
type AvatarDump = { [key: number]: Avatar };
/**
* 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.
*/
export function getAvatars(): { [key: number]: Avatar } {
return avatars as { [key: number] : Avatar };
export function getAvatars(): AvatarDump {
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[] {
return items.map(item => {
const values = Object.values(item) as
[string, string, string, string];
const id = parseInt(values[0]);
return {
id: item[0],
name: item[1],
quality: item[2],
type: item[3]
};
id,
name: values[1],
type: values[2] as ItemType,
quality: values[3] as Quality
}
});
}

View File

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