const std = @import("std"); const Store = @import("../store.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_id: Store.CodeId, skill: Skill, level: u64, drops: DropRates, pub fn parse(store: *Store, 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 store.getCodeIdJson(allocator, obj, "code")) orelse return error.MissingProperty, .level = @intCast(level), .skill = SkillUtils.fromString(skill_str) orelse return error.InvalidSkill, .drops = try DropRate.parseList(store, drops_array) }; } pub fn deinit(self: Resource, allocator: Allocator) void { allocator.free(self.name); }