using System.Collections.Concurrent; using SPTarkov.Common.Annotations; using SPTarkov.Server.Core.Models.Eft.Common.Tables; using SPTarkov.Server.Core.Models.Utils; namespace SPTarkov.Server.Core.Services; /// /// Cache bots in a dictionary, keyed by the bots name, keying by name isnt ideal as its not unique but this is used by the post-raid system which doesnt have any bot ids, only name /// [Injectable(InjectionType.Singleton)] public class MatchBotDetailsCacheService( ISptLogger _logger, LocalisationService _localisationService ) { protected ConcurrentDictionary _botDetailsCache = new(); /// /// Store a bot in the cache, keyed by its name. /// /// Bot details to cache public void CacheBot(BotBase botToCache) { if (botToCache.Info.Nickname is null) { _logger.Warning($"Unable to cache: {botToCache.Info.Settings.Role} bot with id: {botToCache.Id} as it lacks a nickname"); return; } botToCache.Inventory = null; botToCache.Skills = null; botToCache.Stats = null; var key = $"{botToCache.Info.Nickname.Trim()}{botToCache.Info.Side}"; _botDetailsCache.TryAdd(key, botToCache); } /// /// Clean the cache of all bot details. /// public void ClearCache() { _botDetailsCache.Clear(); } /// /// Find a bot in the cache by its name and side. /// /// Name of bot to find /// Side of the bot /// public BotBase? GetBotByNameAndSide(string botName, string botSide) { var botInCache = _botDetailsCache.GetValueOrDefault($"{botName}{botSide}`", null); if (botInCache is null) { _logger.Warning($"Bot not found in match bot cache: {botName.ToLower()} {botSide}"); return null; } return botInCache; } }