From c857359b5aa4418615e6cf5e46a1613265931e60 Mon Sep 17 00:00:00 2001 From: Chomp Date: Tue, 25 Feb 2025 12:53:41 +0000 Subject: [PATCH] Optimised `GetCollectionValue` and `GetPmcNicknameOfMaxLength` --- Libraries/Core/Helpers/BotHelper.cs | 4 +-- .../Core/Services/ItemBaseClassService.cs | 10 +++---- Libraries/Core/Utils/RandomUtil.cs | 27 ++++++++++--------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Libraries/Core/Helpers/BotHelper.cs b/Libraries/Core/Helpers/BotHelper.cs index e32f9135..88f0adf1 100644 --- a/Libraries/Core/Helpers/BotHelper.cs +++ b/Libraries/Core/Helpers/BotHelper.cs @@ -199,9 +199,9 @@ public class BotHelper( $"Unable to filter: {randomType} PMC names to only those under: {maxLength}, none found that match that criteria, selecting from entire name pool instead`,\n" ); - return _randomUtil.GetStringCollectionValue(allNames); + return _randomUtil.GetCollectionValue(allNames); } - return _randomUtil.GetStringCollectionValue(filteredNames); + return _randomUtil.GetCollectionValue(filteredNames.ToList()); } } diff --git a/Libraries/Core/Services/ItemBaseClassService.cs b/Libraries/Core/Services/ItemBaseClassService.cs index 729a22c3..55f48020 100644 --- a/Libraries/Core/Services/ItemBaseClassService.cs +++ b/Libraries/Core/Services/ItemBaseClassService.cs @@ -25,7 +25,7 @@ public class ItemBaseClassService( _itemBaseClassesCache = new Dictionary>(); var items = _databaseService.GetItems(); - var filteredDbItems = items.Where(x => x.Value.Type == "Item"); + var filteredDbItems = items.Where(x => string.Equals(x.Value.Type,"Item", StringComparison.OrdinalIgnoreCase)); foreach (var item in filteredDbItems) { var itemIdToUpdate = item.Value.Id; @@ -76,7 +76,7 @@ public class ItemBaseClassService( return false; } - // The cache is only generated for item templates with `_type === "Item"`, so return false for any other type, + // The cache is only generated for item templates with `_type == "Item"`, so return false for any other type, // including item templates that simply don't exist. if (!CachedItemIsOfItemType(itemTpl)) { @@ -97,9 +97,9 @@ public class ItemBaseClassService( HydrateItemBaseClassCache(); // Check for item again, return false if item not found a second time - if (_itemBaseClassesCache.ContainsKey(itemTpl)) + if (_itemBaseClassesCache.TryGetValue(itemTpl, out var value)) { - return _itemBaseClassesCache[itemTpl].Any(baseClasses.Contains); + return value.Any(baseClasses.Contains); } _logger.Warning(_localisationService.GetText("baseclass-item_not_found_failed", itemTpl)); @@ -114,7 +114,7 @@ public class ItemBaseClassService( */ private bool CachedItemIsOfItemType(string itemTemplateId) { - return _databaseService.GetItems()[itemTemplateId]?.Type == "Item"; + return string.Equals(_databaseService.GetItems()[itemTemplateId]?.Type,"Item", StringComparison.OrdinalIgnoreCase); } /** diff --git a/Libraries/Core/Utils/RandomUtil.cs b/Libraries/Core/Utils/RandomUtil.cs index 5148d1a3..f4fe1a5f 100644 --- a/Libraries/Core/Utils/RandomUtil.cs +++ b/Libraries/Core/Utils/RandomUtil.cs @@ -1,3 +1,4 @@ +using System; using Core.Models.Utils; using Core.Utils.Cloners; using SptCommon.Annotations; @@ -109,21 +110,21 @@ public class RandomUtil(ISptLogger _logger, ICloner _cloner) /// /// The collection of strings to select a random value from. /// A randomly selected string from the array. - public string GetStringCollectionValue(IEnumerable collection) - { - return collection.ElementAt(GetInt(0, collection.Count() - 1)); - } - - /// - /// Returns a random type T from the provided collection of type T. - /// - /// The collection to get the random element from - /// The type of elements in the collection. - /// A random element from the collection. - /// This was formerly getArrayValue() in the node server public T GetCollectionValue(IEnumerable collection) { - return collection.ElementAt(GetInt(0, collection.Count() - 1)); + // We can call `count` directly if it's a list + if (collection is IList list) + { + return list[GetInt(0, list.Count - 1)]; + } + + var count = collection.Count(); // Run query and count elements + if (count == 0) + { + throw new InvalidOperationException("Sequence contains no elements."); + } + + return collection.ElementAt(GetInt(0, count - 1)); } ///