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

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

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

	private:
Hubert Denkmair's avatar
Hubert Denkmair committed
23
24
25
26
		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;

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

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


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

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

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

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

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

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
48
49
50
		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
51

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