prevent player from joining mid game
This commit is contained in:
@@ -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) {
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user