Check inventory has space before adding bitcoins from craft

This commit is contained in:
Dev 2024-01-31 23:39:01 +00:00
parent 0973c1cdad
commit 69735b5037
2 changed files with 86 additions and 11 deletions

View File

@ -969,19 +969,34 @@ export class HideoutHelper
return this.httpResponse.appendErrorToOutput(output, errorMsg); return this.httpResponse.appendErrorToOutput(output, errorMsg);
} }
// Add each coin individually to player const itemsToAdd: Item[][] = [];
for (let index = 0; index < craftedCoinCount; index++) for (let index = 0; index < craftedCoinCount; index++)
{ {
const request = { itemsToAdd.push(
itemWithModsToAdd: [ [{
{
_id: this.hashUtil.generate(), _id: this.hashUtil.generate(),
_tpl: HideoutHelper.bitcoinTpl, _tpl: HideoutHelper.bitcoinTpl,
upd: { upd: {
StackObjectsCount: 1 StackObjectsCount: 1
} }
}]
)
} }
],
// Add each coin individually to player
if (!this.inventoryHelper.canPlaceItemsInInventory(sessionId, itemsToAdd))
{
// no space, exit
return this.httpResponse.appendErrorToOutput(
output,
this.localisationService.getText("inventory-no_stash_space"),
)
}
for (const itemToAdd of itemsToAdd)
{
const request: IAddItemDirectRequest = {
itemWithModsToAdd: itemToAdd,
foundInRaid: true, foundInRaid: true,
useSortingTable: false, useSortingTable: false,
callback: null callback: null

View File

@ -498,6 +498,66 @@ export class InventoryHelper
} }
} }
public canPlaceItemsInInventory(
sessionId: string,
itemsWithChildren: Item[][]
): boolean
{
const pmcData = this.profileHelper.getPmcProfile(sessionId);
const stashFS2D = this.getStashSlotMap(pmcData, sessionId);
for (const itemWithChildren of itemsWithChildren)
{
if (this.canPlaceItemInInventory(stashFS2D, itemWithChildren))
{
return false;
}
}
return true;
}
public canPlaceItemInInventory(
stashFS2D: number[][],
itemWithChildren: Item[]
): boolean
{
// Get x/y size of item
const rootItem = itemWithChildren[0];
const itemSize = this.getItemSize(rootItem._tpl, rootItem._id, itemWithChildren);
// Look for a place to slot item into
const findSlotResult = this.containerHelper.findSlotForItem(stashFS2D, itemSize[0], itemSize[1]);
if (findSlotResult.success)
{
/* Fill in the StashFS_2D with an imaginary item, to simulate it already being added
* so the next item to search for a free slot won't find the same one */
const itemSizeX = findSlotResult.rotation ? itemSize[1] : itemSize[0];
const itemSizeY = findSlotResult.rotation ? itemSize[0] : itemSize[1];
try
{
stashFS2D = this.containerHelper.fillContainerMapWithItem(
stashFS2D,
findSlotResult.x,
findSlotResult.y,
itemSizeX,
itemSizeY,
false,
); // TODO: rotation not passed in, bad?
}
catch (err)
{
return false;
}
// Success! exit
return;
}
return true;
}
protected placeItemInInventory( protected placeItemInInventory(
stashFS2D: number[][], stashFS2D: number[][],
sortingTableFS2D: number[][], sortingTableFS2D: number[][],