MsgPackProtocol.h 15.4 KB
Newer Older
1
2
3
4
5
6
7
8
#pragma once

#include <vector>
#include <msgpack.hpp>
#include "types.h"

namespace MsgPackProtocol
{
Hubert Denkmair's avatar
Hubert Denkmair committed
9
	typedef enum
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	{
		MESSAGE_TYPE_GAME_INFO = 0x00,
		MESSAGE_TYPE_WORLD_UPDATE = 0x01,

		MESSAGE_TYPE_TICK = 0x10,

		MESSAGE_TYPE_BOT_SPAWN = 0x20,
		MESSAGE_TYPE_BOT_KILL = 0x21,
		MESSAGE_TYPE_BOT_MOVE = 0x22,

		MESSAGE_TYPE_FOOD_SPAWN = 0x30,
		MESSAGE_TYPE_FOOD_CONSUME = 0x31,
		MESSAGE_TYPE_FOOD_DECAY = 0x32,

		MESSAGE_TYPE_PLAYER_INFO = 0xF0,
Hubert Denkmair's avatar
Hubert Denkmair committed
25
	} MessageType;
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

	static constexpr const uint8_t PROTOCOL_VERSION = 1;

	struct FoodItem
	{
		guid_t guid;
		Vector2D position;
		real_t value;

		const Vector2D& pos() const { return position; }
	};

	struct SnakeSegmentItem
	{
		guid_t bot_id;
		Vector2D position;
		const Vector2D& pos() const { return position; }
	};

	struct BotItem
	{
		guid_t guid;
		std::string name;
		real_t segment_radius;
		std::vector<SnakeSegmentItem> segments;
		std::vector<uint32_t> color;
Hubert Denkmair's avatar
Hubert Denkmair committed
52
		int database_id;
53
54
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
55
56
	struct Message
	{
Hubert Denkmair's avatar
Hubert Denkmair committed
57
58
		MessageType messageType;
		Message(MessageType type) : messageType(type) {}
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
59
60
61
62
		virtual ~Message() = default;
	};

