artificer/api/schemas/task.zig

41 lines
1.1 KiB
Zig

const std = @import("std");
const Server = @import("../server.zig");
const json_utils = @import("../json_utils.zig");
const EnumStringUtils = @import("../enum_string_utils.zig").EnumStringUtils;
const json = std.json;
const ItemId = Server.ItemId;
const Task = @This();
pub const Type = enum {
monsters,
resources,
crafts
};
pub const TypeUtils = EnumStringUtils(Type, .{
.{ "monsters" , Type.monsters },
.{ "resources", Type.resources },
.{ "crafts" , Type.crafts },
});
id: ItemId, // TODO: Refactor `ItemId` to include other object types
type: Type,
total: u64,
pub fn parse(api: *Server, obj: json.ObjectMap) !Task {
const task_type = try json_utils.getStringRequired(obj, "type");
const total = try json_utils.getIntegerRequired(obj, "total");
if (total < 0) {
return error.InvalidTaskTotal;
}
const code = json_utils.getStringRequired(obj, "code");
const id = try api.getItemId(code);
return Task{
.id = id,
.type = TypeUtils.fromString(task_type) orelse return error.InvalidTaskType,
.total = @intCast(total)
};
}