fix(inventory): accept native inventory exchange material tuples
Resolve item definitions from saved stacks and validate the combined quantity when a request repeats a stack.
This commit is contained in:
1 parent
3c1210f249
commit
179185c3f6
2 files changed
+16
-3
No files matched your search
@@ -196,10 +196,15 @@ function validateRecipeMaterials(
|
||||
materials: readonly ExchangeMaterialInput[],
|
||||
): string | undefined {
|
||||
const providedByDef = new Map<number, number>();
|
||||
const providedByStack = new Map<string, number>();
|
||||
for (const mat of materials) {
|
||||
if (!mat.itemid || !Number.isSafeInteger(mat.quantity) || mat.quantity < 1) return 'BAD_MATERIALS';
|
||||
const held = externalItemByUid(profile, String(mat.itemid));
|
||||
if (!held || held.itemDefId !== Number(mat.itemdefid) || held.quantity < mat.quantity) return 'NOT_ENOUGH_ITEMS';
|
||||
const held = externalItemByUid(profile, mat.itemid);
|
||||
const stackQuantity = (providedByStack.get(mat.itemid) ?? 0) + mat.quantity;
|
||||
if (!held || (mat.itemdefid !== undefined && held.itemDefId !== mat.itemdefid) || held.quantity < stackQuantity) {
|
||||
return 'NOT_ENOUGH_ITEMS';
|
||||
}
|
||||
providedByStack.set(mat.itemid, stackQuantity);
|
||||
providedByDef.set(held.itemDefId, (providedByDef.get(held.itemDefId) ?? 0) + mat.quantity);
|
||||
}
|
||||
const matches = gen.recipes.some((recipe) =>
|
||||
|
||||
@@ -9,7 +9,8 @@ import type { InventoryItemJson } from './itemdef-types.ts';
|
||||
|
||||
export interface ExchangeMaterialInput {
|
||||
readonly itemid: string;
|
||||
readonly itemdefid: number;
|
||||
/** Present in the object form. The native tuple identifies the stack by itemid. */
|
||||
readonly itemdefid?: number;
|
||||
readonly quantity: number;
|
||||
}
|
||||
|
||||
@@ -58,6 +59,13 @@ export function parseExchangeItemsRequest(rawText: string): ParsedExchangeReques
|
||||
if (!Number.isSafeInteger(outputItemDefId) || !Number.isSafeInteger(quantity)) return undefined;
|
||||
const rawMaterials = Array.isArray(parsed.materials) ? parsed.materials : [];
|
||||
const materials: ExchangeMaterialInput[] = rawMaterials.flatMap((m) => {
|
||||
if (Array.isArray(m) && m.length === 2) {
|
||||
const [itemId, count] = m;
|
||||
return [{
|
||||
itemid: typeof itemId === 'string' ? itemId : '',
|
||||
quantity: typeof count === 'number' ? count : 0,
|
||||
}];
|
||||
}
|
||||
if (typeof m !== 'object' || m === null) return [];
|
||||
const rec = m as Record<string, unknown>;
|
||||
return [{
|
||||
|
||||
Reference in new issue
Block a user