|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const main = @import("main"); |
| 4 | +const ConnectionManager = main.network.ConnectionManager; |
| 5 | +const settings = main.settings; |
| 6 | +const Vec2f = main.vec.Vec2f; |
| 7 | + |
| 8 | +const gui = @import("../gui.zig"); |
| 9 | +const GuiComponent = gui.GuiComponent; |
| 10 | +const GuiWindow = gui.GuiWindow; |
| 11 | +const Button = @import("../components/Button.zig"); |
| 12 | +const Label = @import("../components/Label.zig"); |
| 13 | +const TextInput = @import("../components/TextInput.zig"); |
| 14 | +const VerticalList = @import("../components/VerticalList.zig"); |
| 15 | +const HorizontalList = @import("../components/HorizontalList.zig"); |
| 16 | + |
| 17 | +pub var window = GuiWindow{ |
| 18 | + .contentSize = Vec2f{128, 256}, |
| 19 | + .closeIfMouseIsGrabbed = true, |
| 20 | +}; |
| 21 | + |
| 22 | +const padding: f32 = 8; |
| 23 | +var userList: []*main.server.User = &.{}; |
| 24 | +var entityCount: u32 = 0; |
| 25 | + |
| 26 | +fn kickbyConnection(conn: *main.network.Connection) void { |
| 27 | + conn.disconnect(); |
| 28 | +} |
| 29 | + |
| 30 | +fn kickByPlayerIndex(playerIndex: usize) void { |
| 31 | + const command = std.fmt.allocPrint(main.globalAllocator.allocator, "kick @{d}", .{playerIndex}) catch unreachable; |
| 32 | + main.sync.ClientSide.executeCommand(.{.chatCommand = .{.message = command}}); |
| 33 | +} |
| 34 | + |
| 35 | +pub fn onOpen() void { |
| 36 | + const list = VerticalList.init(.{padding, 16 + padding}, 300, 16); |
| 37 | + if (main.server.world == null) blk: { |
| 38 | + entityCount = main.client.entity_manager.entities.len; |
| 39 | + if (entityCount == 0) { |
| 40 | + list.add(Label.init(.{0, 0}, 200, "No other players", .left)); |
| 41 | + break :blk; |
| 42 | + } |
| 43 | + |
| 44 | + for (main.client.entity_manager.entities.items()) |ent| { |
| 45 | + if (ent.playerIndex == null) continue; |
| 46 | + const row = HorizontalList.init(); |
| 47 | + |
| 48 | + const string = std.fmt.allocPrint(main.stackAllocator.allocator, "{f}", .{std.fmt.alt(ent, .formatWithPlayerIndex)}) catch unreachable; |
| 49 | + defer main.stackAllocator.free(string); |
| 50 | + row.add(Label.init(.{0, 0}, 200, string, .left)); |
| 51 | + row.add(Button.initText(.{0, 0}, 100, "Kick", .initWithInt(kickByPlayerIndex, ent.playerIndex.?))); |
| 52 | + list.add(row); |
| 53 | + } |
| 54 | + } else { |
| 55 | + main.server.connectionManager.mutex.lock(); |
| 56 | + defer main.server.connectionManager.mutex.unlock(); |
| 57 | + std.debug.assert(userList.len == 0); |
| 58 | + userList = main.globalAllocator.alloc(*main.server.User, main.server.connectionManager.connections.items.len); |
| 59 | + for (main.server.connectionManager.connections.items, 0..) |connection, i| { |
| 60 | + userList[i] = connection.user.?; |
| 61 | + userList[i].increaseRefCount(); |
| 62 | + if (userList[i].id == main.game.Player.id and connection.isConnected()) continue; |
| 63 | + const row = HorizontalList.init(); |
| 64 | + if (connection.handShakeState.load(.monotonic) == .complete) { |
| 65 | + const string = std.fmt.allocPrint(main.stackAllocator.allocator, "{f}", .{connection.user.?}) catch unreachable; |
| 66 | + defer main.stackAllocator.free(string); |
| 67 | + row.add(Label.init(.{0, 0}, 200, string, .left)); |
| 68 | + row.add(Button.initText(.{0, 0}, 100, "Kick", .initWithPtr(kickbyConnection, connection))); |
| 69 | + } else { |
| 70 | + const ip = std.fmt.allocPrint(main.stackAllocator.allocator, "{f}", .{connection.remoteAddress}) catch unreachable; |
| 71 | + defer main.stackAllocator.free(ip); |
| 72 | + row.add(Label.init(.{0, 0}, 200, ip, .left)); |
| 73 | + row.add(Button.initText(.{0, 0}, 100, "Cancel", .initWithPtr(kickbyConnection, connection))); |
| 74 | + } |
| 75 | + list.add(row); |
| 76 | + } |
| 77 | + if (userList.len == 1) { |
| 78 | + list.add(Label.init(.{0, 0}, 200, "No other players", .left)); |
| 79 | + } |
| 80 | + } |
| 81 | + list.finish(.center); |
| 82 | + window.rootComponent = list.toComponent(); |
| 83 | + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @as(Vec2f, @splat(padding)); |
| 84 | + gui.updateWindowPositions(); |
| 85 | +} |
| 86 | + |
| 87 | +pub fn onClose() void { |
| 88 | + if (main.server.world != null) { |
| 89 | + for (userList) |user| { |
| 90 | + user.decreaseRefCount(); |
| 91 | + } |
| 92 | + main.globalAllocator.free(userList); |
| 93 | + userList = &.{}; |
| 94 | + } |
| 95 | + if (window.rootComponent) |*comp| { |
| 96 | + comp.deinit(); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +pub fn update() void { |
| 101 | + if (main.server.world == null) { |
| 102 | + if (main.client.entity_manager.entities.len != entityCount) { |
| 103 | + onClose(); |
| 104 | + onOpen(); |
| 105 | + } |
| 106 | + } else { |
| 107 | + main.server.connectionManager.mutex.lock(); |
| 108 | + const serverListLen = main.server.connectionManager.connections.items.len; |
| 109 | + main.server.connectionManager.mutex.unlock(); |
| 110 | + if (userList.len != serverListLen) { |
| 111 | + onClose(); |
| 112 | + onOpen(); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments