mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-07 21:58:06 -05:00
Add PromptClientInput method in Utilities.cs
A new utility method named 'PromptClientInput' has been added in the Utilities.cs file. This method accepts client, prompt, and validator as inputs and allows taking action based on client responses. Included subscription and unsubscription to the 'ClientMessaged' game event, and handling of cancellation token to control the execution flow.
This commit is contained in:
parent
78c5b43ed4
commit
036a467bd0
@ -19,10 +19,12 @@ using Microsoft.Extensions.Logging;
|
||||
using SharedLibraryCore.Configuration;
|
||||
using SharedLibraryCore.Database.Models;
|
||||
using SharedLibraryCore.Dtos.Meta;
|
||||
using SharedLibraryCore.Events.Game;
|
||||
using SharedLibraryCore.Events.Server;
|
||||
using SharedLibraryCore.Exceptions;
|
||||
using SharedLibraryCore.Helpers;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using SharedLibraryCore.Interfaces.Events;
|
||||
using SharedLibraryCore.Localization;
|
||||
using SharedLibraryCore.RCon;
|
||||
using static System.Threading.Tasks.Task;
|
||||
@ -1372,5 +1374,48 @@ namespace SharedLibraryCore
|
||||
|
||||
public static void ExecuteAfterDelay(this Func<CancellationToken, Task> action, int delayMs,
|
||||
CancellationToken token = default) => ExecuteAfterDelay(delayMs, action, token);
|
||||
|
||||
public static async Task<string> PromptClientInput(this EFClient client, string prompt, Func<string, Task<bool>> validator,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
var clientResponse = new ManualResetEventSlim(false);
|
||||
string response = null;
|
||||
|
||||
try
|
||||
{
|
||||
IGameEventSubscriptions.ClientMessaged += OnResponse;
|
||||
await client.TellAsync([prompt], token);
|
||||
|
||||
var tokenSource = new CancellationTokenSource(DefaultCommandTimeout);
|
||||
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(tokenSource.Token, token);
|
||||
|
||||
clientResponse.Wait(linkedTokenSource.Token);
|
||||
|
||||
return response;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IGameEventSubscriptions.ClientMessaged -= OnResponse;
|
||||
}
|
||||
|
||||
async Task OnResponse(ClientMessageEvent messageEvent, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!messageEvent.Origin.ClientId.Equals(client.ClientId) || cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
response = messageEvent.Message;
|
||||
|
||||
if (await validator(response))
|
||||
{
|
||||
clientResponse.Set();
|
||||
}
|
||||
else
|
||||
{
|
||||
await client.TellAsync([prompt], cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user