diff --git a/Libraries/Core/Helpers/Dialogue/AbstractDialogChatBot.cs b/Libraries/Core/Helpers/Dialogue/AbstractDialogChatBot.cs index bd45ec80..3b5abf35 100644 --- a/Libraries/Core/Helpers/Dialogue/AbstractDialogChatBot.cs +++ b/Libraries/Core/Helpers/Dialogue/AbstractDialogChatBot.cs @@ -16,14 +16,80 @@ public abstract class AbstractDialogChatBot( public abstract UserDialogInfo GetChatBot(); - public string HandleMessage(string sessionId, SendMessageRequest request) + public string? HandleMessage(string sessionId, SendMessageRequest request) { - throw new NotImplementedException(); + if ((request.Text ?? "").Length == 0) { + _logger.Error("Command came in as empty text! Invalid data!"); + return request.DialogId; + } + + var splitCommand = request.Text.Split(" "); + + var commandos = _chatCommands.Where((c) => c.GetCommandPrefix() == splitCommand.FirstOrDefault()); + if (commandos.FirstOrDefault()?.GetCommands().Contains(splitCommand[1]) ?? false) { + return commandos.FirstOrDefault().Handle(splitCommand[1], GetChatBot(), sessionId, request); + } + + if (splitCommand.FirstOrDefault().ToLower() == "help") { + _mailSendService.SendUserMessageToPlayer( + sessionId, + GetChatBot(), + "The available commands will be listed below:", + [], + null + ); + // due to BSG being dumb with messages we need a mandatory timeout between messages so they get out on the right order + // TODO: there must be a better way of doing this + _ = new Timer( + __ => + { + foreach (var chatCommand in _chatCommands) + { + _mailSendService.SendUserMessageToPlayer( + sessionId, + GetChatBot(), + $"Commands available for \"{chatCommand.GetCommandPrefix()}\" prefix:", [], null + ); + + _ = new Timer( + ___ => + { + foreach (var subCommand in chatCommand.GetCommands()) + { + _mailSendService.SendUserMessageToPlayer( + sessionId, + GetChatBot(), + $"Subcommand {subCommand}:\\n{chatCommand.GetCommandHelp(subCommand)}", + [], + null + ); + } + }, null, TimeSpan.FromMicroseconds(1000), Timeout.InfiniteTimeSpan + ); + } + }, null, TimeSpan.FromMicroseconds(1000), Timeout.InfiniteTimeSpan + ); + + return request.DialogId; + } + + _mailSendService.SendUserMessageToPlayer( + sessionId, + GetChatBot(), + GetUnrecognizedCommandMessage(), + [], + null + ); + + return null; } public void RegisterChatCommand(IChatCommand chatCommand) { - throw new NotImplementedException(); + if (_chatCommands.Any((cc) => cc.GetCommandPrefix() == chatCommand.GetCommandPrefix())) { + throw new Exception($"The command \"{chatCommand.GetCommandPrefix()}\" attempting to be registered already exists."); + } + _chatCommands.Add(chatCommand); } protected string GetUnrecognizedCommandMessage()