Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hopefully save the hand and also when the player leaves or server closes #943

Merged
merged 17 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/Inventory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ pub const Sync = struct { // MARK: Sync
}
}
},
.playerInventory, .other => {},
.playerInventory, .hand, .other => {},
.alreadyFreed => unreachable,
}
const inventory = ServerInventory.init(len, typ, source);
Expand All @@ -329,6 +329,21 @@ pub const Sync = struct { // MARK: Sync

inventory.inv.loadFromZon(inventoryZon);
},
.hand => {
const dest: []u8 = main.stackAllocator.alloc(u8, std.base64.url_safe.Encoder.calcSize(user.name.len));
defer main.stackAllocator.free(dest);
const hashedName = std.base64.url_safe.Encoder.encode(dest, user.name);

const path = std.fmt.allocPrint(main.stackAllocator.allocator, "saves/{s}/players/{s}.zig.zon", .{main.server.world.?.name, hashedName}) catch unreachable;
defer main.stackAllocator.free(path);

const playerData = main.files.readToZon(main.stackAllocator, path) catch .null;
defer playerData.deinit(main.stackAllocator);

const inventoryZon = playerData.getChild("hand");

inventory.inv.loadFromZon(inventoryZon);
},
OneAvargeCoder193 marked this conversation as resolved.
Show resolved Hide resolved
.other => {},
.alreadyFreed => unreachable,
}
Expand All @@ -349,11 +364,13 @@ pub const Sync = struct { // MARK: Sync
return inventories.items[serverId].inv;
}

pub fn getInventoryFromSource(source: SourceType) ?Inventory {
pub fn getUserInventoryFromSource(user: *main.server.User, source: SourceType) ?Inventory {
OneAvargeCoder193 marked this conversation as resolved.
Show resolved Hide resolved
main.utils.assertLocked(&mutex);
for(inventories.items) |inv| {
if (inv.source == source) {
return inv.inv;
for (inv.users.items) |u| {
OneAvargeCoder193 marked this conversation as resolved.
Show resolved Hide resolved
if (u == user and inv.source == source) {
return inv.inv;
}
}
}
return null;
Expand Down Expand Up @@ -883,7 +900,7 @@ pub const Command = struct { // MARK: Command
data.append(@intFromEnum(self.inv.type));
data.append(@intFromEnum(self.source));
switch(self.source) {
.playerInventory, .sharedTestingInventory, .other => {},
.playerInventory, .sharedTestingInventory, .hand, .other => {},
.alreadyFreed => unreachable,
}
}
Expand All @@ -898,6 +915,7 @@ pub const Command = struct { // MARK: Command
const source: Source = switch(sourceType) {
.playerInventory => .{.playerInventory = {}},
.sharedTestingInventory => .{.sharedTestingInventory = {}},
.hand => .{.hand = {}},
.other => .{.other = {}},
.alreadyFreed => unreachable,
};
Expand Down Expand Up @@ -1420,12 +1438,14 @@ const SourceType = enum(u8) {
alreadyFreed = 0,
playerInventory = 1,
sharedTestingInventory = 2,
hand = 3,
other = 0xff, // TODO: List every type separately here.
};
const Source = union(SourceType) {
alreadyFreed: void,
playerInventory: void,
sharedTestingInventory: void,
hand: void,
other: void,
};

Expand Down
5 changes: 5 additions & 0 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ pub const World = struct { // MARK: World
main.gui.deinit();
main.gui.init();

main.server.world.?.saveAllPlayers() catch |err| {
std.log.err("Failed to save players in server: {s}", .{@errorName(err)});
return;
};

OneAvargeCoder193 marked this conversation as resolved.
Show resolved Hide resolved
Player.inventory.deinit(main.globalAllocator);

main.threadPool.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/gui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ pub const inventory = struct { // MARK: inventory
var initialized: bool = false;

pub fn init() void {
carried = Inventory.init(main.globalAllocator, 1, .normal, .other);
carried = Inventory.init(main.globalAllocator, 1, .normal, .hand);
leftClickSlots = .init(main.globalAllocator);
rightClickSlots = .init(main.globalAllocator);
carriedItemSlot = ItemSlot.init(.{0, 0}, carried, 0, .default, .normal);
Expand Down
6 changes: 6 additions & 0 deletions src/server/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ pub const User = struct { // MARK: User

pub fn deinit(self: *User) void {
std.debug.assert(self.refCount.load(.monotonic) == 0);

world.?.savePlayer(self) catch |err| {
OneAvargeCoder193 marked this conversation as resolved.
Show resolved Hide resolved
std.log.err("Failed to save player: {s}", .{@errorName(err)});
return;
};

main.items.Inventory.Sync.ServerSide.disconnectUser(self);
std.debug.assert(self.inventoryClientToServerIdMap.count() == 0); // leak
self.inventoryClientToServerIdMap.deinit();
Expand Down
6 changes: 5 additions & 1 deletion src/server/world.zig
Original file line number Diff line number Diff line change
Expand Up @@ -815,9 +815,13 @@ pub const ServerWorld = struct { // MARK: ServerWorld
{
main.items.Inventory.Sync.ServerSide.mutex.lock();
defer main.items.Inventory.Sync.ServerSide.mutex.unlock();
if (main.items.Inventory.Sync.ServerSide.getInventoryFromSource(.playerInventory)) |inv| {
if (main.items.Inventory.Sync.ServerSide.getUserInventoryFromSource(user, .playerInventory)) |inv| {
playerZon.put("inventory", inv.save(main.stackAllocator));
}

if (main.items.Inventory.Sync.ServerSide.getUserInventoryFromSource(user, .hand)) |inv| {
playerZon.put("hand", inv.save(main.stackAllocator));
}
}

const playerPath = std.fmt.allocPrint(main.stackAllocator.allocator, "saves/{s}/players", .{self.name}) catch unreachable;
Expand Down
Loading