From f672f05db955c496fe5698fa8ea475a8c049213d Mon Sep 17 00:00:00 2001 From: Sainan Date: Tue, 25 Feb 2025 04:38:17 -0800 Subject: [PATCH] fix: handle bundles being given to addItems (#1005) This is needed for the Hex noggles email attachment Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1005 Co-authored-by: Sainan Co-committed-by: Sainan --- src/services/inventoryService.ts | 7 ++++++ src/services/purchaseService.ts | 42 ++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 81da3eb1..6bce6e5e 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -38,6 +38,7 @@ import { getExalted, getKeyChainItems } from "@/src/services/itemDataService"; import { IEquipmentClient, IEquipmentDatabase, IItemConfig } from "../types/inventoryTypes/commonInventoryTypes"; import { ExportArcanes, + ExportBundles, ExportCustoms, ExportDrones, ExportFlavour, @@ -59,6 +60,7 @@ import { toOid } from "../helpers/inventoryHelpers"; import { generateRewardSeed } from "@/src/controllers/api/getNewRewardSeedController"; import { addStartingGear } from "@/src/controllers/api/giveStartingGearController"; import { addQuestKey, completeQuest } from "@/src/services/questService"; +import { handleBundleAcqusition } from "./purchaseService"; export const createInventory = async ( accountOwnerId: Types.ObjectId, @@ -157,6 +159,11 @@ export const addItem = async ( typeName: string, quantity: number = 1 ): Promise<{ InventoryChanges: IInventoryChanges }> => { + // Bundles are technically StoreItems but a) they don't have a normal counterpart, and b) they are used in non-StoreItem contexts, e.g. email attachments. + if (typeName in ExportBundles) { + return { InventoryChanges: await handleBundleAcqusition(typeName, inventory, quantity) }; + } + // Strict typing if (typeName in ExportRecipes) { const recipeChanges = [ diff --git a/src/services/purchaseService.ts b/src/services/purchaseService.ts index 36d91713..76a6d111 100644 --- a/src/services/purchaseService.ts +++ b/src/services/purchaseService.ts @@ -200,6 +200,31 @@ const handleItemPrices = ( } }; +export const handleBundleAcqusition = async ( + storeItemName: string, + inventory: TInventoryDatabaseDocument, + quantity: number = 1, + inventoryChanges: IInventoryChanges = {} +): Promise => { + const bundle = ExportBundles[storeItemName]; + logger.debug("acquiring bundle", bundle); + for (const component of bundle.components) { + combineInventoryChanges( + inventoryChanges, + ( + await handleStoreItemAcquisition( + component.typeName, + inventory, + component.purchaseQuantity * quantity, + component.durability, + true + ) + ).InventoryChanges + ); + } + return inventoryChanges; +}; + export const handleStoreItemAcquisition = async ( storeItemName: string, inventory: TInventoryDatabaseDocument, @@ -212,22 +237,7 @@ export const handleStoreItemAcquisition = async ( }; logger.debug(`handling acquision of ${storeItemName}`); if (storeItemName in ExportBundles) { - const bundle = ExportBundles[storeItemName]; - logger.debug("acquiring bundle", bundle); - for (const component of bundle.components) { - combineInventoryChanges( - purchaseResponse.InventoryChanges, - ( - await handleStoreItemAcquisition( - component.typeName, - inventory, - component.purchaseQuantity * quantity, - component.durability, - true - ) - ).InventoryChanges - ); - } + await handleBundleAcqusition(storeItemName, inventory, quantity, purchaseResponse.InventoryChanges); } else { const storeCategory = getStoreItemCategory(storeItemName); const internalName = storeItemName.replace("/StoreItems", "");