Skip to content

Commit

Permalink
FORMAT-AUTO: automatic format on pull request #124
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Oct 23, 2023
1 parent 671e332 commit 8a396bd
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 68 deletions.
5 changes: 3 additions & 2 deletions src/Client/Systems/Graphic/DeathSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

namespace Systems {

void setPlayerAnimRectDeath(std::size_t id) {
void setPlayerAnimRectDeath(std::size_t id)
{
Registry::components<Types::AnimRect> arrAnimRect =
Registry::getInstance().getComponents<Types::AnimRect>();

Expand All @@ -25,7 +26,7 @@ namespace Systems {

// MAP FOR DEATH FUNCTIONS FOR EACH ENTITY
const std::unordered_map<std::type_index, std::function<void(std::size_t)>> deathFunctions = {
{std::type_index(typeid(Types::Player)), setPlayerAnimRectDeath},
{std::type_index(typeid(Types::Player)), setPlayerAnimRectDeath},
{std::type_index(typeid(Types::OtherPlayer)), setPlayerAnimRectDeath}
};

Expand Down
42 changes: 24 additions & 18 deletions src/Client/Systems/Network/ClientNetwork.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "CustomTypes.hpp"
#include "ClientNetwork.hpp"
#include <algorithm>
#include "CustomTypes.hpp"
#include "ECSCustomTypes.hpp"
#include "Json.hpp"
#include "Maths.hpp"
Expand Down Expand Up @@ -34,8 +34,8 @@ namespace Systems {
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto enemyDeath = std::any_cast<struct msgEnemyDeath_s>(any);
Registry::components<Types::Enemy> enemies = Registry::getInstance().getComponents<Types::Enemy>();
auto &arrHealth = Registry::getInstance().getComponents<struct health_s>();
std::vector<std::size_t> ids = enemies.getExistingsId();
auto &arrHealth = Registry::getInstance().getComponents<struct health_s>();
std::vector<std::size_t> ids = enemies.getExistingsId();

for (auto id : ids) {
if (enemies[id].getConstId().id == enemyDeath.enemyId.id) {
Expand Down Expand Up @@ -75,22 +75,26 @@ namespace Systems {

static void createNewPlayer(const struct msgCreatePlayer_s &newPlayer)
{
Registry &registry = Registry::getInstance();
auto &arrPlayer = registry.getComponents<Types::Player>();
Registry &registry = Registry::getInstance();
auto &arrPlayer = registry.getComponents<Types::Player>();
auto &arrOtherPlayer = registry.getComponents<Types::OtherPlayer>();
auto idsPlayer = registry.getEntitiesByComponents(
auto idsPlayer = registry.getEntitiesByComponents(
{typeid(Types::Position), typeid(Types::Player), typeid(struct health_s)});
auto idsOtherPlayer = registry.getEntitiesByComponents(
{typeid(Types::Position), typeid(Types::OtherPlayer), typeid(struct health_s)});
auto player = std::find_if(idsPlayer.begin(), idsPlayer.end(), [&arrPlayer, &newPlayer](std::size_t id) {
return arrPlayer[id].constId == newPlayer.playerId;
});
auto otherPlayers = std::find_if(idsOtherPlayer.begin(), idsOtherPlayer.end(), [&arrOtherPlayer, &newPlayer](std::size_t id) {
return arrOtherPlayer[id].constId == newPlayer.playerId;
});
auto player =
std::find_if(idsPlayer.begin(), idsPlayer.end(), [&arrPlayer, &newPlayer](std::size_t id) {
return arrPlayer[id].constId == newPlayer.playerId;
});
auto otherPlayers = std::find_if(
idsOtherPlayer.begin(),
idsOtherPlayer.end(),
[&arrOtherPlayer, &newPlayer](std::size_t id) {
return arrOtherPlayer[id].constId == newPlayer.playerId;
});

if (player != idsPlayer.end() || otherPlayers != idsOtherPlayer.end()) {
auto id = player != idsPlayer.end() ? *player : *otherPlayers;
auto id = player != idsPlayer.end() ? *player : *otherPlayers;
Registry::getInstance().removeEntity(id);
}
initPlayer(newPlayer.playerId, newPlayer.pos, newPlayer.life, newPlayer.isOtherPlayer);
Expand Down Expand Up @@ -244,11 +248,13 @@ namespace Systems {
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto playerDeath = std::any_cast<struct msgPlayerDeath_s>(any);
auto &arrPlayer = Registry::getInstance().getComponents<Types::Player>();
auto &arrOtherPlayers = Registry::getInstance().getComponents<Types::OtherPlayer>();
auto &arrHealth = Registry::getInstance().getComponents<struct health_s>();
auto playersIds = Registry::getInstance().getEntitiesByComponents({typeid(Types::Player), typeid(struct health_s)});
auto otherPlayersIds = Registry::getInstance().getEntitiesByComponents({typeid(Types::OtherPlayer), typeid(struct health_s)});
auto &arrPlayer = Registry::getInstance().getComponents<Types::Player>();
auto &arrOtherPlayers = Registry::getInstance().getComponents<Types::OtherPlayer>();
auto &arrHealth = Registry::getInstance().getComponents<struct health_s>();
auto playersIds = Registry::getInstance().getEntitiesByComponents(
{typeid(Types::Player), typeid(struct health_s)});
auto otherPlayersIds = Registry::getInstance().getEntitiesByComponents(
{typeid(Types::OtherPlayer), typeid(struct health_s)});

for (auto &id : playersIds) {
if (arrPlayer[id].constId == playerDeath.playerId) {
Expand Down
16 changes: 10 additions & 6 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ namespace Systems {
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry &registry = Registry::getInstance();
auto &deadList = registry.getComponents<Types::Dead>();
auto &deadList = registry.getComponents<Types::Dead>();
auto deadIdList = deadList.getExistingsId();
Clock &clock = registry.getClock();
std::size_t decrease = 0;
Expand All @@ -359,7 +359,7 @@ namespace Systems {
executeDeathFunction(std::size_t id, Registry::components<Types::Dead> arrDead, std::size_t &decrease)
{
#ifdef CLIENT
auto &arrPlayer = Registry::getInstance().getComponents<Types::Player>();
auto &arrPlayer = Registry::getInstance().getComponents<Types::Player>();
auto &arrOtherPlayer = Registry::getInstance().getComponents<Types::OtherPlayer>();
#endif

Expand Down Expand Up @@ -395,9 +395,9 @@ namespace Systems {
#endif
if (arrEnemies.exist(arrId)) {
#ifdef CLIENT
Nitwork::NitworkClient::getInstance().addEnemyDeathMsg(arrEnemies[arrId].getConstId().id);
Nitwork::NitworkClient::getInstance().addEnemyDeathMsg(arrEnemies[arrId].getConstId().id);
#else
Nitwork::NitworkServer::getInstance().addEnemyDeathMessage(arrEnemies[arrId].getConstId().id);
Nitwork::NitworkServer::getInstance().addEnemyDeathMessage(arrEnemies[arrId].getConstId().id);
#endif
}
}
Expand Down Expand Up @@ -463,7 +463,11 @@ namespace Systems {
}
}

void initPlayer(unsigned int constId, const struct position_absolute_s &pos, const struct health_s &life, bool otherPlayer)
void initPlayer(
unsigned int constId,
const struct position_absolute_s &pos,
const struct health_s &life,
bool otherPlayer)
{
JsonType playerType = JsonType::DEFAULT_PLAYER;

Expand Down Expand Up @@ -498,7 +502,7 @@ namespace Systems {
Types::Player playerComp = {constId};
Registry::getInstance().getComponents<Types::Player>().insertBack(playerComp);
}
Types::Position position = {pos.x, pos.y};
Types::Position position = {pos.x, pos.y};
Types::CollisionRect collisionRect = {Types::CollisionRect(
Json::getInstance().getDataByVector({"player", "collisionRect"}, playerType))};

Expand Down
6 changes: 5 additions & 1 deletion src/ECS/Systems/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ namespace Systems {
void deathChecker(std::size_t, std::size_t);
void updatePhysics(std::size_t, std::size_t);
void initWave(std::size_t managerId, std::size_t systemId);
void initPlayer(unsigned int constId, const struct position_absolute_s &pos, const struct health_s &life, bool otherPlayer = false);
void initPlayer(
unsigned int constId,
const struct position_absolute_s &pos,
const struct health_s &life,
bool otherPlayer = false);
void createMissile(Types::Position pos, Types::Missiles &typeOfMissile);
std::vector<std::function<void(std::size_t, std::size_t)>> getECSSystems();
std::vector<std::function<void(std::size_t, std::size_t)>> getBulletSystems();
Expand Down
4 changes: 2 additions & 2 deletions src/Nitwork/ANitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ namespace Nitwork {
void handleBody(const actionHandler &handler, const struct header_s &header)
{
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
auto *body = reinterpret_cast<B *>(
_receiveBuffer.data() + HEADER_SIZE + sizeof(struct action_s));
auto *body =
reinterpret_cast<B *>(_receiveBuffer.data() + HEADER_SIZE + sizeof(struct action_s));
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
handleBodyDatas<B>(handler, header, *body, boost::system::error_code());
}
Expand Down
10 changes: 3 additions & 7 deletions src/Nitwork/NitworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,9 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = PLAYER_DEATH,
},
.msg =
{
.magick = MAGICK_PLAYER_DEATH,
.playerId = id
},
.magick = PLAYER_DEATH,
},
.msg = {.magick = MAGICK_PLAYER_DEATH, .playerId = id},
};
Packet packet(
packetPlayerDeath.action.magick,
Expand Down
46 changes: 28 additions & 18 deletions src/Nitwork/NitworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ namespace Nitwork {
}

/* Handle packet (msg) Section */
void NitworkServer::sendAlliesAlreadyPresent(boost::asio::ip::udp::endpoint &endpoint,
n_id_t playerId)
void NitworkServer::sendAlliesAlreadyPresent(boost::asio::ip::udp::endpoint &endpoint, n_id_t playerId)
{
auto &arrPos = Registry::getInstance().getComponents<Types::Position>();
auto &arrHealth = Registry::getInstance().getComponents<struct health_s>();
auto &arrPos = Registry::getInstance().getComponents<Types::Position>();
auto &arrHealth = Registry::getInstance().getComponents<struct health_s>();
auto &arrOtherPlayers = Registry::getInstance().getComponents<Types::OtherPlayer>();
auto ids = Registry::getInstance().getEntitiesByComponents({typeid(Types::Position), typeid(Types::OtherPlayer), typeid(struct health_s)});
auto ids = Registry::getInstance().getEntitiesByComponents(
{typeid(Types::Position), typeid(Types::OtherPlayer), typeid(struct health_s)});

for (const auto &e : _playersIds) {
auto allieId = e.second;
Expand All @@ -153,12 +153,11 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action = {.magick = INIT},
.msg = {
.magick = MAGICK_NEW_PLAYER,
.playerId = allieId,
.pos = {arrPos[*idIt].x, arrPos[*idIt].y},
.life = arrHealth[*idIt],
.isOtherPlayer = true
}
.magick = MAGICK_NEW_PLAYER,
.playerId = allieId,
.pos = {arrPos[*idIt].x, arrPos[*idIt].y},
.life = arrHealth[*idIt],
.isOtherPlayer = true}
};
sendNewAllie(allieId, packetMsgCreatePlayer, endpoint, false);
}
Expand All @@ -177,21 +176,30 @@ namespace Nitwork {
return;
}
_endpoints.emplace_back(endpoint);
auto playerId = static_cast<n_id_t>(_endpoints.size() - 1);
auto playerId = static_cast<n_id_t>(_endpoints.size() - 1);
auto &jsonInstance = Json::getInstance();

struct packetCreatePlayer_s packetMsgCreatePlayer = {
.header = {0, 0, 0, 0, 1, 0},
.action = {.magick = NEW_PLAYER},
.msg = {.magick = MAGICK_NEW_PLAYER,
.msg = {
.magick = MAGICK_NEW_PLAYER,
.playerId = playerId,
.pos = {jsonInstance.getDataByVector<int>({"player", "position", "x"}, JsonType::DEFAULT_PLAYER), jsonInstance.getDataByVector<int>({"player", "position", "y"}, JsonType::DEFAULT_PLAYER)},
.pos =
{jsonInstance
.getDataByVector<int>({"player", "position", "x"}, JsonType::DEFAULT_PLAYER),
jsonInstance
.getDataByVector<int>({"player", "position", "y"}, JsonType::DEFAULT_PLAYER)},
.life = {jsonInstance.getDataByVector<int>({"player", "health"}, JsonType::DEFAULT_PLAYER)},
.isOtherPlayer = false}
};
addPlayerInitMessage(endpoint, packetMsgCreatePlayer.msg);
packetMsgCreatePlayer.msg.isOtherPlayer = true;
Systems::initPlayer(playerId, packetMsgCreatePlayer.msg.pos, packetMsgCreatePlayer.msg.life, packetMsgCreatePlayer.msg.isOtherPlayer);
Systems::initPlayer(
playerId,
packetMsgCreatePlayer.msg.pos,
packetMsgCreatePlayer.msg.life,
packetMsgCreatePlayer.msg.isOtherPlayer);
sendNewAllie(playerId, packetMsgCreatePlayer, endpoint);
sendAlliesAlreadyPresent(endpoint, playerId);
}
Expand Down Expand Up @@ -244,13 +252,15 @@ namespace Nitwork {
/* End Handle packet (msg) Section */

/* Message Creation Section */
void NitworkServer::addPlayerInitMessage(boost::asio::ip::udp::endpoint &endpoint, const msgCreatePlayer_s &playerMsg)
void NitworkServer::addPlayerInitMessage(
boost::asio::ip::udp::endpoint &endpoint,
const msgCreatePlayer_s &playerMsg)
{
std::lock_guard<std::mutex> lock(_receivedPacketsIdsMutex);
struct packetCreatePlayer_s packetCreatePlayer = {
.header = {0, 0, 0, 0, 1, 0},
.action = {.magick = INIT},
.msg = playerMsg
.msg = playerMsg
};
Packet packet(
packetCreatePlayer.action.magick,
Expand Down Expand Up @@ -380,7 +390,7 @@ namespace Nitwork {
struct packetCreatePlayer_s packetCreatePlayer = {
.header = {0, 0, 0, 0, 1, 0},
.action = {.magick = NEW_PLAYER},
.msg = playerMsg
.msg = playerMsg
};
Packet packet(
packetCreatePlayer.action.magick,
Expand Down
10 changes: 6 additions & 4 deletions src/Nitwork/NitworkServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ namespace Nitwork {
boost::asio::ip::udp::endpoint &endpoint,
const struct enemy_infos_s &enemyInfos);

void addPlayerInitMessage(boost::asio::ip::udp::endpoint &endpoint, const msgCreatePlayer_s &playerMsg);
void addPlayerInitMessage(
boost::asio::ip::udp::endpoint &endpoint,
const msgCreatePlayer_s &playerMsg);

void broadcastNewBulletMsg(
const struct msgNewBullet_s &msg,
Expand Down Expand Up @@ -193,9 +195,9 @@ namespace Nitwork {
sendData<struct packetPositionAbsoluteBroadcast_s>(packet);
}},
{PLAYER_DEATH,
[this](Packet &packet) {
sendData<struct packetPlayerDeath_s>(packet);
}},
[this](Packet &packet) {
sendData<struct packetPlayerDeath_s>(packet);
}},
};
};
} // namespace Nitwork
22 changes: 12 additions & 10 deletions src/Server/Systems/Network/ServerNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,27 @@ namespace Systems {
auto &arrHealth = registry.getComponents<struct health_s>();
auto &arrPos = registry.getComponents<Types::Position>();
auto &arrOtherPlayers = registry.getComponents<Types::OtherPlayer>();
std::vector<std::size_t> ids =
registry.getEntitiesByComponents({typeid(struct health_s), typeid(Types::OtherPlayer), typeid(Types::Position)});
std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(struct health_s), typeid(Types::OtherPlayer), typeid(Types::Position)});

for (auto &id : ids) {
auto &life = arrHealth[id];
auto &otherPlayer = arrOtherPlayers[id];
auto &pos = arrPos[id];
auto &pos = arrPos[id];
if (otherPlayer.constId == msgPlayerDeath.playerId) {
if (life.hp > 0) {
Nitwork::NitworkServer::getInstance().addNewPlayerMsg(
endpoint,
{
.magick = MAGICK_NEW_PLAYER,
.playerId = otherPlayer.constId,
.pos = {pos.x, pos.y},
.life = life,
.isOtherPlayer = (Nitwork::NitworkServer::getInstance().getPlayerId(endpoint) != otherPlayer.constId) ? true : false,
});

.magick = MAGICK_NEW_PLAYER,
.playerId = otherPlayer.constId,
.pos = {pos.x, pos.y},
.life = life,
.isOtherPlayer = (Nitwork::NitworkServer::getInstance().getPlayerId(endpoint)
!= otherPlayer.constId)
? true
: false,
});
}
return;
}
Expand Down

0 comments on commit 8a396bd

Please sign in to comment.