artificer/lib/api/schemas/craft.zig

71 lines
2.4 KiB
Zig

// zig fmt: off
const Store = @import("../store.zig");
const std = @import("std");
const json_utils = @import("../json_utils.zig");
const SimpleItem = @import("./simple_item.zig");
const Character = @import("./character.zig");
const EnumStringUtils = @import("../enum_string_utils.zig").EnumStringUtils;
const Craft = @This();
pub const max_items = 8;
pub const Items = SimpleItem.BoundedArray(max_items);
pub const Skill = enum {
weaponcrafting,
gearcrafting,
jewelrycrafting,
cooking,
woodcutting,
mining,
alchemy,
const Utils = EnumStringUtils(Skill, .{
.{ "weaponcrafting" , Skill.weaponcrafting },
.{ "gearcrafting" , Skill.gearcrafting },
.{ "jewelrycrafting", Skill.jewelrycrafting },
.{ "cooking" , Skill.cooking },
.{ "woodcutting" , Skill.woodcutting },
.{ "mining" , Skill.mining },
.{ "alchemy" , Skill.alchemy },
});
pub const toString = Utils.toString;
pub const fromString = Utils.fromString;
pub fn toCharacterSkill(self: Skill) Character.Skill {
return switch (self) {
.weaponcrafting => Character.Skill.weaponcrafting,
.gearcrafting => Character.Skill.gearcrafting,
.jewelrycrafting => Character.Skill.jewelrycrafting,
.cooking => Character.Skill.cooking,
.woodcutting => Character.Skill.woodcutting,
.mining => Character.Skill.mining,
.alchemy => Character.Skill.alchemy,
};
}
};
skill: Skill,
level: u64,
quantity: u64,
items: Items,
pub fn parse(store: *Store, obj: std.json.ObjectMap) !Craft {
const skill = json_utils.getString(obj, "skill") orelse return error.MissingProperty;
const level = json_utils.getInteger(obj, "level") orelse return error.MissingProperty;
if (level < 1) return error.InvalidLevel;
const quantity = json_utils.getInteger(obj, "quantity") orelse return error.MissingProperty;
if (quantity < 1) return error.InvalidQuantity;
const items = json_utils.getArray(obj, "items") orelse return error.MissingProperty;
return Craft{
.skill = Skill.fromString(skill) orelse return error.InvalidSkill,
.level = @intCast(level),
.quantity = @intCast(quantity),
.items = try Items.parse(store, items)
};
}