Exact source used by this build. SHA-256: 653b0861f022815d788d1cfab8732dcd9ba93cadf2fe8941805b5f6834b478ed.
1 import itemsJson from "../../../generated/items.json";
2 import { CROPS } from "../crops";
3 import { ItemSchema } from "./schema";
4 import type { Item } from "./schema";
5 import type { ContentDomain } from "../types";
6
7 export * from "./schema";
8 export const ITEMS = ItemSchema.array().parse(itemsJson);
9 export const ITEM_BY_ID = Object.fromEntries(ITEMS.map((item) => [item.id, item]));
10 /** Reusable reference check for every future recipe, loot table and content producer. */
11 export function validateItemReferences(ids: readonly string[], items: readonly { id: string }[] = ITEMS): void {
12 const known = new Set(items.map((item) => item.id));
13 for (const id of ids) if (!known.has(id)) throw new Error(`Missing item id: ${id}`);
14 }
15 export function validateItemCatalog(items: readonly Item[], crops: readonly { id: string }[] = CROPS): void {
16 if (new Set(items.map((item) => item.id)).size !== items.length) throw new Error("item ids must be unique");
17 const ids = new Set(items.map((item) => item.id));
18 for (const item of items) for (const alias of item.legacyIds) {
19 if (ids.has(alias)) throw new Error(`Duplicate item id or alias: ${alias}`);
20 ids.add(alias);
21 }
22 for (const item of items) if (item.category === "seed" && !crops.some((crop) => crop.id === item.cropId)) throw new Error(`seed ${item.id} refers to a missing crop`);
23 for (const crop of crops) {
24 if (!items.some((item) => item.category === "seed" && item.cropId === crop.id)) throw new Error(`crop ${crop.id} has no seed item`);
25 if (!items.some((item) => item.category === "crop" && item.id === crop.id)) throw new Error(`crop ${crop.id} has no produce item`);
26 }
27 }
28 export const itemsDomain: ContentDomain = {
29 id: "items", data: ITEMS,
30 validate() {
31 ItemSchema.array().parse(itemsJson);
32 validateItemCatalog(ITEMS);
33 },
34 };
35 itemsDomain.validate();
36