From ccb55bd025332c9aa8a15c9126e83d1ac2b6cb12 Mon Sep 17 00:00:00 2001 From: Chomp Date: Mon, 2 Jun 2025 17:20:54 +0100 Subject: [PATCH] Expanded `GetGenerationWeights` scope to reduce code verbosity Improved `InitCacheForBotRole` Removed unnecessary code from `AddItemsToPool` --- .../Services/BotLootCacheService.cs | 114 ++++++++---------- 1 file changed, 48 insertions(+), 66 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs index a458832a..244f4902 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs @@ -8,7 +8,6 @@ using SPTarkov.Server.Core.Models.Enums; using SPTarkov.Server.Core.Models.Spt.Bots; using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Server.Core.Utils.Cloners; -using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; namespace SPTarkov.Server.Core.Services; @@ -244,12 +243,10 @@ public class BotLootCacheService( } // Assign whitelisted special items to bot if any exist - var specialLootItems = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.SpecialItems?.Whitelist); - - // No whitelist, find and assign from combined item pool - if (!specialLootItems.Any()) - // key = tpl, value = weight + var (specialLootItems, addSpecialLootItems) = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.SpecialItems?.Whitelist); + if (addSpecialLootItems) // key = tpl, value = weight { + // No whitelist, find and assign from combined item pool foreach (var itemKvP in specialLootPool) { var itemTemplate = _itemHelper.GetItem(itemKvP.Key).Value; @@ -263,26 +260,13 @@ public class BotLootCacheService( } } - var healingItemsInWhitelist = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Healing?.Whitelist); - var addHealingItems = !healingItemsInWhitelist.Any(); // Nothing found in whitelist, we need to add items from combinedLootPool - - var drugItemsInWhitelist = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Drugs?.Whitelist); - var addDrugItems = !drugItemsInWhitelist.Any(); - - var foodItemsInWhitelist = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Food?.Whitelist); - var foodItems = !foodItemsInWhitelist.Any(); - - var drinkItemsInWhitelist = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Food?.Whitelist); - var addDrinkItems = !drinkItemsInWhitelist.Any(); - - var currencyItemsInWhitelist = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Currency?.Whitelist); - var addCurrencyItems = !currencyItemsInWhitelist.Any(); - - var stimItemsInWhitelist = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Stims?.Whitelist); - var addStimItems = !stimItemsInWhitelist.Any(); - - var grenadeItemsInWhitelist = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Grenades?.Whitelist); - var addGrenadeItems = !grenadeItemsInWhitelist.Any(); + var (healingItemsInWhitelist, addHealingItems) = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Healing?.Whitelist); + var (drugItemsInWhitelist, addDrugItems) = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Drugs?.Whitelist); + var (foodItemsInWhitelist, addFoodItems) = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Food?.Whitelist); + var (drinkItemsInWhitelist, addDrinkItems) = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Food?.Whitelist); + var (currencyItemsInWhitelist, addCurrencyItems) = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Currency?.Whitelist); + var (stimItemsInWhitelist, addStimItems) = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Stims?.Whitelist); + var (grenadeItemsInWhitelist, addGrenadeItems) = GetGenerationWeights(botJsonTemplate.BotGeneration?.Items?.Grenades?.Whitelist); foreach (var itemKvP in combinedLootPool) { @@ -319,7 +303,7 @@ public class BotLootCacheService( } } - if (foodItems) + if (addFoodItems) { if (_itemHelper.IsOfBaseclass(itemTemplate.Id, BaseClasses.FOOD)) { @@ -450,12 +434,18 @@ public class BotLootCacheService( cacheForRole.SecureLoot = filteredSecureLoot; } + /// + /// Helper function - Filter out items from passed in pool based on a passed in delegate + /// + /// Pool to filter + /// Delegate to filter pool by + /// protected Dictionary FilterItemPool(Dictionary lootPool, Func shouldBeSkipped) { var filteredItems = new Dictionary(); - foreach (var itemKvP in lootPool) + foreach (var (itemTpl, itemWeight) in lootPool) { - var (isValidItem, itemTemplate) = _itemHelper.GetItem(itemKvP.Key); + var (isValidItem, itemTemplate) = _itemHelper.GetItem(itemTpl); if (!isValidItem) { continue; @@ -466,7 +456,7 @@ public class BotLootCacheService( continue; } - filteredItems.TryAdd(itemKvP.Key, itemKvP.Value); + filteredItems.TryAdd(itemTpl, itemWeight); } return filteredItems; @@ -476,23 +466,25 @@ public class BotLootCacheService( /// Return provided weights or an empty dictionary /// /// Weights to return - /// Dictionary - protected static Dictionary GetGenerationWeights(Dictionary? weights) + /// Dictionary and should pool be hydrated by items in combined loot pool + protected static (Dictionary, bool populateFromCombinedPool) GetGenerationWeights(Dictionary? weights) { - return weights ?? []; + var result = weights ?? []; + return (result, !result.Any()); // empty dict = should be populated from combined pool } + /// + /// merge item tpls + weightings to passed in dictionary + /// If exits already, skip + /// + /// Dictionary to add item to + /// Dictionary of items to add protected void AddItemsToPool(Dictionary poolToAddTo, Dictionary poolOfItemsToAdd) { - foreach (var tpl in poolOfItemsToAdd) + foreach (var (tpl, weight) in poolOfItemsToAdd) { // Skip adding items that already exist - if (poolToAddTo.ContainsKey(tpl.Key)) - { - continue; - } - - poolToAddTo.TryAdd(tpl.Key, poolOfItemsToAdd[tpl.Key]); + poolToAddTo.TryAdd(tpl, weight); } } @@ -567,34 +559,24 @@ public class BotLootCacheService( /// Bot role to hydrate protected void InitCacheForBotRole(string botRole) { - if ( - !_lootCache.TryAdd( - botRole, - new BotLootCache - { - BackpackLoot = new Dictionary(), - PocketLoot = new Dictionary(), - VestLoot = new Dictionary(), - SecureLoot = new Dictionary(), - CombinedPoolLoot = new Dictionary(), - - SpecialItems = new Dictionary(), - GrenadeItems = new Dictionary(), - DrugItems = new Dictionary(), - FoodItems = new Dictionary(), - DrinkItems = new Dictionary(), - CurrencyItems = new Dictionary(), - HealingItems = new Dictionary(), - StimItems = new Dictionary() - } - ) - ) - { - if (_logger.IsLogEnabled(LogLevel.Debug)) + _lootCache.TryAdd(botRole, new BotLootCache { - _logger.Debug($"Unable to add loot cache for bot role: {botRole} - already exists"); + BackpackLoot = new Dictionary(), + PocketLoot = new Dictionary(), + VestLoot = new Dictionary(), + SecureLoot = new Dictionary(), + CombinedPoolLoot = new Dictionary(), + + SpecialItems = new Dictionary(), + GrenadeItems = new Dictionary(), + DrugItems = new Dictionary(), + FoodItems = new Dictionary(), + DrinkItems = new Dictionary(), + CurrencyItems = new Dictionary(), + HealingItems = new Dictionary(), + StimItems = new Dictionary() } - } + ); } ///