Skip to content

Commit 0da73a7

Browse files
committed
Dig resources and handle winning and losing
1 parent fd1b98e commit 0da73a7

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/client.zig

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,29 @@ pub const Client = struct {
167167
if (std.mem.eql(u8, playerName, &self.name)) {
168168
try std.io.getStdOut().writer().print("lost all units\nrejoining...\n", .{});
169169
self.game.?.clear();
170-
self.state = State.Connected;
170+
self.state = State.Ready;
171171
try self.send("j\n");
172172
} else {
173173
print("{s} left\n", .{playerName});
174174
try self.game.?.deletePlayer(playerName);
175175
}
176176
self.game.?.board.printUnits(&self.game.?);
177177
},
178+
@intFromEnum(Message.Type.lost) => {
179+
const playerName = buff[1..]; // player name
180+
try std.io.getStdOut().writer().print("lost the game to {s}\nrejoining...\n", .{playerName});
181+
self.game.?.clear();
182+
self.state = State.Ready;
183+
try self.send("j\n");
184+
},
185+
@intFromEnum(Message.Type.win) => {
186+
const playerName = buff[1..]; // player name
187+
std.debug.assert(std.mem.eql(u8, playerName, &self.name));
188+
try std.io.getStdOut().writer().print("won the game\nrejoining...\n", .{});
189+
self.game.?.clear();
190+
self.state = State.Ready;
191+
try self.send("j\n");
192+
},
178193
@intFromEnum(Message.Type.move) => {
179194
var it = std.mem.tokenizeAny(u8, buff[1..], " \n");
180195

@@ -223,14 +238,19 @@ pub const Client = struct {
223238
const cds = coordOps.towards(unit.*.x, unit.*.y, fc.x, fc.y);
224239
const max_len = 10; // TO DO figure out max_len from config sent by server
225240
var buf: [max_len]u8 = undefined;
226-
try self.send("m");
227-
try self.send(try std.fmt.bufPrint(&buf, "{}", .{unit.*.id}));
228-
try self.send(" ");
229-
try self.send(try std.fmt.bufPrint(&buf, "{}", .{cds.x}));
230-
try self.send(" ");
231-
try self.send(try std.fmt.bufPrint(&buf, "{}", .{cds.y}));
232-
try self.send("\n");
233-
print("move from {d} {d}\n", .{ unit.*.x, unit.*.y });
241+
if (cds.x == unit.*.x and cds.y == unit.*.y) {
242+
try self.send("d");
243+
try self.send(try std.fmt.bufPrint(&buf, "{}", .{unit.*.id}));
244+
try self.send("\n");
245+
} else {
246+
try self.send("m");
247+
try self.send(try std.fmt.bufPrint(&buf, "{}", .{unit.*.id}));
248+
try self.send(" ");
249+
try self.send(try std.fmt.bufPrint(&buf, "{}", .{cds.x}));
250+
try self.send(" ");
251+
try self.send(try std.fmt.bufPrint(&buf, "{}", .{cds.y}));
252+
try self.send("\n");
253+
}
234254
}
235255
}
236256
},

src/coordinateOps.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ pub fn towards(sx: u32, sy: u32, tx: u32, ty: u32) coords {
2626
return cds;
2727
}
2828

29+
pub fn same(x1: u32, y1: u32, x2: u32, y2: u32) bool {
30+
return (x1 == x2 and y1 == y2);
31+
}
32+
2933
pub const coords = struct {
3034
x: u32,
3135
y: u32,
@@ -37,4 +41,8 @@ pub const coords = struct {
3741
pub fn towards(sc: coords, tc: coords) coords {
3842
return ops.towards(sc.x, sc.y, tc.x, tc.y);
3943
}
44+
45+
pub fn same(c1: coords, c2: coords) bool {
46+
return (c1.x == c2.x and c1.y == c2.y);
47+
}
4048
};

0 commit comments

Comments
 (0)