2022-02-18 01:56:12 +00:00
|
|
|
|
namespace Server;
|
|
|
|
|
|
|
|
|
|
public static class CommandHandler {
|
2022-03-15 05:59:23 +00:00
|
|
|
|
public delegate Response Handler(string[] args);
|
2022-02-18 01:56:12 +00:00
|
|
|
|
|
|
|
|
|
public static Dictionary<string, Handler> Handlers = new Dictionary<string, Handler>();
|
|
|
|
|
|
|
|
|
|
static CommandHandler() {
|
|
|
|
|
RegisterCommand("help", _ => $"Valid commands: {string.Join(", ", Handlers.Keys)}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RegisterCommand(string name, Handler handler) {
|
|
|
|
|
Handlers[name] = handler;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 00:48:24 +00:00
|
|
|
|
public static void RegisterCommandAliases(Handler handler, params string[] names) {
|
|
|
|
|
foreach (string name in names) {
|
|
|
|
|
Handlers.Add(name, handler);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 05:59:23 +00:00
|
|
|
|
public static Response GetResult(string input) {
|
2022-02-18 01:56:12 +00:00
|
|
|
|
try {
|
|
|
|
|
string[] args = input.Split(' ');
|
|
|
|
|
if (args.Length == 0) return "No command entered, see help command for valid commands";
|
|
|
|
|
string commandName = args[0];
|
|
|
|
|
return Handlers.TryGetValue(commandName, out Handler? handler) ? handler(args[1..]) : $"Invalid command {args[0]}, see help command for valid commands";
|
2022-03-01 21:08:53 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2022-02-18 01:56:12 +00:00
|
|
|
|
return $"An error occured while trying to process your command: {e}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-15 05:59:23 +00:00
|
|
|
|
|
|
|
|
|
public class Response {
|
|
|
|
|
public string[] ReturnStrings = null!;
|
|
|
|
|
private Response(){}
|
|
|
|
|
|
|
|
|
|
public static implicit operator Response(string value) => new Response {
|
|
|
|
|
ReturnStrings = value.Split('\n')
|
|
|
|
|
};
|
|
|
|
|
public static implicit operator Response(string[] values) => new Response {
|
|
|
|
|
ReturnStrings = values
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-02-18 01:56:12 +00:00
|
|
|
|
}
|