From c887b25ef8c4d29c6d2c296c74e9226e6576805b Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Sun, 4 May 2025 20:38:44 +0100 Subject: [PATCH] WIP --- demo/src/app/lib/Game.zig | 16 +++++--- demo/src/app/views/websockets.zig | 14 ++++--- demo/src/app/views/websockets/index.zmpl | 3 ++ src/jetzig/channels/Channel.zig | 47 +++++++++++++++--------- src/jetzig/http/Server.zig | 2 + src/jetzig/websockets/Websocket.zig | 6 +++ 6 files changed, 60 insertions(+), 28 deletions(-) diff --git a/demo/src/app/lib/Game.zig b/demo/src/app/lib/Game.zig index eaf9994..32daafc 100644 --- a/demo/src/app/lib/Game.zig +++ b/demo/src/app/lib/Game.zig @@ -52,10 +52,6 @@ pub fn evaluate(game: *Game) void { for (game.grid) |cell| { if (cell == .empty) full = false; } - if (full) { - game.victor = .tie; - return; - } const patterns = [_][3]usize{ .{ 0, 1, 2 }, @@ -77,7 +73,15 @@ pub fn evaluate(game: *Game) void { } std.debug.assert(!(cpu_victor and player_victor)); - if (cpu_victor) game.victor = .cpu; - if (player_victor) game.victor = .player; + if (cpu_victor) { + game.victor = .cpu; + break; + } + if (player_victor) { + game.victor = .player; + break; + } + } else if (full) { + game.victor = .tie; } } diff --git a/demo/src/app/views/websockets.zig b/demo/src/app/views/websockets.zig index 168bcd8..a823d4f 100644 --- a/demo/src/app/views/websockets.zig +++ b/demo/src/app/views/websockets.zig @@ -11,14 +11,18 @@ pub fn index(request: *jetzig.Request) !jetzig.View { pub const Channel = struct { pub fn open(channel: jetzig.channels.Channel) !void { - var state = try channel.connect("game"); + var state = try channel.state("game"); if (state.get("cells") == null) try initGame(channel); try channel.sync(); } pub const Actions = struct { + pub fn join(channel: jetzig.channels.Channel, token: []const u8) !void { + try channel.connect("game", token); + } + pub fn move(channel: jetzig.channels.Channel, cell: usize) !void { - var state = try channel.connect("game"); + var state = try channel.state("game"); const cells = state.getT(.array, "cells") orelse { return; }; @@ -42,14 +46,14 @@ pub const Channel = struct { }; fn resetGame(channel: jetzig.channels.Channel) !void { - var state = try channel.connect("game"); + var state = try channel.state("game"); try state.put("victor", null); var cells = try state.put("cells", .array); for (0..9) |_| try cells.append(null); } fn initGame(channel: jetzig.channels.Channel) !void { - var state = try channel.connect("game"); + var state = try channel.state("game"); var results = try state.put("results", .object); try results.put("cpu", 0); try results.put("player", 0); @@ -74,7 +78,7 @@ pub const Channel = struct { } fn setVictor(channel: jetzig.channels.Channel, victor: Game.State) !void { - var state = try channel.connect("game"); + var state = try channel.state("game"); try state.put("victor", @tagName(victor)); var results = state.getT(.object, "results") orelse return; const count = results.getT(.integer, @tagName(victor)) orelse return; diff --git a/demo/src/app/views/websockets/index.zmpl b/demo/src/app/views/websockets/index.zmpl index 6f07fe8..07b8b4f 100644 --- a/demo/src/app/views/websockets/index.zmpl +++ b/demo/src/app/views/websockets/index.zmpl @@ -43,6 +43,9 @@ > 🏆 + +

Share this link to invite another player

+