fix: item count validation when starting a recipe #3030

Merged
Sainan merged 1 commits from recipe-no-check into main 2025-11-15 00:26:15 -08:00
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 });