47 lines
1.1 KiB
Zig
47 lines
1.1 KiB
Zig
const std = @import("std");
|
|
const Store = @import("../store.zig");
|
|
const BoundedSlotsArray = @import("./slot_array.zig").BoundedSlotsArray;
|
|
const json_utils = @import("../json_utils.zig");
|
|
const json = std.json;
|
|
|
|
const Fight = @This();
|
|
|
|
pub const Drops = BoundedSlotsArray(8);
|
|
|
|
xp: u64,
|
|
gold: u64,
|
|
drops: Drops,
|
|
won: bool,
|
|
|
|
pub fn parse(store: *Store, obj: json.ObjectMap) !Fight {
|
|
const result = try json_utils.getStringRequired(obj, "result");
|
|
|
|
var won = false;
|
|
if (std.mem.eql(u8, result, "win")) {
|
|
won = true;
|
|
} else if (std.mem.eql(u8, result, "lose")) {
|
|
won = false;
|
|
} else {
|
|
return error.InvalidProperty;
|
|
}
|
|
|
|
const drops_obj = json_utils.getArray(obj, "drops") orelse return error.MissingProperty;
|
|
|
|
const xp = try json_utils.getIntegerRequired(obj, "xp");
|
|
if (xp < 0) {
|
|
return error.InvalidXp;
|
|
}
|
|
|
|
const gold = try json_utils.getIntegerRequired(obj, "gold");
|
|
if (gold < 0) {
|
|
return error.InvalidGold;
|
|
}
|
|
|
|
return Fight{
|
|
.xp = @intCast(xp),
|
|
.gold = @intCast(gold),
|
|
.drops = try Drops.parse(store, drops_obj),
|
|
.won = won,
|
|
};
|
|
}
|