From 020cfa7ab8426c21e336e6e13881f270f2ead429 Mon Sep 17 00:00:00 2001 From: Chomp Date: Thu, 3 Jul 2025 13:13:59 +0100 Subject: [PATCH] Cleaned up `GetStashSlotMap` and related sub functions --- .../Helpers/InventoryHelper.cs | 63 ++++++++++--------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs index 83490bf5..c172cdeb 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs @@ -108,7 +108,7 @@ public class InventoryHelper( var itemWithModsToAddClone = _cloner.Clone(request.ItemWithModsToAdd); // Get stash layouts ready for use - var stashFS2D = GetStashSlotMap(pmcData, sessionId); + var stashFS2D = GetStashSlotMap(pmcData); if (stashFS2D is null) { _logger.Error($"Unable to get stash map for players: {sessionId} stash"); @@ -224,7 +224,7 @@ public class InventoryHelper( { var pmcData = _profileHelper.GetPmcProfile(sessionId); - var stashFS2D = _cloner.Clone(GetStashSlotMap(pmcData, sessionId)); + var stashFS2D = _cloner.Clone(GetStashSlotMap(pmcData)); if (stashFS2D is null) { _logger.Error($"Unable to get stash map for players: {sessionId} stash"); @@ -370,7 +370,7 @@ public class InventoryHelper( /// /// Find a location to place an item into inventory and place it /// - /// 2-dimensional representation of the container slots + /// 2-dimensional representation of the container /// 2-dimensional representation of the sorting table slots /// Item to place with children /// Players inventory @@ -1055,14 +1055,13 @@ public class InventoryHelper( /// 0 value = free, 1 = taken /// /// Player profile - /// session id /// 2-dimensional array - protected int[][]? GetStashSlotMap(PmcData pmcData, string sessionID) + protected int[][] GetStashSlotMap(PmcData pmcData) { - var playerStashSize = GetPlayerStashSize(sessionID); + var (horizontal, vertical) = GetPlayerStashSize(pmcData); return GetContainerMap( - playerStashSize[0], - playerStashSize[1], + horizontal, + vertical, pmcData.Inventory.Items, pmcData.Inventory.Stash ); @@ -1097,57 +1096,59 @@ public class InventoryHelper( /// /// Get Players Stash Size /// - /// Players id - /// Dictionary of 2 values, horizontal and vertical stash size - protected List GetPlayerStashSize(string sessionId) + /// Profile to get stash size of + /// Horizontal and vertical size of stash + protected (int, int) GetPlayerStashSize(PmcData pmcData) { - var profile = _profileHelper.GetPmcProfile(sessionId); - var stashRowBonus = profile.Bonuses.FirstOrDefault(bonus => - bonus.Type == BonusType.StashRows - ); - - // this sets automatically a stash size from items.json (it's not added anywhere yet because we still use base stash) - var stashTPL = GetStashType(sessionId); - if (stashTPL is null) + // TODO: what?? + // This sets automatically a stash size from items.json (it's not added anywhere yet because we still use base stash) + var stashTpl = GetProfileStashTpl(pmcData); + if (stashTpl is null) { _logger.Error(_serverLocalisationService.GetText("inventory-missing_stash_size")); + + return (0, 0); } - var stashItemResult = _itemHelper.GetItem(stashTPL); - if (!stashItemResult.Key) + // Look up details of stash in db + var (isValidItem, stashItemDbItem) = _itemHelper.GetItem(stashTpl); + if (!isValidItem) { _logger.Error( - _serverLocalisationService.GetText("inventory-stash_not_found", stashTPL) + _serverLocalisationService.GetText("inventory-stash_not_found", stashTpl) ); - return new List(); + return (0, 0); } - var stashItemDetails = stashItemResult.Value; - var firstStashItemGrid = stashItemDetails.Properties.Grids[0]; + // Find the main 'grid' of the stash we can use to get size + var firstStashItemGrid = stashItemDbItem?.Properties?.Grids?.FirstOrDefault(); + // Get horizontal and vertical size var stashH = firstStashItemGrid.Props.CellsH != 0 ? firstStashItemGrid.Props.CellsH : 10; var stashV = firstStashItemGrid.Props.CellsV != 0 ? firstStashItemGrid.Props.CellsV : 66; // Player has a bonus, apply to vertical size + var stashRowBonus = pmcData.Bonuses.FirstOrDefault(bonus => + bonus.Type == BonusType.StashRows + ); if (stashRowBonus is not null) { stashV += (int)stashRowBonus.Value; } - return [stashH.Value, stashV.Value]; + return (stashH.Value, stashV.Value); } /// /// Get the players stash items tpl /// - /// Player id + /// Profile to get tpl /// Stash tpl - protected string? GetStashType(string sessionId) + protected string? GetProfileStashTpl(PmcData profile) { - var pmcData = _profileHelper.GetPmcProfile(sessionId); - var stashObj = pmcData.Inventory.Items.FirstOrDefault(item => - item.Id == pmcData.Inventory.Stash + var stashObj = profile.Inventory.Items.FirstOrDefault(item => + item.Id == profile.Inventory.Stash ); if (stashObj is null) {