artificer/api/schemas/resource.zig

53 lines
1.5 KiB
Zig

const std = @import("std");
const Server = @import("../server.zig");
const EnumStringUtils = @import("../enum_string_utils.zig").EnumStringUtils;
const json_utils = @import("../json_utils.zig");
const json = std.json;
const Allocator = std.mem.Allocator;
const DropRate = @import("./drop_rate.zig");
const DropRates = DropRate.DropRates;
const Resource = @This();
pub const Skill = enum {
mining,
woodcutting,
fishing,
};
pub const SkillUtils = EnumStringUtils(Skill, .{
.{ "mining" , .mining },
.{ "woodcutting", .woodcutting },
.{ "fishing" , .fishing },
});
name: []u8,
code: []u8,
skill: Skill,
level: u64,
drops: DropRates,
pub fn parse(api: *Server, obj: json.ObjectMap, allocator: Allocator) !Resource {
const drops_array = json_utils.getArray(obj, "drops") orelse return error.MissingProperty;
const level = try json_utils.getIntegerRequired(obj, "level");
if (level < 0) {
return error.InvalidLevel;
}
const skill_str = try json_utils.getStringRequired(obj, "skill");
return Resource{
.name = try json_utils.dupeStringRequired(allocator, obj, "name"),
.code = try json_utils.dupeStringRequired(allocator, obj, "code"),
.level = @intCast(level),
.skill = SkillUtils.fromString(skill_str) orelse return error.InvalidSkill,
.drops = try DropRate.parseList(api, drops_array)
};
}
pub fn deinit(self: Resource, allocator: Allocator) void {
allocator.free(self.name);
allocator.free(self.code);
}