2024-05-28 13:45:06 +02:00
|
|
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
2024-01-25 14:49:45 +01:00
|
|
|
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
|
|
|
import { logger } from "@/src/utils/logger";
|
|
|
|
import { RequestHandler } from "express";
|
2024-12-31 01:36:28 +01:00
|
|
|
import { getRecipe } from "@/src/services/itemDataService";
|
2025-03-27 20:52:06 +01:00
|
|
|
import { addItem, freeUpSlot, getInventory, updateCurrency } from "@/src/services/inventoryService";
|
2024-12-31 01:36:28 +01:00
|
|
|
import { unixTimesInMs } from "@/src/constants/timeConstants";
|
|
|
|
import { Types } from "mongoose";
|
2025-03-13 04:25:59 -07:00
|
|
|
import { InventorySlot, ISpectreLoadout } from "@/src/types/inventoryTypes/inventoryTypes";
|
2025-02-28 06:47:34 -08:00
|
|
|
import { toOid } from "@/src/helpers/inventoryHelpers";
|
|
|
|
import { ExportWeapons } from "warframe-public-export-plus";
|
2024-01-25 14:49:45 +01:00
|
|
|
|
|
|
|
interface IStartRecipeRequest {
|
|
|
|
RecipeName: string;
|
|
|
|
Ids: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const startRecipeController: RequestHandler = async (req, res) => {
|
2025-01-24 14:27:10 +01:00
|
|
|
const startRecipeRequest = getJSONfromString<IStartRecipeRequest>(String(req.body));
|
2024-01-25 14:49:45 +01:00
|
|
|
logger.debug("StartRecipe Request", { startRecipeRequest });
|
|
|
|
|
2024-05-28 13:45:06 +02:00
|
|
|
const accountId = await getAccountIdForRequest(req);
|
2024-01-25 14:49:45 +01:00
|
|
|
|
2024-12-31 01:36:28 +01:00
|
|
|
const recipeName = startRecipeRequest.RecipeName;
|
|
|
|
const recipe = getRecipe(recipeName);
|
|
|
|
|
|
|
|
if (!recipe) {
|
|
|
|
throw new Error(`unknown recipe ${recipeName}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const inventory = await getInventory(accountId);
|
2024-12-31 01:41:29 +01:00
|
|
|
updateCurrency(inventory, recipe.buildPrice, false);
|
2024-12-31 01:36:28 +01:00
|
|
|
|
2025-02-28 06:47:34 -08:00
|
|
|
const pr =
|
|
|
|
inventory.PendingRecipes[
|
|
|
|
inventory.PendingRecipes.push({
|
|
|
|
ItemType: recipeName,
|
|
|
|
CompletionDate: new Date(Date.now() + recipe.buildTime * unixTimesInMs.second),
|
|
|
|
_id: new Types.ObjectId()
|
|
|
|
}) - 1
|
|
|
|
];
|
2024-12-31 01:36:28 +01:00
|
|
|
|
2025-02-28 06:47:34 -08:00
|
|
|
for (let i = 0; i != recipe.ingredients.length; ++i) {
|
|
|
|
if (startRecipeRequest.Ids[i]) {
|
|
|
|
const category = ExportWeapons[recipe.ingredients[i].ItemType].productCategory;
|
|
|
|
if (category != "LongGuns" && category != "Pistols" && category != "Melee") {
|
|
|
|
throw new Error(`unexpected equipment ingredient type: ${category}`);
|
|
|
|
}
|
|
|
|
const equipmentIndex = inventory[category].findIndex(x => x._id.equals(startRecipeRequest.Ids[i]));
|
|
|
|
if (equipmentIndex == -1) {
|
|
|
|
throw new Error(`could not find equipment item to use for recipe`);
|
|
|
|
}
|
|
|
|
pr[category] ??= [];
|
|
|
|
pr[category].push(inventory[category][equipmentIndex]);
|
|
|
|
inventory[category].splice(equipmentIndex, 1);
|
2025-03-13 04:25:59 -07:00
|
|
|
freeUpSlot(inventory, InventorySlot.WEAPONS);
|
2025-02-28 06:47:34 -08:00
|
|
|
} else {
|
2025-03-27 20:52:06 +01:00
|
|
|
await addItem(inventory, recipe.ingredients[i].ItemType, recipe.ingredients[i].ItemCount * -1);
|
2025-02-28 06:47:34 -08:00
|
|
|
}
|
|
|
|
}
|
2024-12-31 01:36:28 +01:00
|
|
|
|
2025-01-06 01:21:37 +01:00
|
|
|
if (recipe.secretIngredientAction == "SIA_SPECTRE_LOADOUT_COPY") {
|
|
|
|
const spectreLoadout: ISpectreLoadout = {
|
|
|
|
ItemType: recipe.resultType,
|
|
|
|
Suits: "",
|
|
|
|
LongGuns: "",
|
|
|
|
Pistols: "",
|
|
|
|
Melee: ""
|
|
|
|
};
|
|
|
|
for (
|
|
|
|
let secretIngredientsIndex = 0;
|
|
|
|
secretIngredientsIndex != recipe.secretIngredients!.length;
|
|
|
|
++secretIngredientsIndex
|
|
|
|
) {
|
|
|
|
const type = recipe.secretIngredients![secretIngredientsIndex].ItemType;
|
|
|
|
const oid = startRecipeRequest.Ids[recipe.ingredients.length + secretIngredientsIndex];
|
|
|
|
if (oid == "ffffffffffffffffffffffff") {
|
|
|
|
// user chose to preserve the active loadout
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (type == "/Lotus/Types/Game/PowerSuits/PlayerPowerSuit") {
|
2025-01-19 01:58:24 +01:00
|
|
|
const item = inventory.Suits.id(oid)!;
|
2025-01-06 01:21:37 +01:00
|
|
|
spectreLoadout.Suits = item.ItemType;
|
|
|
|
} else if (type == "/Lotus/Weapons/Tenno/Pistol/LotusPistol") {
|
2025-01-19 01:58:24 +01:00
|
|
|
const item = inventory.Pistols.id(oid)!;
|
2025-01-06 01:21:37 +01:00
|
|
|
spectreLoadout.Pistols = item.ItemType;
|
|
|
|
spectreLoadout.PistolsModularParts = item.ModularParts;
|
|
|
|
} else if (type == "/Lotus/Weapons/Tenno/LotusLongGun") {
|
2025-01-19 01:58:24 +01:00
|
|
|
const item = inventory.LongGuns.id(oid)!;
|
2025-01-06 01:21:37 +01:00
|
|
|
spectreLoadout.LongGuns = item.ItemType;
|
|
|
|
spectreLoadout.LongGunsModularParts = item.ModularParts;
|
|
|
|
} else {
|
2025-01-20 12:21:39 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
2025-01-06 01:21:37 +01:00
|
|
|
console.assert(type == "/Lotus/Types/Game/LotusMeleeWeapon");
|
2025-01-19 01:58:24 +01:00
|
|
|
const item = inventory.Melee.id(oid)!;
|
2025-01-06 01:21:37 +01:00
|
|
|
spectreLoadout.Melee = item.ItemType;
|
|
|
|
spectreLoadout.MeleeModularParts = item.ModularParts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
spectreLoadout.Suits != "" &&
|
|
|
|
spectreLoadout.LongGuns != "" &&
|
|
|
|
spectreLoadout.Pistols != "" &&
|
|
|
|
spectreLoadout.Melee != ""
|
|
|
|
) {
|
|
|
|
inventory.PendingSpectreLoadouts ??= [];
|
|
|
|
const existingIndex = inventory.PendingSpectreLoadouts.findIndex(x => x.ItemType == recipe.resultType);
|
|
|
|
if (existingIndex != -1) {
|
|
|
|
inventory.PendingSpectreLoadouts.splice(existingIndex, 1);
|
|
|
|
}
|
|
|
|
inventory.PendingSpectreLoadouts.push(spectreLoadout);
|
|
|
|
logger.debug("pending spectre loadout", spectreLoadout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-28 06:47:34 -08:00
|
|
|
await inventory.save();
|
2024-12-31 01:36:28 +01:00
|
|
|
|
2025-02-28 06:47:34 -08:00
|
|
|
res.json({ RecipeId: toOid(pr._id) });
|
2024-01-25 14:49:45 +01:00
|
|
|
};
|