0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-13 08:50:43 -05:00

Cleaned up various functions to improve readability

Removed unused function `splitStackIntoSmallerChildStacks()`
This commit is contained in:
Dev 2024-06-30 20:38:23 +01:00
parent 625bb1aebd
commit 15eb6eb69f
2 changed files with 35 additions and 99 deletions

View File

@ -1203,16 +1203,18 @@ export class HideoutController
*/ */
public recordShootingRangePoints(sessionId: string, pmcData: IPmcData, request: IRecordShootingRangePoints): void public recordShootingRangePoints(sessionId: string, pmcData: IPmcData, request: IRecordShootingRangePoints): void
{ {
// Check if counter exists, add placeholder if it doesnt const shootingRangeKey = "ShootingRangePoints";
if (!pmcData.Stats.Eft.OverallCounters.Items.some((counter) => counter.Key.includes("ShootingRangePoints"))) const overallCounterItems = pmcData.Stats.Eft.OverallCounters.Items;
// Find counter by key
let shootingRangeHighScore = overallCounterItems.find((counter) => counter.Key.includes(shootingRangeKey));
if (!shootingRangeHighScore)
{ {
pmcData.Stats.Eft.OverallCounters.Items.push({ Key: ["ShootingRangePoints"], Value: 0 }); // Counter not found, add blank one
overallCounterItems.push({ Key: [shootingRangeKey], Value: 0 });
shootingRangeHighScore = overallCounterItems.find((counter) => counter.Key.includes(shootingRangeKey));
} }
// Find counter by key and update value
const shootingRangeHighScore = pmcData.Stats.Eft.OverallCounters.Items
.find((counter) => counter.Key.includes("ShootingRangePoints"),
);
shootingRangeHighScore.Value = request.points; shootingRangeHighScore.Value = request.points;
} }

View File

