From 83408c76fc2a763e1ab181438b504368695f2fe4 Mon Sep 17 00:00:00 2001 From: Chomp Date: Tue, 5 Aug 2025 23:55:13 +0100 Subject: [PATCH] Reduced complexity of `GetCompatibleTplFromArray()` Flagged ItemHelper as singleton due to number of frozensets --- .../Helpers/ItemHelper.cs | 50 ++++++------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs index 4ddb594e..54050717 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs @@ -15,7 +15,7 @@ using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; namespace SPTarkov.Server.Core.Helpers; -[Injectable] +[Injectable(InjectionType.Singleton)] public class ItemHelper( ISptLogger logger, RandomUtil randomUtil, @@ -1650,7 +1650,7 @@ public class ItemHelper( } var chosenTpl = GetCompatibleTplFromArray(itemPool, incompatibleModTpls); - if (chosenTpl is null) + if (chosenTpl.IsEmpty()) { if (logger.IsLogEnabled(LogLevel.Debug)) { @@ -1666,7 +1666,7 @@ public class ItemHelper( Item modItemToAdd = new() { Id = new MongoId(), - Template = chosenTpl.Value, + Template = chosenTpl, ParentId = result[0].Id, SlotId = slot.Name, }; @@ -1686,38 +1686,18 @@ public class ItemHelper( /// /// Get a compatible tpl from the array provided where it is not found in the provided incompatible mod tpls parameter /// - /// Tpls to randomly choose from - /// Incompatible tpls to not allow + /// Tpls to randomly choose from + /// Incompatible tpls to disallow /// Chosen tpl or undefined - public MongoId? GetCompatibleTplFromArray(HashSet possibleTpls, HashSet incompatibleModTpls) + public MongoId GetCompatibleTplFromArray(HashSet tplPool, HashSet tplBlacklist) { - if (!possibleTpls.Any()) + if (!tplPool.Any()) { - return null; + return MongoId.Empty(); } - MongoId? chosenTpl = null; - var count = 0; - while (chosenTpl is null) - { - // Loop over choosing a random tpl until one is found or count variable reaches the same size as the possible tpls array - var tpl = randomUtil.GetArrayValue(possibleTpls); - if (incompatibleModTpls.Contains(tpl)) - { - // Incompatible tpl was chosen, try again - count++; - if (count >= possibleTpls.Count) - { - return null; - } - - continue; - } - - chosenTpl = tpl; - } - - return chosenTpl; + var compatibleTpls = tplPool.Except(tplBlacklist).ToList(); + return compatibleTpls.Any() ? randomUtil.GetArrayValue(compatibleTpls) : MongoId.Empty(); } /// @@ -1784,10 +1764,12 @@ public class ItemHelper( return itemWithChildren; } - // Add a blank upd object to passed in item if it does not exist already - // item to add upd to - // text to write to log when upd object was not found - // Returns True when upd object was added + /// + /// Add a blank upd object to passed in item if it does not exist already + /// + /// item to add upd to + /// text to write to log when upd object was not found + /// True when upd object was added public bool AddUpdObjectToItem(Item item, string? warningMessageWhenMissing = null) { if (item.Upd is not null)