From d99416a5a4bc43f52c7a69b8c4632eeae162f285 Mon Sep 17 00:00:00 2001 From: Chomp Date: Mon, 14 Jul 2025 17:32:09 +0100 Subject: [PATCH] Removed use of reflection inside `RemoveChristmasItemsFromBotInventory` Replaced more strings with mongoIds --- .../Generators/BotInventoryGenerator.cs | 12 +++--- .../Models/Eft/Common/Tables/BotType.cs | 2 +- .../Spt/Bots/GenerateEquipmentProperties.cs | 3 +- .../Services/BotEquipmentFilterService.cs | 5 +-- .../Services/SeasonalEventService.cs | 42 ++++++++----------- 5 files changed, 29 insertions(+), 35 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs b/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs index f834fe69..4a803154 100644 --- a/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs +++ b/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs @@ -221,8 +221,8 @@ public class BotInventoryGenerator( var pmcProfile = profileHelper.GetPmcProfile(sessionId); var botEquipmentRole = botGeneratorHelper.GetBotEquipmentRole(botRole); - // Iterate over all equipment slots of bot, do it in specifc order to reduce conflicts - // e.g. ArmorVest should be generated after TactivalVest + // Iterate over all equipment slots of bot, do it in specific order to reduce conflicts + // e.g. ArmorVest should be generated after TacticalVest // or FACE_COVER before HEADWEAR foreach (var (equipmentSlot, value) in templateInventory.Equipment) { @@ -409,14 +409,14 @@ public class BotInventoryGenerator( /// /// is bot a PMC /// - protected Dictionary? GetPocketPoolByGameEdition( + protected Dictionary? GetPocketPoolByGameEdition( string chosenGameVersion, BotTypeInventory templateInventory, bool isPmc ) { return chosenGameVersion == GameEditions.UNHEARD && isPmc - ? new Dictionary { [ItemTpl.POCKETS_1X4_TUE] = 1 } + ? new Dictionary { [ItemTpl.POCKETS_1X4_TUE] = 1 } : templateInventory.Equipment.GetValueOrDefault(EquipmentSlots.Pockets); } @@ -426,7 +426,7 @@ public class BotInventoryGenerator( /// Equipment to filter TacticalVest of /// Role of bot vests are being filtered for public void FilterRigsToThoseWithProtection( - Dictionary> templateEquipment, + Dictionary> templateEquipment, string botRole ) { @@ -456,7 +456,7 @@ public class BotInventoryGenerator( /// Role of bot vests are being filtered for /// Should the function return all rigs when 0 unarmored are found public void FilterRigsToThoseWithoutProtection( - Dictionary> templateEquipment, + Dictionary> templateEquipment, string botRole, bool allowEmptyResult = true ) diff --git a/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Tables/BotType.cs b/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Tables/BotType.cs index 25a60260..8e32478a 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Tables/BotType.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Tables/BotType.cs @@ -446,7 +446,7 @@ public record BotTypeInventory public Dictionary? ExtensionData { get; set; } [JsonPropertyName("equipment")] - public Dictionary>? Equipment { get; set; } + public Dictionary>? Equipment { get; set; } public GlobalAmmo? Ammo { get; set; } diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/GenerateEquipmentProperties.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/GenerateEquipmentProperties.cs index 71db8cf9..b98065e2 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/GenerateEquipmentProperties.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/GenerateEquipmentProperties.cs @@ -1,4 +1,5 @@ using System.Text.Json.Serialization; +using SPTarkov.Server.Core.Models.Common; using SPTarkov.Server.Core.Models.Eft.Common.Tables; using SPTarkov.Server.Core.Models.Enums; using SPTarkov.Server.Core.Models.Spt.Config; @@ -20,7 +21,7 @@ public record GenerateEquipmentProperties /// Equipment pool for root slot being generated /// [JsonPropertyName("rootEquipmentPool")] - public Dictionary? RootEquipmentPool { get; set; } + public Dictionary? RootEquipmentPool { get; set; } [JsonPropertyName("modPool")] public GlobalMods? ModPool { get; set; } diff --git a/Libraries/SPTarkov.Server.Core/Services/BotEquipmentFilterService.cs b/Libraries/SPTarkov.Server.Core/Services/BotEquipmentFilterService.cs index 3ab433bb..22bcd4df 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotEquipmentFilterService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotEquipmentFilterService.cs @@ -267,8 +267,7 @@ public class BotEquipmentFilterService( } // Filter equipment slot items to just items in whitelist - baseBotNode.BotInventory.Equipment[equipmentSlotKey.Key] = - new Dictionary(); + baseBotNode.BotInventory.Equipment[equipmentSlotKey.Key] = []; foreach (var dict in botEquipment) { if (whitelistEquipmentForSlot.Contains(dict.Key)) @@ -384,7 +383,7 @@ public class BotEquipmentFilterService( /// Bot item dictionary to adjust protected void AdjustWeighting( AdjustmentDetails? weightingAdjustments, - Dictionary> botItemPool, + Dictionary> botItemPool, bool showEditWarnings = true ) { diff --git a/Libraries/SPTarkov.Server.Core/Services/SeasonalEventService.cs b/Libraries/SPTarkov.Server.Core/Services/SeasonalEventService.cs index 1f8195a4..3d9f6b27 100644 --- a/Libraries/SPTarkov.Server.Core/Services/SeasonalEventService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/SeasonalEventService.cs @@ -363,9 +363,10 @@ public class SeasonalEventService( var christmasItems = GetChristmasEventItems(); // Remove christmas related equipment + botInventory.Equipment ??= new Dictionary>(); foreach (var equipmentSlotKey in _equipmentSlotsToFilter) { - if (botInventory.Equipment[equipmentSlotKey] is null) + if (!botInventory.Equipment.TryGetValue(equipmentSlotKey, out var equipment)) { logger.Warning( serverLocalisationService.GetText( @@ -373,39 +374,32 @@ public class SeasonalEventService( new { equipmentSlot = equipmentSlotKey, botRole } ) ); + + continue; } - var equipment = botInventory.Equipment[equipmentSlotKey]; botInventory.Equipment[equipmentSlotKey] = equipment .Where(i => !_christmasEventItems.Contains(i.Key)) .ToDictionary(); } - // Remove christmas related loot from loot containers - var props = botInventory.Items.GetType().GetProperties(); - foreach (var lootContainerKey in _lootContainersToFilter) + var containersToCheck = new List> { - var propInfo = props.FirstOrDefault(p => - string.Equals( - p.Name.ToLowerInvariant(), - lootContainerKey.ToLowerInvariant(), - StringComparison.OrdinalIgnoreCase - ) - ); - var prop = (Dictionary?)propInfo.GetValue(botInventory.Items); + botInventory.Items.Backpack, + botInventory.Items.Pockets, + botInventory.Items.SecuredContainer, + botInventory.Items.TacticalVest, + botInventory.Items.SpecialLoot, + }; - if (prop is null) + foreach (var container in containersToCheck) + { + // Find all Christmas items in container + var keysToRemove = container.Keys.Where(key => christmasItems.Contains(key)).ToList(); + foreach (var key in keysToRemove) { - logger.Warning( - serverLocalisationService.GetText( - "seasonal-missing_loot_container_slot_on_bot", - new { lootContainer = lootContainerKey, botRole } - ) - ); + container.Remove(key); } - - var newProp = prop.Where(tpl => !christmasItems.Contains(tpl.Key)).ToDictionary(); - propInfo.SetValue(botInventory.Items, newProp); } } @@ -413,7 +407,7 @@ public class SeasonalEventService( /// Make adjusted to server code based on the name of the event passed in /// /// globals.json - /// Name of the event to enable. e.g. Christmas + /// Name of the event to enable. e.g. Christmas private void UpdateGlobalEvents(Config globalConfig, SeasonalEvent eventType) { logger.Success(serverLocalisationService.GetText("season-event_is_active", eventType.Type));