From 2cb720ab94722667de6eeb5e0c5242cb378eb473 Mon Sep 17 00:00:00 2001 From: Chomp Date: Mon, 2 Jun 2025 16:23:45 +0100 Subject: [PATCH] Added early return when result is 0 items Added early return when 'type' of loot is unknown --- .../Services/BotLootCacheService.cs | 66 +++++++++++-------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs index 09cd4890..a810f6da 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs @@ -40,7 +40,7 @@ public class BotLootCacheService( } /// - /// Get the fully created loot array, ordered by price low to high + /// Get a dictionary of lootable item Tpls with their corresponding weight /// /// bot to get loot for /// is the bot a pmc @@ -68,7 +68,7 @@ public class BotLootCacheService( return []; } - Dictionary result = null; + Dictionary result; switch (lootType) { case LootCacheType.Special: @@ -122,37 +122,47 @@ public class BotLootCacheService( } ) ); - break; + + return []; } - if (itemPriceMinMax is not null) + if (!result.Any()) { - var filteredResult = result.Where(i => - { - var itemPrice = _itemHelper.GetItemPrice(i.Key); - if (itemPriceMinMax?.Min is not null && itemPriceMinMax?.Max is not null) - { - return itemPrice >= itemPriceMinMax?.Min && itemPrice <= itemPriceMinMax?.Max; - } - - if (itemPriceMinMax?.Min is not null && itemPriceMinMax?.Max is null) - { - return itemPrice >= itemPriceMinMax?.Min; - } - - if (itemPriceMinMax?.Min is null && itemPriceMinMax?.Max is not null) - { - return itemPrice <= itemPriceMinMax?.Max; - } - - return false; - } - ); - - return _cloner.Clone(filteredResult.ToDictionary(pair => pair.Key, pair => pair.Value)); + // No loot, exit + return result; } - return _cloner.Clone(result); + if (itemPriceMinMax is null) + { + // No filtering requested, exit + return _cloner.Clone(result); + } + + // Filter the loot pool prior to returning + var filteredResult = result.Where(i => + { + var itemPrice = _itemHelper.GetItemPrice(i.Key); + if (itemPriceMinMax?.Min is not null && itemPriceMinMax?.Max is not null) + { + return itemPrice >= itemPriceMinMax?.Min && itemPrice <= itemPriceMinMax?.Max; + } + + if (itemPriceMinMax?.Min is not null && itemPriceMinMax?.Max is null) + { + return itemPrice >= itemPriceMinMax?.Min; + } + + if (itemPriceMinMax?.Min is null && itemPriceMinMax?.Max is not null) + { + return itemPrice <= itemPriceMinMax?.Max; + } + + return false; + } + ); + + return _cloner.Clone(filteredResult.ToDictionary(pair => pair.Key, pair => pair.Value)); + } ///