fix: item count validation when starting a recipe (#3030)
Some checks failed
Build Docker image / docker (push) Has been cancelled
Build / build (push) Has been cancelled

Can't assume it's all MiscItems. Instead upgraded the warnings to errors in the inventoryService functions.

Reviewed-on: #3030
Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com>
Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit was merged in pull request #3030.
This commit is contained in:
2025-11-15 00:26:14 -08:00
committed by Sainan
parent 9f94a17eda
commit 55d051ff37
2 changed files with 14 additions and 15 deletions

View File

@@ -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);
}
}

View File

@@ -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 });