	struct GameInfoMessage : public Message
63
64
65
66
	{
		double world_size_x = 0;
		double world_size_y = 0;
		double food_decay_per_frame = 0;
Hubert Denkmair's avatar
Hubert Denkmair committed
67
		GameInfoMessage(): Message(MESSAGE_TYPE_GAME_INFO) {}
68
69
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
70
	struct PlayerInfoMessage : public Message
71
72
	{
		guid_t player_id; // id der von dieser Verbindung gesteuerten Schlange
Hubert Denkmair's avatar
Hubert Denkmair committed
73
		PlayerInfoMessage(): Message(MESSAGE_TYPE_PLAYER_INFO) {}
74
75
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
76
	struct TickMessage : public Message
77
78
	{
		guid_t frame_id; // frame counter since start of server
Hubert Denkmair's avatar
Hubert Denkmair committed
79
		TickMessage(): Message(MESSAGE_TYPE_TICK) {}
80
81
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
82
	struct WorldUpdateMessage : public Message
83
84
85
	{
		std::vector<BotItem> bots;
		std::vector<FoodItem> food;
Hubert Denkmair's avatar
Hubert Denkmair committed
86
		WorldUpdateMessage(): Message(MESSAGE_TYPE_WORLD_UPDATE) {}
87
88
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
89
	struct BotSpawnMessage : public Message
90
91
	{
		BotItem bot;
Hubert Denkmair's avatar
Hubert Denkmair committed
92
		BotSpawnMessage(): Message(MESSAGE_TYPE_BOT_SPAWN) {}
93
94
95
96
97
98
99
100
101
102
	};

	struct BotMoveItem
	{
		guid_t bot_id;
		std::vector<SnakeSegmentItem> new_segments;
		size_t current_length;
		real_t current_segment_radius;
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
103
	struct BotMoveMessage : public Message
104
105
	{
		std::vector<BotMoveItem> items;
Hubert Denkmair's avatar
Hubert Denkmair committed
106
		BotMoveMessage(): Message(MESSAGE_TYPE_BOT_MOVE) {}
107
108
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
109
	struct BotKillMessage : public Message
110
111
112
	{
		guid_t killer_id;
		guid_t victim_id; // victim is deleted in this frame
Hubert Denkmair's avatar
Hubert Denkmair committed
113
		BotKillMessage(): Message(MESSAGE_TYPE_BOT_KILL) {}
114
115
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
116
	struct FoodSpawnMessage : public Message
117
118
	{
		std::vector<FoodItem> new_food;
Hubert Denkmair's avatar
Hubert Denkmair committed
119
		FoodSpawnMessage(): Message(MESSAGE_TYPE_FOOD_SPAWN) {}
120
121
122
123
124
125
126
127
	};

	struct FoodConsumeItem
	{
		guid_t food_id; // food is deleted in this frame
		guid_t bot_id; // bot consuming the food
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
128
	struct FoodConsumeMessage : public Message
129
130
	{
		std::vector<FoodConsumeItem> items;
Hubert Denkmair's avatar
Hubert Denkmair committed
131
		FoodConsumeMessage(): Message(MESSAGE_TYPE_FOOD_CONSUME) {}
132
133
	};

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
134
	struct FoodDecayMessage : public Message
135
136
	{
		std::vector<guid_t> food_ids; // food is deleted in this frame
Hubert Denkmair's avatar
Hubert Denkmair committed
137
		FoodDecayMessage(): Message(MESSAGE_TYPE_FOOD_DECAY) {}
138
139
	};

Hubert Denkmair's avatar
Hubert Denkmair committed
140
	void pack(msgpack::sbuffer& buf, const Message& msg);
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
}

namespace msgpack {
	MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
		namespace adaptor {

			template <> struct pack<MsgPackProtocol::GameInfoMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::GameInfoMessage const& v) const
				{
					o.pack_array(5);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_GAME_INFO));
					o.pack(v.world_size_x);
					o.pack(v.world_size_y);
					o.pack(v.food_decay_per_frame);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::GameInfoMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::GameInfoMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
166
167
168
169
					if (o.via.array.size != 5) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.world_size_x;
					o.via.array.ptr[3] >> v.world_size_y;
					o.via.array.ptr[4] >> v.food_decay_per_frame;
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::PlayerInfoMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::PlayerInfoMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_PLAYER_INFO));
					o.pack(v.player_id);
					return o;
				}
			};

186
187
188
189
190
191
192
193
194
195
196
			template<> struct convert<MsgPackProtocol::PlayerInfoMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::PlayerInfoMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
					if (o.via.array.size != 3) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.player_id;
					return o;
				}
			};

197
198
199
200
201
202
203
204
205
206
207
208
			template <> struct pack<MsgPackProtocol::TickMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::TickMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_TICK));
					o.pack(v.frame_id);
					return o;
				}
			};

209
210
211
212
213
214
215
216
217
218
219
			template<> struct convert<MsgPackProtocol::TickMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::TickMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
					if (o.via.array.size != 3) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.frame_id;
					return o;
				}
			};

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
			template <> struct pack<MsgPackProtocol::WorldUpdateMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::WorldUpdateMessage const& v) const
				{
					o.pack_array(4);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_WORLD_UPDATE));
					o.pack(v.bots);
					o.pack(v.food);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::WorldUpdateMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::WorldUpdateMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
238
239
240
					if (o.via.array.size != 4) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.bots;
					o.via.array.ptr[3] >> v.food;
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::BotSpawnMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotSpawnMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_BOT_SPAWN));
					o.pack(v.bot);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::BotSpawnMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::BotSpawnMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
262
263
					if (o.via.array.size != 3) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.bot;
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::BotMoveMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotMoveMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_BOT_MOVE));
					o.pack(v.items);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::BotMoveMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::BotMoveMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
285
286
					if (o.via.array.size != 3) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.items;
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::BotMoveItem>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotMoveItem const& v) const
				{
					o.pack_array(4);
					o.pack(v.bot_id);
					o.pack(v.new_segments);
					o.pack(v.current_length);
					o.pack(v.current_segment_radius);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::BotMoveItem>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::BotMoveItem& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
309
310
311
312
313
					if (o.via.array.size != 4) throw msgpack::type_error();
					o.via.array.ptr[0] >> v.bot_id;
					o.via.array.ptr[1] >> v.new_segments;
					o.via.array.ptr[2] >> v.current_length;
					o.via.array.ptr[3] >> v.current_segment_radius;
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::BotKillMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotKillMessage const& v) const
				{
					o.pack_array(4);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_BOT_KILL));
					o.pack(v.killer_id);
					o.pack(v.victim_id);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::BotKillMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::BotKillMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
