diff --git a/Libraries/SPTarkov.Server.Core/Controllers/BotController.cs b/Libraries/SPTarkov.Server.Core/Controllers/BotController.cs index a0bc1189..fd70e82f 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/BotController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/BotController.cs @@ -258,8 +258,9 @@ public class BotController( } results.Add(bot); + // Store bot details in cache so post-raid PMC messages can use data - _matchBotDetailsCacheService.CacheBot(_cloner.Clone(bot)); + _matchBotDetailsCacheService.CacheBot(bot); } catch (Exception e) { diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/BotDetailsForChatMessages.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/BotDetailsForChatMessages.cs new file mode 100644 index 00000000..e680ed1e --- /dev/null +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/BotDetailsForChatMessages.cs @@ -0,0 +1,36 @@ +using SPTarkov.Server.Core.Models.Enums; + +namespace SPTarkov.Server.Core.Models.Spt.Bots; + +public class BotDetailsForChatMessages +{ + public string Nickname + { + get; + set; + } + + public DogtagSide Side + { + get; + set; + } + + public int? Aid + { + get; + set; + } + + public int? Level + { + get; + set; + } + + public MemberCategory? Type + { + get; + set; + } +} diff --git a/Libraries/SPTarkov.Server.Core/Services/LocationLifecycleService.cs b/Libraries/SPTarkov.Server.Core/Services/LocationLifecycleService.cs index 370afe22..bdf850db 100644 --- a/Libraries/SPTarkov.Server.Core/Services/LocationLifecycleService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/LocationLifecycleService.cs @@ -861,6 +861,8 @@ public class LocationLifecycleService if (postRaidProfile.Stats.Eft.Aggressor is not null) { + // get the aggressor ID from the client request body + postRaidProfile.Stats.Eft.Aggressor.ProfileId = request.Results.KillerId; _pmcChatResponseService.SendKillerResponse(sessionId, pmcProfile, postRaidProfile.Stats.Eft.Aggressor); } diff --git a/Libraries/SPTarkov.Server.Core/Services/MatchBotDetailsCacheService.cs b/Libraries/SPTarkov.Server.Core/Services/MatchBotDetailsCacheService.cs index 23a61cd6..f03077aa 100644 --- a/Libraries/SPTarkov.Server.Core/Services/MatchBotDetailsCacheService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/MatchBotDetailsCacheService.cs @@ -1,6 +1,8 @@ using System.Collections.Concurrent; using SPTarkov.Common.Annotations; using SPTarkov.Server.Core.Models.Eft.Common.Tables; +using SPTarkov.Server.Core.Models.Enums; +using SPTarkov.Server.Core.Models.Spt.Bots; using SPTarkov.Server.Core.Models.Utils; namespace SPTarkov.Server.Core.Services; @@ -10,11 +12,16 @@ namespace SPTarkov.Server.Core.Services; /// [Injectable(InjectionType.Singleton)] public class MatchBotDetailsCacheService( - ISptLogger _logger, - LocalisationService _localisationService + ISptLogger _logger ) { - protected ConcurrentDictionary _botDetailsCache = new(); + private static readonly HashSet _sidesToCache = + [ + "pmcUSEC", + "pmcBEAR" + ]; + + protected readonly ConcurrentDictionary BotDetailsCache = new(); /// /// Store a bot in the cache, keyed by its name. @@ -22,18 +29,31 @@ public class MatchBotDetailsCacheService( /// Bot details to cache public void CacheBot(BotBase botToCache) { - if (botToCache.Info.Nickname is null) + if (botToCache is null || botToCache.Id 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; + 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; + } - var key = $"{botToCache.Info.Nickname.Trim()}{botToCache.Info.Side}"; - _botDetailsCache.TryAdd(key, botToCache); + // If bot isn't a PMC, skip + if (botToCache.Info?.Settings?.Role is null || !_sidesToCache.Contains(botToCache.Info.Settings.Role)) + { + return; + } + + BotDetailsCache.TryAdd(botToCache.Id, new BotDetailsForChatMessages() + { + Nickname = botToCache.Info.Nickname.Trim(), + Side = botToCache.Info.Side == "pmcUSEC" ? DogtagSide.Usec : DogtagSide.Bear, + Aid = botToCache.Aid, + Type = botToCache.Info.MemberCategory, + Level = botToCache.Info.Level, + }); } /// @@ -41,7 +61,7 @@ public class MatchBotDetailsCacheService( /// public void ClearCache() { - _botDetailsCache.Clear(); + BotDetailsCache.Clear(); } /// @@ -50,13 +70,17 @@ public class MatchBotDetailsCacheService( /// Name of bot to find /// Side of the bot /// - public BotBase? GetBotByNameAndSide(string botName, string botSide) + public BotDetailsForChatMessages? GetBotById(string? id) { - var botInCache = _botDetailsCache.GetValueOrDefault($"{botName}{botSide}`", null); + if (id == null) + { + return null; + } + + var botInCache = BotDetailsCache.GetValueOrDefault(id, null); if (botInCache is null) { - _logger.Warning($"Bot not found in match bot cache: {botName.ToLower()} {botSide}"); - + _logger.Warning($"Bot not found in match bot cache: {id}"); return null; } diff --git a/Libraries/SPTarkov.Server.Core/Services/PmcChatResponseService.cs b/Libraries/SPTarkov.Server.Core/Services/PmcChatResponseService.cs index 24f4b364..05eb269f 100644 --- a/Libraries/SPTarkov.Server.Core/Services/PmcChatResponseService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/PmcChatResponseService.cs @@ -83,39 +83,27 @@ public class PmcChatResponseService( return; } - // find bot by name in cache - var killerDetailsInCache = _matchBotDetailsCacheService.GetBotByNameAndSide(killer.Name, killer.Side); + // find bot by id in cache + var killerDetailsInCache = _matchBotDetailsCacheService.GetBotById(killer.ProfileId); if (killerDetailsInCache is null) { return; } - // If killer wasn't a PMC, skip - var pmcTypes = new HashSet - { - "pmcUSEC", - "pmcBEAR" - }; - if (!pmcTypes.Contains(killerDetailsInCache.Info.Settings.Role)) - { - return; - } - - // Because we've cached PMC sides as "Savage" for the client, we need to figure out - // what side it really is - var side = killerDetailsInCache.Info.Settings.Role == "pmcUSEC" ? "Usec" : "Bear"; + // Because we've cached PMC sides as "Savage" for the client, + // we need to figure out what side it really is + var side = killerDetailsInCache.Side == DogtagSide.Usec ? "Usec" : "Bear"; var killerDetails = new UserDialogInfo { - Id = killerDetailsInCache.Id, + Id = killer.ProfileId, Aid = killerDetailsInCache.Aid, Info = new UserDialogDetails { - Nickname = killerDetailsInCache.Info.Nickname, + Nickname = killerDetailsInCache.Nickname, Side = side, - Level = killerDetailsInCache.Info.Level, - MemberCategory = killerDetailsInCache.Info.MemberCategory, - SelectedMemberCategory = killerDetailsInCache.Info.SelectedMemberCategory + Level = killerDetailsInCache.Level, + MemberCategory = killerDetailsInCache.Type } };