From 7d0b2d368a887dc16cc8ddd1830c0eae85510eb9 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Fri, 14 Nov 2025 14:26:48 +0100 Subject: [PATCH] fix: item count validation when starting a recipe Can't assume it's all MiscItems. Instead upgraded the warnings to errors in the inventoryService functions. --- src/controllers/api/startRecipeController.ts | 9 +-------- src/services/inventoryService.ts | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/controllers/api/startRecipeController.ts b/src/controllers/api/startRecipeController.ts index b9e6bad2..dff18dd3 100644 --- a/src/controllers/api/startRecipeController.ts +++ b/src/controllers/api/startRecipeController.ts @@ -71,14 +71,7 @@ export const startRecipeController: RequestHandler = async (req, res) => { } else { const itemType = recipe.ingredients[i].ItemType; const itemCount = recipe.ingredients[i].ItemCount; - const inventoryItem = inventory.MiscItems.find(i => i.ItemType === itemType); - if (inventoryItem && inventoryItem.ItemCount >= itemCount) { - await addItem(inventory, itemType, itemCount * -1); - } else { - throw new Error( - `insufficient ${itemType} (in inventory ${inventoryItem?.ItemCount} - needed ${itemCount}) for recipe ${recipeName}` - ); - } + await addItem(inventory, itemType, itemCount * -1); } } diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 83c09af2..2728744c 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -1849,8 +1849,8 @@ export const addMiscItems = (inventory: TInventoryDatabaseDocument, itemsArray: if (MiscItems[itemIndex].ItemCount == 0) { MiscItems.splice(itemIndex, 1); - } else if (MiscItems[itemIndex].ItemCount <= 0) { - logger.warn(`inventory.MiscItems has a negative count for ${ItemType}`); + } else if (MiscItems[itemIndex].ItemCount < 0) { + throw new Error(`inventory.MiscItems has a negative count for ${ItemType} after subtracting ${ItemCount}`); } }); }; @@ -1871,8 +1871,10 @@ const applyArrayChanges = ( arr[itemIndex].ItemCount += change.ItemCount; if (arr[itemIndex].ItemCount == 0) { arr.splice(itemIndex, 1); - } else if (arr[itemIndex].ItemCount <= 0) { - logger.warn(`inventory.${key} has a negative count for ${change.ItemType}`); + } else if (arr[itemIndex].ItemCount < 0) { + throw new Error( + `inventory.${key} has a negative count for ${change.ItemType} after subtracting ${change.ItemCount}` + ); } } } @@ -1918,8 +1920,10 @@ export const addMods = (inventory: TInventoryDatabaseDocument, itemsArray: IRawU RawUpgrades[itemIndex].ItemCount += ItemCount; if (RawUpgrades[itemIndex].ItemCount == 0) { RawUpgrades.splice(itemIndex, 1); - } else if (RawUpgrades[itemIndex].ItemCount <= 0) { - logger.warn(`inventory.RawUpgrades has a negative count for ${ItemType}`); + } else if (RawUpgrades[itemIndex].ItemCount < 0) { + throw new Error( + `inventory.RawUpgrades has a negative count for ${ItemType} after subtracting ${ItemCount}` + ); } }); }; @@ -1934,7 +1938,9 @@ export const addFusionTreasures = (inventory: TInventoryDatabaseDocument, itemsA if (FusionTreasures[itemIndex].ItemCount == 0) { FusionTreasures.splice(itemIndex, 1); } else if (FusionTreasures[itemIndex].ItemCount <= 0) { - logger.warn(`inventory.FusionTreasures has a negative count for ${ItemType}`); + throw new Error( + `inventory.FusionTreasures has a negative count for ${ItemType} after subtracting ${ItemCount}` + ); } } else { FusionTreasures.push({ ItemCount, ItemType, Sockets });