From a36c457aac604d8fec560a8d11e404dd72c1687d Mon Sep 17 00:00:00 2001 From: romain Date: Sun, 15 Oct 2023 20:35:02 +0200 Subject: [PATCH 1/2] CLIENT-NETWORK: Add system receive broadcast position absolute MINOR --- src/Client/Systems/Network/ClientNetwork.cpp | 25 ++++++++++++++++++++ src/Client/Systems/Network/ClientNetwork.hpp | 1 + src/Nitwork/NitworkClient.hpp | 11 +++++++++ src/Nitwork/NitworkServer.hpp | 2 +- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Client/Systems/Network/ClientNetwork.cpp b/src/Client/Systems/Network/ClientNetwork.cpp index dca02c10..76090f31 100644 --- a/src/Client/Systems/Network/ClientNetwork.cpp +++ b/src/Client/Systems/Network/ClientNetwork.cpp @@ -166,6 +166,31 @@ namespace Systems { Systems::createMissile(position, missileType); } + void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint & /* unused*/) + { + std::lock_guard lock(Registry::getInstance().mutex); + + const struct msgPositionAbsoluteBroadcast_s &msg = std::any_cast(any); + struct Types::Position position = { + static_cast(msg.pos.x), + static_cast(msg.pos.y), + }; + auto &arrPos = Registry::getInstance().getComponents(); + auto &arrOtherPlayers = Registry::getInstance().getComponents(); + auto ids = Registry::getInstance().getEntitiesByComponents({typeid(Types::Position), typeid(Types::OtherPlayer)}); + auto otherPlayer = std::find_if( + ids.begin(), + ids.end(), + [&arrOtherPlayers, &msg](std::size_t id) { + return arrOtherPlayers[id].constId == msg.playerId; + }); + + if (otherPlayer == ids.end()) { + return; + } + arrPos[*otherPlayer] = position; + } + std::vector> getNetworkSystems() { return {sendPositionRelative, sendPositionAbsolute}; diff --git a/src/Client/Systems/Network/ClientNetwork.hpp b/src/Client/Systems/Network/ClientNetwork.hpp index 1c2e94e5..5c71f99c 100644 --- a/src/Client/Systems/Network/ClientNetwork.hpp +++ b/src/Client/Systems/Network/ClientNetwork.hpp @@ -13,5 +13,6 @@ namespace Systems { void receiveNewAllie(std::any &any, boost::asio::ip::udp::endpoint &); void sendPositionRelative(std::size_t /* unused */, std::size_t /* unused */); void receiveNewBullet(std::any &any, boost::asio::ip::udp::endpoint &endpoint); + void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint &endpoint); std::vector> getNetworkSystems(); } // namespace Systems diff --git a/src/Nitwork/NitworkClient.hpp b/src/Nitwork/NitworkClient.hpp index 4395fc74..05aa6a2f 100644 --- a/src/Nitwork/NitworkClient.hpp +++ b/src/Nitwork/NitworkClient.hpp @@ -140,6 +140,17 @@ namespace Nitwork { Systems::receiveNewBullet(any, endpoint); } } + }, + { + POSITION_ABSOLUTE_BROADCAST, + { + [this](actionHandler &handler, const struct header_s &header) { + handleBody(handler, header); + }, + [](std::any &any, boost::asio::ip::udp::endpoint &endpoint) { + Systems::receiveBroadcastAbsolutePosition(any, endpoint); + } + } } }; std::map< diff --git a/src/Nitwork/NitworkServer.hpp b/src/Nitwork/NitworkServer.hpp index 03eab7b7..190256f7 100644 --- a/src/Nitwork/NitworkServer.hpp +++ b/src/Nitwork/NitworkServer.hpp @@ -161,7 +161,7 @@ namespace Nitwork { }}, {POSITION_ABSOLUTE_BROADCAST, [this](Packet &packet) { - sendData(packet); + sendData(packet); }}, }; }; From bc2ccb66e829044826efbd59c49ad41fc710921b Mon Sep 17 00:00:00 2001 From: Github Actions Date: Sun, 15 Oct 2023 18:37:59 +0000 Subject: [PATCH 2/2] FORMAT-AUTO: automatic format on pull request #104 --- src/Client/Systems/Network/ClientNetwork.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Client/Systems/Network/ClientNetwork.cpp b/src/Client/Systems/Network/ClientNetwork.cpp index 76090f31..485d463a 100644 --- a/src/Client/Systems/Network/ClientNetwork.cpp +++ b/src/Client/Systems/Network/ClientNetwork.cpp @@ -170,20 +170,19 @@ namespace Systems { { std::lock_guard lock(Registry::getInstance().mutex); - const struct msgPositionAbsoluteBroadcast_s &msg = std::any_cast(any); + const struct msgPositionAbsoluteBroadcast_s &msg = + std::any_cast(any); struct Types::Position position = { static_cast(msg.pos.x), static_cast(msg.pos.y), }; - auto &arrPos = Registry::getInstance().getComponents(); + auto &arrPos = Registry::getInstance().getComponents(); auto &arrOtherPlayers = Registry::getInstance().getComponents(); - auto ids = Registry::getInstance().getEntitiesByComponents({typeid(Types::Position), typeid(Types::OtherPlayer)}); - auto otherPlayer = std::find_if( - ids.begin(), - ids.end(), - [&arrOtherPlayers, &msg](std::size_t id) { - return arrOtherPlayers[id].constId == msg.playerId; - }); + auto ids = Registry::getInstance().getEntitiesByComponents( + {typeid(Types::Position), typeid(Types::OtherPlayer)}); + auto otherPlayer = std::find_if(ids.begin(), ids.end(), [&arrOtherPlayers, &msg](std::size_t id) { + return arrOtherPlayers[id].constId == msg.playerId; + }); if (otherPlayer == ids.end()) { return;