aboutsummaryrefslogtreecommitdiff
path: root/src/methods/execute_command.zig
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2024-05-05 16:51:48 +0200
committerJan200101 <sentrycraft123@gmail.com>2024-05-05 16:51:48 +0200
commit54365b02ef3e36a631354aa1af3bd33f90db3cf7 (patch)
treeb7f02a04c28437a1c44c427c440fe3f0750bed04 /src/methods/execute_command.zig
downloadSouthRPC-54365b02ef3e36a631354aa1af3bd33f90db3cf7.tar.gz
SouthRPC-54365b02ef3e36a631354aa1af3bd33f90db3cf7.zip
Zig rework
Diffstat (limited to 'src/methods/execute_command.zig')
-rw-r--r--src/methods/execute_command.zig53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/methods/execute_command.zig b/src/methods/execute_command.zig
new file mode 100644
index 0000000..c253738
--- /dev/null
+++ b/src/methods/execute_command.zig
@@ -0,0 +1,53 @@
+const std = @import("std");
+
+const engine = @import("../engine.zig");
+
+const Allocator = std.mem.Allocator;
+
+pub const method = .{
+ .name = "execute_command",
+ .func = execute_command,
+};
+
+fn execute_command(allocator: Allocator, params: ?std.json.Value) !?std.json.Value {
+ if (params == null) {
+ return error.InvalidParameters;
+ }
+
+ const command = blk: {
+ const command_item = switch (params.?) {
+ .array => param_blk: {
+ const command_array = params.?.array;
+
+ if (command_array.capacity < 1) {
+ return error.NoCommand;
+ }
+
+ break :param_blk command_array.items[0];
+ },
+
+ .object => param_blk: {
+ const command_object = params.?.object;
+
+ break :param_blk command_object.get("command") orelse return error.NoCommand;
+ },
+
+ else => return error.NoCommand,
+ };
+
+ if (command_item != .string) {
+ return error.InvalidCommand;
+ }
+
+ break :blk try allocator.dupeZ(u8, command_item.string);
+ };
+
+ if (engine.Cbuf) |Cbuf| {
+ const cur_player = Cbuf.GetCurrentPlayer();
+ Cbuf.AddText(cur_player, command, .kCommandSrcCode);
+ } else {
+ return error.NoEngine;
+ }
+
+ return null;
+}