begin client-server

This commit is contained in:
2025-08-06 20:10:56 +02:00
parent 89213e9a97
commit c813c49707
26 changed files with 264 additions and 188 deletions

View File

@@ -14,6 +14,7 @@ class IServerSocket {
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;
void Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet);
void Broadcast(const protocol::PacketBase& a_Packet);
IServerSocket() {}
virtual ~IServerSocket() {}

View File

@@ -9,10 +9,6 @@ namespace server {
class Server;
class IServerState : private utils::SlotGuard {
protected:
void SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet);
void SetNewState(const std::shared_ptr<IServerState>& a_NewState);
public:
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
virtual void Update(float a_Delta) = 0;
@@ -22,6 +18,12 @@ class IServerState : private utils::SlotGuard {
IServerState();
virtual ~IServerState();
protected:
void SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet);
void BroadcastPacket(const protocol::PacketBase& a_Packet);
void SetNewState(const std::shared_ptr<IServerState>& a_NewState);
virtual void Init() {}
private:
Server* m_Server;

View File

@@ -35,6 +35,14 @@ class PlayerIds {
m_PeerToPlayer.erase(a_PeerId);
m_PlayerToPeer.erase(playerId);
}
auto begin() {
return m_PeerToPlayer.begin();
}
auto end() {
return m_PeerToPlayer.end();
}
};
} // namespace server

View File

@@ -3,6 +3,7 @@
#include <memory>
#include <server/IServerSocket.h>
#include <server/IServerState.h>
#include <chrono>
namespace td {
namespace server {
@@ -11,19 +12,23 @@ class Server {
private:
std::shared_ptr<IServerSocket> m_Socket;
std::shared_ptr<IServerState> m_State;
std::chrono::time_point<std::chrono::system_clock> m_LastTime;
public:
Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {}
Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket), m_LastTime(std::chrono::system_clock::now()) {}
void Update(float a_Delta) {
m_State->Update(a_Delta);
}
void Update();
void UpdateState(const std::shared_ptr<IServerState>& a_State) {
m_State = a_State;
m_State->SetServer(this);
}
private:
void Update(float a_Delta) {
m_State->Update(a_Delta);
}
friend class IServerState;
};

View File

@@ -12,6 +12,9 @@ class FakeSocket : public IServerSocket {
utils::Signal<PeerID, const protocol::PacketBase&> OnSend;
void ConnectFakePeer(PeerID a_Peer);
void DisconnectFakePeer(PeerID a_Peer);
protected:
virtual void SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) override;
};

View File

@@ -1,21 +1,33 @@
#pragma once
#include <server/IServerState.h>
#include <td/game/World.h>
#include <td/simulation/ServerSimulation.h>
namespace td {
namespace server {
class GameState : public IServerState{
class GameStateHandler;
class GameState : public IServerState {
private:
/* data */
std::shared_ptr<game::World> m_World;
sim::ServerSimulation m_Simulation;
float m_Time;
public:
GameState(/* args */) {}
GameState(const std::shared_ptr<game::World>& a_World);
~GameState() {}
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
virtual void Update(float a_Delta) override;
virtual void OnPlayerJoin(PlayerID a_Id) override;
virtual void OnPlayerLeave(PlayerID a_Id) override;
protected:
virtual void Init() override;
friend class GameStateHandler;
};
} // namespace server

View File

@@ -0,0 +1,25 @@
#pragma once
#include <td/protocol/packet/Packets.h>
namespace td {
namespace server {
class GameState;
class GameStateHandler : public protocol::PacketHandler {
private:
GameState& m_GameState;
PlayerID m_PlayerId;
using protocol::PacketHandler::Handle;
public:
GameStateHandler(GameState& a_GameState, PlayerID a_PlayerId);
virtual void Handle(const protocol::packets::SpawnTroopPacket& a_Packet) override;
virtual void Handle(const protocol::packets::PlaceTowerPacket& a_Packet) override;
};
} // namespace server
} // namespace td

View File

@@ -5,11 +5,12 @@
namespace td {
namespace server {
// this class is temporary useless
class LobbyState : public IServerState {
private:
/* data */
std::shared_ptr<game::World> m_World;
public:
LobbyState(/* args */) {}
LobbyState(const std::shared_ptr<game::World>& a_World) : m_World(a_World) {}
~LobbyState() {}
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;