From e84781be5f17a90655280857a47244f73b831c03 Mon Sep 17 00:00:00 2001 From: Dev Date: Sun, 23 Jul 2023 11:51:04 +0100 Subject: [PATCH] Handle moving items into sorting table when stash has no space --- project/src/helpers/InventoryHelper.ts | 59 ++++++++++++++----- .../eft/inventory/IAddItemTempObject.ts | 2 + 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/project/src/helpers/InventoryHelper.ts b/project/src/helpers/InventoryHelper.ts index 6ae5c9ff..209742a6 100644 --- a/project/src/helpers/InventoryHelper.ts +++ b/project/src/helpers/InventoryHelper.ts @@ -101,7 +101,7 @@ export class InventoryHelper return this.httpResponse.appendErrorToOutput(output, message); } - // handle when item being bought is a preset + // Handle when item being bought is a preset const item = fenceItems[itemIndex]; if (item.upd?.sptPresetId) { @@ -137,6 +137,7 @@ export class InventoryHelper // Find an empty slot in stash for each of the items being added let stashFS2D = this.getStashSlotMap(pmcData, sessionID); + let sortingTableFS2D = this.getStashSlotMap(pmcData, sessionID); for (const itemToAdd of itemsToAdd) { @@ -161,7 +162,8 @@ export class InventoryHelper return this.httpResponse.appendErrorToOutput(output, this.localisationService.getText("inventory-no_stash_space")); } - + // Store details for object, incuding container item will be placed in + itemToAdd.containerId = pmcData.Inventory.stash; itemToAdd.location = { x: findSlotResult.x, y: findSlotResult.y, @@ -170,7 +172,28 @@ export class InventoryHelper } else { - return this.httpResponse.appendErrorToOutput(output, this.localisationService.getText("inventory-no_stash_space")); + const findStashSlotResult = this.containerHelper.findSlotForItem(stashFS2D, itemSize[0], itemSize[1]); + const itemSizeX = findStashSlotResult.rotation ? itemSize[1] : itemSize[0]; + const itemSizeY = findStashSlotResult.rotation ? itemSize[0] : itemSize[1]; + try + { + sortingTableFS2D = this.containerHelper.fillContainerMapWithItem(sortingTableFS2D, findStashSlotResult.x, findStashSlotResult.y, itemSizeX, itemSizeY, false); // TODO: rotation not passed in, bad? + } + catch (err) + { + const errorText = typeof err === "string" ? ` -> ${err}` : ""; + this.logger.error(this.localisationService.getText("inventory-fill_container_failed", errorText)); + + return this.httpResponse.appendErrorToOutput(output, this.localisationService.getText("inventory-no_stash_space")); + } + + // Store details for object, incuding container item will be placed in + itemToAdd.containerId = pmcData.Inventory.sortingTable; + itemToAdd.location = { + x: findStashSlotResult.x, + y: findStashSlotResult.y, + r: findStashSlotResult.rotation ? 1 : 0, + rotation: findStashSlotResult.rotation}; } } @@ -235,7 +258,7 @@ export class InventoryHelper output.profileChanges[sessionID].items.new.push({ _id: idForItemToAdd, _tpl: itemToAdd.itemRef._tpl, - parentId: pmcData.Inventory.stash, + parentId: itemToAdd.containerId, slotId: "hideout", location: { x: itemToAdd.location.x, y: itemToAdd.location.y, r: itemToAdd.location.rotation ? 1 : 0 }, upd: upd @@ -244,7 +267,7 @@ export class InventoryHelper pmcData.Inventory.items.push({ _id: idForItemToAdd, _tpl: itemToAdd.itemRef._tpl, - parentId: pmcData.Inventory.stash, + parentId: itemToAdd.containerId, slotId: "hideout", location: { x: itemToAdd.location.x, y: itemToAdd.location.y, r: itemToAdd.location.rotation ? 1 : 0 }, upd: this.jsonUtil.clone(upd) // Clone upd to prevent multi-purchases of same item referencing same upd object in memory @@ -785,20 +808,11 @@ export class InventoryHelper return this.getContainerMap(playerStashSize[0], playerStashSize[1], pmcData.Inventory.items, pmcData.Inventory.stash); } - protected getStashType(sessionID: string): string + protected getSortingTableSlotMap(pmcData: IPmcData): number[][] { - const pmcData = this.profileHelper.getPmcProfile(sessionID); - const stashObj = pmcData.Inventory.items.find(item => item._id === pmcData.Inventory.stash); - if (!stashObj) - { - this.logger.error(this.localisationService.getText("inventory-unable_to_find_stash")); - - return ""; - } - return stashObj._tpl; + return this.getContainerMap(10, 45, pmcData.Inventory.items, pmcData.Inventory.stash); } - /* Get Player Stash Proper Size * input: null * output: [stashSizeWidth, stashSizeHeight] @@ -816,6 +830,19 @@ export class InventoryHelper return [stashX, stashY]; } + protected getStashType(sessionID: string): string + { + const pmcData = this.profileHelper.getPmcProfile(sessionID); + const stashObj = pmcData.Inventory.items.find(item => item._id === pmcData.Inventory.stash); + if (!stashObj) + { + this.logger.error(this.localisationService.getText("inventory-unable_to_find_stash")); + + return ""; + } + return stashObj._tpl; + } + /** * Internal helper function to transfer an item from one profile to another. * fromProfileData: Profile of the source. diff --git a/project/src/models/eft/inventory/IAddItemTempObject.ts b/project/src/models/eft/inventory/IAddItemTempObject.ts index 81f52c6a..c992fd70 100644 --- a/project/src/models/eft/inventory/IAddItemTempObject.ts +++ b/project/src/models/eft/inventory/IAddItemTempObject.ts @@ -6,4 +6,6 @@ export interface IAddItemTempObject count: number isPreset: boolean location?: Location + // Container item will be placed in - stash or sorting table + containerId?: string } \ No newline at end of file