prevent player from joining mid game

This commit is contained in:
2023-09-02 20:27:26 +02:00
parent bb76e9493f
commit 09bba12814
2 changed files with 21 additions and 7 deletions

View File

@@ -94,9 +94,22 @@ void ServerConnexion::SendKeepAlive() {
void ServerConnexion::HandlePacket(const protocol::PlayerLoginPacket* packet) {
SAFE_CHECK(m_Player->GetName().empty() && !packet->GetPlayerName().empty());
if (m_Server->GetGame().GetGameState() != game::GameState::Lobby) {
protocol::DisconnectPacket packet("Cannot join during game");
SendPacket(&packet);
CloseConnection();
return;
}
SAFE_CHECK(!packet->GetPlayerName().empty());
if (!m_Player) { // player does not exist yet
auto playerPos = m_Server->GetPlayers().insert({ m_ID, game::Player{m_ID} });
m_Player = &playerPos.first->second;
}
SAFE_CHECK(m_Player->GetName().empty());
m_Player->SetName(packet->GetPlayerName());
@@ -176,7 +189,6 @@ void ServerConnexion::HandlePacket(const protocol::DisconnectPacket* packet) {
void ServerConnexion::SetServer(Server* server) {
m_Server = server;
m_Player = &m_Server->GetPlayers().at(m_ID);
InitConnection();
SendKeepAlive();
}