@ -10,9 +10,7 @@ import { IPmcData } from "@spt/models/eft/common/IPmcData";
import { Inventory } from "@spt/models/eft/common/tables/IBotBase"; import { Inventory } from "@spt/models/eft/common/tables/IBotBase";
import { Item, Location, Upd } from "@spt/models/eft/common/tables/IItem"; import { Item, Location, Upd } from "@spt/models/eft/common/tables/IItem";
import { IAddItemDirectRequest } from "@spt/models/eft/inventory/IAddItemDirectRequest"; import { IAddItemDirectRequest } from "@spt/models/eft/inventory/IAddItemDirectRequest";
import { AddItem } from "@spt/models/eft/inventory/IAddItemRequestData";
import { IAddItemsDirectRequest } from "@spt/models/eft/inventory/IAddItemsDirectRequest"; import { IAddItemsDirectRequest } from "@spt/models/eft/inventory/IAddItemsDirectRequest";
import { IAddItemTempObject } from "@spt/models/eft/inventory/IAddItemTempObject";
import { IInventoryMergeRequestData } from "@spt/models/eft/inventory/IInventoryMergeRequestData"; import { IInventoryMergeRequestData } from "@spt/models/eft/inventory/IInventoryMergeRequestData";
import { IInventoryMoveRequestData } from "@spt/models/eft/inventory/IInventoryMoveRequestData"; import { IInventoryMoveRequestData } from "@spt/models/eft/inventory/IInventoryMoveRequestData";
import { IInventoryRemoveRequestData } from "@spt/models/eft/inventory/IInventoryRemoveRequestData"; import { IInventoryRemoveRequestData } from "@spt/models/eft/inventory/IInventoryRemoveRequestData";
@ -363,8 +361,8 @@ export class InventoryHelper
* Find a location to place an item into inventory and place it * Find a location to place an item into inventory and place it
* @param stashFS2D 2-dimensional representation of the container slots * @param stashFS2D 2-dimensional representation of the container slots
* @param sortingTableFS2D 2-dimensional representation of the sorting table slots * @param sortingTableFS2D 2-dimensional representation of the sorting table slots
* @param itemWithChildren Item to place * @param itemWithChildren Item to place with children
* @param playerInventory * @param playerInventory Players inventory
* @param useSortingTable Should sorting table to be used if main stash has no space * @param useSortingTable Should sorting table to be used if main stash has no space
* @param output output to send back to client * @param output output to send back to client
*/ */
@ -398,13 +396,7 @@ export class InventoryHelper
} }
catch (err) catch (err)
{ {
const errorText = typeof err === "string" ? ` -> ${err}` : err.message; handleContainerPlacementError(err, output);
this.logger.error(this.localisationService.getText("inventory-fill_container_failed", errorText));
this.httpResponse.appendErrorToOutput(
output,
this.localisationService.getText("inventory-no_stash_space"),
);
return; return;
} }
@ -444,13 +436,7 @@ export class InventoryHelper
} }
catch (err) catch (err)
{ {
const errorText = typeof err === "string" ? ` -> ${err}` : ""; handleContainerPlacementError(err, output);
this.logger.error(this.localisationService.getText("inventory-fill_container_failed", errorText));
this.httpResponse.appendErrorToOutput(
output,
this.localisationService.getText("inventory-no_stash_space"),
);
return; return;
} }
@ -474,76 +460,16 @@ export class InventoryHelper
return; return;
} }
}
/** function handleContainerPlacementError(err: any, output: IItemEventRouterResponse): void
* Split an items stack size based on its StackMaxSize value
* @param assortItems Items to add to inventory
* @param requestItem Details of purchased item to add to inventory
* @param result Array split stacks are appended to
*/
protected splitStackIntoSmallerChildStacks(
assortItems: Item[],
requestItem: AddItem,
result: IAddItemTempObject[],
): void
{
for (const item of assortItems)
{ {
// Iterated item matches root item const errorText = typeof err === "string" ? ` -> ${err}` : err.message;
if (item._id === requestItem.item_id) this.logger.error(this.localisationService.getText("inventory-fill_container_failed", errorText));
{
// Get item details from db
const itemDetails = this.itemHelper.getItem(item._tpl)[1];
const itemToAdd: IAddItemTempObject = {
itemRef: item,
count: requestItem.count,
isPreset: !!requestItem.sptIsPreset,
};
// Split stacks if the size is higher than allowed by items StackMaxSize property this.httpResponse.appendErrorToOutput(
let maxStackCount = 1; output,
if (requestItem.count > itemDetails._props.StackMaxSize) this.localisationService.getText("inventory-no_stash_space"),
{ );
let remainingCountOfItemToAdd = requestItem.count;
const calc
= requestItem.count
- Math.floor(requestItem.count / itemDetails._props.StackMaxSize)
* itemDetails._props.StackMaxSize;
maxStackCount
= calc > 0
? maxStackCount + Math.floor(remainingCountOfItemToAdd / itemDetails._props.StackMaxSize)
: Math.floor(remainingCountOfItemToAdd / itemDetails._props.StackMaxSize);
// Iterate until totalCountOfPurchasedItem is 0
for (let i = 0; i < maxStackCount; i++)
{
// Keep splitting items into stacks until none left
if (remainingCountOfItemToAdd > 0)
{
const newChildItemToAdd = this.cloner.clone(itemToAdd);
if (remainingCountOfItemToAdd > itemDetails._props.StackMaxSize)
{
// Reduce total count of item purchased by stack size we're going to add to inventory
remainingCountOfItemToAdd -= itemDetails._props.StackMaxSize;
newChildItemToAdd.count = itemDetails._props.StackMaxSize;
}
else
{
newChildItemToAdd.count = remainingCountOfItemToAdd;
}
result.push(newChildItemToAdd);
}
}
}
else
{
// Item count is within allowed stack size, just add it
result.push(itemToAdd);
}
}
} }
} }
@ -586,12 +512,11 @@ export class InventoryHelper
// We expect that each inventory item and each insured item has unique "_id", respective "itemId". // We expect that each inventory item and each insured item has unique "_id", respective "itemId".
// Therefore we want to use a NON-Greedy function and escape the iteration as soon as we find requested item. // Therefore we want to use a NON-Greedy function and escape the iteration as soon as we find requested item.
const inventoryIndex = inventoryItems.findIndex((item) => item._id === childId); const inventoryIndex = inventoryItems.findIndex((item) => item._id === childId);
if (inventoryIndex > -1) if (inventoryIndex !== -1)
{ {
inventoryItems.splice(inventoryIndex, 1); inventoryItems.splice(inventoryIndex, 1);
} }
else
if (inventoryIndex === -1)
{ {
this.logger.warning(this.localisationService.getText("inventory-unable_to_remove_item_id_not_found", this.logger.warning(this.localisationService.getText("inventory-unable_to_remove_item_id_not_found",
{ {
@ -601,7 +526,7 @@ export class InventoryHelper
} }
const insuredIndex = insuredItems.findIndex((item) => item.itemId === childId); const insuredIndex = insuredItems.findIndex((item) => item.itemId === childId);
if (insuredIndex > -1) if (insuredIndex !== -1)
{ {
insuredItems.splice(insuredIndex, 1); insuredItems.splice(insuredIndex, 1);
} }
@ -724,8 +649,14 @@ export class InventoryHelper
return this.getSizeByInventoryItemHash(itemTpl, itemID, this.getInventoryItemHash(inventoryItems)); return this.getSizeByInventoryItemHash(itemTpl, itemID, this.getInventoryItemHash(inventoryItems));
} }
// note from 2027: there IS a thing i didn't explore and that is Merges With Children /**
// -> Prepares item Width and height returns [sizeX, sizeY] * Calculates the size of an item including attachements
* takes into account if item is folded
* @param itemTpl Items template id
* @param itemID Items id
* @param inventoryItemHash Hashmap of inventory items
* @returns An array representing the [width, height] of the item
*/
protected getSizeByInventoryItemHash( protected getSizeByInventoryItemHash(
itemTpl: string, itemTpl: string,
itemID: string, itemID: string,
@ -757,7 +688,7 @@ export class InventoryHelper
// return default size of 1x1 // return default size of 1x1
this.logger.error(this.localisationService.getText("inventory-return_default_size", itemTpl)); this.logger.error(this.localisationService.getText("inventory-return_default_size", itemTpl));
return [1, 1]; return [1, 1]; // Invalid input data, return defaults
} }
const rootItem = inventoryItemHash.byItemId[itemID]; const rootItem = inventoryItemHash.byItemId[itemID];
@ -775,6 +706,8 @@ export class InventoryHelper
let forcedRight = 0; let forcedRight = 0;
let outX = tmpItem._props.Width; let outX = tmpItem._props.Width;
const outY = tmpItem._props.Height; const outY = tmpItem._props.Height;
// Item types to ignore
const skipThisItems: string[] = [ const skipThisItems: string[] = [
BaseClasses.BACKPACK, BaseClasses.BACKPACK,
BaseClasses.SEARCHABLE_ITEM, BaseClasses.SEARCHABLE_ITEM,
@ -788,6 +721,7 @@ export class InventoryHelper
outX -= tmpItem._props.SizeReduceRight; outX -= tmpItem._props.SizeReduceRight;
} }
// Calculate size contribution from child items/attachments
if (!skipThisItems.includes(tmpItem._parent)) if (!skipThisItems.includes(tmpItem._parent))
{ {
while (toDo.length > 0) while (toDo.length > 0)