artificer/api/schemas/map.zig

38 lines
1.2 KiB
Zig

const std = @import("std");
const Server = @import("../server.zig");
const Position = @import("../position.zig");
const json_utils = @import("../json_utils.zig");
const json = std.json;
const Allocator = std.mem.Allocator;
const Map = @This();
const MapContent = @import("./map_content.zig");
name: []u8,
skin: []u8,
position: Position,
content: ?MapContent,
pub fn parse(api: *Server, obj: json.ObjectMap, allocator: Allocator) !Map {
const content = json_utils.getObject(obj, "content");
const x = json_utils.getInteger(obj, "x") orelse return error.MissingProperty;
const y = json_utils.getInteger(obj, "y") orelse return error.MissingProperty;
return Map{
.name = (try json_utils.dupeString(allocator, obj, "name")) orelse return error.MissingProperty,
.skin = (try json_utils.dupeString(allocator, obj, "skin")) orelse return error.MissingProperty,
.position = Position.init(x, y),
.content = try MapContent.parse(api, content, allocator)
};
}
pub fn deinit(self: Map, allocator: Allocator) void {
allocator.free(self.name);
allocator.free(self.skin);
if (self.content) |content| {
content.deinit(allocator);
}
}