Commit 797510c4 authored by Hubert Denkmair's avatar Hubert Denkmair

cleanup

parent 927579d0
......@@ -52,32 +52,43 @@ namespace MsgPackProtocol
std::vector<uint32_t> color;
};
struct GameInfoMessage
struct Message
{
virtual ~Message() = default;
virtual void pack(msgpack::sbuffer& buf) const = 0;
};
struct GameInfoMessage : public Message
{
double world_size_x = 0;
double world_size_y = 0;
double food_decay_per_frame = 0;
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
struct PlayerInfoMessage
struct PlayerInfoMessage : public Message
{
guid_t player_id; // id der von dieser Verbindung gesteuerten Schlange
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
struct TickMessage
struct TickMessage : public Message
{
guid_t frame_id; // frame counter since start of server
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
struct WorldUpdateMessage
struct WorldUpdateMessage : public Message
{
std::vector<BotItem> bots;
std::vector<FoodItem> food;
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
struct BotSpawnMessage
struct BotSpawnMessage : public Message
{
BotItem bot;
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
struct BotMoveItem
......@@ -88,20 +99,23 @@ namespace MsgPackProtocol
real_t current_segment_radius;
};
struct BotMoveMessage
struct BotMoveMessage : public Message
{
std::vector<BotMoveItem> items;
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
struct BotKillMessage
struct BotKillMessage : public Message
{
guid_t killer_id;
guid_t victim_id; // victim is deleted in this frame
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
struct FoodSpawnMessage
struct FoodSpawnMessage : public Message
{
std::vector<FoodItem> new_food;
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
struct FoodConsumeItem
......@@ -110,14 +124,16 @@ namespace MsgPackProtocol
guid_t bot_id; // bot consuming the food
};
struct FoodConsumeMessage
struct FoodConsumeMessage : public Message
{
std::vector<FoodConsumeItem> items;
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
struct FoodDecayMessage
struct FoodDecayMessage : public Message
{
std::vector<guid_t> food_ids; // food is deleted in this frame
void pack(msgpack::sbuffer& buf) const override { msgpack::pack(buf, *this); }
};
}
......
......@@ -32,7 +32,7 @@ int RelayServer::Run()
con->FrameComplete(frame_id, _tcpProtocol);
}
);
std::cout << "frame " << frame_id << " complete." << std::endl;
//std::cout << "frame " << frame_id << " complete." << std::endl;
}
);
epoll.AddFileDescriptor(_clientSocket, EPOLLIN|EPOLLPRI|EPOLLERR);
......
......@@ -7,6 +7,8 @@
#include <iostream>
TcpProtocol::TcpProtocol()
: _food(_worldUpdate.food)
, _bots(_worldUpdate.bots)
{
_buf.resize(BUFFER_SIZE);
}
......@@ -44,50 +46,6 @@ bool TcpProtocol::Read(int socket)
return true;
}
const MsgPackProtocol::GameInfoMessage &TcpProtocol::GetGameInfo() const
{
return _gameInfo;
}
bool TcpProtocol::GetWorldUpdate(msgpack::sbuffer &buf) const
{
MsgPackProtocol::WorldUpdateMessage msg;
msg.bots = _bots;
msg.food = _foodVect;
msgpack::pack(buf, msg);
return true;
}
bool TcpProtocol::GetFoodSpawnMessages(msgpack::sbuffer &buf) const
{
for (auto& msg: _foodSpawnMessages)
{
msgpack::pack(buf, msg);
// FIXME this will fail with multiple foodSpawnMessages, since only one message should be in one buffer
}
return true;
}
bool TcpProtocol::GetFoodConsumeMessages(msgpack::sbuffer &buf) const
{
for (auto& msg: _foodConsumeMessages)
{
msgpack::pack(buf, msg);
// FIXME this will fail with multiple foodSpawnMessages, since only one message should be in one buffer
}
return true;
}
bool TcpProtocol::GetFoodDecayMessages(msgpack::sbuffer &buf) const
{
for (auto& msg: _foodDecayMessages)
{
msgpack::pack(buf, msg);
// FIXME this will fail with multiple foodSpawnMessages, since only one message should be in one buffer
}
return true;
}
void TcpProtocol::OnMessageReceived(const char* data, size_t count)
{
msgpack::object_handle obj;
......@@ -151,27 +109,25 @@ void TcpProtocol::OnGameInfoReceived(const MsgPackProtocol::GameInfoMessage& msg
void TcpProtocol::OnWorldUpdateReceived(const MsgPackProtocol::WorldUpdateMessage &msg)
{
_bots = msg.bots;
_foodVect = msg.food;
_food = msg.food;
}
void TcpProtocol::OnTickReceived(const MsgPackProtocol::TickMessage &msg)
{
_frameCompleteCallback(msg.frame_id);
_foodSpawnMessages.clear();
_foodConsumeMessages.clear();
_foodDecayMessages.clear();
_pendingMessages.clear();
}
void TcpProtocol::OnFoodSpawnReceived(const MsgPackProtocol::FoodSpawnMessage& msg)
{
_foodSpawnMessages.push_back(msg);
_foodVect.insert(_foodVect.end(), msg.new_food.begin(), msg.new_food.end());
_pendingMessages.push_back(std::make_unique<MsgPackProtocol::FoodSpawnMessage>(msg));
_food.insert(_food.end(), msg.new_food.begin(), msg.new_food.end());
}
void TcpProtocol::OnFoodConsumedReceived(const MsgPackProtocol::FoodConsumeMessage &msg)
{
_foodConsumeMessages.push_back(msg);
_foodVect.erase(std::remove_if(_foodVect.begin(), _foodVect.end(), [&msg](const FoodItem& food) {
_pendingMessages.push_back(std::make_unique<MsgPackProtocol::FoodConsumeMessage>(msg));
_food.erase(std::remove_if(_food.begin(), _food.end(), [&msg](const FoodItem& food) {
for (auto& item: msg.items)
{
if (food.guid == item.food_id)
......@@ -185,24 +141,27 @@ void TcpProtocol::OnFoodConsumedReceived(const MsgPackProtocol::FoodConsumeMessa
void TcpProtocol::OnFoodDecayedReceived(const MsgPackProtocol::FoodDecayMessage &msg)
{
_foodDecayMessages.push_back(msg);
_foodVect.erase(std::remove_if(_foodVect.begin(), _foodVect.end(), [&msg](const FoodItem& food) {
_pendingMessages.push_back(std::make_unique<MsgPackProtocol::FoodDecayMessage>(msg));
_food.erase(std::remove_if(_food.begin(), _food.end(), [&msg](const FoodItem& food) {
return std::find(msg.food_ids.begin(), msg.food_ids.end(), food.guid) != msg.food_ids.end();
}));
}
void TcpProtocol::OnBotSpawnReceived(const MsgPackProtocol::BotSpawnMessage &msg)
{
_pendingMessages.push_back(std::make_unique<MsgPackProtocol::BotSpawnMessage>(msg));
_bots.push_back(msg.bot);
}
void TcpProtocol::OnBotKillReceived(const MsgPackProtocol::BotKillMessage& msg)
{
_pendingMessages.push_back(std::make_unique<MsgPackProtocol::BotKillMessage>(msg));
_bots.erase(std::remove_if(_bots.begin(), _bots.end(), [msg](const BotItem& bot) { return bot.guid == msg.victim_id; }));
}
void TcpProtocol::OnBotMoveReceived(const MsgPackProtocol::BotMoveMessage &msg)
{
_pendingMessages.push_back(std::make_unique<MsgPackProtocol::BotMoveMessage>(msg));
for (auto& item: msg.items)
{
auto it = std::find_if(_bots.begin(), _bots.end(), [item](const BotItem& bot) { return bot.guid == item.bot_id; });
......
......@@ -21,11 +21,10 @@ class TcpProtocol
void SetFrameCompleteCallback(FrameCompleteCallback callback);
bool Read(int socket);
const MsgPackProtocol::GameInfoMessage& GetGameInfo() const;
bool GetWorldUpdate(msgpack::sbuffer& buf) const;
bool GetFoodSpawnMessages(msgpack::sbuffer& buf) const;
bool GetFoodConsumeMessages(msgpack::sbuffer& buf) const;
bool GetFoodDecayMessages(msgpack::sbuffer& buf) const;
const MsgPackProtocol::GameInfoMessage& GetGameInfo() const { return _gameInfo; }
const MsgPackProtocol::WorldUpdateMessage& GetWorldUpdate() const { return _worldUpdate; }
const std::vector<std::unique_ptr<MsgPackProtocol::Message>>& GetPendingMessages() const { return _pendingMessages; }
private:
static constexpr const size_t SPATIAL_MAP_TILES_X = 128;
......@@ -41,17 +40,16 @@ class TcpProtocol
size_t _bufTail=0;
FrameCompleteCallback _frameCompleteCallback;
std::vector<FoodItem> _foodVect;
std::unique_ptr<FoodMap> _foodMap;
MsgPackProtocol::GameInfoMessage _gameInfo;
MsgPackProtocol::WorldUpdateMessage _worldUpdate;
std::vector<FoodItem>& _food;
std::vector<BotItem>& _bots;
std::unique_ptr<FoodMap> _foodMap;
std::vector<std::unique_ptr<MsgPackProtocol::Message>> _pendingMessages;
std::vector<MsgPackProtocol::FoodSpawnMessage> _foodSpawnMessages;
std::vector<MsgPackProtocol::FoodConsumeMessage> _foodConsumeMessages;
std::vector<MsgPackProtocol::FoodDecayMessage> _foodDecayMessages;
std::vector<BotItem> _bots;
std::unique_ptr<SnakeSegmentMap> _segments;
void OnMessageReceived(const char *data, size_t count);
void OnGameInfoReceived(const MsgPackProtocol::GameInfoMessage& msg);
......
......@@ -9,28 +9,30 @@ WebsocketConnection::WebsocketConnection(uWS::WebSocket<uWS::SERVER> *websocket)
void WebsocketConnection::FrameComplete(uint64_t frame_id, const TcpProtocol &proto)
{
(void) frame_id;
if (!_firstFrameSent)
{
msgpack::sbuffer buf;
msgpack::pack(buf, proto.GetGameInfo());
_websocket->send(buf.data(), buf.size(), uWS::OpCode::BINARY);
buf.clear();
proto.GetWorldUpdate(buf);
_websocket->send(buf.data(), buf.size(), uWS::OpCode::BINARY);
sendInitialData(proto);
_firstFrameSent = true;
}
else
msgpack::sbuffer buf;
for (auto& msg: proto.GetPendingMessages())
{
msgpack::sbuffer buf;
proto.GetFoodSpawnMessages(buf);
_websocket->send(buf.data(), buf.size(), uWS::OpCode::BINARY);
buf.clear();
proto.GetFoodConsumeMessages(buf);
_websocket->send(buf.data(), buf.size(), uWS::OpCode::BINARY);
buf.clear();
proto.GetFoodDecayMessages(buf);
msg->pack(buf);
_websocket->send(buf.data(), buf.size(), uWS::OpCode::BINARY);
}
}
void WebsocketConnection::sendInitialData(const TcpProtocol &proto)
{
msgpack::sbuffer buf;
buf.clear();
proto.GetGameInfo().pack(buf);
_websocket->send(buf.data(), buf.size(), uWS::OpCode::BINARY);
buf.clear();
proto.GetWorldUpdate().pack(buf);
_websocket->send(buf.data(), buf.size(), uWS::OpCode::BINARY);
}
......@@ -14,4 +14,6 @@ class WebsocketConnection
uWS::WebSocket<uWS::SERVER> *_websocket;
bool _firstFrameSent = false;
void sendInitialData(const TcpProtocol& proto);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment