Skip to content

Commit

Permalink
feat: add API version support in Client and Game classes
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Nov 11, 2024
1 parent 9c88934 commit 9b131b5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Flakkari/Protocol/Header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ using ulong = unsigned long; // 64 bits (max: 18446744073709551615)
*
*/
enum class ApiVersion : byte {
V_0,
V_0 = 0,
V_1 = 1,
MAX_VERSION
};

Expand Down
10 changes: 8 additions & 2 deletions Flakkari/Server/Client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Flakkari {

Client::Client(const std::shared_ptr<Network::Address> &address, const std::string &name)
Client::Client(const std::shared_ptr<Network::Address> &address, const std::string &name,
Protocol::ApiVersion apiVersion)
: _address(address), _gameName(name), _name(address->toString().value_or(""))
{
_apiVersion = apiVersion;
_lastActivity = std::chrono::steady_clock::now();
}

Expand Down Expand Up @@ -42,6 +44,10 @@ bool Client::incrementWarningCount()
return _warningCount >= _maxWarningCount;
}

void Client::addPacketToQueue(const Protocol::Packet<Protocol::CommandId> &packet) { _receiveQueue.push_back(packet); }
void Client::addPacketToQueue(const Protocol::Packet<Protocol::CommandId> &packet)
{
_apiVersion = packet.header._apiVersion;
_receiveQueue.push_back(packet);
}

} /* namespace Flakkari */
6 changes: 5 additions & 1 deletion Flakkari/Server/Client/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class Client {
*
* @param address The client's address
* @param name The Game's name
* @param apiVersion The API version used by the client
*/
Client(const std::shared_ptr<Network::Address> &address, const std::string &name);
Client(const std::shared_ptr<Network::Address> &address, const std::string &name, Protocol::ApiVersion apiVersion);
~Client();

/**
Expand Down Expand Up @@ -118,6 +119,8 @@ class Client {

[[nodiscard]] unsigned short getMaxPacketHistory() const { return _maxPacketHistory; }

[[nodiscard]] Protocol::ApiVersion getApiVersion() const { return _apiVersion; }

[[nodiscard]] Network::PacketQueue<Protocol::Packet<Protocol::CommandId>> &getReceiveQueue()
{
return _receiveQueue;
Expand All @@ -131,6 +134,7 @@ class Client {
std::string _gameName;
bool _isConnected = true;
std::string _name;
Protocol::ApiVersion _apiVersion;
unsigned short _warningCount = 0;
unsigned short _maxWarningCount = 5;
unsigned short _maxPacketHistory = 10;
Expand Down
5 changes: 3 additions & 2 deletions Flakkari/Server/Client/ClientManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ ClientManager::addClient(const std::shared_ptr<Network::Address> &client, Networ
}

std::string gameName = std::string((const char *) packet.payload.getData(), packet.payload.getSize());
_clients[clientString] = std::make_shared<Client>(client, gameName);
auto apiVersion = packet.header._apiVersion;
_clients[clientString] = std::make_shared<Client>(client, gameName, apiVersion);

return std::make_pair(gameName, _clients[clientString]);
}
Expand Down Expand Up @@ -82,7 +83,7 @@ void ClientManager::checkInactiveClients()
}
}

void ClientManager::sendPacketToClient(std::shared_ptr<Network::Address> client, const Network::Buffer &packet)
void ClientManager::sendPacketToClient(const std::shared_ptr<Network::Address> &client, const Network::Buffer &packet)
{
std::thread([this, client, packet] { _socket->sendTo(client, packet); }).detach();
}
Expand Down
25 changes: 16 additions & 9 deletions Flakkari/Server/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void Game::sendAllEntities(const std::string &sceneName, std::shared_ptr<Client>

Protocol::Packet<Protocol::CommandId> packet;
packet.header._commandId = Protocol::CommandId::REQ_ENTITY_SPAWN;
packet.header._apiVersion = player->getApiVersion();
packet << i;
packet.injectString(sceneName);
Protocol::PacketFactory::addComponentsToPacketByEntity<Protocol::CommandId>(packet, registry, i);
Expand Down Expand Up @@ -218,7 +219,7 @@ void Game::loadScene(const std::string &sceneName)
}
}

void Game::sendOnSameScene(const std::string &sceneName, const Network::Buffer &message)
void Game::sendOnSameScene(const std::string &sceneName, Protocol::Packet<Protocol::CommandId> &packet)
{
for (auto &player : _players)
{
Expand All @@ -229,12 +230,14 @@ void Game::sendOnSameScene(const std::string &sceneName, const Network::Buffer &
if (player->getSceneName() != sceneName)
continue;

ClientManager::GetInstance().sendPacketToClient(player->getAddress(), message);
packet.header._apiVersion = player->getApiVersion();

ClientManager::GetInstance().sendPacketToClient(player->getAddress(), packet.serialize());
ClientManager::UnlockInstance();
}
}

void Game::sendOnSameSceneExcept(const std::string &sceneName, const Network::Buffer &message,
void Game::sendOnSameSceneExcept(const std::string &sceneName, Protocol::Packet<Protocol::CommandId> &packet,
std::shared_ptr<Client> except)
{
for (auto &player : _players)
Expand All @@ -248,7 +251,9 @@ void Game::sendOnSameSceneExcept(const std::string &sceneName, const Network::Bu
if (player == except)
continue;

ClientManager::GetInstance().sendPacketToClient(player->getAddress(), message);
packet.header._apiVersion = player->getApiVersion();

ClientManager::GetInstance().sendPacketToClient(player->getAddress(), packet.serialize());
ClientManager::UnlockInstance();
}
}
Expand All @@ -262,7 +267,7 @@ void Game::checkDisconnect()
Protocol::Packet<Protocol::CommandId> packet;
packet.header._commandId = Protocol::CommandId::REQ_DISCONNECT;
packet << player->getEntity();
sendOnSameScene(player->getSceneName(), packet.serialize());
sendOnSameScene(player->getSceneName(), packet);
_scenes[player->getSceneName()].kill_entity(player->getEntity());
removePlayer(player);
}
Expand Down Expand Up @@ -291,7 +296,7 @@ void Game::sendUpdatePosition(std::shared_ptr<Client> player, Engine::ECS::Compo
std::to_string(pos.position.vec.y) + ")" + ", Vel: (" + std::to_string(vel.velocity.vec.x) + ", " +
std::to_string(vel.velocity.vec.y) + ")" + ", Acc: (" + std::to_string(vel.acceleration.vec.x) +
", " + std::to_string(vel.acceleration.vec.y) + ")" + ">");
sendOnSameScene(player->getSceneName(), packet.serialize());
sendOnSameScene(player->getSceneName(), packet);
}

void Game::handleEvent(std::shared_ptr<Client> player, Protocol::Packet<Protocol::CommandId> packet)
Expand Down Expand Up @@ -383,7 +388,7 @@ void Game::handleEvent(std::shared_ptr<Client> player, Protocol::Packet<Protocol
shootPacket.injectString(player->getSceneName());
shootPacket << player->getEntity();
// create a bullet with player as parent
sendOnSameScene(player->getSceneName(), shootPacket.serialize());
sendOnSameScene(player->getSceneName(), shootPacket);
}
return;
}
Expand Down Expand Up @@ -474,6 +479,7 @@ bool Game::addPlayer(std::shared_ptr<Client> player)

Protocol::Packet<Protocol::CommandId> packet;
packet.header._commandId = Protocol::CommandId::REP_CONNECT;
packet.header._apiVersion = player->getApiVersion();
packet << newEntity;
packet.injectString(sceneGame);
packet.injectString(player->getName().value_or(""));
Expand All @@ -483,11 +489,12 @@ bool Game::addPlayer(std::shared_ptr<Client> player)

Protocol::Packet<Protocol::CommandId> packet2;
packet2.header._commandId = Protocol::CommandId::REQ_ENTITY_SPAWN;
packet2.header._apiVersion = packet.header._apiVersion;
packet2 << newEntity;
packet2.injectString(sceneGame);
packet2.injectString(p_Template);

sendOnSameSceneExcept(sceneGame, packet2.serialize(), player);
sendOnSameSceneExcept(sceneGame, packet2, player);
return true;
}

Expand All @@ -506,7 +513,7 @@ bool Game::removePlayer(std::shared_ptr<Client> player)
packet.header._commandId = Protocol::CommandId::REQ_ENTITY_DESTROY;
packet << entity;

sendOnSameScene(sceneGame, packet.serialize());
sendOnSameScene(sceneGame, packet);

registry.kill_entity(entity);
_players.erase(it);
Expand Down
4 changes: 2 additions & 2 deletions Flakkari/Server/Game/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class Game {

public: // Actions
void sendAllEntities(const std::string &sceneName, std::shared_ptr<Client> player);
void sendOnSameScene(const std::string &sceneName, const Network::Buffer &message);
void sendOnSameScene(const std::string &sceneName, Protocol::Packet<Protocol::CommandId> &packet);

void sendOnSameSceneExcept(const std::string &sceneName, const Network::Buffer &message,
void sendOnSameSceneExcept(const std::string &sceneName, Protocol::Packet<Protocol::CommandId> &packet,
std::shared_ptr<Client> except);

/**
Expand Down

0 comments on commit 9b131b5

Please sign in to comment.