From 98a186ff2f5632b995a462d9cbb8e38d76c00c0d Mon Sep 17 00:00:00 2001 From: Chomp Date: Wed, 6 Aug 2025 15:15:58 +0100 Subject: [PATCH] Refactored logic of `GetPmcNicknameOfMaxLength()` --- .../SPTarkov.Server.Core/Helpers/BotHelper.cs | 93 +++++++++++++------ 1 file changed, 66 insertions(+), 27 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/BotHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/BotHelper.cs index 3856ae2f..cfb9ce04 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/BotHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/BotHelper.cs @@ -181,40 +181,79 @@ public class BotHelper(ISptLogger logger, DatabaseService databaseSer } /// - /// Get a name from a PMC that fits the desired length + /// Get a PMC name that fits the desired length /// /// Max length of name, inclusive /// OPTIONAL - what side PMC to get name from (usec/bear) /// name of PMC public string GetPmcNicknameOfMaxLength(int maxLength, string? side = null) { - var chosenFaction = (side ?? (randomUtil.GetInt(0, 1) == 0 ? Sides.Usec : Sides.Bear)).ToLowerInvariant(); - var cacheKey = $"{chosenFaction}{maxLength}"; - if (!_pmcNameCache.TryGetValue(cacheKey, out var eligibleNames)) - { - if (!databaseService.GetBots().Types.TryGetValue(chosenFaction, out var chosenFactionDetails)) - { - logger.Error($"Unknown faction: {chosenFaction} Defaulting to: {Sides.Usec}"); - chosenFaction = Sides.Usec.ToLowerInvariant(); - chosenFactionDetails = databaseService.GetBots().Types[chosenFaction]; - } - - var matchingNames = chosenFactionDetails.FirstNames.Where(name => name.Length <= maxLength).ToList(); - if (!matchingNames.Any()) - { - logger.Warning( - $"Unable to filter: {chosenFaction} PMC names to only those under: {maxLength}, none found that match that criteria, selecting from entire name pool instead" - ); - - // Return a random string from names - return randomUtil.GetRandomElement(chosenFactionDetails.FirstNames); - } - - _pmcNameCache.TryAdd(cacheKey, matchingNames); - - eligibleNames = matchingNames; - } + var chosenFaction = GetPmcFactionBySide(side); + var eligibleNames = GetOrAddEligiblePmcNamesFromCache(chosenFaction, maxLength); return randomUtil.GetRandomElement(eligibleNames); } + + /// + /// Choose a faction based on the passed in side (usec/bear) or choose randomly if not provided + /// + /// usec/bear + /// usec/bear + protected string GetPmcFactionBySide(string? side) + { + var chosenFaction = (side ?? (randomUtil.GetInt(0, 1) == 0 ? Sides.Usec : Sides.Bear)).ToLowerInvariant(); + return chosenFaction; + } + + /// + /// Cache Pmcs against their length and faction, return values that match requirement + /// + /// bear/usec + /// Max length of name + /// Collection of names + protected List GetOrAddEligiblePmcNamesFromCache(string chosenFaction, int maxLength) + { + var cacheKey = $"{chosenFaction}{maxLength.ToString()}"; + if (_pmcNameCache.TryGetValue(cacheKey, out var eligibleNames)) + { + // Exists in cache, return + return eligibleNames; + } + + // Not found in cache, generate and store + var generatedNames = GatherPmcNamesOfLength(chosenFaction, maxLength); + _pmcNameCache.TryAdd(cacheKey, generatedNames); + + return generatedNames; + } + + /// + /// Get names that match the side and length defined in parameters + /// + /// bear/usec + /// max length of name to return + /// + protected List GatherPmcNamesOfLength(string chosenFaction, int maxLength) + { + // Ensure faction is legit before gathering + if (!databaseService.GetBots().Types.TryGetValue(chosenFaction, out var chosenFactionDetails)) + { + logger.Error($"Unknown faction: {chosenFaction} Defaulting to: {Sides.Usec}"); + chosenFaction = Sides.Usec.ToLowerInvariant(); + chosenFactionDetails = databaseService.GetBots().Types[chosenFaction]; + } + + var matchingNames = chosenFactionDetails.FirstNames.Where(name => name.Length <= maxLength).ToList(); + if (matchingNames.Any()) + { + return matchingNames; + } + + logger.Warning( + $"Unable to filter: {chosenFaction} PMC names to only those under: {maxLength}, none found that match that criteria, selecting from entire name pool instead" + ); + + // Return a random string from names + return chosenFactionDetails.FirstNames.ToList(); + } }