diff --git a/project/src/helpers/InventoryHelper.ts b/project/src/helpers/InventoryHelper.ts index 04c3db76..795764d7 100644 --- a/project/src/helpers/InventoryHelper.ts +++ b/project/src/helpers/InventoryHelper.ts @@ -200,322 +200,6 @@ export class InventoryHelper } } - /** - * @deprecated - use addItemToStash() - * - * BUG: Passing the same item multiple times with a count of 1 will cause multiples of that item to be added (e.g. x3 separate objects of tar cola with count of 1 = 9 tarcolas being added to inventory) - * @param pmcData Profile to add items to - * @param request request data to add items - * @param output response to send back to client - * @param sessionID Session id - * @param callback Code to execute later (function) - * @param foundInRaid Item added will be flagged as found in raid - * @param addUpd Additional upd properties for items being added to inventory - * @param useSortingTable Allow items to go into sorting table when stash has no space - * @returns IItemEventRouterResponse - */ - public addItem( - pmcData: IPmcData, - request: IAddItemRequestData, - output: IItemEventRouterResponse, - sessionID: string, - callback: () => void, - foundInRaid = false, - addUpd = null, - useSortingTable = false, - ): IItemEventRouterResponse - { - /** All items to add + their children */ - const itemsToAddPool: Item[] = []; - - /** Root items to add to inventory */ - const rootItemsToAdd: IAddItemTempObject[] = []; - - for (const requestItem of request.items) - { - if (this.presetHelper.isPreset(requestItem.item_id)) - { - const preset = this.jsonUtil.clone(this.presetHelper.getPreset(requestItem.item_id)); - const presetItems = preset._items; - - // Add FiR status to preset if needed - if (foundInRaid || this.inventoryConfig.newItemsMarkedFound) - { - for (const item of presetItems) - { - if (!item.upd) - { - item.upd = {}; - } - if (foundInRaid) - { - item.upd.SpawnedInSession = true; - } - } - } - - // Push preset data into pool array - itemsToAddPool.push(...presetItems); - requestItem.sptIsPreset = true; - - // Remap requests item id to preset root items id - requestItem.item_id = presetItems[0]._id; - } - else if (this.paymentHelper.isMoneyTpl(requestItem.item_id)) - { - itemsToAddPool.push({ _id: requestItem.item_id, _tpl: requestItem.item_id }); - } - else if (request.tid === Traders.FENCE) - { - const fenceItems = this.fenceService.getRawFenceAssorts().items; - const itemIndex = fenceItems.findIndex((i) => i._id === requestItem.item_id); - if (itemIndex === -1) - { - this.logger.debug(`Tried to buy item ${requestItem.item_id} from fence that no longer exists`); - const message = this.localisationService.getText("ragfair-offer_no_longer_exists"); - return this.httpResponse.appendErrorToOutput(output, message); - } - - const purchasedItemWithChildren = this.itemHelper.findAndReturnChildrenAsItems( - fenceItems, - requestItem.item_id, - ); - addUpd = purchasedItemWithChildren[0].upd; // Must persist the fence upd properties (e.g. durability/currentHp) - itemsToAddPool.push(...purchasedItemWithChildren); - } - else if (request.tid === "RandomLootContainer") - { - itemsToAddPool.push({ _id: requestItem.item_id, _tpl: requestItem.item_id }); - } - else - { - // Only grab the relevant trader items and add unique values - const traderItems = this.traderAssortHelper.getAssort(sessionID, request.tid).items; - const relevantItems = this.itemHelper.findAndReturnChildrenAsItems(traderItems, requestItem.item_id); - const toAdd = relevantItems.filter((traderItem) => - !itemsToAddPool.some((item) => traderItem._id === item._id) - ); // what's this - itemsToAddPool.push(...toAdd); - } - - // Split stacks into allowed sizes if needed - // e.g. when buying 300 ammo from flea but max stack size is 50 - this.splitStackIntoSmallerStacks(itemsToAddPool, requestItem, rootItemsToAdd); - } - - // Find an empty slot in stash for each of the items being added - const stashFS2D = this.getStashSlotMap(pmcData, sessionID); - const sortingTableFS2D = this.getSortingTableSlotMap(pmcData); - - for (const itemToAdd of rootItemsToAdd) - { - // Update Items `location` properties - const itemWithChildren = this.itemHelper.findAndReturnChildrenAsItems(itemsToAddPool, itemToAdd.itemRef._id); - const errorOutput = this.placeItemInInventoryLegacy( - itemToAdd, - stashFS2D, - sortingTableFS2D, - itemWithChildren, - pmcData.Inventory, - useSortingTable, - output, - ); - if (errorOutput) - { - return errorOutput; - } - } - - // Found slot for every item (stash or sorting table), run callback and catch failure (e.g. payMoney() as player doesnt have enough) - try - { - if (typeof callback === "function") - { - callback(); - } - } - catch (err) - { - // Callback failed - const message = typeof err === "string" ? err : this.localisationService.getText("http-unknown_error"); - - return this.httpResponse.appendErrorToOutput(output, message); - } - - // Update UPD properties and add to output.profileChanges/pmcData.Inventory.items arrays - for (const rootItemToAdd of rootItemsToAdd) - { - let newIdForItem = this.hashUtil.generate(); - const originalKeyToNewKeyMap: string[][] = [[rootItemToAdd.itemRef._id, newIdForItem]]; // Every item id + randomly generated id - let rootItemUpd: Upd = { StackObjectsCount: rootItemToAdd.count }; - - // If item being added is preset, load preset's upd data too. - if (rootItemToAdd.isPreset) - { - // Iterate over properties in upd and add them - for (const updID in rootItemToAdd.itemRef.upd) - { - rootItemUpd[updID] = rootItemToAdd.itemRef.upd[updID]; - } - - if (addUpd) - { - // Iterate over properties in addUpd and add them - for (const updID in addUpd) - { - rootItemUpd[updID] = addUpd[updID]; - } - } - } - - // Item has buff, add to item being sent to player - if (rootItemToAdd.itemRef.upd?.Buff) - { - rootItemUpd.Buff = this.jsonUtil.clone(rootItemToAdd.itemRef.upd.Buff); - } - - // Add ragfair upd properties - if (addUpd) - { - rootItemUpd = { ...addUpd, ...rootItemUpd }; - } - - // Hideout items need to be marked as found in raid - // Or in case people want all items to be marked as found in raid - if (foundInRaid || this.inventoryConfig.newItemsMarkedFound) - { - rootItemUpd.SpawnedInSession = true; - } - - // Remove invalid properties prior to adding to inventory - this.removeTraderRagfairRelatedUpdProperties(rootItemUpd); - - // Add root item to client return object - output.profileChanges[sessionID].items.new.push({ - _id: newIdForItem, - _tpl: rootItemToAdd.itemRef._tpl, - parentId: rootItemToAdd.containerId, - slotId: "hideout", - location: { x: rootItemToAdd.location.x, y: rootItemToAdd.location.y, r: rootItemToAdd.location.rotation ? 1 : 0 }, - upd: this.jsonUtil.clone(rootItemUpd), - }); - - // Add root item to player inventory - pmcData.Inventory.items.push({ - _id: newIdForItem, - _tpl: rootItemToAdd.itemRef._tpl, - parentId: rootItemToAdd.containerId, - slotId: "hideout", - location: { x: rootItemToAdd.location.x, y: rootItemToAdd.location.y, r: rootItemToAdd.location.rotation ? 1 : 0 }, - upd: this.jsonUtil.clone(rootItemUpd), // Clone upd to prevent multi-purchases of same item referencing same upd object in memory - }); - - // Edge case - ammo boxes need cartridges added to result - if (this.itemHelper.isOfBaseclass(rootItemToAdd.itemRef._tpl, BaseClasses.AMMO_BOX)) - { - this.hydrateAmmoBoxWithAmmo(pmcData, rootItemToAdd, originalKeyToNewKeyMap[0][1], sessionID, output, foundInRaid); - } - - // Loop over item + children - while (originalKeyToNewKeyMap.length > 0) - { - // Iterate item + children being added - for (const arrayIndex in itemsToAddPool) - { - const itemDetails = itemsToAddPool[arrayIndex]; - // Does parent match original key - if (itemDetails?.parentId !== originalKeyToNewKeyMap[0][0]) - { - // Skip when items parent isnt on remap (root item) - continue; - } - - // Create new id for child item - newIdForItem = this.hashUtil.generate(); - const itemSlotId = itemDetails.slotId; - - // If its from ItemPreset, load presets upd data too. - if (rootItemToAdd.isPreset) - { - rootItemUpd = { StackObjectsCount: rootItemToAdd.count }; - - for (const updID in itemDetails.upd) - { - rootItemUpd[updID] = itemDetails.upd[updID]; - } - - if (foundInRaid || this.inventoryConfig.newItemsMarkedFound) - { - rootItemUpd.SpawnedInSession = true; - } - } - // Is root item - if (itemSlotId === "hideout") - { - // Add child item to client return object - output.profileChanges[sessionID].items.new.push({ - _id: newIdForItem, - _tpl: itemDetails._tpl, - parentId: originalKeyToNewKeyMap[0][1], - slotId: itemSlotId, - location: { x: rootItemToAdd.location.x, y: rootItemToAdd.location.y, r: "Horizontal" }, - upd: this.jsonUtil.clone(rootItemUpd), - }); - - // Add child item to player inventory - pmcData.Inventory.items.push({ - _id: newIdForItem, - _tpl: itemDetails._tpl, - parentId: originalKeyToNewKeyMap[0][1], - slotId: itemDetails.slotId, - location: { x: rootItemToAdd.location.x, y: rootItemToAdd.location.y, r: "Horizontal" }, - upd: this.jsonUtil.clone(rootItemUpd), - }); - } - else - { - // Child of item being added - - // Item already has location property, use it - const itemLocation = {}; - if (itemDetails.location !== undefined) - { - itemLocation["location"] = itemDetails.location; - } - // Clone upd so we dont adjust the underlying data - const upd = this.jsonUtil.clone(itemDetails.upd); - output.profileChanges[sessionID].items.new.push({ - _id: newIdForItem, - _tpl: itemDetails._tpl, - parentId: originalKeyToNewKeyMap[0][1], - slotId: itemSlotId, - ...itemLocation, - upd: upd, - }); - - pmcData.Inventory.items.push({ - _id: newIdForItem, - _tpl: itemDetails._tpl, - parentId: originalKeyToNewKeyMap[0][1], - slotId: itemDetails.slotId, - ...itemLocation, - upd: upd, - }); - this.logger.debug(`Added: ${itemDetails._tpl} with id: ${newIdForItem} to inventory`); - } - - // Add mapping of child item to new id - originalKeyToNewKeyMap.push([itemDetails._id, newIdForItem]); - } - - // Remove mapping now we're done with it - originalKeyToNewKeyMap.splice(0, 1); - } - } - - return output; - } - /** * Remove properties from a Upd object used by a trader/ragfair * @param upd Object to update