Commit cc829b68 authored by Hubert Denkmair's avatar Hubert Denkmair

make it work a little

parent b19bd718
......@@ -34,7 +34,7 @@ RelayServer::RelayServer()
_tcpProtocol.SetFrameCompleteCallback(
[this](uint64_t frame_id)
{
std::cout << "frame " << frame_id << " complete." << std::endl;
//std::cout << "frame " << frame_id << " complete." << std::endl;
for (auto& it: _connections)
{
......@@ -97,7 +97,6 @@ bool RelayServer::OnConnectionEstablished(TcpSocket &socket)
con->set_write_handler(
[&socket](websocketpp::connection_hdl, char const* data, size_t size)
{
std::cerr << "reply" << std::endl;
if (socket.Write(data, size, false) != static_cast<ssize_t>(size))
{
return websocketpp::transport::iostream::error::make_error_code(
......
......@@ -2,6 +2,7 @@
#include <stdint.h>
#include <unistd.h>
#include <array>
#include <algorithm>
#include <msgpack.hpp>
#include <iostream>
......@@ -48,6 +49,45 @@ 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;
......@@ -104,54 +144,51 @@ void TcpProtocol::OnMessageReceived(const char* data, size_t count)
void TcpProtocol::OnGameInfoReceived(const MsgPackProtocol::GameInfoMessage& msg)
{
_segments = std::make_unique<SnakeSegmentMap>(msg.world_size_x, msg.world_size_y, 1000);
_food = std::make_unique<FoodMap>(msg.world_size_x, msg.world_size_y, 1000);
_foodMap = std::make_unique<FoodMap>(msg.world_size_x, msg.world_size_y, 1000);
_gameInfo = msg;
}
void TcpProtocol::OnWorldUpdateReceived(const MsgPackProtocol::WorldUpdateMessage &msg)
{
for (auto& bot: msg.bots)
{
_bots.push_back(bot);
}
if (_food == nullptr) { return; }
for (auto& food: msg.food)
{
_food->addElement(food);
}
_bots = msg.bots;
_foodVect = msg.food;
}
void TcpProtocol::OnTickReceived(const MsgPackProtocol::TickMessage &msg)
{
_frameCompleteCallback(msg.frame_id);
_foodSpawnMessages.clear();
_foodConsumeMessages.clear();
_foodDecayMessages.clear();
}
void TcpProtocol::OnFoodSpawnReceived(const MsgPackProtocol::FoodSpawnMessage& msg)
{
if (_food == nullptr) { return; }
for (auto& item: msg.new_food)
{
_food->addElement(item);
}
_foodSpawnMessages.push_back(msg);
_foodVect.insert(_foodVect.end(), msg.new_food.begin(), msg.new_food.end());
}
void TcpProtocol::OnFoodConsumedReceived(const MsgPackProtocol::FoodConsumeMessage &msg)
{
if (_food == nullptr) { return; }
for (auto& item: msg.items)
{
_food->erase_if([item](const FoodItem& food) { return food.guid == item.food_id; });
}
_foodConsumeMessages.push_back(msg);
_foodVect.erase(std::remove_if(_foodVect.begin(), _foodVect.end(), [&msg](const FoodItem& food) {
for (auto& item: msg.items)
{
if (food.guid == item.food_id)
{
return true;
}
}
return false;
}));
}
void TcpProtocol::OnFoodDecayedReceived(const MsgPackProtocol::FoodDecayMessage &msg)
{
if (_food == nullptr) { return; }
for (auto& item: msg.food_ids)
{
_food->erase_if([item](const FoodItem& food) { return food.guid == item; });
}
_foodDecayMessages.push_back(msg);
_foodVect.erase(std::remove_if(_foodVect.begin(), _foodVect.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)
......
......@@ -22,6 +22,10 @@ class TcpProtocol
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;
private:
static constexpr const size_t SPATIAL_MAP_TILES_X = 128;
......@@ -37,9 +41,14 @@ class TcpProtocol
size_t _bufTail=0;
FrameCompleteCallback _frameCompleteCallback;
std::unique_ptr<FoodMap> _food;
std::vector<FoodItem> _foodVect;
std::unique_ptr<FoodMap> _foodMap;
MsgPackProtocol::GameInfoMessage _gameInfo;
std::vector<MsgPackProtocol::FoodSpawnMessage> _foodSpawnMessages;
std::vector<MsgPackProtocol::FoodConsumeMessage> _foodConsumeMessages;
std::vector<MsgPackProtocol::FoodDecayMessage> _foodDecayMessages;
std::vector<BotItem> _bots;
std::unique_ptr<SnakeSegmentMap> _segments;
......
......@@ -9,17 +9,38 @@ WebsocketConnection::WebsocketConnection(int socket, WebsocketServer::connection
void WebsocketConnection::Eof()
{
_websocket->eof();
_websocket->fatal_error();
}
void WebsocketConnection::DataReceived(const char *data, size_t count)
{
_websocket->read_some(data, count);
_websocket->read_all(data, count);
}
void WebsocketConnection::FrameComplete(uint64_t frame_id, const TcpProtocol &proto)
{
msgpack::sbuffer buf;
msgpack::pack(buf, proto.GetGameInfo());
_websocket->send(buf.data(), buf.size());
if (!_firstFrameSent)
{
msgpack::sbuffer buf;
msgpack::pack(buf, proto.GetGameInfo());
_websocket->send(buf.data(), buf.size());
buf.clear();
proto.GetWorldUpdate(buf);
_websocket->send(buf.data(), buf.size());
_firstFrameSent = true;
}
else
{
msgpack::sbuffer buf;
proto.GetFoodSpawnMessages(buf);
_websocket->send(buf.data(), buf.size());
buf.clear();
proto.GetFoodConsumeMessages(buf);
_websocket->send(buf.data(), buf.size());
buf.clear();
proto.GetFoodDecayMessages(buf);
_websocket->send(buf.data(), buf.size());
}
}
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