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

@@ -163,10 +163,14 @@ void Server::BroadcastPacket(const protocol::Packet* packet) {
} }
void Server::RemoveConnexion(std::uint8_t connexionID) { void Server::RemoveConnexion(std::uint8_t connexionID) {
std::string playerName = GetGame().GetPlayerById(connexionID)->GetName(); m_Connections.erase(connexionID);
td::game::Player* player = GetGame().GetPlayerById(connexionID);
SAFE_CHECK(player);
std::string playerName = player->GetName();
GetGame().RemovePlayer(connexionID); GetGame().RemovePlayer(connexionID);
m_Connections.erase(connexionID);
m_Lobby.OnPlayerLeave(connexionID); m_Lobby.OnPlayerLeave(connexionID);
utils::LOG(utils::format("\t[%s] left !", playerName.c_str())); utils::LOG(utils::format("\t[%s] left !", playerName.c_str()));
@@ -176,8 +180,6 @@ void Server::RemoveConnexion(std::uint8_t connexionID) {
void Server::OnPlayerJoin(std::uint8_t id) { void Server::OnPlayerJoin(std::uint8_t id) {
m_Lobby.OnPlayerJoin(id); m_Lobby.OnPlayerJoin(id);
GetPlayers().insert({ id, game::Player{id} });
} }
void Server::OnPlayerLeave(std::uint8_t id) { void Server::OnPlayerLeave(std::uint8_t id) {

View File

@@ -94,9 +94,22 @@ void ServerConnexion::SendKeepAlive() {
void ServerConnexion::HandlePacket(const protocol::PlayerLoginPacket* packet) { 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()); m_Player->SetName(packet->GetPlayerName());
@@ -176,7 +189,6 @@ void ServerConnexion::HandlePacket(const protocol::DisconnectPacket* packet) {
void ServerConnexion::SetServer(Server* server) { void ServerConnexion::SetServer(Server* server) {
m_Server = server; m_Server = server;
m_Player = &m_Server->GetPlayers().at(m_ID);
InitConnection(); InitConnection();
SendKeepAlive(); SendKeepAlive();
} }