Skip to content

Commit e1fb906

Browse files
author
Chomp
committed
Optimised PrioritiseStashSort to reduce iterating over list, reduces time taken to purchase items when player has a LOT of money stacks in stash
1 parent ee186fd commit e1fb906

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Libraries/SPTarkov.Server.Core/Services/PaymentService.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,9 @@ protected List<Item> GetSortedMoneyItemsInInventory(PmcData pmcData, MongoId cur
370370
moneyItemsInInventory = noLocked.ToList();
371371
}
372372

373-
// Prioritise items in stash to top of array
374-
moneyItemsInInventory.Sort((a, b) => PrioritiseStashSort(a, b, pmcData.Inventory.Items, itemsInStashCache));
373+
// Sort money stacks to prioritise items in stash and not in secure to top of array
374+
var inventoryParent = pmcData.Inventory.Items.ToDictionary(item => item.Id.ToString(), item => item.Template);
375+
moneyItemsInInventory.Sort((a, b) => PrioritiseStashSort(a, b, inventoryParent, itemsInStashCache));
375376

376377
return moneyItemsInInventory;
377378
}
@@ -401,13 +402,13 @@ protected IReadOnlyDictionary<MongoId, InventoryLocation> GetItemInStashCache(Li
401402
/// </summary>
402403
/// <param name="a"> First money stack item </param>
403404
/// <param name="b"> Second money stack item </param>
404-
/// <param name="inventoryItems"> Players inventory items</param>
405+
/// <param name="itemIdToTplCache"> item id (as string) and template id KvP</param>
405406
/// <param name="itemInStashCache">Cache of item IDs and if they're in stash</param>
406407
/// <returns> Sort order, -1 if A has priority, 1 if B has priority, 0 if they match </returns>
407408
protected int PrioritiseStashSort(
408409
Item a,
409410
Item b,
410-
List<Item> inventoryItems,
411+
Dictionary<string, MongoId> itemIdToTplCache,
411412
IReadOnlyDictionary<MongoId, InventoryLocation> itemInStashCache
412413
)
413414
{
@@ -441,11 +442,14 @@ IReadOnlyDictionary<MongoId, InventoryLocation> itemInStashCache
441442
if (aInContainer && bInContainer)
442443
{
443444
// Containers where taking money from would inconvenience player
444-
var aImmediateParent = inventoryItems.FirstOrDefault(item => item.Id == a.ParentId);
445-
var bImmediateParent = inventoryItems.FirstOrDefault(item => item.Id == b.ParentId);
446445

447-
var aInDeprioContainer = InventoryConfig.DeprioritisedMoneyContainers.Contains(aImmediateParent.Template);
448-
var bInDeprioContainer = InventoryConfig.DeprioritisedMoneyContainers.Contains(bImmediateParent.Template);
446+
// Get template Id of items' parent so we can see if items in a container we want to de prioritise
447+
var aImmediateParentTemplate = itemIdToTplCache.FirstOrDefault(item => string.Equals(item.Key, a.ParentId, StringComparison.OrdinalIgnoreCase));
448+
var bImmediateParentTemplate = itemIdToTplCache.FirstOrDefault(item => string.Equals(item.Key, b.ParentId, StringComparison.OrdinalIgnoreCase));
449+
450+
// e.g. secure container
451+
var aInDeprioContainer = InventoryConfig.DeprioritisedMoneyContainers.Contains(aImmediateParentTemplate.Value);
452+
var bInDeprioContainer = InventoryConfig.DeprioritisedMoneyContainers.Contains(bImmediateParentTemplate.Value);
449453

450454
// Prioritize B
451455
if (!aInDeprioContainer && bInDeprioContainer)

0 commit comments

Comments
 (0)