artificer/api/equipment.zig

91 lines
2.7 KiB
Zig

const std = @import("std");
const json_utils = @import("json_utils.zig");
const Server = @import("./server.zig");
const ItemId = Server.ItemId;
const json = std.json;
const Equipment = @This();
pub const Consumable = struct {
id: ?ItemId,
quantity: i64,
fn parse(api: *Server, obj: json.ObjectMap, name: []const u8, quantity: []const u8) !Consumable {
return Consumable{
.id = try api.getItemIdJson(obj, name),
.quantity = try json_utils.getIntegerRequired(obj, quantity),
};
}
};
pub const Slot = enum {
weapon,
shield,
helmet,
body_armor,
leg_armor,
boots,
ring1,
ring2,
amulet,
artifact1,
artifact2,
consumable1,
consumable2,
fn name(self: Slot) []const u8 {
return switch (self) {
.weapon => "weapon",
.shield => "shield",
.helmet => "helmet",
.body_armor => "body_armor",
.leg_armor => "leg_armor",
.boots => "boots",
.ring1 => "ring1",
.ring2 => "ring2",
.amulet => "amulet",
.artifact1 => "artifact1",
.artifact2 => "artifact2",
.consumable1 => "consumable1",
.consumable2 => "consumable2",
};
}
};
weapon: ?ItemId,
shield: ?ItemId,
helmet: ?ItemId,
body_armor: ?ItemId,
leg_armor: ?ItemId,
boots: ?ItemId,
ring1: ?ItemId,
ring2: ?ItemId,
amulet: ?ItemId,
artifact1: ?ItemId,
artifact2: ?ItemId,
artifact3: ?ItemId,
consumable1: Consumable,
consumable2: Consumable,
pub fn parse(api: *Server, obj: json.ObjectMap) !Equipment {
return Equipment{
.weapon = try api.getItemIdJson(obj, "weapon_slot"),
.shield = try api.getItemIdJson(obj, "shield_slot"),
.helmet = try api.getItemIdJson(obj, "helmet_slot"),
.body_armor = try api.getItemIdJson(obj, "body_armor_slot"),
.leg_armor = try api.getItemIdJson(obj, "leg_armor_slot"),
.boots = try api.getItemIdJson(obj, "boots_slot"),
.ring1 = try api.getItemIdJson(obj, "ring1_slot"),
.ring2 = try api.getItemIdJson(obj, "ring2_slot"),
.amulet = try api.getItemIdJson(obj, "amulet_slot"),
.artifact1 = try api.getItemIdJson(obj, "artifact1_slot"),
.artifact2 = try api.getItemIdJson(obj, "artifact2_slot"),
.artifact3 = try api.getItemIdJson(obj, "artifact3_slot"),
.consumable1 = try Consumable.parse(api, obj, "consumable1_slot", "consumable1_slot_quantity"),
.consumable2 = try Consumable.parse(api, obj, "consumable2_slot", "consumable2_slot_quantity"),
};
}