Skip to content

Commit

Permalink
NITWORK: Fix connect lobby bug, (action cast bug)
Browse files Browse the repository at this point in the history
PATCH
  • Loading branch information
romainpanno committed Nov 5, 2023
1 parent e5f869a commit b4a4908
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 132 deletions.
4 changes: 2 additions & 2 deletions src/Game/GameSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ namespace Systems {
Registry::getInstance().getComponents<Types::Player>();
auto arrHealth = Registry::getInstance().getComponents<Types::Health>();

struct health_s health = {arrHealth[id].hp};
if (arrPlayer.exist(id)) {
Nitwork::NitworkClient::getInstance().addLifeUpdateMsg(arrPlayer[id].constId, {arrHealth[id].hp});
Nitwork::NitworkClient::getInstance().addLifeUpdateMsg(arrPlayer[id].constId, health);
}
}

Expand Down Expand Up @@ -146,7 +147,6 @@ namespace Systems {
registry.addEventCallback(Events::ENTITY_DEATH, sendMissileDeath);
#ifdef CLIENT
registry.addEventCallback(Events::ENTITY_DEATH, sendPlayerDeathToServer);

registry.addEventCallback(Events::TAKE_DAMAGE, sendLifeUpdateToServer);
#endif
registry.addAllie(static_cast<std::size_t>(AlliesType::PLAYERS), typeid(Types::Player));
Expand Down
1 change: 0 additions & 1 deletion src/Nitwork/ANitwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ namespace Nitwork {
});
for (auto &action : _actions) {
try {
Logger::fatal("NITWORK: Adding action to queue: " + std::to_string(action.first.id));
action.second(action.first.data, action.first.endpoint);
} catch (std::exception &e) {
Logger::fatal("StartInputHandler:");
Expand Down
7 changes: 7 additions & 0 deletions src/Nitwork/ANitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "B-luga/Logger.hpp"
#include "INitwork.hpp"
#include "Zstd.hpp"
#include <type_traits>

namespace Nitwork {

Expand Down Expand Up @@ -63,9 +64,15 @@ namespace Nitwork {
HEADER_CODE2};
data.header = newHeader;
}
#ifdef DEBUG
if constexpr (!std::is_same_v<T, struct packetListLobby_s>) {
Logger::fatal("id of packet = " + std::to_string(id) + " action type = " + std::to_string(data.action.magick));
}
#endif
std::shared_ptr<std::vector<char>> compressedPacket =
std::make_shared<std::vector<char>>(Zstd::compress(data));


_socket.async_send_to(
boost::asio::buffer(*compressedPacket),
packet.endpoint,
Expand Down
48 changes: 24 additions & 24 deletions src/Nitwork/Nitwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,30 @@ typedef unsigned char n_nbAction_t;
*/
enum n_actionType_t {
NO_ACTION = 0,
INIT = 1,
READY = 2,
START_WAVE = 3,
LIFE_UPDATE = 5,
ENEMY_DEATH = 6,
POSITION_RELATIVE = 7,
POSITION_ABSOLUTE = 8,
NEW_MISSILE = 9,
NEW_ENEMY = 10,
NEW_PLAYER = 11,
POSITION_RELATIVE_BROADCAST = 12,
POSITION_ABSOLUTE_BROADCAST = 13,
PLAYER_DEATH = 14,
MISSILE_DEATH = 15,
LIST_LOBBY = 16,
INFO_LOBBY = 17,
CONNECT_MAIN_SERVER = 18,
CONNECT_MAIN_SERVER_RESP = 19,
CONNECT_LOBBY = 20,
CONNECT_LOBBY_RESP = 21,
DISCONNECT_LOBBY = 22,
CREATE_LOBBY = 23,
END_GAME = 24,
N_ACTION_TYPE_MAX,
NITWORK_INIT = 1,
NITWORK_READY = 2,
NITWORK_START_WAVE = 3,
NITWORK_LIFE_UPDATE = 5,
NITWORK_ENEMY_DEATH = 6,
NITWORK_POSITION_RELATIVE = 7,
NITWORK_POSITION_ABSOLUTE = 8,
NITWORK_NEW_MISSILE = 9,
NITWORK_NEW_ENEMY = 10,
NITWORK_NEW_PLAYER = 11,
NITWORK_POSITION_RELATIVE_BROADCAST = 12,
NITWORK_POSITION_ABSOLUTE_BROADCAST = 13,
NITWORK_PLAYER_DEATH = 14,
NITWORK_MISSILE_DEATH = 15,
NITWORK_LIST_LOBBY = 16,
NITWORK_INFO_LOBBY = 17,
NITWORK_CONNECT_MAIN_SERVER = 18,
NITWORK_CONNECT_MAIN_SERVER_RESP = 19,
NITWORK_CONNECT_LOBBY = 20,
NITWORK_CONNECT_LOBBY_RESP = 21,
NITWORK_DISCONNECT_LOBBY = 22,
NITWORK_CREATE_LOBBY = 23,
NITWORK_END_GAME = 24,
NITWORK_N_ACTION_TYPE_MAX,
};

PACK(struct header_s {
Expand Down
44 changes: 22 additions & 22 deletions src/Nitwork/NitworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = DISCONNECT_LOBBY,
.magick = NITWORK_DISCONNECT_LOBBY,
},
.msg = {.magick = MAGICK_DISCONNECT_LOBBY},
};
Logger::error("------------------------------------ Sending disconnect lobby msg: " + std::to_string(DISCONNECT_LOBBY) + " " + std::to_string(MAGICK_DISCONNECT_LOBBY));
Logger::error("------------------------------------ Sending disconnect lobby msg: " + std::to_string(NITWORK_DISCONNECT_LOBBY) + " " + std::to_string(MAGICK_DISCONNECT_LOBBY));
Packet packet(
packetDisconnectLobby.action.magick,
std::make_any<struct packetDisconnectLobby_s>(packetDisconnectLobby),
Expand All @@ -168,11 +168,11 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = INIT,
.magick = NITWORK_INIT,
},
.msgInit = {.magick = MAGICK_INIT}
};
Logger::error("------------------------------------ Sending init msg: " + std::to_string(INIT) + " " + std::to_string(MAGICK_INIT));
Logger::error("------------------------------------ Sending init msg: " + std::to_string(NITWORK_INIT) + " " + std::to_string(MAGICK_INIT));
Packet packet(
packetMsgInit.action.magick,
std::make_any<struct packetMsgInit_s>(packetMsgInit),
Expand All @@ -185,10 +185,10 @@ namespace Nitwork {
std::lock_guard<std::mutex> lock(_receivedPacketsIdsMutex);
struct packetMsgReady_s packetMsgReady = {
.header = {0, 0, 0, 0, 1, 0},
.action = {.magick = READY},
.action = {.magick = NITWORK_READY},
.msgReady = {.magick = MAGICK_READY}
};
Logger::error("------------------------------------ Sending ready msg: " + std::to_string(READY) + " " + std::to_string(MAGICK_READY));
Logger::error("------------------------------------ Sending ready msg: " + std::to_string(NITWORK_READY) + " " + std::to_string(MAGICK_READY));
Packet packet(
packetMsgReady.action.magick,
std::make_any<struct packetMsgReady_s>(packetMsgReady),
Expand All @@ -203,7 +203,7 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = POSITION_RELATIVE,
.magick = NITWORK_POSITION_RELATIVE,
},
.msg =
{
Expand All @@ -227,7 +227,7 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = NEW_MISSILE,
.magick = NITWORK_NEW_MISSILE,
},
.msg =
{
Expand All @@ -252,7 +252,7 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = POSITION_ABSOLUTE,
.magick = NITWORK_POSITION_ABSOLUTE,
},
.msg =
{
Expand All @@ -275,7 +275,7 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = LIFE_UPDATE,
.magick = NITWORK_LIFE_UPDATE,
},
.msgLifeUpdate =
{
Expand All @@ -300,7 +300,7 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = ENEMY_DEATH,
.magick = NITWORK_ENEMY_DEATH,
},
.msgEnemyDeath =
{
Expand All @@ -322,7 +322,7 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = PLAYER_DEATH,
.magick = NITWORK_PLAYER_DEATH,
},
.msg = {.magick = MAGICK_PLAYER_DEATH, .playerId = id},
};
Expand All @@ -340,15 +340,15 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = MISSILE_DEATH,
.magick = NITWORK_MISSILE_DEATH,
},
.msgMissileDeath =
{
.magick = MAGICK_MISSILE_DEATH,
.missileId = id,
},
};
Logger::error("------------------------------------ Sending missile death msg: " + std::to_string(MISSILE_DEATH) + " " + std::to_string(MAGICK_MISSILE_DEATH));
Logger::error("------------------------------------ Sending missile death msg: " + std::to_string(NITWORK_MISSILE_DEATH) + " " + std::to_string(MAGICK_MISSILE_DEATH));
Packet packet(
packetMissileDeath.action.magick,
std::make_any<struct packetMissileDeath_s>(packetMissileDeath),
Expand All @@ -362,11 +362,11 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = LIST_LOBBY,
.magick = NITWORK_LIST_LOBBY,
},
.msg = {.magick = MAGICK_REQUEST_LIST_LOBBY},
};
Logger::error("------------------------------------ Sending list lobby msg: " + std::to_string(LIST_LOBBY) + " " + std::to_string(MAGICK_REQUEST_LIST_LOBBY));
Logger::error("------------------------------------ Sending list lobby msg: " + std::to_string(NITWORK_LIST_LOBBY) + " " + std::to_string(MAGICK_REQUEST_LIST_LOBBY));
Packet packet(
packetListLobby.action.magick,
std::make_any<struct packetRequestListLobby_s>(packetListLobby),
Expand All @@ -384,7 +384,7 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = CREATE_LOBBY,
.magick = NITWORK_CREATE_LOBBY,
},
.msg = {
.magick = MAGICK_CREATE_LOBBY,
Expand All @@ -393,7 +393,7 @@ namespace Nitwork {
.maxNbPlayer = maxNbPlayer,
.ownerInfos = {.ip = "", .port = _mainServerEndpoint.port()}}
};
Logger::error("------------------------------------ Sending create lobby msg: " + std::to_string(CREATE_LOBBY) + " " + std::to_string(MAGICK_CREATE_LOBBY));
Logger::error("------------------------------------ Sending create lobby msg: " + std::to_string(NITWORK_CREATE_LOBBY) + " " + std::to_string(MAGICK_CREATE_LOBBY));
std::strcpy(packetCreateLobby.msg.name, (name.size() > 32 ? name.substr(0, 32) : name).c_str());
std::strcpy(
packetCreateLobby.msg.ownerInfos.ip,
Expand All @@ -415,11 +415,11 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = CONNECT_MAIN_SERVER,
.magick = NITWORK_CONNECT_MAIN_SERVER,
},
.msg = {.magick = MAGICK_CONNECT_MAIN_SERVER},
};
Logger::error("------------------------------------ Sending connect main server msg: " + std::to_string(CONNECT_MAIN_SERVER) + " " + std::to_string(MAGICK_CONNECT_MAIN_SERVER));
Logger::error("------------------------------------ Sending connect main server msg: " + std::to_string(NITWORK_CONNECT_MAIN_SERVER) + " " + std::to_string(MAGICK_CONNECT_MAIN_SERVER));
Packet packet(
packetConnectMainServer.action.magick,
std::make_any<struct packetConnectMainServer_s>(packetConnectMainServer),
Expand All @@ -434,11 +434,11 @@ namespace Nitwork {
.header = {0, 0, 0, 0, 1, 0},
.action =
{
.magick = CONNECT_LOBBY,
.magick = NITWORK_CONNECT_LOBBY,
},
.msg = {.magick = MAGICK_CONNECT_LOBBY},
};
Logger::error("------------------------------------ Sending connect lobby msg: " + std::to_string(CONNECT_LOBBY) + " " + std::to_string(MAGICK_CONNECT_LOBBY));
Logger::error("------------------------------------ Sending connect lobby msg: " + std::to_string(NITWORK_CONNECT_LOBBY) + " " + std::to_string(MAGICK_CONNECT_LOBBY));
Packet packet(
packetConnectLobby.action.magick,
std::make_any<struct packetConnectLobby_s>(packetConnectLobby),
Expand Down
Loading

0 comments on commit b4a4908

Please sign in to comment.