Merge pull request #268 from qe201020335/optimize_chatbot
Optimize Chatbot with Dictionary
This commit is contained in:
@@ -13,7 +13,8 @@ public abstract class AbstractDialogChatBot(
|
||||
IEnumerable<IChatCommand> chatCommands
|
||||
) : IDialogueChatBot
|
||||
{
|
||||
protected List<IChatCommand> _chatCommands = chatCommands.ToList();
|
||||
protected IDictionary<string, IChatCommand> _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();
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace SPTarkov.Server.Core.Helpers.Dialog.Commando;
|
||||
public class SptCommandoCommands : IChatCommand
|
||||
{
|
||||
protected LocalisationService _localisationService;
|
||||
protected List<ISptCommand> _sptCommands;
|
||||
protected IDictionary<string, ISptCommand> _sptCommands;
|
||||
|
||||
public SptCommandoCommands(
|
||||
ConfigServer configServer,
|
||||
@@ -20,15 +20,14 @@ public class SptCommandoCommands : IChatCommand
|
||||
IEnumerable<ISptCommand> sptCommands
|
||||
)
|
||||
{
|
||||
_sptCommands = sptCommands.ToList();
|
||||
_sptCommands = sptCommands.ToDictionary(command => command.GetCommand());
|
||||
_localisationService = localisationService;
|
||||
var coreConfigs = configServer.GetConfig<CoreConfig>();
|
||||
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<string> 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user