-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.h
595 lines (519 loc) · 14.3 KB
/
messages.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#pragma once
#include <map>
#include <set>
#include <vector>
#include "buffer.h"
/*
* This header file contains the definitions of all classes used
* store, manipulate, parse and un-parse messages
* between the Client and either the GUI or the game server.
*/
/*
* =============================================================================
* General and simple Data classes
* =============================================================================
*/
/* General class interface for structured data representation. */
template <typename T> concept Data = requires(Buffer & buffer, T & t) {
{ buffer << t } -> std::same_as<Buffer &>;
{ buffer >> t } -> std::same_as<Buffer &>;
};
template <typename T>
concept ComparableData = Data<T> && requires(T & a, T & b) {
{ a < b } -> std::same_as<bool>;
};
/* Integral leaf nodes for structured data representation. */
class DataU8 {
public:
uint8_t value{0};
bool operator<(const DataU8 & other) const {
return value < other.value;
}
};
Buffer & operator<<(Buffer & buffer, const DataU8 & data) {
buffer.writeU8(data.value);
return buffer;
}
Buffer & operator>>(Buffer & buffer, DataU8 & data) {
data.value = buffer.readU8();
return buffer;
}
class DataU16 {
public:
uint16_t value{0};
bool operator<(const DataU16 & other) const {
return value < other.value;
}
};
Buffer & operator<<(Buffer & buffer, const DataU16 & data) {
buffer.writeU16(data.value);
return buffer;
}
Buffer & operator>>(Buffer & buffer, DataU16 & data) {
data.value = buffer.readU16();
return buffer;
}
class DataU32 {
public:
uint32_t value{0};
bool operator<(const DataU32 & other) const {
return value < other.value;
}
};
Buffer & operator<<(Buffer & buffer, const DataU32 & data) {
buffer.writeU32(data.value);
return buffer;
}
Buffer & operator>>(Buffer & buffer, DataU32 & data) {
data.value = buffer.readU32();
return buffer;
}
/* String leaf node for structured data representation. */
class DataString {
public:
std::string value;
bool operator<(const DataString & other) const {
return value < other.value;
}
};
Buffer & operator<<(Buffer & buffer, const DataString & data) {
buffer.writeU8((uint8_t)data.value.size());
buffer.writeStr((data.value));
return buffer;
}
Buffer & operator>>(Buffer & buffer, DataString & data) {
data.value = buffer.readStr(buffer.readU8());
return buffer;
}
/* Internal list node for structured data representation. */
template <Data T> class DataList {
public:
std::vector<T> list;
};
template <Data T>
Buffer & operator<<(Buffer & buffer, const DataList<T> & data) {
buffer.writeU32((uint32_t)data.list.size());
for (const T & i : data.list) {
buffer << i;
}
return buffer;
}
template <Data T> Buffer & operator>>(Buffer & buffer, DataList<T> & data) {
size_t size = buffer.readU32();
data.list.clear();
for (size_t i = 0; i < size; i++) {
T t;
buffer >> t;
data.list.push_back(t);
}
return buffer;
}
/* Internal multiset node for structured data representation. */
template <ComparableData T> class DataSet {
public:
std::set<T> set;
};
template <ComparableData T>
Buffer & operator<<(Buffer & buffer, const DataSet<T> & data) {
buffer.writeU32((uint32_t)data.set.size());
for (const T & i : data.set) {
buffer << i;
}
return buffer;
}
template <ComparableData T>
Buffer & operator>>(Buffer & buffer, DataSet<T> & data) {
size_t size = buffer.readU32();
data.set.clear();
for (size_t i = 0; i < size; i++) {
T t;
buffer >> t;
data.set.insert(t);
}
return buffer;
}
/* Internal map node for structured data representation. */
template <ComparableData K, Data V> class DataMap {
public:
std::map<K, V> map;
};
template <ComparableData K, Data V>
Buffer & operator<<(Buffer & buffer, const DataMap<K, V> & data) {
buffer.writeU32((uint32_t)data.map.size());
for (const auto & i : data.map) {
buffer << i.first << i.second;
}
return buffer;
}
template <ComparableData K, Data V>
Buffer & operator>>(Buffer & buffer, DataMap<K, V> & data) {
data.map.clear();
uint32_t length = buffer.readU32();
for (unsigned int i = 0; i < length; i++) {
K key;
V value;
buffer >> key >> value;
data.map.insert({key, value});
}
return buffer;
}
enum class GameState {
Lobby,
Game
};
/*
* =============================================================================
* Specific Data classes
* =============================================================================
*/
class DataPlayer {
public:
DataString name;
DataString address;
bool operator<(const DataPlayer & other) const {
if (name < other.name) {
return true;
}
if (other.name < name) {
return false;
}
return address < other.address;
}
};
Buffer & operator<<(Buffer & buffer, const DataPlayer & data) {
return buffer << data.name << data.address;
}
Buffer & operator>>(Buffer & buffer, DataPlayer & data) {
return buffer >> data.name >> data.address;
}
enum class DirectionEnum : uint8_t {
Up = 0,
Right = 1,
Down = 2,
Left = 3
};
class DataDirection {
public:
DirectionEnum direction{0};
};
Buffer & operator<<(Buffer & buffer, const DataDirection & data) {
buffer.writeU8(static_cast<uint8_t>(data.direction));
return buffer;
}
Buffer & operator>>(Buffer & buffer, DataDirection & data) {
uint8_t enumValue = buffer.readU8();
if (enumValue > 3) {
throw BadType();
}
data.direction = static_cast<DirectionEnum>(enumValue);
return buffer;
}
class DataPosition {
public:
DataU16 x;
DataU16 y;
bool operator<(const DataPosition & other) const {
if (x < other.x) {
return true;
}
if (other.x < x) {
return false;
}
return y < other.y;
}
};
Buffer & operator<<(Buffer & buffer, const DataPosition & data) {
return buffer << data.x << data.y;
}
Buffer & operator>>(Buffer & buffer, DataPosition & data) {
return buffer >> data.x >> data.y;
}
class DataBomb {
public:
DataPosition position;
DataU16 timer;
bool operator<(const DataBomb & other) const {
if (timer < other.timer) {
return true;
}
if (other.timer < timer) {
return false;
}
return position < other.position;
}
};
Buffer & operator<<(Buffer & buffer, const DataBomb & data) {
return buffer << data.position << data.timer;
}
Buffer & operator>>(Buffer & buffer, DataBomb & data) {
return buffer >> data.position >> data.timer;
}
enum class EventEnum : uint8_t {
BombPlaced = 0,
BombExploded = 1,
PlayerMoved = 2,
BlockPlaced = 3
};
class DataEvent {
public:
EventEnum type{0};
DataU32 bombID;
DataPosition position;
DataList<DataU8> playersDestroyed;
DataList<DataPosition> blocksDestroyed;
DataU8 playerID;
};
Buffer & operator>>(Buffer & buffer, DataEvent & data) {
uint8_t enumValue = buffer.readU8();
if (enumValue > 3) {
throw BadType();
}
data.type = static_cast<EventEnum>(enumValue);
switch (data.type) {
case EventEnum::BombPlaced:
return buffer >> data.bombID >> data.position;
case EventEnum::BombExploded:
return buffer >> data.bombID >> data.playersDestroyed >>
data.blocksDestroyed;
case EventEnum::PlayerMoved:
return buffer >> data.playerID >> data.position;
case EventEnum::BlockPlaced:
return buffer >> data.position;
default:
return buffer;
}
}
Buffer & operator<<(Buffer & buffer, const DataEvent & data) {
buffer.writeU8(static_cast<uint8_t>(data.type));
switch (data.type) {
case EventEnum::BombPlaced:
return buffer << data.bombID << data.position;
case EventEnum::BombExploded:
return buffer << data.bombID << data.playersDestroyed
<< data.blocksDestroyed;
case EventEnum::PlayerMoved:
return buffer << data.playerID << data.position;
case EventEnum::BlockPlaced:
return buffer << data.position;
default:
return buffer;
}
}
/*
* =============================================================================
* Sendable Data classes
* =============================================================================
*/
/*
* These are the four message types mentioned in the project statement.
* They automatically load data from the network into the buffer before parsing.
* They automatically send themselves after successful pasting.
*/
/*
* =============================================================================
* Client-Server messages
* =============================================================================
*/
enum class ClientMessageEnum : uint8_t {
Join = 0,
PlaceBomb = 1,
PlaceBlock = 2,
Move = 3
};
class DataClientMessage {
public:
ClientMessageEnum type{0};
DataString name;
DataDirection direction;
};
Buffer & operator<<(Buffer & buffer, const DataClientMessage & data) {
buffer.writeU8(static_cast<uint8_t>(data.type));
switch (data.type) {
case ClientMessageEnum::Join:
return buffer << data.name << Buffer::eSend;
case ClientMessageEnum::Move:
return buffer << data.direction << Buffer::eSend;
default:
return buffer << Buffer::eSend;
}
}
Buffer & operator>>(Buffer & buffer, DataClientMessage & data) {
buffer >> Buffer::eReceive;
uint8_t enumValue = buffer.readU8();
if (enumValue > 3) {
throw BadType();
}
data.type = static_cast<ClientMessageEnum>(enumValue);
switch (data.type) {
case ClientMessageEnum::Join:
return buffer >> data.name >> Buffer::eEnd;
case ClientMessageEnum::Move:
return buffer >> data.direction >> Buffer::eEnd;
default:
return buffer >> Buffer::eEnd;
}
}
enum class ServerMessageEnum : uint8_t {
Hello = 0,
AcceptedPlayer = 1,
GameStarted = 2,
Turn = 3,
GameEnded = 4
};
class DataServerMessage {
public:
ServerMessageEnum type{0};
DataString serverName;
DataU8 playerCount;
DataU16 sizeX;
DataU16 sizeY;
DataU16 gameLength;
DataU16 explosionRadius;
DataU16 bombTimer;
DataU8 playerID;
DataPlayer player;
DataU16 turn;
DataMap<DataU8, DataPlayer> players;
DataList<DataEvent> events;
DataMap<DataU8, DataU32> scores;
};
Buffer & operator<<(Buffer & buffer, const DataServerMessage & data) {
buffer.writeU8(static_cast<uint8_t>(data.type));
switch (data.type) {
case ServerMessageEnum::Hello:
return buffer << data.serverName << data.playerCount << data.sizeX
<< data.sizeY << data.gameLength << data.explosionRadius
<< data.bombTimer << Buffer::eSend;
case ServerMessageEnum::AcceptedPlayer:
return buffer << data.playerID << data.player << Buffer::eSend;
case ServerMessageEnum::GameStarted:
return buffer << data.players << Buffer::eSend;
case ServerMessageEnum::Turn:
return buffer << data.turn << data.events << Buffer::eSend;
case ServerMessageEnum::GameEnded:
return buffer << data.scores << Buffer::eSend;
default:
return buffer;
}
}
Buffer & operator>>(Buffer & buffer, DataServerMessage & data) {
buffer >> Buffer::eReceive;
uint8_t enumValue = buffer.readU8();
if (enumValue > 4) {
throw BadType();
}
data.type = static_cast<ServerMessageEnum>(enumValue);
switch (data.type) {
case ServerMessageEnum::Hello:
return buffer >> data.serverName >> data.playerCount >> data.sizeX >>
data.sizeY >> data.gameLength >> data.explosionRadius >>
data.bombTimer >> Buffer::eEnd;
case ServerMessageEnum::AcceptedPlayer:
return buffer >> data.playerID >> data.player >> Buffer::eEnd;
case ServerMessageEnum::GameStarted:
return buffer >> data.players >> Buffer::eEnd;
case ServerMessageEnum::Turn:
return buffer >> data.turn >> data.events >> Buffer::eEnd;
case ServerMessageEnum::GameEnded:
return buffer >> data.scores >> Buffer::eEnd;
default:
return buffer;
}
}
/*
* =============================================================================
* Client-GUI messages
* =============================================================================
*/
enum class DrawMessageEnum : uint8_t {
Lobby = 0,
Game = 1
};
class DataDrawMessage {
public:
DrawMessageEnum type{0};
DataString serverName;
DataU8 playerCount;
DataU16 sizeX;
DataU16 sizeY;
DataU16 gameLength;
DataU16 explosionRadius;
DataU16 bombTimer;
DataU16 turn;
DataMap<DataU8, DataPlayer> players;
DataMap<DataU8, DataPosition> playerPositions;
DataSet<DataPosition> blocks;
DataList<DataBomb> bombs;
DataSet<DataPosition> explosions;
DataMap<DataU8, DataU32> scores;
};
Buffer & operator<<(Buffer & buffer, const DataDrawMessage & data) {
buffer.writeU8(static_cast<uint8_t>(data.type));
switch (data.type) {
case DrawMessageEnum::Lobby:
return buffer << data.serverName << data.playerCount << data.sizeX
<< data.sizeY << data.gameLength << data.explosionRadius
<< data.bombTimer << data.players << Buffer::eSend;
case DrawMessageEnum::Game:
return buffer << data.serverName << data.sizeX << data.sizeY
<< data.gameLength << data.turn << data.players
<< data.playerPositions << data.blocks << data.bombs
<< data.explosions << data.scores << Buffer::eSend;
default:
return buffer;
}
}
Buffer & operator>>(Buffer & buffer, DataDrawMessage & data) {
buffer >> Buffer::eReceive;
uint8_t enumValue = buffer.readU8();
if (enumValue > 1) {
throw BadType();
}
data.type = static_cast<DrawMessageEnum>(enumValue);
switch (data.type) {
case DrawMessageEnum::Lobby:
return buffer >> data.serverName >> data.playerCount >> data.sizeX >>
data.sizeY >> data.gameLength >> data.explosionRadius >>
data.bombTimer >> data.players >> Buffer::eEnd;
case DrawMessageEnum::Game:
return buffer >> data.serverName >> data.sizeX >> data.sizeY >>
data.gameLength >> data.turn >> data.players >>
data.playerPositions >> data.blocks >> data.bombs >>
data.explosions >> data.scores >> Buffer::eEnd;
default:
return buffer;
}
}
enum class InputMessageEnum : uint8_t {
PlaceBomb = 0,
PlaceBlock = 1,
Move = 2
};
class DataInputMessage {
public:
InputMessageEnum type{0};
DataDirection direction;
};
Buffer & operator<<(Buffer & buffer, const DataInputMessage & data) {
buffer.writeU8(static_cast<uint8_t>(data.type));
switch (data.type) {
case InputMessageEnum::Move:
return buffer << data.direction << Buffer::eSend;
default:
return buffer << Buffer::eSend;
}
}
Buffer & operator>>(Buffer & buffer, DataInputMessage & data) {
buffer >> Buffer::eReceive;
uint8_t enumValue = buffer.readU8();
if (enumValue > 2) {
throw BadType();
}
data.type = static_cast<InputMessageEnum>(enumValue);
switch (data.type) {
case InputMessageEnum::Move:
return buffer >> data.direction >> Buffer::eEnd;
default:
return buffer >> Buffer::eEnd;
}
}