add support for single-argument commands

This commit is contained in:
Elijah R 2024-04-03 20:12:23 -04:00
parent 0440abd027
commit bdee3e57bf

View file

@ -39,7 +39,8 @@ public class CollabVMClient {
private TurnUpdateEventArgs _currentturn; private TurnUpdateEventArgs _currentturn;
private VoteUpdateEventArgs _currentvote; private VoteUpdateEventArgs _currentvote;
private WebProxy? _proxy; private WebProxy? _proxy;
private Dictionary<string, Action<string, string[]>> commands; private Dictionary<string, Action<string, string[]>> commandsSeparatedArgs;
private Dictionary<string, Action<string, string>> commandsOneArg;
// Tasks and related // Tasks and related
private TaskCompletionSource<Node[]> GotNodeList; private TaskCompletionSource<Node[]> GotNodeList;
private TaskCompletionSource<bool> GotConnectionToNode; private TaskCompletionSource<bool> GotConnectionToNode;
@ -92,7 +93,8 @@ public class CollabVMClient {
} }
this.username = username; this.username = username;
this.node = node; this.node = node;
this.commands = new(); this.commandsSeparatedArgs = new();
this.commandsOneArg = new();
this._rank = Rank.Unregistered; this._rank = Rank.Unregistered;
this._perms = Permissions.None; this._perms = Permissions.None;
this._connected = false; this._connected = false;
@ -830,9 +832,18 @@ public class CollabVMClient {
/// Register a command for users on the VM to run /// Register a command for users on the VM to run
/// </summary> /// </summary>
/// <param name="cmd">The command which triggers the callback. For example, "!ban" would match "!ban guest12345"</param> /// <param name="cmd">The command which triggers the callback. For example, "!ban" would match "!ban guest12345"</param>
/// <param name="callback">Function to be called when a user executes the command. The first parameter is a username and the last is an array of arguments</param> /// <param name="callback">Function to be called when a user executes the command. The first parameter is a username and the last is an array of arguments supplied by the user</param>
public void RegisterCommand(string cmd, Action<string, string[]> callback) { public void RegisterCommand(string cmd, Action<string, string[]> callback) {
this.commands.Add(cmd, callback); this.commandsSeparatedArgs.Add(cmd, callback);
}
/// <summary>
/// Register a command for users on the VM to run
/// </summary>
/// <param name="cmd">The command which triggers the callback. For example, "!ban" would match "!ban guest12345"</param>
/// <param name="callback">Function to be called when a user executes the command. The first parameter is a username and the last is a string containing the argument supplied by the user.</param>
public void RegisterCommand(string cmd, Action<string, string> callback) {
this.commandsOneArg.Add(cmd, callback);
} }
/// <summary> /// <summary>
@ -855,8 +866,10 @@ public class CollabVMClient {
catch { catch {
return; return;
} }
if (commands.ContainsKey(args[0])) if (commandsSeparatedArgs.ContainsKey(args[0]))
commands[args[0]](username, args.Skip(1).ToArray()); commandsSeparatedArgs[args[0]](username, args.Skip(1).ToArray());
else if (commandsOneArg.ContainsKey(args[0]))
commandsOneArg[args[0]](username, cmd.Substring(cmd.IndexOf(" ") + 1));
} }
public Image GetFramebuffer() => framebuffer.CloneAs<Rgba32>(); public Image GetFramebuffer() => framebuffer.CloneAs<Rgba32>();