Skip to content

Commit 1d6f62f

Browse files
committed
Read the whole config
1 parent 0d3b240 commit 1d6f62f

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/client.zig

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ const Stream = std.net.Stream;
33
const Allocator = std.mem.Allocator;
44
const print = std.debug.print;
55
const Message = @import("message.zig");
6-
const Game = @import("game.zig").Game;
7-
const Unit = @import("game.zig").Unit;
6+
const game = @import("game.zig");
7+
const Game = game.Game;
8+
const Unit = game.Unit;
9+
const Config = game.Config;
810
const coordOps = @import("coordinateOps.zig");
911

1012
const singleReadSize = 50;
@@ -61,12 +63,22 @@ pub const Client = struct {
6163
switch (t) {
6264
@intFromEnum(Message.Type.config) => {
6365
if (self.state == State.Connected) {
64-
var it = std.mem.tokenizeAny(u8, buff[1..], " ");
65-
_ = it.next(); // skip millis
66-
_ = it.next(); // skip max players
67-
const x = try std.fmt.parseUnsigned(u32, it.next().?, 10);
68-
const y = try std.fmt.parseUnsigned(u32, it.next().?, 10);
69-
self.game = try Game.init(x, y, allocator);
66+
var it = std.mem.tokenizeAny(u8, buff[1..], " \n");
67+
68+
const c = Config{
69+
.millis = try std.fmt.parseUnsigned(u32, it.next().?, 10),
70+
.maxPlayers = try std.fmt.parseUnsigned(u32, it.next().?, 10),
71+
.boardX = try std.fmt.parseUnsigned(u32, it.next().?, 10),
72+
.boardY = try std.fmt.parseUnsigned(u32, it.next().?, 10),
73+
.unitsToWin = try std.fmt.parseUnsigned(u32, it.next().?, 10),
74+
.startResources = try std.fmt.parseUnsigned(u32, it.next().?, 10),
75+
.resourceHp = try std.fmt.parseUnsigned(u32, it.next().?, 10),
76+
.unitHp = try std.fmt.parseUnsigned(u32, it.next().?, 10),
77+
.unitDamage = try std.fmt.parseUnsigned(u32, it.next().?, 10),
78+
.allowedNameCharacters = it.next().?,
79+
};
80+
81+
self.game = try Game.init(c, allocator);
7082
try self.send("n");
7183
try self.send(&(self.name));
7284
try self.send("\n");

src/game.zig

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@ const print = std.debug.print;
44
const distance = @import("coordinateOps.zig").distance;
55
const coords = @import("coordinateOps.zig").coords;
66

7-
pub const Unit = struct { id: u32 = undefined, hp: u32 = undefined, x: u32 = undefined, y: u32 = undefined, owner: ?*Player = undefined };
7+
pub const Unit = struct {
8+
id: u32 = undefined,
9+
hp: u32 = undefined,
10+
x: u32 = undefined,
11+
y: u32 = undefined,
12+
owner: ?*Player = undefined,
13+
};
814

9-
pub const Field = struct { unit: ?*Unit, res_hp: ?u32 };
15+
pub const Field = struct {
16+
unit: ?*Unit,
17+
res_hp: ?u32,
18+
};
1019

1120
pub const Board = struct {
1221
fields: []Field,
@@ -109,14 +118,52 @@ pub const Player = struct {
109118
}
110119
};
111120

121+
pub const Config = struct {
122+
millis: u32,
123+
maxPlayers: u32,
124+
boardX: u32,
125+
boardY: u32,
126+
startResources: u32,
127+
unitsToWin: u32,
128+
resourceHp: u32,
129+
unitHp: u32,
130+
unitDamage: u32,
131+
allowedNameCharacters: []const u8,
132+
};
133+
112134
pub const Game = struct {
113135
board: Board,
114136
players: std.ArrayList(*Player),
115137
units: std.AutoHashMap(u32, *Unit),
138+
139+
unitsToWin: u32,
140+
resourceHp: u32,
141+
unitHp: u32,
142+
unitDamage: u32,
143+
allowedNameCharacters: []const u8,
144+
116145
allocator: std.mem.Allocator,
117146

118-
pub fn init(x: u32, y: u32, allocator: std.mem.Allocator) !Game {
119-
return Game{ .board = try Board.init(x, y, allocator), .units = std.AutoHashMap(u32, *Unit).init(allocator), .players = std.ArrayList(*Player).init(allocator), .allocator = allocator };
147+
pub fn init(config: Config, allocator: std.mem.Allocator) !Game {
148+
var b = try Board.init(config.boardX, config.boardY, allocator);
149+
errdefer b.deinit();
150+
151+
var p = try std.ArrayList(*Player).initCapacity(allocator, config.maxPlayers);
152+
errdefer p.deinit();
153+
154+
return Game{
155+
.board = b,
156+
.units = std.AutoHashMap(u32, *Unit).init(allocator),
157+
.players = p,
158+
159+
.unitsToWin = config.unitsToWin,
160+
.resourceHp = config.resourceHp,
161+
.unitHp = config.unitHp,
162+
.unitDamage = config.unitDamage,
163+
.allowedNameCharacters = try allocator.dupe(u8, config.allowedNameCharacters),
164+
165+
.allocator = allocator,
166+
};
120167
}
121168

122169
pub fn findPlayer(self: *Game, playerName: []const u8) ?*Player {

0 commit comments

Comments
 (0)