diff --git a/src/net/common/servergamestate.cpp b/src/net/common/servergamestate.cpp index 7b9fa67b..a807c738 100644 --- a/src/net/common/servergamestate.cpp +++ b/src/net/common/servergamestate.cpp @@ -460,6 +460,15 @@ AbstractServerGameStateReceiving::AcceptNewSession(boost::shared_ptr ++player_i; } + // Send notifications for connected spectators to client. + PlayerDataList tmpSpectatorList(server->GetSessionManager().GetSpectatorDataList()); + PlayerDataList::iterator spectator_i = tmpSpectatorList.begin(); + PlayerDataList::iterator spectator_end = tmpSpectatorList.end(); + while (spectator_i != spectator_end) { + server->GetLobbyThread().GetSender().Send(session, CreateNetPacketSpectatorJoined(server->GetId(), *(*spectator_i))); + ++spectator_i; + } + // Send "Player Joined"/"Spectator Joined" to other fully connected clients. if (spectateOnly) { server->SendToAllPlayers(CreateNetPacketSpectatorJoined(server->GetId(), *session->GetPlayerData()), SessionData::Game | SessionData::Spectating | SessionData::SpectatorWaiting); diff --git a/src/net/common/sessionmanager.cpp b/src/net/common/sessionmanager.cpp index 56a7e7b0..bd8b65eb 100644 --- a/src/net/common/sessionmanager.cpp +++ b/src/net/common/sessionmanager.cpp @@ -161,6 +161,28 @@ SessionManager::GetPlayerDataList() const return playerList; } +PlayerDataList +SessionManager::GetSpectatorDataList() const +{ + PlayerDataList spectatorList; + boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); + + SessionMap::const_iterator session_i = m_sessionMap.begin(); + SessionMap::const_iterator session_end = m_sessionMap.end(); + + while (session_i != session_end) { + // Get all spectators of the game. + if (session_i->second->GetState() == SessionData::Spectating || session_i->second->GetState() == SessionData::SpectatorWaiting) { + boost::shared_ptr tmpPlayer(session_i->second->GetPlayerData()); + if (!tmpPlayer.get() || tmpPlayer->GetName().empty()) + throw ServerException(__FILE__, __LINE__, ERR_NET_INVALID_SESSION, 0); + spectatorList.push_back(tmpPlayer); + } + ++session_i; + } + return spectatorList; +} + PlayerIdList SessionManager::GetPlayerIdList(int state) const { diff --git a/src/net/sessionmanager.h b/src/net/sessionmanager.h index ac3cd3df..8e90a544 100644 --- a/src/net/sessionmanager.h +++ b/src/net/sessionmanager.h @@ -59,6 +59,7 @@ public: boost::shared_ptr GetSessionByUniquePlayerId(unsigned uniqueId, bool initSessions = false) const; PlayerDataList GetPlayerDataList() const; + PlayerDataList GetSpectatorDataList() const; PlayerIdList GetPlayerIdList(int state) const; bool IsPlayerConnected(const std::string &playerName) const; bool IsPlayerConnected(unsigned uniqueId) const;