From d3a819b00f02a13ef76e62e3e41320faf7318aed Mon Sep 17 00:00:00 2001 From: qe201020335 Date: Mon, 26 May 2025 01:07:38 -0400 Subject: [PATCH] Optimize chatbot with dictionary --- .../Helpers/Dialogue/AbstractDialogChatBot.cs | 21 ++++++++-------- .../Dialogue/Commando/SptCommandoCommands.cs | 25 +++++++------------ 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/AbstractDialogChatBot.cs b/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/AbstractDialogChatBot.cs index b07eb7cf..36baf3be 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/AbstractDialogChatBot.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/AbstractDialogChatBot.cs @@ -13,7 +13,8 @@ public abstract class AbstractDialogChatBot( IEnumerable chatCommands ) : IDialogueChatBot { - protected List _chatCommands = chatCommands.ToList(); + protected IDictionary _chatCommands = + chatCommands.ToDictionary(command => command.GetCommandPrefix()); public abstract UserDialogInfo GetChatBot(); @@ -28,10 +29,11 @@ public abstract class AbstractDialogChatBot( var splitCommand = request.Text.Split(" "); - var commandos = _chatCommands.Where(c => c.GetCommandPrefix() == splitCommand.FirstOrDefault()); - if (commandos.FirstOrDefault()?.GetCommands().Contains(splitCommand[1]) ?? false) + if (splitCommand.Length > 1 && + _chatCommands.TryGetValue(splitCommand[0], out var commando) && + commando.GetCommands().Contains(splitCommand[1])) { - return commandos.FirstOrDefault().Handle(splitCommand[1], GetChatBot(), sessionId, request); + return commando.Handle(splitCommand[1], GetChatBot(), sessionId, request); } if (string.Equals(splitCommand.FirstOrDefault(), "help", StringComparison.OrdinalIgnoreCase)) @@ -63,7 +65,7 @@ public abstract class AbstractDialogChatBot( TimeoutCallback.RunInTimespan( () => { - foreach (var chatCommand in _chatCommands) + foreach (var chatCommand in _chatCommands.Values) { _mailSendService.SendUserMessageToPlayer( sessionId, @@ -99,14 +101,11 @@ public abstract class AbstractDialogChatBot( public void RegisterChatCommand(IChatCommand chatCommand) { - if (_chatCommands.Any(cc => cc.GetCommandPrefix() == chatCommand.GetCommandPrefix())) + var prefix = chatCommand.GetCommandPrefix(); + if (!_chatCommands.TryAdd(prefix, chatCommand)) { - throw new Exception( - $"The command \"{chatCommand.GetCommandPrefix()}\" attempting to be registered already exists." - ); + throw new Exception($"The command \"{prefix}\" attempting to be registered already exists."); } - - _chatCommands.Add(chatCommand); } protected abstract string GetUnrecognizedCommandMessage(); diff --git a/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/Commando/SptCommandoCommands.cs b/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/Commando/SptCommandoCommands.cs index 7b0e96f9..eb608cc0 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/Commando/SptCommandoCommands.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/Commando/SptCommandoCommands.cs @@ -12,7 +12,7 @@ namespace SPTarkov.Server.Core.Helpers.Dialog.Commando; public class SptCommandoCommands : IChatCommand { protected LocalisationService _localisationService; - protected List _sptCommands; + protected IDictionary _sptCommands; public SptCommandoCommands( ConfigServer configServer, @@ -20,15 +20,14 @@ public class SptCommandoCommands : IChatCommand IEnumerable sptCommands ) { - _sptCommands = sptCommands.ToList(); + _sptCommands = sptCommands.ToDictionary(command => command.GetCommand()); _localisationService = localisationService; var coreConfigs = configServer.GetConfig(); var commandoId = coreConfigs.Features?.ChatbotFeatures.Ids.GetValueOrDefault("commando"); if (!(coreConfigs.Features.ChatbotFeatures.CommandoFeatures.GiveCommandEnabled && coreConfigs.Features.ChatbotFeatures.EnabledBots.ContainsKey(commandoId))) { - var giveCommand = _sptCommands.FirstOrDefault(x => x.GetCommand().ToLower() == "give"); - _sptCommands.Remove(giveCommand); + _sptCommands.Remove("give"); } } @@ -39,33 +38,27 @@ public class SptCommandoCommands : IChatCommand public string GetCommandHelp(string command) { - return _sptCommands.FirstOrDefault(c => c.GetCommand() == command)?.GetCommandHelp(); + return _sptCommands.TryGetValue(command, out var value) ? value.GetCommandHelp() : ""; } public List GetCommands() { - return _sptCommands.Select(c => c.GetCommand()).ToList(); + return _sptCommands.Keys.ToList(); } public string Handle(string command, UserDialogInfo commandHandler, string sessionId, SendMessageRequest request) { - return _sptCommands - .First(c => c.GetCommand() == command) - .PerformAction(commandHandler, sessionId, request); + return _sptCommands[command].PerformAction(commandHandler, sessionId, request); } public void RegisterSptCommandoCommand(ISptCommand command) { - if (_sptCommands.Any(c => c.GetCommand() == command.GetCommand())) + var key = command.GetCommand(); + if (!_sptCommands.TryAdd(key, command)) { throw new Exception( - _localisationService.GetText( - "chat-unable_to_register_command_already_registered", - command.GetCommand() - ) + _localisationService.GetText("chat-unable_to_register_command_already_registered", key) ); } - - _sptCommands.Add(command); } }