fix: handle recipes requiring non-MiscItem items #1348
@ -11,13 +11,13 @@ import {
|
|||||||
getInventory,
|
getInventory,
|
||||||
updateCurrency,
|
updateCurrency,
|
||||||
addItem,
|
addItem,
|
||||||
addMiscItems,
|
|
||||||
addRecipes,
|
addRecipes,
|
||||||
occupySlot
|
occupySlot,
|
||||||
|
combineInventoryChanges
|
||||||
} from "@/src/services/inventoryService";
|
} from "@/src/services/inventoryService";
|
||||||
import { IInventoryChanges } from "@/src/types/purchaseTypes";
|
import { IInventoryChanges } from "@/src/types/purchaseTypes";
|
||||||
import { IEquipmentClient } from "@/src/types/inventoryTypes/commonInventoryTypes";
|
import { IEquipmentClient } from "@/src/types/inventoryTypes/commonInventoryTypes";
|
||||||
import { IMiscItem, InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes";
|
import { InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||||
|
|
||||||
export interface IClaimCompletedRecipeRequest {
|
export interface IClaimCompletedRecipeRequest {
|
||||||
RecipeIds: IOid[];
|
RecipeIds: IOid[];
|
||||||
@ -51,14 +51,14 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
|
|||||||
...updateCurrency(inventory, recipe.buildPrice * -1, false)
|
...updateCurrency(inventory, recipe.buildPrice * -1, false)
|
||||||
};
|
};
|
||||||
|
|
||||||
const nonMiscItemIngredients = new Set();
|
const equipmentIngredients = new Set();
|
||||||
for (const category of ["LongGuns", "Pistols", "Melee"] as const) {
|
for (const category of ["LongGuns", "Pistols", "Melee"] as const) {
|
||||||
if (pendingRecipe[category]) {
|
if (pendingRecipe[category]) {
|
||||||
pendingRecipe[category].forEach(item => {
|
pendingRecipe[category].forEach(item => {
|
||||||
const index = inventory[category].push(item) - 1;
|
const index = inventory[category].push(item) - 1;
|
||||||
inventoryChanges[category] ??= [];
|
inventoryChanges[category] ??= [];
|
||||||
inventoryChanges[category].push(inventory[category][index].toJSON<IEquipmentClient>());
|
inventoryChanges[category].push(inventory[category][index].toJSON<IEquipmentClient>());
|
||||||
nonMiscItemIngredients.add(item.ItemType);
|
equipmentIngredients.add(item.ItemType);
|
||||||
|
|
||||||
occupySlot(inventory, InventorySlot.WEAPONS, false);
|
occupySlot(inventory, InventorySlot.WEAPONS, false);
|
||||||
inventoryChanges.WeaponBin ??= { Slots: 0 };
|
inventoryChanges.WeaponBin ??= { Slots: 0 };
|
||||||
@ -66,14 +66,14 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const miscItemChanges: IMiscItem[] = [];
|
for (const ingredient of recipe.ingredients) {
|
||||||
recipe.ingredients.forEach(ingredient => {
|
if (!equipmentIngredients.has(ingredient.ItemType)) {
|
||||||
if (!nonMiscItemIngredients.has(ingredient.ItemType)) {
|
combineInventoryChanges(
|
||||||
miscItemChanges.push(ingredient);
|
inventoryChanges,
|
||||||
|
await addItem(inventory, ingredient.ItemType, ingredient.ItemCount)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
addMiscItems(inventory, miscItemChanges);
|
|
||||||
inventoryChanges.MiscItems = miscItemChanges;
|
|
||||||
|
|
||||||
await inventory.save();
|
await inventory.save();
|
||||||
res.json(inventoryChanges); // Not a bug: In the specific case of cancelling a recipe, InventoryChanges are expected to be the root.
|
res.json(inventoryChanges); // Not a bug: In the specific case of cancelling a recipe, InventoryChanges are expected to be the root.
|
||||||
|
@ -3,7 +3,7 @@ import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
|||||||
import { logger } from "@/src/utils/logger";
|
import { logger } from "@/src/utils/logger";
|
||||||
import { RequestHandler } from "express";
|
import { RequestHandler } from "express";
|
||||||
import { getRecipe } from "@/src/services/itemDataService";
|
import { getRecipe } from "@/src/services/itemDataService";
|
||||||
import { addMiscItems, freeUpSlot, getInventory, updateCurrency } from "@/src/services/inventoryService";
|
import { addItem, freeUpSlot, getInventory, updateCurrency } from "@/src/services/inventoryService";
|
||||||
import { unixTimesInMs } from "@/src/constants/timeConstants";
|
import { unixTimesInMs } from "@/src/constants/timeConstants";
|
||||||
import { Types } from "mongoose";
|
import { Types } from "mongoose";
|
||||||
import { InventorySlot, ISpectreLoadout } from "@/src/types/inventoryTypes/inventoryTypes";
|
import { InventorySlot, ISpectreLoadout } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||||
@ -55,12 +55,7 @@ export const startRecipeController: RequestHandler = async (req, res) => {
|
|||||||
inventory[category].splice(equipmentIndex, 1);
|
inventory[category].splice(equipmentIndex, 1);
|
||||||
freeUpSlot(inventory, InventorySlot.WEAPONS);
|
freeUpSlot(inventory, InventorySlot.WEAPONS);
|
||||||
} else {
|
} else {
|
||||||
addMiscItems(inventory, [
|
await addItem(inventory, recipe.ingredients[i].ItemType, recipe.ingredients[i].ItemCount * -1);
|
||||||
{
|
|
||||||
ItemType: recipe.ingredients[i].ItemType,
|
|
||||||
ItemCount: recipe.ingredients[i].ItemCount * -1
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user