From a5d9a54c7fedf941046d50cdf97bc648618d76e1 Mon Sep 17 00:00:00 2001 From: Chomp Date: Mon, 2 Jun 2025 16:59:53 +0100 Subject: [PATCH] made use of `FilterItemPool` helper method to reduce code verbosity Fixed inverted trygetvalue check --- .../Services/BotLootCacheService.cs | 115 +++++++----------- 1 file changed, 47 insertions(+), 68 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs index 7a2ca6e9..a458832a 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs @@ -376,61 +376,26 @@ public class BotLootCacheService( } // Get backpack loot (excluding magazines, bullets, grenades, drink, food and healing/stim items) - var filteredBackpackItems = new Dictionary(); - foreach (var itemKvP in backpackLootPool) - { - var itemResult = _itemHelper.GetItem(itemKvP.Key); - if (itemResult.Value is null) - { - continue; - } - - var itemTemplate = itemResult.Value; - if ( - IsBulletOrGrenade(itemTemplate.Properties) || - IsMagazine(itemTemplate.Properties) || - IsMedicalItem(itemTemplate.Properties) || - IsGrenade(itemTemplate.Properties) || - IsFood(itemTemplate.Id) || - IsDrink(itemTemplate.Id) || - IsCurrency(itemTemplate.Id) - ) - // Is type we don't want as backpack loot, skip - { - continue; - } - - filteredBackpackItems.TryAdd(itemKvP.Key, itemKvP.Value); - } + var filteredBackpackItems = FilterItemPool(backpackLootPool, (itemTemplate) => + IsBulletOrGrenade(itemTemplate.Properties) || + IsMagazine(itemTemplate.Properties) || + IsMedicalItem(itemTemplate.Properties) || + IsGrenade(itemTemplate.Properties) || + IsFood(itemTemplate.Id) || + IsDrink(itemTemplate.Id) || + IsCurrency(itemTemplate.Id)); // Get pocket loot (excluding magazines, bullets, grenades, drink, food medical and healing/stim items) - var filteredPocketItems = new Dictionary(); - foreach (var itemKvP in pocketLootPool) - { - var itemResult = _itemHelper.GetItem(itemKvP.Key); - if (itemResult.Value is null) - { - continue; - } - - var itemTemplate = itemResult.Value; - if ( - IsBulletOrGrenade(itemTemplate.Properties) || - IsMagazine(itemTemplate.Properties) || - IsMedicalItem(itemTemplate.Properties) || - IsGrenade(itemTemplate.Properties) || - IsFood(itemTemplate.Id) || - IsDrink(itemTemplate.Id) || - IsCurrency(itemTemplate.Id) || - itemTemplate.Properties.Height is null || // lacks height - itemTemplate.Properties.Width is null // lacks width - ) - { - continue; - } - - filteredPocketItems.TryAdd(itemKvP.Key, itemKvP.Value); - } + var filteredPocketItems = FilterItemPool(pocketLootPool, (itemTemplate) => + IsBulletOrGrenade(itemTemplate.Properties) || + IsMagazine(itemTemplate.Properties) || + IsMedicalItem(itemTemplate.Properties) || + IsGrenade(itemTemplate.Properties) || + IsFood(itemTemplate.Id) || + IsDrink(itemTemplate.Id) || + IsCurrency(itemTemplate.Id) || + itemTemplate.Properties.Height is null || // lacks height + itemTemplate.Properties.Width is null); // lacks width // Get vest loot (excluding magazines, bullets, grenades, medical and healing/stim items) var filteredVestItems = new Dictionary(); @@ -460,25 +425,17 @@ public class BotLootCacheService( } // Get secure loot (excluding magazines, bullets) - var filteredSecureLoot = new Dictionary(); - foreach (var itemKvP in secureLootPool) + var filteredSecureLoot = FilterItemPool(secureLootPool, (itemTemplate) => + IsBulletOrGrenade(itemTemplate.Properties) || + IsMagazine(itemTemplate.Properties)); + + if(!_lootCache.TryGetValue(botRole, out var cacheForRole)) { - var itemResult = _itemHelper.GetItem(itemKvP.Key); - if (itemResult.Value is null) - { - continue; - } + _logger.Error($"Unable to get loot cache value using key: {botRole}"); - var itemTemplate = itemResult.Value; - if (IsBulletOrGrenade(itemTemplate.Properties) || IsMagazine(itemTemplate.Properties)) - { - continue; - } - - filteredSecureLoot.TryAdd(itemKvP.Key, itemKvP.Value); + return; } - var cacheForRole = _lootCache[botRole]; cacheForRole.HealingItems = healingItemsInWhitelist; cacheForRole.DrugItems = drugItemsInWhitelist; cacheForRole.FoodItems = foodItemsInWhitelist; @@ -493,6 +450,28 @@ public class BotLootCacheService( cacheForRole.SecureLoot = filteredSecureLoot; } + protected Dictionary FilterItemPool(Dictionary lootPool, Func shouldBeSkipped) + { + var filteredItems = new Dictionary(); + foreach (var itemKvP in lootPool) + { + var (isValidItem, itemTemplate) = _itemHelper.GetItem(itemKvP.Key); + if (!isValidItem) + { + continue; + } + + if (shouldBeSkipped(itemTemplate)) + { + continue; + } + + filteredItems.TryAdd(itemKvP.Key, itemKvP.Value); + } + + return filteredItems; + } + /// /// Return provided weights or an empty dictionary ///