From eb571a86403131d63595cc110cbad77ab49b879c Mon Sep 17 00:00:00 2001 From: lotodore Date: Mon, 2 Sep 2013 00:37:02 +0200 Subject: [PATCH] Automatically close games if only spectators are left. --- src/net/common/serverlobbythread.cpp | 2 +- src/net/common/sessionmanager.cpp | 30 +++++++++++++++++++--------- src/net/sessionmanager.h | 7 +++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 68b7c9f3..21a16d46 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -1827,7 +1827,7 @@ ServerLobbyThread::TimerRemoveGame(const boost::system::error_code &ec) GameMap::iterator next = i; ++next; boost::shared_ptr tmpGame = i->second; - if (!tmpGame->GetSessionManager().HasSessions()) + if (!tmpGame->GetSessionManager().HasSessionWithState(SessionData::Game)) InternalRemoveGame(tmpGame); // This will delete the game. i = next; } diff --git a/src/net/common/sessionmanager.cpp b/src/net/common/sessionmanager.cpp index c77ee161..4d711318 100644 --- a/src/net/common/sessionmanager.cpp +++ b/src/net/common/sessionmanager.cpp @@ -46,13 +46,6 @@ SessionManager::~SessionManager() Clear(); } -bool -SessionManager::HasSessions() const -{ - boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); - return !m_sessionMap.empty(); -} - void SessionManager::AddSession(boost::shared_ptr session) { @@ -295,14 +288,14 @@ SessionManager::Clear() } unsigned -SessionManager::GetRawSessionCount() +SessionManager::GetRawSessionCount() const { boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); return (unsigned)m_sessionMap.size(); } unsigned -SessionManager::GetSessionCountWithState(int state) +SessionManager::GetSessionCountWithState(int state) const { unsigned counter = 0; boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); @@ -318,6 +311,25 @@ SessionManager::GetSessionCountWithState(int state) return counter; } +bool +SessionManager::HasSessionWithState(int state) const +{ + bool retVal = false; + boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); + + SessionMap::const_iterator i = m_sessionMap.begin(); + SessionMap::const_iterator end = m_sessionMap.end(); + + while (i != end) { + if ((i->second->GetState() & state) != 0) { + retVal = true; + break; + } + ++i; + } + return retVal; +} + void SessionManager::SendToAllSessions(SenderHelper &sender, boost::shared_ptr packet, int state) { diff --git a/src/net/sessionmanager.h b/src/net/sessionmanager.h index 81ff5916..ac3cd3df 100644 --- a/src/net/sessionmanager.h +++ b/src/net/sessionmanager.h @@ -50,8 +50,6 @@ public: SessionManager(); virtual ~SessionManager(); - bool HasSessions() const; - void AddSession(boost::shared_ptr sessionData); void SetSessionPlayerData(SessionId session, boost::shared_ptr playerData); bool RemoveSession(SessionId session); @@ -72,8 +70,9 @@ public: void ResetAllReadyFlags(); void Clear(); - unsigned GetRawSessionCount(); - unsigned GetSessionCountWithState(int state); + unsigned GetRawSessionCount() const; + unsigned GetSessionCountWithState(int state) const; + bool HasSessionWithState(int state) const; void SendToAllSessions(SenderHelper &sender, boost::shared_ptr packet, int state); void SendLobbyMsgToAllSessions(SenderHelper &sender, boost::shared_ptr packet, int state);