feat: close server when game finished

This commit is contained in:
2021-12-19 11:57:47 +01:00
parent 409268b604
commit 36a1ab0572
5 changed files with 44 additions and 22 deletions

View File

@@ -19,16 +19,16 @@ class Client {
private: private:
render::Renderer* m_Renderer; render::Renderer* m_Renderer;
ClientConnexion m_Connexion; ClientConnexion m_Connexion;
ClientGame m_Game; std::unique_ptr<ClientGame> m_Game;
bool m_Connected; bool m_Connected;
public: public:
Client(render::Renderer* renderer) : m_Renderer(renderer), m_Game(this), m_Connected(false) {} Client(render::Renderer* renderer) : m_Renderer(renderer), m_Game(std::make_unique<ClientGame>(this)), m_Connected(false) {}
const ClientGame& getGame() const { return m_Game; } const ClientGame& getGame() const { return *m_Game; }
const ClientConnexion& getConnexion() const { return m_Connexion; } const ClientConnexion& getConnexion() const { return m_Connexion; }
render::Renderer* getRenderer() const { return m_Renderer; } render::Renderer* getRenderer() const { return m_Renderer; }
ClientGame& getGame() { return m_Game; } ClientGame& getGame() { return *m_Game; }
ClientConnexion& getConnexion() { return m_Connexion; } ClientConnexion& getConnexion() { return m_Connexion; }
void tick(std::uint64_t delta); void tick(std::uint64_t delta);
@@ -45,6 +45,8 @@ public:
void placeTower(game::TowerType type, const glm::vec2& position); void placeTower(game::TowerType type, const glm::vec2& position);
void upgradeTower(game::TowerID tower, game::TowerLevel level); void upgradeTower(game::TowerID tower, game::TowerLevel level);
void removeTower(game::TowerID tower); void removeTower(game::TowerID tower);
private:
void reset();
}; };
} // namespace client } // namespace client

View File

@@ -61,12 +61,11 @@ private:
bool m_ServerRunning; bool m_ServerRunning;
public: public:
Server(const std::string& worldFilePath); Server(const std::string& worldFilePath);
virtual ~Server() {} virtual ~Server();
bool start(std::uint16_t port); bool start(std::uint16_t port);
void stop(); void stop(); // force the server to stop
void close(); // at the end of a game
void lauchGame();
void removeConnexion(std::uint8_t connexionID); void removeConnexion(std::uint8_t connexionID);
@@ -74,6 +73,8 @@ public:
float getTPS() const { return m_TickCounter.getTPS(); } float getTPS() const { return m_TickCounter.getTPS(); }
bool isRunning() { return m_ServerRunning; }
const ServerGame& getGame() const { return m_Game; } const ServerGame& getGame() const { return m_Game; }
ServerGame& getGame() { return m_Game; } ServerGame& getGame() { return m_Game; }
@@ -88,6 +89,7 @@ private:
void accept(); void accept();
void updateSockets(); void updateSockets();
void clean();
void startThread(); void startThread();
void stopThread(); void stopThread();
void tick(std::uint64_t delta); void tick(std::uint64_t delta);

View File

@@ -40,13 +40,19 @@ void Client::tick(std::uint64_t delta) {
m_Connected = m_Connexion.updateSocket(); m_Connected = m_Connexion.updateSocket();
if (!m_Connected) { if (!m_Connected) {
std::cout << "Disconnected ! (Reason : " << m_Connexion.getDisconnectReason() << ")\n"; std::cout << "Disconnected ! (Reason : " << m_Connexion.getDisconnectReason() << ")\n";
reset();
} else { } else {
m_Game.tick(delta); m_Game->tick(delta);
} }
} }
void Client::render() { void Client::render() {
m_Game.renderWorld(); m_Game->renderWorld();
}
void Client::reset() {
m_Game.reset(0);
m_Game = std::make_unique<ClientGame>(this);
} }
void Client::sendMobs(const std::vector<protocol::MobSend>& mobSends) { void Client::sendMobs(const std::vector<protocol::MobSend>& mobSends) {

View File

@@ -9,8 +9,9 @@ Server::Server(const std::string& worldFilePath) : m_ServerRunning(false) {
m_Game.getWorld()->loadMapFromFile(worldFilePath); m_Game.getWorld()->loadMapFromFile(worldFilePath);
} }
void Server::lauchGame() { Server::~Server() {
m_Game.startGame(); if (m_Thread.joinable())
m_Thread.join();
} }
void Server::startThread() { void Server::startThread() {
@@ -29,13 +30,16 @@ void Server::startThread() {
} }
} }
clean();
}); });
} }
void Server::close() {
stopThread();
}
void Server::stopThread() { void Server::stopThread() {
m_ServerRunning = false; m_ServerRunning = false;
if (m_Thread.joinable())
m_Thread.join();
} }
bool Server::start(std::uint16_t port) { bool Server::start(std::uint16_t port) {
@@ -54,19 +58,24 @@ bool Server::start(std::uint16_t port) {
return true; return true;
} }
void Server::stop() { void Server::clean() {
if (!m_ServerRunning)
return;
stopThread();
protocol::DisconnectPacket packet("Server closed");
broadcastPacket(&packet);
m_Listener.close(); m_Listener.close();
m_Listener.destroy(); m_Listener.destroy();
m_Connections.clear(); m_Connections.clear();
getPlayers().clear(); getPlayers().clear();
std::cout << "Server successfully stopped !\n";
}
void Server::stop() {
if (!m_ServerRunning)
return;
protocol::DisconnectPacket packet("Server closed");
broadcastPacket(&packet);
stopThread();
} }
void Server::tick(std::uint64_t delta) { void Server::tick(std::uint64_t delta) {

View File

@@ -95,6 +95,9 @@ void ServerGame::OnGameClose() {
// Disconnect clients // Disconnect clients
protocol::DisconnectPacket packet("Game finished"); protocol::DisconnectPacket packet("Game finished");
m_Server->broadcastPacket(&packet); m_Server->broadcastPacket(&packet);
// Closing server
m_Server->close();
} }
} // namespace game } // namespace game