From 4905f618addf0a20fa65edf2a8bbc4da727fba3c Mon Sep 17 00:00:00 2001 From: lotodore Date: Mon, 8 Dec 2008 23:06:11 +0000 Subject: [PATCH] Allow kicking of computer players. --- src/net/common/servergamethread.cpp | 48 ++++++++++++++++++++++++++++- src/net/servergamethread.h | 2 ++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/net/common/servergamethread.cpp b/src/net/common/servergamethread.cpp index e069c3b7..893b4e2d 100644 --- a/src/net/common/servergamethread.cpp +++ b/src/net/common/servergamethread.cpp @@ -310,6 +310,12 @@ ServerGameThread::InternalKickPlayer(unsigned playerId) // Only kick if the player was found. if (tmpSession.sessionData.get()) MoveSessionToLobby(tmpSession, NTF_NET_REMOVED_KICKED); + else + { + boost::shared_ptr tmpData(RemoveComputerPlayer(playerId)); + if (tmpData) + RemovePlayerData(tmpData, NTF_NET_REMOVED_KICKED); + } } void @@ -507,6 +513,45 @@ ServerGameThread::AddComputerPlayer(boost::shared_ptr player) GetLobbyThread().AddComputerPlayer(player); } +boost::shared_ptr +ServerGameThread::RemoveComputerPlayer(unsigned playerId) +{ + boost::shared_ptr tmpPlayer; + { + boost::mutex::scoped_lock lock(m_computerPlayerListMutex); + PlayerDataList::iterator i = m_computerPlayerList.begin(); + PlayerDataList::iterator end = m_computerPlayerList.end(); + while (i != end) + { + if ((*i)->GetUniqueId() == playerId) + { + tmpPlayer = *i; + m_computerPlayerList.erase(i); + break; + } + ++i; + } + } + GetLobbyThread().RemoveComputerPlayer(tmpPlayer); + return tmpPlayer; +} + +bool +ServerGameThread::IsComputerPlayerActive(unsigned playerId) const +{ + bool retVal = false; + boost::mutex::scoped_lock lock(m_computerPlayerListMutex); + PlayerDataList::const_iterator i = m_computerPlayerList.begin(); + PlayerDataList::const_iterator end = m_computerPlayerList.end(); + while (i != end) + { + if ((*i)->GetUniqueId() == playerId) + retVal = true; + ++i; + } + return retVal; +} + void ServerGameThread::ResetComputerPlayerList() { @@ -613,7 +658,8 @@ ServerGameThread::RemoveDisconnectedPlayers() while (i != end) { boost::shared_ptr tmpPlayer = *i; - if (!GetSessionManager().IsPlayerConnected(tmpPlayer->getMyUniqueID()) && tmpPlayer->getMyType() == PLAYER_TYPE_HUMAN) + if ((tmpPlayer->getMyType() == PLAYER_TYPE_HUMAN && !GetSessionManager().IsPlayerConnected(tmpPlayer->getMyUniqueID())) + || (tmpPlayer->getMyType() == PLAYER_TYPE_COMPUTER && !IsComputerPlayerActive(tmpPlayer->getMyUniqueID()))) { // Setting player cash to 0 will deactivate the player. tmpPlayer->setMyCash(0); diff --git a/src/net/servergamethread.h b/src/net/servergamethread.h index 2518a553..e4bf8cfd 100644 --- a/src/net/servergamethread.h +++ b/src/net/servergamethread.h @@ -96,6 +96,8 @@ protected: PlayerDataList GetFullPlayerDataList() const; void AddComputerPlayer(boost::shared_ptr player); + boost::shared_ptr RemoveComputerPlayer(unsigned playerId); + bool IsComputerPlayerActive(unsigned playerId) const; void ResetComputerPlayerList(); void GracefulRemoveSession(SessionWrapper session, int reason);