From 5f8b0592ccae3f50422909eba9b330325ed7752e Mon Sep 17 00:00:00 2001 From: Dev Date: Sun, 25 Feb 2024 11:43:49 +0000 Subject: [PATCH] Updated `foldItem()` to use a `.find()` call instead of looping over all inventory items --- .../src/controllers/InventoryController.ts | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/project/src/controllers/InventoryController.ts b/project/src/controllers/InventoryController.ts index c9c148e5..8a01e519 100644 --- a/project/src/controllers/InventoryController.ts +++ b/project/src/controllers/InventoryController.ts @@ -443,32 +443,34 @@ export class InventoryController /** * Handles folding of Weapons */ - public foldItem(pmcData: IPmcData, body: IInventoryFoldRequestData, sessionID: string): IItemEventRouterResponse + public foldItem(pmcData: IPmcData, request: IInventoryFoldRequestData, sessionID: string): IItemEventRouterResponse { + // May need to reassign to scav profile let playerData = pmcData; - // Fix for folding weapons while on they're in the Scav inventory - if (body.fromOwner && body.fromOwner.type === "Profile" && body.fromOwner.id !== playerData._id) + // We may be folding data on scav profile, get that profile instead + if (request.fromOwner && request.fromOwner.type === "Profile" && request.fromOwner.id !== playerData._id) { playerData = this.profileHelper.getScavProfile(sessionID); } - // TODO - replace with single .find() call - for (const item of playerData.Inventory.items) + const itemToFold = playerData.Inventory.items.find((item) => item?._id === request.item); + if (!itemToFold) { - if (item._id && item._id === body.item) - { - if (!item.upd) - { - item.upd = {}; - } - - item.upd.Foldable = { Folded: body.value }; - return this.eventOutputHolder.getOutput(sessionID); - } + // Item not found + this.logger.warning(`Unable to fold item: ${request.item}. Not found`); + return { warnings: [], profileChanges: {} }; } - return { warnings: [], profileChanges: {} }; + // Item may not have upd object + if (!itemToFold.upd) + { + itemToFold.upd = {}; + } + + itemToFold.upd.Foldable = { Folded: request.value }; + + return this.eventOutputHolder.getOutput(sessionID); } /**