Refactor Bot chat messages at raid end

This commit is contained in:
hulkhan22
2025-04-28 21:58:45 +02:00
parent 59eb922cfc
commit bbae435498
5 changed files with 88 additions and 37 deletions
@@ -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)
{
@@ -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;
}
}
@@ -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);
}
@@ -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;
/// </summary>
[Injectable(InjectionType.Singleton)]
public class MatchBotDetailsCacheService(
ISptLogger<MatchBotDetailsCacheService> _logger,
LocalisationService _localisationService
ISptLogger<MatchBotDetailsCacheService> _logger
)
{
protected ConcurrentDictionary<string, BotBase> _botDetailsCache = new();
private static readonly HashSet<string> _sidesToCache =
[
"pmcUSEC",
"pmcBEAR"
];
protected readonly ConcurrentDictionary<string, BotDetailsForChatMessages> BotDetailsCache = new();
/// <summary>
/// Store a bot in the cache, keyed by its name.
@@ -22,18 +29,31 @@ public class MatchBotDetailsCacheService(
/// <param name="botToCache"> Bot details to cache </param>
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,
});
}
/// <summary>
@@ -41,7 +61,7 @@ public class MatchBotDetailsCacheService(
/// </summary>
public void ClearCache()
{
_botDetailsCache.Clear();
BotDetailsCache.Clear();
}
/// <summary>
@@ -50,13 +70,17 @@ public class MatchBotDetailsCacheService(
/// <param name="botName"> Name of bot to find </param>
/// <param name="botSide"> Side of the bot </param>
/// <returns></returns>
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;
}
@@ -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<string>
{
"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
}
};