From 51ada1613f003d6b184bede91ac4922ed9dd4385 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 26 Jan 2025 20:17:12 +0000 Subject: [PATCH] added timeoutcallbacks --- .../Helpers/Dialogue/AbstractDialogChatBot.cs | 42 ++++++++++++------- .../Core/Utils/Callbacks/TimeoutCallback.cs | 16 +++++++ 2 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 Libraries/Core/Utils/Callbacks/TimeoutCallback.cs diff --git a/Libraries/Core/Helpers/Dialogue/AbstractDialogChatBot.cs b/Libraries/Core/Helpers/Dialogue/AbstractDialogChatBot.cs index ca55a3bf..242c6645 100644 --- a/Libraries/Core/Helpers/Dialogue/AbstractDialogChatBot.cs +++ b/Libraries/Core/Helpers/Dialogue/AbstractDialogChatBot.cs @@ -3,6 +3,7 @@ using Core.Models.Eft.Dialog; using Core.Models.Eft.Profile; using Core.Models.Utils; using Core.Services; +using Core.Utils.Callbacks; namespace Core.Helpers.Dialogue; @@ -18,19 +19,22 @@ public abstract class AbstractDialogChatBot( public string? HandleMessage(string sessionId, SendMessageRequest request) { - if ((request.Text ?? "").Length == 0) { + 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) { + 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") { + if (splitCommand.FirstOrDefault()?.ToLower() == "help") + { _mailSendService.SendUserMessageToPlayer( sessionId, GetChatBot(), @@ -40,19 +44,21 @@ public abstract class AbstractDialogChatBot( ); // 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( - __ => + TimeoutCallback.RunInTimespan( + () => { foreach (var chatCommand in _chatCommands) { _mailSendService.SendUserMessageToPlayer( sessionId, GetChatBot(), - $"Commands available for \"{chatCommand.GetCommandPrefix()}\" prefix:", [], null - ); + $"Commands available for \"{chatCommand.GetCommandPrefix()}\" prefix:", + [], + null + ); - _ = new Timer( - ___ => + TimeoutCallback.RunInTimespan( + () => { foreach (var subCommand in chatCommand.GetCommands()) { @@ -64,12 +70,14 @@ public abstract class AbstractDialogChatBot( null ); } - }, null, TimeSpan.FromMicroseconds(1000), Timeout.InfiniteTimeSpan + }, + TimeSpan.FromSeconds(1) ); } - }, null, TimeSpan.FromMicroseconds(1000), Timeout.InfiniteTimeSpan + }, + TimeSpan.FromSeconds(1) ); - + return request.DialogId; } @@ -86,9 +94,13 @@ public abstract class AbstractDialogChatBot( public void RegisterChatCommand(IChatCommand chatCommand) { - if (_chatCommands.Any((cc) => cc.GetCommandPrefix() == chatCommand.GetCommandPrefix())) { - throw new Exception($"The command \"{chatCommand.GetCommandPrefix()}\" attempting to be registered already exists."); + if (_chatCommands.Any(cc => cc.GetCommandPrefix() == chatCommand.GetCommandPrefix())) + { + throw new Exception( + $"The command \"{chatCommand.GetCommandPrefix()}\" attempting to be registered already exists." + ); } + _chatCommands.Add(chatCommand); } diff --git a/Libraries/Core/Utils/Callbacks/TimeoutCallback.cs b/Libraries/Core/Utils/Callbacks/TimeoutCallback.cs new file mode 100644 index 00000000..890a0842 --- /dev/null +++ b/Libraries/Core/Utils/Callbacks/TimeoutCallback.cs @@ -0,0 +1,16 @@ +namespace Core.Utils.Callbacks; + +public static class TimeoutCallback +{ + + public static Task RunInTimespan(Action action, TimeSpan timeSpan) + { + return Task.Factory.StartNew( + () => + { + Thread.Sleep(timeSpan); + action(); + } + ); + } +}