From 55d051ff37c9e93b73a3a64299e3258a2dde5334 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Sat, 15 Nov 2025 00:26:14 -0800 Subject: [PATCH] fix: item count validation when starting a recipe (#3030) Can't assume it's all MiscItems. Instead upgraded the warnings to errors in the inventoryService functions. Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3030 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com> --- 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 });