feat: Implement claiming & rushing recipes

This commit is contained in:
Sainan 2024-05-31 09:22:35 +02:00
parent c6c5979fea
commit 9a8982c440
2 changed files with 22 additions and 24 deletions

View File

@ -3,11 +3,11 @@
import { RequestHandler } from "express"; import { RequestHandler } from "express";
import { logger } from "@/src/utils/logger"; import { logger } from "@/src/utils/logger";
import { getItemByBlueprint, getItemCategoryByUniqueName } from "@/src/services/itemDataService"; import { getItemByBlueprint } from "@/src/services/itemDataService";
import { IOid } from "@/src/types/commonTypes"; import { IOid } from "@/src/types/commonTypes";
import { getJSONfromString } from "@/src/helpers/stringHelpers"; import { getJSONfromString } from "@/src/helpers/stringHelpers";
import { getAccountIdForRequest } from "@/src/services/loginService"; import { getAccountIdForRequest } from "@/src/services/loginService";
import { getInventory } from "@/src/services/inventoryService"; import { getInventory, updateCurrency, addItem } from "@/src/services/inventoryService";
export interface IClaimCompletedRecipeRequest { export interface IClaimCompletedRecipeRequest {
RecipeIds: IOid[]; RecipeIds: IOid[];
@ -19,12 +19,10 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
if (!accountId) throw new Error("no account id"); if (!accountId) throw new Error("no account id");
console.log(claimCompletedRecipeRequest);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const pendingRecipe = inventory.PendingRecipes.find( const pendingRecipe = inventory.PendingRecipes.find(
recipe => recipe._id?.toString() === claimCompletedRecipeRequest.RecipeIds[0].$oid recipe => recipe._id?.toString() === claimCompletedRecipeRequest.RecipeIds[0].$oid
); );
console.log(pendingRecipe);
if (!pendingRecipe) { if (!pendingRecipe) {
logger.error(`no pending recipe found with id ${claimCompletedRecipeRequest.RecipeIds[0].$oid}`); logger.error(`no pending recipe found with id ${claimCompletedRecipeRequest.RecipeIds[0].$oid}`);
throw new Error(`no pending recipe found with id ${claimCompletedRecipeRequest.RecipeIds[0].$oid}`); throw new Error(`no pending recipe found with id ${claimCompletedRecipeRequest.RecipeIds[0].$oid}`);
@ -36,29 +34,28 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
// throw new Error(`recipe ${pendingRecipe._id} is not ready to be completed`); // throw new Error(`recipe ${pendingRecipe._id} is not ready to be completed`);
// } // }
//get completed Items inventory.PendingRecipes.pull(pendingRecipe._id);
const completedItemName = getItemByBlueprint(pendingRecipe.ItemType)?.uniqueName; await inventory.save();
if (!completedItemName) { const buildable = getItemByBlueprint(pendingRecipe.ItemType);
if (!buildable) {
logger.error(`no completed item found for recipe ${pendingRecipe._id}`); logger.error(`no completed item found for recipe ${pendingRecipe._id}`);
throw new Error(`no completed item found for recipe ${pendingRecipe._id}`); throw new Error(`no completed item found for recipe ${pendingRecipe._id}`);
} }
const itemCategory = getItemCategoryByUniqueName(completedItemName) as keyof typeof inventory;
console.log(itemCategory);
//TODO: remove all Schema.Mixed for inventory[itemCategory] not to be any
//add item
//inventory[itemCategory].
//add additional item components like mods or weapons for a sentinel. if (req.query.cancel) {
//const additionalItemComponents = itemComponents[uniqueName] // TODO: Refund items
//add these items to inventory res.json({});
//return changes as InventoryChanges } else {
let currencyChanges = {};
//remove pending recipe if (req.query.rush && buildable.skipBuildTimePrice) {
inventory.PendingRecipes.pull(pendingRecipe._id); currencyChanges = await updateCurrency(buildable.skipBuildTimePrice, true, accountId);
// await inventory.save(); }
res.json({
logger.debug("Claiming Completed Recipe", { completedItemName }); InventoryChanges: {
...currencyChanges,
res.json({ InventoryChanges: {} }); ...(await addItem(accountId, buildable.uniqueName, buildable.buildQuantity)).InventoryChanges
}
});
}
}; };

View File

@ -125,6 +125,7 @@ export const addItem = async (
} }
}; };
case "Sentinels": case "Sentinels":
// TOOD: Sentinels should also grant their DefaultUpgrades & SentinelWeapon.
const sentinel = await addSentinel(typeName, accountId); const sentinel = await addSentinel(typeName, accountId);
await updateSlots(accountId, "SentinelBin", 0, 1); await updateSlots(accountId, "SentinelBin", 0, 1);
return { return {