37 lines
1020 B
Zig
37 lines
1020 B
Zig
const std = @import("std");
|
|
const Store = @import("../store.zig");
|
|
const json_utils = @import("../json_utils.zig");
|
|
const EnumStringUtils = @import("../enum_string_utils.zig").EnumStringUtils;
|
|
const json = std.json;
|
|
|
|
const Task = @This();
|
|
|
|
pub const Type = enum {
|
|
monsters,
|
|
resources,
|
|
crafts
|
|
};
|
|
pub const TypeUtils = EnumStringUtils(Type, .{
|
|
.{ "monsters" , Type.monsters },
|
|
.{ "resources", Type.resources },
|
|
.{ "crafts" , Type.crafts },
|
|
});
|
|
|
|
code_id: Store.CodeId,
|
|
type: Type,
|
|
total: u64,
|
|
|
|
pub fn parse(store: *Store, obj: json.ObjectMap) !Task {
|
|
const task_type = try json_utils.getStringRequired(obj, "type");
|
|
const total = try json_utils.getIntegerRequired(obj, "total");
|
|
if (total < 0) {
|
|
return error.InvalidTaskTotal;
|
|
}
|
|
|
|
return Task{
|
|
.code_id = (try store.getCodeIdJson(obj, "code")) orelse return error.MissingProperty,
|
|
.type = TypeUtils.fromString(task_type) orelse return error.InvalidTaskType,
|
|
.total = @intCast(total)
|
|
};
|
|
}
|