TcpProtocol.h 1.82 KB
Newer Older
Hubert Denkmair's avatar
Hubert Denkmair committed
1
2
3
4
5
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <functional>
#include <vector>
Hubert Denkmair's avatar
Hubert Denkmair committed
6
7
#include <memory>
#include "SpatialMap.h"
8
9
10
11
12
#include "MsgPackProtocol.h"

using BotItem = MsgPackProtocol::BotItem;
using FoodItem = MsgPackProtocol::FoodItem;
using SnakeSegmentItem = MsgPackProtocol::SnakeSegmentItem;
Hubert Denkmair's avatar
Hubert Denkmair committed
13
14
15
16

class TcpProtocol
{
	public:
Hubert Denkmair's avatar
Hubert Denkmair committed
17
		typedef std::function<void(std::vector<char>&)> MessageReceivedCallback;
Hubert Denkmair's avatar
Hubert Denkmair committed
18
19
20
21
		void SetMessageReceivedCallback(MessageReceivedCallback callback);
		bool Read(int socket);

	private:
Hubert Denkmair's avatar
Hubert Denkmair committed
22
23
24
25
		static constexpr const size_t SPATIAL_MAP_TILES_X = 128;
		static constexpr const size_t SPATIAL_MAP_TILES_Y = 128;
		static constexpr const size_t SPATIAL_MAP_RESERVE_COUNT = 10;

26
		typedef SpatialMap<FoodItem, SPATIAL_MAP_TILES_X, SPATIAL_MAP_TILES_Y> FoodMap;
Hubert Denkmair's avatar
Hubert Denkmair committed
27

28
		typedef SpatialMap<SnakeSegmentItem, SPATIAL_MAP_TILES_X, SPATIAL_MAP_TILES_Y> SnakeSegmentMap;
Hubert Denkmair's avatar
Hubert Denkmair committed
29
30


Hubert Denkmair's avatar
Hubert Denkmair committed
31
		size_t _awaitedSize = 0;
32
		std::vector<char> _buf;
Hubert Denkmair's avatar
Hubert Denkmair committed
33
		MessageReceivedCallback _messageReceivedCallback;
Hubert Denkmair's avatar
Hubert Denkmair committed
34

Hubert Denkmair's avatar
Hubert Denkmair committed
35
		std::unique_ptr<FoodMap> _food;
36
		MsgPackProtocol::GameInfoMessage _gameInfo;
Hubert Denkmair's avatar
Hubert Denkmair committed
37

38
		std::vector<BotItem> _bots;
Hubert Denkmair's avatar
Hubert Denkmair committed
39
40
		std::unique_ptr<SnakeSegmentMap> _segments;

Hubert Denkmair's avatar
Hubert Denkmair committed
41
		void OnMessageReceived(std::vector<char>& data);
Hubert Denkmair's avatar
Hubert Denkmair committed
42

43
		void OnGameInfoReceived(const MsgPackProtocol::GameInfoMessage& msg);
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
44
		void OnWorldUpdateReceived(const MsgPackProtocol::WorldUpdateMessage& msg);
45
		void OnTickReceived(const MsgPackProtocol::TickMessage& msg);
Hubert Denkmair's avatar
Hubert Denkmair committed
46

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
47
48
49
		void OnFoodSpawnReceived(const MsgPackProtocol::FoodSpawnMessage& msg);
		void OnFoodConsumedReceived(const MsgPackProtocol::FoodConsumeMessage& msg);
		void OnFoodDecayedReceived(const MsgPackProtocol::FoodDecayMessage& msg);
Hubert Denkmair's avatar
Hubert Denkmair committed
50

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
51
		void OnBotSpawnReceived(const MsgPackProtocol::BotSpawnMessage& msg);
52
		void OnBotKillReceived(const MsgPackProtocol::BotKillMessage &msg);
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
53
		void OnBotMoveReceived(const MsgPackProtocol::BotMoveMessage &msg);
Hubert Denkmair's avatar
Hubert Denkmair committed
54
};