Updated foldItem() to use a .find() call instead of looping over all inventory items

This commit is contained in:
Dev 2024-02-25 11:43:49 +00:00
parent 2839ce8c77
commit 5f8b0592cc

View File

@ -443,32 +443,34 @@ export class InventoryController
/** /**
* Handles folding of Weapons * 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; let playerData = pmcData;
// Fix for folding weapons while on they're in the Scav inventory // We may be folding data on scav profile, get that profile instead
if (body.fromOwner && body.fromOwner.type === "Profile" && body.fromOwner.id !== playerData._id) if (request.fromOwner && request.fromOwner.type === "Profile" && request.fromOwner.id !== playerData._id)
{ {
playerData = this.profileHelper.getScavProfile(sessionID); playerData = this.profileHelper.getScavProfile(sessionID);
} }
// TODO - replace with single .find() call const itemToFold = playerData.Inventory.items.find((item) => item?._id === request.item);
for (const item of playerData.Inventory.items) if (!itemToFold)
{ {
if (item._id && item._id === body.item) // Item not found
{ this.logger.warning(`Unable to fold item: ${request.item}. Not found`);
if (!item.upd) return { warnings: [], profileChanges: {} };
{
item.upd = {};
}
item.upd.Foldable = { Folded: body.value };
return this.eventOutputHolder.getOutput(sessionID);
}
} }
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);
} }
/** /**