added timeoutcallbacks

This commit is contained in:
Alex
2025-01-26 20:17:12 +00:00
parent f4f4779488
commit 51ada1613f
2 changed files with 43 additions and 15 deletions
@@ -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);
}
@@ -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();
}
);
}
}