mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-07 21:58:06 -05:00
Refactor PromptClientInput to accept string array for prompt (#336)
* Refactor PromptClientInput to accept string array for prompt Updated the `PromptClientInput` method to accept a string array instead of a single string. This change ensures that multiple prompts can be handled, improving input flexibility and client communication. Additionally, modified related method calls to maintain consistency with the new input type. * Refactor PromptClientInput to support parsed result and errors Updated PromptClientInput to handle parsed input results and return error messages instead of raw strings. Introduced ParsedInputResult<TResult> class to encapsulate parsing results and errors, enhancing client validation and feedback mechanism.
This commit is contained in:
parent
83f80b4fd2
commit
4d1e7c2692
16
SharedLibraryCore/Helpers/ParsedInputResult.cs
Normal file
16
SharedLibraryCore/Helpers/ParsedInputResult.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SharedLibraryCore.Helpers;
|
||||||
|
|
||||||
|
public class ParsedInputResult<TResult>
|
||||||
|
{
|
||||||
|
public TResult? Result { get; set; }
|
||||||
|
public string? RawInput { get; set; }
|
||||||
|
public List<string> ErrorMessages { get; set; } = [];
|
||||||
|
|
||||||
|
public ParsedInputResult<TResult> WithError(string errorMessage)
|
||||||
|
{
|
||||||
|
ErrorMessages.Add(errorMessage);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
@ -1375,27 +1375,31 @@ namespace SharedLibraryCore
|
|||||||
public static void ExecuteAfterDelay(this Func<CancellationToken, Task> action, int delayMs,
|
public static void ExecuteAfterDelay(this Func<CancellationToken, Task> action, int delayMs,
|
||||||
CancellationToken token = default) => ExecuteAfterDelay(delayMs, action, token);
|
CancellationToken token = default) => ExecuteAfterDelay(delayMs, action, token);
|
||||||
|
|
||||||
public static async Task<string> PromptClientInput(this EFClient client, string prompt, Func<string, Task<bool>> validator,
|
public static async Task<ParsedInputResult<TResult>> PromptClientInput<TResult>(this EFClient client, string[] prompts,
|
||||||
CancellationToken token = default)
|
Func<string, Task<ParsedInputResult<TResult>>> parser, string tokenExpiredMessage, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
var clientResponse = new ManualResetEventSlim(false);
|
var clientResponse = new ManualResetEventSlim(false);
|
||||||
string response = null;
|
ParsedInputResult<TResult>? response = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
IGameEventSubscriptions.ClientMessaged += OnResponse;
|
IGameEventSubscriptions.ClientMessaged += OnResponse;
|
||||||
await client.TellAsync([prompt], token);
|
await client.TellAsync(prompts, token);
|
||||||
|
|
||||||
using var tokenSource = new CancellationTokenSource(DefaultCommandTimeout);
|
using var tokenSource = new CancellationTokenSource(DefaultCommandTimeout);
|
||||||
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(tokenSource.Token, token);
|
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(tokenSource.Token, token);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
clientResponse.Wait(linkedTokenSource.Token);
|
clientResponse.Wait(linkedTokenSource.Token);
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
return null;
|
await client.TellAsync([tokenExpiredMessage], token);
|
||||||
|
return new ParsedInputResult<TResult> { ErrorMessages = [tokenExpiredMessage] };
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@ -1410,16 +1414,17 @@ namespace SharedLibraryCore
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
response = messageEvent.Message;
|
response = await parser(messageEvent.Message);
|
||||||
|
response.RawInput = messageEvent.Message;
|
||||||
|
|
||||||
if (await validator(response))
|
if (response.ErrorMessages.Count is 0)
|
||||||
{
|
{
|
||||||
// ReSharper disable once AccessToDisposedClosure
|
// ReSharper disable once AccessToDisposedClosure
|
||||||
clientResponse.Set();
|
clientResponse.Set();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await client.TellAsync([prompt], cancellationToken);
|
await client.TellAsync(response.ErrorMessages.Concat(prompts), cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user