Optimised GetCollectionValue and GetPmcNicknameOfMaxLength
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class ItemBaseClassService(
|
||||
_itemBaseClassesCache = new Dictionary<string, HashSet<string>>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<RandomUtil> _logger, ICloner _cloner)
|
||||
/// </summary>
|
||||
/// <param name="collection">The collection of strings to select a random value from.</param>
|
||||
/// <returns>A randomly selected string from the array.</returns>
|
||||
public string GetStringCollectionValue(IEnumerable<string> collection)
|
||||
{
|
||||
return collection.ElementAt(GetInt(0, collection.Count() - 1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random type T from the provided collection of type T.
|
||||
/// </summary>
|
||||
/// <param name="collection">The collection to get the random element from</param>
|
||||
/// <typeparam name="T">The type of elements in the collection.</typeparam>
|
||||
/// <returns>A random element from the collection.</returns>
|
||||
/// <remarks>This was formerly getArrayValue() in the node server</remarks>
|
||||
public T GetCollectionValue<T>(IEnumerable<T> collection)
|
||||
{
|
||||
return collection.ElementAt(GetInt(0, collection.Count() - 1));
|
||||
// We can call `count` directly if it's a list
|
||||
if (collection is IList<T> 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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user