add StateMachine

This commit is contained in:
2025-08-09 11:23:17 +02:00
parent ac3e949323
commit cba790f804
23 changed files with 174 additions and 244 deletions

View File

@@ -1,36 +0,0 @@
#pragma once
#include <server/IServerSocket.h>
#include <td/misc/SlotGuard.h>
namespace td {
namespace server {
class Server;
class IServerState : 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) = 0;
virtual void OnPlayerLeave(PlayerID a_Id) = 0;
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;
void SetServer(Server* a_Server);
friend class Server;
};
} // namespace server
} // namespace td

View File

@@ -1,30 +1,21 @@
#pragma once
#include <memory>
#include <server/IServerSocket.h>
#include <server/IServerState.h>
#include <chrono>
#include <td/common/StateMachine.h>
namespace td {
namespace server {
class Server {
class ServerState;
class Server : public StateMachine<Server, void, float> {
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), m_LastTime(std::chrono::system_clock::now()) {}
Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {}
void Update();
void UpdateState(const std::shared_ptr<IServerState>& a_State);
private:
void Update(float a_Delta);
friend class IServerState;
friend class ServerState;
};
} // namespace server

View File

@@ -0,0 +1,23 @@
#pragma once
#include <server/Server.h>
#include <td/misc/SlotGuard.h>
namespace td {
namespace server {
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;
ServerState(Server& a_Server);
virtual ~ServerState();
protected:
void SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet);
void BroadcastPacket(const protocol::PacketBase& a_Packet);
};
} // namespace server
} // namespace td

View File

@@ -1,11 +1,11 @@
#pragma once
#include <server/IServerState.h>
#include <server/ServerState.h>
namespace td {
namespace server {
class EndGameState : public IServerState {
class EndGameState : public ServerState {
private:
/* data */
public:

View File

@@ -1,6 +1,6 @@
#pragma once
#include <server/IServerState.h>
#include <server/ServerState.h>
#include <td/game/World.h>
#include <td/simulation/ServerSimulation.h>
@@ -9,23 +9,18 @@ namespace server {
class GameStateHandler;
class GameState : public IServerState {
class GameState : public ServerState {
private:
std::shared_ptr<game::World> m_World;
sim::ServerSimulation m_Simulation;
float m_Time;
public:
GameState(const std::shared_ptr<game::World>& a_World);
GameState(Server& a_Server, 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;
};

View File

@@ -1,22 +1,20 @@
#pragma once
#include <server/IServerState.h>
#include <server/ServerState.h>
namespace td {
namespace server {
// this class is temporary useless
class LobbyState : public IServerState {
class LobbyState : public ServerState {
private:
std::shared_ptr<game::World> m_World;
public:
LobbyState(const std::shared_ptr<game::World>& a_World) : m_World(a_World) {}
LobbyState(Server& a_Server, const std::shared_ptr<game::World>& a_World);
~LobbyState() {}
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;
};
} // namespace server