Skip to content

Commit

Permalink
feat(Game): Add new functions to Game class and update PacketFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Jan 13, 2024
1 parent 6f61517 commit e1e47d3
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 38 deletions.
42 changes: 21 additions & 21 deletions Flakkari/Protocol/PacketFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,34 @@ class PacketFactory {
if (childEntity.has_value()) {
packet << Protocol::API::ComponentId::CHILD;
packet << childEntity->size();
packet << childEntity->name.c_str();
packet << childEntity->name;
}

auto evolve = registry.getComponents<Engine::ECS::Components::Common::Evolve>();
auto evolveEntity = evolve[entity];
// auto evolve = registry.getComponents<Engine::ECS::Components::Common::Evolve>();
// auto evolveEntity = evolve[entity];

if (evolveEntity.has_value()) {
packet << Protocol::API::ComponentId::EVOLVE;
packet << evolveEntity->size();
packet << evolveEntity->name.c_str();
}
// if (evolveEntity.has_value()) {
// packet << Protocol::API::ComponentId::EVOLVE;
// packet << evolveEntity->size();
// packet << evolveEntity->name;
// }

auto id = registry.getComponents<Engine::ECS::Components::Common::Id>();
auto idEntity = id[entity];
// auto id = registry.getComponents<Engine::ECS::Components::Common::Id>();
// auto idEntity = id[entity];

if (idEntity.has_value()) {
packet << Protocol::API::ComponentId::ID;
packet << idEntity->size();
packet << idEntity->id;
}
// if (idEntity.has_value()) {
// packet << Protocol::API::ComponentId::ID;
// packet << idEntity->size();
// packet << idEntity->id;
// }

auto parent = registry.getComponents<Engine::ECS::Components::Common::Parent>();
auto parentEntity = parent[entity];

if (parentEntity.has_value()) {
packet << Protocol::API::ComponentId::PARENT;
packet << parentEntity->size();
packet << parentEntity->name.c_str();
packet << parentEntity->entity;
}

auto tag = registry.getComponents<Engine::ECS::Components::Common::Tag>();
Expand All @@ -80,7 +80,7 @@ class PacketFactory {
if (tagEntity.has_value()) {
packet << Protocol::API::ComponentId::TAG;
packet << tagEntity->size();
packet << tagEntity->tag.c_str();
packet << tagEntity->tag;
}

auto name = registry.getComponents<Engine::ECS::Components::Common::Template>();
Expand All @@ -89,7 +89,7 @@ class PacketFactory {
if (nameEntity.has_value()) {
packet << Protocol::API::ComponentId::TEMPLATE;
packet << nameEntity->size();
packet << nameEntity->name.c_str();
packet << nameEntity->name;
}
}

Expand All @@ -105,7 +105,7 @@ class PacketFactory {
static void add2dToPacketByEntity (
Protocol::API::Packet<Id> &packet, Engine::ECS::Registry &registry, Engine::ECS::Entity entity
) {
auto transform = r.getComponents<ECS::Components::_2D::Transform>();
auto transform = registry.getComponents<Engine::ECS::Components::_2D::Transform>();
auto pos = transform[entity];

if (pos.has_value()) {
Expand All @@ -118,7 +118,7 @@ class PacketFactory {
packet << pos->scale.y;
}

auto movable = r.getComponents<ECS::Components::_2D::Movable>();
auto movable = registry.getComponents<Engine::ECS::Components::_2D::Movable>();
auto vel = movable[entity];

if (vel.has_value()) {
Expand All @@ -130,7 +130,7 @@ class PacketFactory {
packet << vel->acceleration.y;
}

auto control = r.getComponents<ECS::Components::_2D::Control>();
auto control = registry.getComponents<Engine::ECS::Components::_2D::Control>();
auto ctrl = control[entity];

if (ctrl.has_value()) {
Expand Down
127 changes: 117 additions & 10 deletions Flakkari/Server/Client/ClientManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,64 @@ std::shared_ptr<ClientManager> ClientManager::getInstance()
return _instance;
}

void ClientManager::setSocket(std::shared_ptr<Network::Socket> socket) {
getInstance()->_socket = socket;
}

void ClientManager::addClient(std::shared_ptr<Network::Address> client)
{
auto &clients = getInstance()->_clients;
if (clients.find(client->toString().value_or("")) == clients.end()) {

if (ClientManager::isBanned(client)) {
FLAKKARI_LOG_LOG("Client " + client->toString().value_or("Unknown") + " tried to connect but is banned");
return;
}

if (clients.find(client->toString().value_or("")) != clients.end())
return clients[client->toString().value_or("")]->keepAlive(), void();

clients[client->toString().value_or("")] = std::make_shared<Client>(client);
FLAKKARI_LOG_LOG("Client " + client->toString().value_or("Unknown") + " connected");
GameManager::addClientToGame("R-Type", clients[client->toString().value_or("")]);
} else
clients[client->toString().value_or("")]->keepAlive();
}

void ClientManager::removeClient(std::shared_ptr<Network::Address> client)
{
auto &clients = getInstance()->_clients;
if (clients.find(client->toString().value_or("")) != clients.end()) {

if (clients.find(client->toString().value_or("")) == clients.end())
return;

GameManager::removeClientFromGame("R-Type", clients[client->toString().value_or("")]);
clients.erase(client->toString().value_or(""));
}

void ClientManager::banClient(std::shared_ptr<Network::Address> client)
{
auto &clients = getInstance()->_clients;
auto &bannedClients = getInstance()->_bannedClients;

if (clients.find(client->toString().value_or("")) == clients.end())
return;

bannedClients.push_back(client->getIp().value());

FLAKKARI_LOG_LOG("Client " + client->toString().value_or("Unknown") + " banned");
GameManager::removeClientFromGame("R-Type", clients[client->toString().value_or("")]);
clients.erase(client->toString().value_or(""));
}

bool ClientManager::isBanned(std::shared_ptr<Network::Address> client)
{
auto &bannedClients = getInstance()->_bannedClients;

return std::find(bannedClients.begin(), bannedClients.end(), client->getIp().value_or("")) != bannedClients.end();
}

void ClientManager::checkInactiveClients()
{
auto &clients = getInstance()->_clients;

for (auto it = clients.begin(); it != clients.end();) {
if (!it->second->isConnected()) {
FLAKKARI_LOG_LOG("Client " + it->first + " disconnected");
Expand All @@ -56,20 +91,92 @@ void ClientManager::checkInactiveClients()
}
}

void ClientManager::sendPacketToClient (
std::shared_ptr<Network::Address> client, const Network::Buffer &packet
) {
auto socket = getInstance()->_socket;

(void)std::async(std::launch::async, [socket, client, packet] {
socket->sendTo(client, packet);
});
}

void ClientManager::sendPacketToAllClients(const Network::Buffer &packet)
{
auto instance = getInstance();
auto clients = instance->_clients;
auto socket = instance->_socket;

for (auto &tmp_client : clients) {
if (tmp_client.second->isConnected())
socket->sendTo(tmp_client.second->getAddress(), packet);
}
}

void ClientManager::sendPacketToAllClientsExcept (
std::shared_ptr<Network::Address> client, const Network::Buffer &packet
) {
auto instance = getInstance();
auto clients = instance->_clients;
auto socket = instance->_socket;

for (auto &tmp_client : clients) {
if (tmp_client.second->isConnected() && tmp_client.second->getAddress()->toString().value_or("") != client->toString().value_or(""))
socket->sendTo(tmp_client.second->getAddress(), packet);
}
}

void ClientManager::receivePacketFromClient (
std::shared_ptr<Network::Address> client, const Network::Buffer &buffer
) {
auto &clients = getInstance()->_clients;
auto &bannedClients = getInstance()->_bannedClients;
auto clientName = client->toString().value_or("Unknown");

if (std::find(bannedClients.begin(), bannedClients.end(), client->getIp().value_or("")) != bannedClients.end()) {
FLAKKARI_LOG_LOG("Client " + clientName + " tried to connect but is banned");
return;
}

if (clients.find(client->toString().value_or("")) == clients.end())
return;
auto &tmp_client = clients[client->toString().value_or("")];

FLAKKARI_LOG_LOG("Client " + clientName + " sent a packet: " + std::string(buffer));

Protocol::API::Packet<Protocol::API::CommandId> packet;
if (packet.deserialize(buffer)) {
tmp_client->_receiveQueue.push_back(packet);
return;
}

FLAKKARI_LOG_LOG("Client " + clientName + " sent an invalid packet");

if (!tmp_client->incrementWarningCount())
return;

FLAKKARI_LOG_LOG("Client " + clientName + " has been banned");

bannedClients.push_back(client->getIp().value());
FLAKKARI_LOG_LOG("Client " + clientName + " banned");
GameManager::removeClientFromGame("R-Type", tmp_client);
clients.erase(client->toString().value_or(""));
}

std::shared_ptr<Client> ClientManager::getClient(std::shared_ptr<Network::Address> client) {
return getInstance()->_clients[client->toString().value_or("")];
}

std::shared_ptr<Client> ClientManager::getClient(std::string ip) {
return getInstance()->_clients[ip];
std::shared_ptr<Client> ClientManager::getClient(std::string id) {
return getInstance()->_clients[id];
}

std::shared_ptr<Network::Address> ClientManager::getAddress(std::string ip) {
return getInstance()->_clients[ip]->getAddress();
std::shared_ptr<Network::Address> ClientManager::getAddress(std::string id) {
return getInstance()->_clients[id]->getAddress();
}

std::shared_ptr<Client> ClientManager::operator[](std::string ip) {
return _clients[ip];
std::shared_ptr<Client> ClientManager::operator[](std::string id) {
return _clients[id];
}

} /* namespace Flakkari */
58 changes: 51 additions & 7 deletions Flakkari/Server/Client/ClientManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#ifndef CLIENTMANAGER_HPP_
#define CLIENTMANAGER_HPP_

#include "Network/Address.hpp"
#include "Network/Socket.hpp"
#include "Client.hpp"

#include <unordered_map>
Expand Down Expand Up @@ -51,7 +51,9 @@ class ClientManager {
using id_t = short;

public:
std::unordered_map<std::string /*ip*/, std::shared_ptr<Client>> _clients;
std::vector<std::string /*ip*/> _bannedClients;
std::shared_ptr<Network::Socket> _socket;

public:
ClientManager(const ClientManager &) = delete;
Expand All @@ -71,6 +73,8 @@ class ClientManager {
*/
~ClientManager() = default;

static void setSocket(std::shared_ptr<Network::Socket> socket);

/**
* @brief Get the instance of the client manager
*
Expand All @@ -92,6 +96,15 @@ class ClientManager {
*/
static void removeClient(std::shared_ptr<Network::Address> client);

/**
* @brief Ban a client from the server
*
* @param client The client's address
*/
static void banClient(std::shared_ptr<Network::Address> client);

[[nodiscard]] static bool isBanned(std::shared_ptr<Network::Address> client);

/**
* @brief Check if the clients are still connected to the server
* and remove the inactive clients from the client manager
Expand All @@ -103,6 +116,37 @@ class ClientManager {
*/
static void checkInactiveClients();

/**
* @brief Send a packet to a client
*
* @param client The client's address
* @param packet The packet to send
*/
static void sendPacketToClient(std::shared_ptr<Network::Address> client, const Network::Buffer &packet);

/**
* @brief Send a packet to all clients
*
* @param packet The packet to send
*/
static void sendPacketToAllClients(const Network::Buffer &packet);

/**
* @brief Send a packet to all clients except one
*
* @param client The client's address
* @param packet The packet to send
*/
static void sendPacketToAllClientsExcept(std::shared_ptr<Network::Address> client, const Network::Buffer &packet);

/**
* @brief Receive a packet from a client
*
* @param client The client's address
* @param packet The packet received
*/
static void receivePacketFromClient(std::shared_ptr<Network::Address> client, const Network::Buffer &packet);

/**
* @brief Get the Client object
*
Expand All @@ -114,26 +158,26 @@ class ClientManager {
/**
* @brief Get the Client object
*
* @param ip The client's ip address and port
* @param id The client's id
* @return std::shared_ptr<Client> The client object
*/
static std::shared_ptr<Client> getClient(std::string ip);
static std::shared_ptr<Client> getClient(std::string id);

/**
* @brief Get the Address object
*
* @param ip The client's ip address and port
* @param id The client's id
* @return std::shared_ptr<Network::Address> The client's address
*/
static std::shared_ptr<Network::Address> getAddress(std::string ip);
static std::shared_ptr<Network::Address> getAddress(std::string id);

/**
* @brief Get the client object from the client manager
*
* @param ip The client's ip address and port
* @param id The client's id
* @return std::shared_ptr<Client> The client object
*/
std::shared_ptr<Client> operator[](std::string ip);
std::shared_ptr<Client> operator[](std::string id);
};

} /* namespace Flakkari */
Expand Down
Loading

0 comments on commit e1e47d3

Please sign in to comment.