add playerlist packet

This commit is contained in:
2025-08-19 18:42:02 +02:00
parent 631e14e66e
commit ee39c1e429
3 changed files with 26 additions and 13 deletions

View File

@@ -27,16 +27,16 @@ struct MapData {
namespace pdata { namespace pdata {
/** Client attempts to login (very basic) */
struct PlayerLogin {
std::string m_PlayerName;
};
/** Server indicates success */ /** Server indicates success */
struct LoggingSuccess { struct LoggingSuccess {
PlayerID m_PlayerId; PlayerID m_PlayerId;
}; };
/** Client attempts to login (very basic) */
struct PlayerLogin {
std::string m_PlayerName;
};
/** Player joins the lobby */ /** Player joins the lobby */
struct PlayerJoin { struct PlayerJoin {
PlayerInfo m_Player; PlayerInfo m_Player;
@@ -47,6 +47,11 @@ struct PlayerLeave {
PlayerID m_PlayerId; PlayerID m_PlayerId;
}; };
/** List of players who joined before */
struct PlayerList {
std::vector<PlayerInfo> m_Players;
};
struct PredictCommand { struct PredictCommand {
CommandPtr m_Command; CommandPtr m_Command;
StepTime m_FrameNumber; StepTime m_FrameNumber;

View File

@@ -29,6 +29,7 @@ enum class PacketID : std::uint8_t {
PlaceTower, PlaceTower,
PlayerJoin, PlayerJoin,
PlayerLeave, PlayerLeave,
PlayerList,
PlayerLogin, PlayerLogin,
PredictCommand, PredictCommand,
SpawnTroop, SpawnTroop,
@@ -58,6 +59,7 @@ using LoggingSuccessPacket = PacketMessage<pdata::LoggingSuccess, PacketID::Logg
using PlaceTowerPacket = PacketMessage<pdata::PlaceTower, PacketID::PlaceTower>; using PlaceTowerPacket = PacketMessage<pdata::PlaceTower, PacketID::PlaceTower>;
using PlayerJoinPacket = PacketMessage<pdata::PlayerJoin, PacketID::PlayerJoin>; using PlayerJoinPacket = PacketMessage<pdata::PlayerJoin, PacketID::PlayerJoin>;
using PlayerLeavePacket = PacketMessage<pdata::PlayerLeave, PacketID::PlayerLeave>; using PlayerLeavePacket = PacketMessage<pdata::PlayerLeave, PacketID::PlayerLeave>;
using PlayerListPacket = PacketMessage<pdata::PlayerList, PacketID::PlayerList>;
using PlayerLoginPacket = PacketMessage<pdata::PlayerLogin, PacketID::PlayerLogin>; using PlayerLoginPacket = PacketMessage<pdata::PlayerLogin, PacketID::PlayerLogin>;
using PredictCommandPacket = PacketMessage<pdata::PredictCommand, PacketID::PredictCommand>; using PredictCommandPacket = PacketMessage<pdata::PredictCommand, PacketID::PredictCommand>;
using SpawnTroopPacket = PacketMessage<pdata::SpawnTroop, PacketID::SpawnTroop>; using SpawnTroopPacket = PacketMessage<pdata::SpawnTroop, PacketID::SpawnTroop>;
@@ -66,11 +68,11 @@ using WorldDataPacket = PacketMessage<pdata::WorldData, PacketID::WorldData>;
} // namespace packets } // namespace packets
using AllPackets = using AllPackets = std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket,
std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket, packets::KeepAlivePacket, packets::KeepAlivePacket, packets::LockStepRequestPacket, packets::LockStepResponsePacket, packets::LockStepsPacket,
packets::LockStepRequestPacket, packets::LockStepResponsePacket, packets::LockStepsPacket, packets::LoggingSuccessPacket, packets::LoggingSuccessPacket, packets::PlaceTowerPacket, packets::PlayerJoinPacket, packets::PlayerLeavePacket,
packets::PlaceTowerPacket, packets::PlayerJoinPacket, packets::PlayerLeavePacket, packets::PlayerLoginPacket, packets::PlayerListPacket, packets::PlayerLoginPacket, packets::PredictCommandPacket, packets::SpawnTroopPacket,
packets::PredictCommandPacket, packets::SpawnTroopPacket, packets::WorldHeaderPacket, packets::WorldDataPacket>; packets::WorldHeaderPacket, packets::WorldDataPacket>;
class PacketHandler : public sp::GenericHandler<AllPackets> { class PacketHandler : public sp::GenericHandler<AllPackets> {
public: public:

View File

@@ -8,11 +8,17 @@ namespace server {
ConnectionHandler::ConnectionHandler(IServerSocket& a_Server, PlayerID a_Player) : m_Server(a_Server), m_Player(a_Player) {} 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) { 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}; protocol::PlayerInfo pInfo{m_Player, a_Packet->m_PlayerName};
m_Server.OnPlayerJoin(m_Player, pInfo);
// TODO
std::vector<protocol::PlayerInfo> 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)); 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) { void ConnectionHandler::Handle(const protocol::packets::DisconnectPacket& a_Packet) {