82 lines
2.3 KiB
Zig
82 lines
2.3 KiB
Zig
const std = @import("std");
|
|
const Server = @import("../server.zig");
|
|
const json_utils = @import("../json_utils.zig");
|
|
const json = std.json;
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
const DropRate = @import("./drop_rate.zig");
|
|
const DropRates = DropRate.DropRates;
|
|
|
|
const Monster = @This();
|
|
|
|
pub const ElementalStats = struct {
|
|
attack: i64,
|
|
resistance: i64,
|
|
|
|
pub fn parse(object: json.ObjectMap, attack: []const u8, resistance: []const u8) !ElementalStats {
|
|
return ElementalStats{
|
|
.attack = try json_utils.getIntegerRequired(object, attack),
|
|
.resistance = try json_utils.getIntegerRequired(object, resistance),
|
|
};
|
|
}
|
|
};
|
|
|
|
name: []u8,
|
|
code: []u8,
|
|
level: u64,
|
|
hp: u64,
|
|
min_gold: u64,
|
|
max_gold: u64,
|
|
|
|
fire: ElementalStats,
|
|
earth: ElementalStats,
|
|
water: ElementalStats,
|
|
air: ElementalStats,
|
|
|
|
drops: DropRates,
|
|
|
|
pub fn parse(api: *Server, obj: json.ObjectMap, allocator: Allocator) !Monster {
|
|
const drops_array = json_utils.getArray(obj, "drops") orelse return error.MissingProperty;
|
|
|
|
const min_gold = try json_utils.getIntegerRequired(obj, "min_gold");
|
|
if (min_gold < 0) {
|
|
return error.InvalidMinGold;
|
|
}
|
|
|
|
const max_gold = try json_utils.getIntegerRequired(obj, "max_gold");
|
|
if (max_gold < 0) {
|
|
return error.InvalidMaxGold;
|
|
}
|
|
|
|
const level = try json_utils.getIntegerRequired(obj, "level");
|
|
if (level < 0) {
|
|
return error.InvalidLevel;
|
|
}
|
|
|
|
const hp = try json_utils.getIntegerRequired(obj, "hp");
|
|
if (hp < 0) {
|
|
return error.InvalidHp;
|
|
}
|
|
|
|
return Monster{
|
|
.name = try json_utils.dupeStringRequired(allocator, obj, "name"),
|
|
.code = try json_utils.dupeStringRequired(allocator, obj, "code"),
|
|
.level = @intCast(level),
|
|
.hp = @intCast(hp),
|
|
|
|
.fire = try ElementalStats.parse(obj, "attack_fire" , "res_fire" ),
|
|
.earth = try ElementalStats.parse(obj, "attack_earth", "res_earth"),
|
|
.water = try ElementalStats.parse(obj, "attack_water", "res_water"),
|
|
.air = try ElementalStats.parse(obj, "attack_air" , "res_air" ),
|
|
|
|
.min_gold = @intCast(min_gold),
|
|
.max_gold = @intCast(max_gold),
|
|
.drops = try DropRate.parseList(api, drops_array)
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: Monster, allocator: Allocator) void {
|
|
allocator.free(self.name);
|
|
allocator.free(self.code);
|
|
}
|