PlayerManager

This commit is contained in:
2025-08-20 12:18:44 +02:00
parent bd56fb0646
commit a02cb2b309
11 changed files with 103 additions and 65 deletions

View File

@@ -1,24 +0,0 @@
#pragma once
#include <server/Server.h>
namespace td {
namespace server {
class IServerSocket;
class ConnectionHandler : public protocol::PacketHandler {
private:
IServerSocket& m_Server;
PlayerID m_Player;
public:
ConnectionHandler(IServerSocket& a_Server, PlayerID a_Player);
~ConnectionHandler() = default;
virtual void Handle(const protocol::packets::PlayerLoginPacket& a_Packet) override;
virtual void Handle(const protocol::packets::DisconnectPacket& a_Packet) override;
};
} // namespace server
} // namespace td

View File

@@ -12,7 +12,7 @@ class IServerSocket {
using PlayerPacketHandlerType = std::unique_ptr<protocol::PacketHandler>(PlayerID);
using PlayerPacketHandler = std::function<PlayerPacketHandlerType>;
utils::Signal<PlayerID, const protocol::PlayerInfo&> OnPlayerJoin;
utils::Signal<PlayerID, const PlayerInfo&> OnPlayerJoin;
utils::Signal<PlayerID> OnPlayerLeave;
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;

View File

@@ -0,0 +1,39 @@
#pragma once
#include <td/protocol/packet/Packets.h>
namespace td {
namespace server {
class IServerSocket;
class PlayerManager {
private:
std::map<PlayerID, PlayerInfo> m_Players;
std::shared_ptr<IServerSocket> m_Socket;
public:
PlayerManager(const std::shared_ptr<IServerSocket>& a_Socket);
~PlayerManager();
void RemovePlayer(PlayerID a_Player);
PlayerInfo GetPlayer(PlayerID a_Player);
private:
class ConnectionHandler : public protocol::PacketHandler {
private:
std::map<PlayerID, PlayerInfo>& m_Players;
IServerSocket& m_Socket;
PlayerID m_Player;
public:
ConnectionHandler(std::map<PlayerID, PlayerInfo>& a_Players, IServerSocket& a_Socket, PlayerID a_Player);
~ConnectionHandler() = default;
virtual void Handle(const protocol::packets::PlayerLoginPacket& a_Packet) override;
virtual void Handle(const protocol::packets::DisconnectPacket& a_Packet) override;
};
};
} // namespace server
} // namespace td

View File

@@ -2,6 +2,7 @@
#include <server/IServerSocket.h>
#include <td/common/StateMachine.h>
#include <server/PlayerManager.h>
namespace td {
namespace server {
@@ -11,6 +12,7 @@ class ServerState;
class Server : public StateMachine<Server, void, float> {
private:
std::shared_ptr<IServerSocket> m_Socket;
PlayerManager m_Players;
float m_LastMspt;
public:

View File

@@ -10,7 +10,7 @@ class ServerState : public Server::State, private utils::SlotGuard {
public:
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
virtual void Update(float a_Delta) = 0;
virtual void OnPlayerJoin(PlayerID a_Id, const td::protocol::PlayerInfo& a_Info) {}
virtual void OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) {}
virtual void OnPlayerLeave(PlayerID a_Id) {}
ServerState(Server& a_Server);