59 lines
1.6 KiB
Zig
59 lines
1.6 KiB
Zig
const std = @import("std");
|
|
const Store = @import("../store.zig");
|
|
const json_utils = @import("../json_utils.zig");
|
|
const json = std.json;
|
|
|
|
const DropRate = @This();
|
|
|
|
item_id: Store.CodeId,
|
|
rate: u64,
|
|
min_quantity: u64,
|
|
max_quantity: u64,
|
|
|
|
pub fn parse(store: *Store, obj: json.ObjectMap) !DropRate {
|
|
const rate = try json_utils.getIntegerRequired(obj, "rate");
|
|
if (rate < 1) {
|
|
return error.InvalidRate;
|
|
}
|
|
|
|
const min_quantity = try json_utils.getIntegerRequired(obj, "min_quantity");
|
|
if (min_quantity < 1) {
|
|
return error.InvalidMinQuantity;
|
|
}
|
|
|
|
const max_quantity = try json_utils.getIntegerRequired(obj, "max_quantity");
|
|
if (max_quantity < 1) {
|
|
return error.InvalidMinQuantity;
|
|
}
|
|
|
|
const code_str = try json_utils.getStringRequired(obj, "code");
|
|
const item_id = try store.getItemId(code_str);
|
|
|
|
return DropRate{
|
|
.item_id = item_id,
|
|
.rate = @intCast(rate),
|
|
.min_quantity = @intCast(min_quantity),
|
|
.max_quantity = @intCast(max_quantity)
|
|
};
|
|
}
|
|
|
|
pub const DropRates = std.BoundedArray(DropRate, 8); // TODO: Maybe rename to "List"?
|
|
|
|
pub fn parseList(store: *Store, array: json.Array) !DropRates {
|
|
var drops = DropRates.init(0) catch unreachable;
|
|
for (array.items) |drop_value| {
|
|
const drop_obj = json_utils.asObject(drop_value) orelse return error.InvalidObject;
|
|
try drops.append(try DropRate.parse(store, drop_obj));
|
|
}
|
|
return drops;
|
|
}
|
|
|
|
pub fn doesListContain(drops: *DropRates, item_id: Store.CodeId) bool {
|
|
for (drops.constSlice()) |drop| {
|
|
if (drop.item_id == item_id) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|