-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.hpp
206 lines (176 loc) · 4.98 KB
/
messages.hpp
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
/* This file contains definitions of types and message formats used through-
* out the project in communication between client, server and GUI server.
* The order of members of the structs below and the order of these types
* in related std::variants is important:
* -the order of members determines the order in which they will be
* serialized.
* -the order of types corresponds directly to the message ids which they
* will be assigned
* Name of the 'msg_id' member is not to be changed - objects are tested
* for presence of such a member in the serialization code
*/
#ifndef BOMBERMAN_MESSAGES_HPP
#define BOMBERMAN_MESSAGES_HPP
#include <cstdint> // uint8_t, ...
#include <string>
#include <vector>
#include <map>
#include <variant>
// Definitions of the most primitive types ---------------------------------
using bomb_id_t = uint32_t;
using bomb_timer_t = uint16_t;
// we could use an enum, but this is just more convenient for serialization
using direction_t = uint8_t;
using explosion_radius_t = uint16_t;
using game_length_t = uint16_t;
using initial_blocks_t = uint16_t;
using msg_id_t = uint8_t;
using player_id_t = uint8_t;
using players_count_t = uint8_t;
using pos_t = uint16_t;
using score_t = uint32_t;
using server_name_t = std::string;
using strlen_t = uint8_t;
using turn_duration_t = uint64_t;
using turn_t = uint16_t;
struct Position {
pos_t x;
pos_t y;
auto operator<=>(const Position&) const = default;
};
struct Bomb {
Position position;
bomb_timer_t timer;
};
struct Player {
std::string name;
std::string address;
};
// Definitions of events ---------------------------------------------------
struct EventBombPlaced {
static constexpr uint8_t msg_id = 0;
bomb_id_t bomb_id;
Position position;
};
struct EventBombExploded {
static constexpr uint8_t msg_id = 1;
bomb_id_t bomb_id;
std::vector<player_id_t> robots_destroyed;
std::vector<Position> blocks_destroyed;
};
struct EventPlayerMoved {
static constexpr uint8_t msg_id = 2;
player_id_t player_id;
Position position;
};
struct EventBlockPlaced {
static constexpr uint8_t msg_id = 3;
Position position;
};
using Event = std::variant<
EventBombPlaced,
EventBombExploded,
EventPlayerMoved,
EventBlockPlaced
>;
// Definitions of messages from client to server ---------------------------
struct ClientMessageJoin {
static constexpr uint8_t msg_id = 0;
std::string name;
};
struct ClientMessagePlaceBomb {
static constexpr uint8_t msg_id = 1;
};
struct ClientMessagePlaceBlock {
static constexpr uint8_t msg_id = 2;
};
struct ClientMessageMove {
static constexpr uint8_t msg_id = 3;
direction_t direction;
};
using ClientMessage = std::variant<
ClientMessageJoin,
ClientMessagePlaceBomb,
ClientMessagePlaceBlock,
ClientMessageMove
>;
// Definitions of messages from server to client ---------------------------
struct ServerMessageHello {
static constexpr uint8_t msg_id = 0;
std::string server_name;
players_count_t players_count;
pos_t size_x;
pos_t size_y;
game_length_t game_length;
explosion_radius_t explosion_radius;
bomb_timer_t bomb_timer;
};
struct ServerMessageAcceptedPlayer {
static constexpr uint8_t msg_id = 1;
player_id_t player_id;
Player player;
};
struct ServerMessageGameStarted {
static constexpr uint8_t msg_id = 2;
std::map<player_id_t, Player> players;
};
struct ServerMessageTurn {
static constexpr uint8_t msg_id = 3;
game_length_t turn;
std::vector<Event> events;
};
struct ServerMessageGameEnded {
static constexpr uint8_t msg_id = 4;
std::map<player_id_t, score_t> scores;
};
using ServerMessage = std::variant<
ServerMessageHello,
ServerMessageAcceptedPlayer,
ServerMessageGameStarted,
ServerMessageTurn,
ServerMessageGameEnded
>;
// Definitions of messages from client to GUI server -----------------------
struct DrawMessageLobby {
static constexpr uint8_t msg_id = 0;
std::string server_name;
players_count_t players_count;
pos_t size_x;
pos_t size_y;
game_length_t game_length;
explosion_radius_t explosion_radius;
bomb_timer_t bomb_timer;
std::map<player_id_t, Player> players;
};
struct DrawMessageGame {
static constexpr uint8_t msg_id = 1;
std::string server_name;
pos_t size_x;
pos_t size_y;
game_length_t game_length;
turn_t turn;
std::map<player_id_t, Player> players;
std::map<player_id_t, Position> player_positions;
std::vector<Position> blocks;
std::vector<Bomb> bombs;
std::vector<Position> explosions;
std::map<player_id_t, score_t> scores;
};
using DrawMessage = std::variant<DrawMessageLobby, DrawMessageGame>;
// Definitions of messages from GUI server to client -----------------------
struct InputMessagePlaceBomb {
static constexpr uint8_t msg_id = 0;
};
struct InputMessagePlaceBlock {
static constexpr uint8_t msg_id = 1;
};
struct InputMessageMove {
static constexpr uint8_t msg_id = 2;
direction_t direction;
};
using InputMessage = std::variant<
InputMessagePlaceBomb,
InputMessagePlaceBlock,
InputMessageMove
>;
#endif // BOMBERMAN_MESSAGES_HPP