forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcraftingRecipeData.ts
More file actions
39 lines (32 loc) · 1.46 KB
/
Copy pathcraftingRecipeData.ts
File metadata and controls
39 lines (32 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import fs from "node:fs";
import path from "node:path";
import type { CraftingRecipe } from "./craftingRecipes";
const DEFAULT_CRAFTING_RECIPES_JSON_PATH = path.resolve(__dirname, "../jsons/craftingRecipes.json");
function normalizeCraftingRecipe(recipe: CraftingRecipe): CraftingRecipe {
return {
id: Number(recipe.id ?? 0),
profession: recipe.profession,
category: String(recipe.category ?? ""),
sortOrder: Number(recipe.sortOrder ?? recipe.id ?? 0),
deleted: Boolean(recipe.deleted ?? false),
itemId: Number(recipe.itemId ?? 0),
skill: Number(recipe.skill ?? 0),
materials: Array.isArray(recipe.materials)
? recipe.materials.map((material) => ({
itemId: Number(material.itemId ?? 0),
amount: Number(material.amount ?? 0),
}))
: [],
};
}
export function normalizeCraftingRecipesData(recipes: CraftingRecipe[]): CraftingRecipe[] {
return recipes.map(normalizeCraftingRecipe).filter((recipe) => recipe.id > 0);
}
export function loadCraftingRecipesDataFromJsonFile(filePath: string): CraftingRecipe[] {
const raw = fs.readFileSync(filePath, "utf8");
return normalizeCraftingRecipesData(JSON.parse(raw) as CraftingRecipe[]);
}
export function loadDefaultCraftingRecipesData(): CraftingRecipe[] {
return loadCraftingRecipesDataFromJsonFile(DEFAULT_CRAFTING_RECIPES_JSON_PATH);
}
export { DEFAULT_CRAFTING_RECIPES_JSON_PATH };