2024-01-25 14:49:45 +01:00
|
|
|
//this is a controller for the claimCompletedRecipe route
|
|
|
|
//it will claim a recipe for the user
|
|
|
|
|
2024-05-06 15:19:42 +02:00
|
|
|
import { RequestHandler } from "express";
|
2024-01-25 14:49:45 +01:00
|
|
|
import { logger } from "@/src/utils/logger";
|
2024-06-19 10:32:16 +02:00
|
|
|
import { getRecipe } from "@/src/services/itemDataService";
|
2024-01-25 14:49:45 +01:00
|
|
|
import { IOid } from "@/src/types/commonTypes";
|
|
|
|
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
2024-05-28 13:45:06 +02:00
|
|
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
2025-03-13 04:25:59 -07:00
|
|
|
import {
|
|
|
|
getInventory,
|
|
|
|
updateCurrency,
|
|
|
|
addItem,
|
|
|
|
addRecipes,
|
2025-03-28 03:07:30 -07:00
|
|
|
occupySlot,
|
|
|
|
combineInventoryChanges
|
2025-03-13 04:25:59 -07:00
|
|
|
} from "@/src/services/inventoryService";
|
2025-02-28 06:47:34 -08:00
|
|
|
import { IInventoryChanges } from "@/src/types/purchaseTypes";
|
|
|
|
import { IEquipmentClient } from "@/src/types/inventoryTypes/commonInventoryTypes";
|
2025-03-28 03:07:30 -07:00
|
|
|
import { InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes";
|
2024-01-25 14:49:45 +01:00
|
|
|
|
|
|
|
export interface IClaimCompletedRecipeRequest {
|
|
|
|
RecipeIds: IOid[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const claimCompletedRecipeController: RequestHandler = async (req, res) => {
|
2025-01-24 14:27:10 +01:00
|
|
|
const claimCompletedRecipeRequest = getJSONfromString<IClaimCompletedRecipeRequest>(String(req.body));
|
2024-05-28 13:45:06 +02:00
|
|
|
const accountId = await getAccountIdForRequest(req);
|
2024-01-25 14:49:45 +01:00
|
|
|
if (!accountId) throw new Error("no account id");
|
|
|
|
|
|
|
|
const inventory = await getInventory(accountId);
|
2025-01-20 12:21:39 +01:00
|
|
|
const pendingRecipe = inventory.PendingRecipes.id(claimCompletedRecipeRequest.RecipeIds[0].$oid);
|
2024-01-25 14:49:45 +01:00
|
|
|
if (!pendingRecipe) {
|
|
|
|
throw new Error(`no pending recipe found with id ${claimCompletedRecipeRequest.RecipeIds[0].$oid}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
//check recipe is indeed ready to be completed
|
|
|
|
// if (pendingRecipe.CompletionDate > new Date()) {
|
|
|
|
// throw new Error(`recipe ${pendingRecipe._id} is not ready to be completed`);
|
|
|
|
// }
|
|
|
|
|
2024-06-15 02:50:43 +02:00
|
|
|
inventory.PendingRecipes.pull(pendingRecipe._id);
|
2024-01-25 14:49:45 +01:00
|
|
|
|
2024-06-19 10:32:16 +02:00
|
|
|
const recipe = getRecipe(pendingRecipe.ItemType);
|
|
|
|
if (!recipe) {
|
2024-12-29 06:00:51 +01:00
|
|
|
throw new Error(`no completed item found for recipe ${pendingRecipe._id.toString()}`);
|
2024-01-25 14:49:45 +01:00
|
|
|
}
|
|
|
|
|
2024-06-15 02:50:43 +02:00
|
|
|
if (req.query.cancel) {
|
2025-02-28 06:47:34 -08:00
|
|
|
const inventoryChanges: IInventoryChanges = {
|
|
|
|
...updateCurrency(inventory, recipe.buildPrice * -1, false)
|
|
|
|
};
|
2024-06-19 10:32:16 +02:00
|
|
|
|
2025-03-28 03:07:30 -07:00
|
|
|
const equipmentIngredients = new Set();
|
2025-02-28 06:47:34 -08:00
|
|
|
for (const category of ["LongGuns", "Pistols", "Melee"] as const) {
|
|
|
|
if (pendingRecipe[category]) {
|
|
|
|
pendingRecipe[category].forEach(item => {
|
|
|
|
const index = inventory[category].push(item) - 1;
|
|
|
|
inventoryChanges[category] ??= [];
|
|
|
|
inventoryChanges[category].push(inventory[category][index].toJSON<IEquipmentClient>());
|
2025-03-28 03:07:30 -07:00
|
|
|
equipmentIngredients.add(item.ItemType);
|
2025-02-28 06:47:34 -08:00
|
|
|
|
2025-03-13 04:25:59 -07:00
|
|
|
occupySlot(inventory, InventorySlot.WEAPONS, false);
|
2025-02-28 06:47:34 -08:00
|
|
|
inventoryChanges.WeaponBin ??= { Slots: 0 };
|
|
|
|
inventoryChanges.WeaponBin.Slots -= 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2025-03-28 03:07:30 -07:00
|
|
|
for (const ingredient of recipe.ingredients) {
|
|
|
|
if (!equipmentIngredients.has(ingredient.ItemType)) {
|
|
|
|
combineInventoryChanges(
|
|
|
|
inventoryChanges,
|
|
|
|
await addItem(inventory, ingredient.ItemType, ingredient.ItemCount)
|
|
|
|
);
|
2025-02-28 06:47:34 -08:00
|
|
|
}
|
2025-03-28 03:07:30 -07:00
|
|
|
}
|
2025-02-28 06:47:34 -08:00
|
|
|
|
|
|
|
await inventory.save();
|
|
|
|
res.json(inventoryChanges); // Not a bug: In the specific case of cancelling a recipe, InventoryChanges are expected to be the root.
|
2024-06-15 02:50:43 +02:00
|
|
|
} else {
|
2024-06-19 10:32:16 +02:00
|
|
|
logger.debug("Claiming Recipe", { recipe, pendingRecipe });
|
2025-01-06 01:21:37 +01:00
|
|
|
|
|
|
|
if (recipe.secretIngredientAction == "SIA_SPECTRE_LOADOUT_COPY") {
|
|
|
|
inventory.PendingSpectreLoadouts ??= [];
|
|
|
|
inventory.SpectreLoadouts ??= [];
|
|
|
|
|
|
|
|
const pendingLoadoutIndex = inventory.PendingSpectreLoadouts.findIndex(
|
|
|
|
x => x.ItemType == recipe.resultType
|
|
|
|
);
|
|
|
|
if (pendingLoadoutIndex != -1) {
|
|
|
|
const loadoutIndex = inventory.SpectreLoadouts.findIndex(x => x.ItemType == recipe.resultType);
|
|
|
|
if (loadoutIndex != -1) {
|
|
|
|
inventory.SpectreLoadouts.splice(loadoutIndex, 1);
|
|
|
|
}
|
|
|
|
logger.debug(
|
|
|
|
"moving spectre loadout from pending to active",
|
|
|
|
inventory.toJSON().PendingSpectreLoadouts![pendingLoadoutIndex]
|
|
|
|
);
|
|
|
|
inventory.SpectreLoadouts.push(inventory.PendingSpectreLoadouts[pendingLoadoutIndex]);
|
|
|
|
inventory.PendingSpectreLoadouts.splice(pendingLoadoutIndex, 1);
|
|
|
|
}
|
2025-03-31 04:14:20 -07:00
|
|
|
} else if (recipe.secretIngredientAction == "SIA_UNBRAND") {
|
|
|
|
inventory.BrandedSuits!.splice(
|
|
|
|
inventory.BrandedSuits!.findIndex(x => x.equals(pendingRecipe.SuitToUnbrand)),
|
|
|
|
1
|
|
|
|
);
|
2025-01-06 01:21:37 +01:00
|
|
|
}
|
|
|
|
|
2024-06-19 10:32:16 +02:00
|
|
|
let InventoryChanges = {};
|
|
|
|
if (recipe.consumeOnUse) {
|
2025-03-08 04:33:45 -08:00
|
|
|
addRecipes(inventory, [
|
2024-06-19 10:32:16 +02:00
|
|
|
{
|
|
|
|
ItemType: pendingRecipe.ItemType,
|
|
|
|
ItemCount: -1
|
|
|
|
}
|
2025-03-08 04:33:45 -08:00
|
|
|
]);
|
2024-06-17 16:41:02 +02:00
|
|
|
}
|
2024-06-19 10:32:16 +02:00
|
|
|
if (req.query.rush) {
|
|
|
|
InventoryChanges = {
|
|
|
|
...InventoryChanges,
|
2025-02-24 05:27:50 -08:00
|
|
|
...updateCurrency(inventory, recipe.skipBuildTimePrice, true)
|
2024-06-19 10:32:16 +02:00
|
|
|
};
|
2024-06-15 02:50:43 +02:00
|
|
|
}
|
2025-03-31 04:14:20 -07:00
|
|
|
if (recipe.secretIngredientAction != "SIA_UNBRAND") {
|
|
|
|
InventoryChanges = {
|
|
|
|
...InventoryChanges,
|
|
|
|
...(await addItem(inventory, recipe.resultType, recipe.num, false))
|
|
|
|
};
|
|
|
|
}
|
2025-01-04 00:25:09 +01:00
|
|
|
await inventory.save();
|
|
|
|
res.json({ InventoryChanges });
|
2024-06-15 02:50:43 +02:00
|
|
|
}
|
2024-01-25 14:49:45 +01:00
|
|
|
};
|