336
337
338
					if (o.via.array.size != 4) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.killer_id;
					o.via.array.ptr[3] >> v.victim_id;
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::FoodSpawnMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::FoodSpawnMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_FOOD_SPAWN));
					o.pack(v.new_food);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::FoodSpawnMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::FoodSpawnMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
360
361
					if (o.via.array.size != 3) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.new_food;
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::FoodConsumeMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::FoodConsumeMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_FOOD_CONSUME));
					o.pack(v.items);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::FoodConsumeMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::FoodConsumeMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
383
384
					if (o.via.array.size != 3) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.items;
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::FoodConsumeItem>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::FoodConsumeItem const& v) const
				{
					o.pack_array(2);
					o.pack(v.food_id);
					o.pack(v.bot_id);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::FoodConsumeItem>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::FoodConsumeItem& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
405
406
407
					if (o.via.array.size != 2) throw msgpack::type_error();
					o.via.array.ptr[0] >> v.food_id;
					o.via.array.ptr[1] >> v.bot_id;
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::FoodDecayMessage>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::FoodDecayMessage const& v) const
				{
					o.pack_array(3);
					o.pack(MsgPackProtocol::PROTOCOL_VERSION);
					o.pack(static_cast<int>(MsgPackProtocol::MESSAGE_TYPE_FOOD_DECAY));
					o.pack(v.food_ids);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::FoodDecayMessage>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::FoodDecayMessage& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
429
430
					if (o.via.array.size != 3) throw msgpack::type_error();
					o.via.array.ptr[2] >> v.food_ids;
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::SnakeSegmentItem>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::SnakeSegmentItem const& v) const
				{
					o.pack_array(2);
					o.pack(v.pos().x());
					o.pack(v.pos().y());
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::SnakeSegmentItem>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::SnakeSegmentItem& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
					if (o.via.array.size != 2) throw msgpack::type_error();
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
452
453
454
455
					v.bot_id = 0;
					v.position = {
						o.via.array.ptr[0].via.f64,
						o.via.array.ptr[1].via.f64,
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
					};
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::FoodItem>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::FoodItem const& v) const
				{
					o.pack_array(4);
					o.pack(v.guid);
					o.pack(v.pos().x());
					o.pack(v.pos().y());
					o.pack(v.value);
					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::FoodItem>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::FoodItem& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
479
480
					if (o.via.array.size != 4) throw msgpack::type_error();
					o.via.array.ptr[0] >> v.guid;
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
481
					v.position = {
482
483
						o.via.array.ptr[1].via.f64,
						o.via.array.ptr[2].via.f64,
484
					};
485
					o.via.array.ptr[3] >> v.value;
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
					return o;
				}
			};

			template <> struct pack<MsgPackProtocol::BotItem>
			{
				template <typename Stream> msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, MsgPackProtocol::BotItem const& v) const
				{
					o.pack_array(5);
					o.pack(v.guid);
					o.pack(v.name);
					o.pack(v.segment_radius);

					// segments
					o.pack(v.segments);

					// FIXME: colormap: array of RGB values
					o.pack_array(3);
					o.pack(0xFF0000);
					o.pack(0x00FF00);
					o.pack(0x0000FF);

					return o;
				}
			};

			template<> struct convert<MsgPackProtocol::BotItem>
			{
				msgpack::object const& operator()(msgpack::object const& o, MsgPackProtocol::BotItem& v) const
				{
					if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
Hubert Denkmair's avatar
Hubert Denkmair committed
517
					if (o.via.array.size != 6) throw msgpack::type_error();
518
519
520
521
522
					o.via.array.ptr[0] >> v.guid;
					o.via.array.ptr[1] >> v.name;
					o.via.array.ptr[2] >> v.segment_radius;
					o.via.array.ptr[3] >> v.segments;
					o.via.array.ptr[4] >> v.color;
Hubert Denkmair's avatar
Hubert Denkmair committed
523
					o.via.array.ptr[5] >> v.database_id;
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
524
525
526
					for (auto &segmentItem: v.segments)
					{
						segmentItem.bot_id = v.guid;
527
528
529
530
531
532
533
534
					}
					return o;
				}
			};

		} // namespace adaptor
	} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // namespace msgpack