From ee39c1e42918f3b5992ca0fefea2cee73262ce0a Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 19 Aug 2025 18:42:02 +0200 Subject: [PATCH] add playerlist packet --- include/td/protocol/packet/PacketData.h | 15 ++++++++++----- include/td/protocol/packet/Packets.h | 12 +++++++----- src/server/ConnectionHandler.cpp | 12 +++++++++--- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/include/td/protocol/packet/PacketData.h b/include/td/protocol/packet/PacketData.h index b461f83..481ed73 100644 --- a/include/td/protocol/packet/PacketData.h +++ b/include/td/protocol/packet/PacketData.h @@ -27,16 +27,16 @@ struct MapData { namespace pdata { -/** Client attempts to login (very basic) */ -struct PlayerLogin { - std::string m_PlayerName; -}; - /** Server indicates success */ struct LoggingSuccess { PlayerID m_PlayerId; }; +/** Client attempts to login (very basic) */ +struct PlayerLogin { + std::string m_PlayerName; +}; + /** Player joins the lobby */ struct PlayerJoin { PlayerInfo m_Player; @@ -47,6 +47,11 @@ struct PlayerLeave { PlayerID m_PlayerId; }; +/** List of players who joined before */ +struct PlayerList { + std::vector m_Players; +}; + struct PredictCommand { CommandPtr m_Command; StepTime m_FrameNumber; diff --git a/include/td/protocol/packet/Packets.h b/include/td/protocol/packet/Packets.h index cd93ee3..9dbfd41 100644 --- a/include/td/protocol/packet/Packets.h +++ b/include/td/protocol/packet/Packets.h @@ -29,6 +29,7 @@ enum class PacketID : std::uint8_t { PlaceTower, PlayerJoin, PlayerLeave, + PlayerList, PlayerLogin, PredictCommand, SpawnTroop, @@ -58,6 +59,7 @@ using LoggingSuccessPacket = PacketMessage; using PlayerJoinPacket = PacketMessage; using PlayerLeavePacket = PacketMessage; +using PlayerListPacket = PacketMessage; using PlayerLoginPacket = PacketMessage; using PredictCommandPacket = PacketMessage; using SpawnTroopPacket = PacketMessage; @@ -66,11 +68,11 @@ using WorldDataPacket = PacketMessage; } // namespace packets -using AllPackets = - std::tuple; +using AllPackets = std::tuple; class PacketHandler : public sp::GenericHandler { public: diff --git a/src/server/ConnectionHandler.cpp b/src/server/ConnectionHandler.cpp index c6afeee..917974f 100644 --- a/src/server/ConnectionHandler.cpp +++ b/src/server/ConnectionHandler.cpp @@ -8,11 +8,17 @@ namespace server { ConnectionHandler::ConnectionHandler(IServerSocket& a_Server, PlayerID a_Player) : m_Server(a_Server), m_Player(a_Player) {} void ConnectionHandler::Handle(const protocol::packets::PlayerLoginPacket& a_Packet) { - std::cout << "[Server] " << a_Packet->m_PlayerName << " tried to join !\n"; protocol::PlayerInfo pInfo{m_Player, a_Packet->m_PlayerName}; - m_Server.OnPlayerJoin(m_Player, pInfo); + + // TODO + std::vector players; + + m_Server.Send(m_Player, protocol::packets::LoggingSuccessPacket(m_Player)); + m_Server.Send(m_Player, protocol::packets::PlayerListPacket(players)); m_Server.Broadcast(protocol::packets::PlayerJoinPacket(pInfo)); - // TODO: send already existing players + + m_Server.OnPlayerJoin(m_Player, pInfo); + std::cout << "[Server] " << a_Packet->m_PlayerName << " tried to join !\n"; } void ConnectionHandler::Handle(const protocol::packets::DisconnectPacket& a_Packet) {