From d82c9a510dc0949399a33ee43be5a6f04d436932 Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 29 Sep 2013 13:15:37 +0200 Subject: [PATCH] Add spectator list during the game (when lobby messages are disabled). --- src/engine/game.cpp | 15 +++++++----- src/gamedata.h | 1 + src/net/clientthread.h | 3 +++ src/net/common/clientstate.cpp | 10 ++++++-- src/net/common/clientthread.cpp | 42 +++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/engine/game.cpp b/src/engine/game.cpp index e1582182..8d1fe4dc 100755 --- a/src/engine/game.cpp +++ b/src/engine/game.cpp @@ -220,8 +220,9 @@ void Game::startHand() boost::shared_ptr Game::getPlayerByUniqueId(unsigned id) { boost::shared_ptr tmpPlayer; - PlayerListIterator i = getSeatsList()->begin(); - PlayerListIterator end = getSeatsList()->end(); + PlayerList tmpList = getSeatsList(); + PlayerListIterator i = tmpList->begin(); + PlayerListIterator end = tmpList->end(); while (i != end) { if ((*i)->getMyUniqueID() == id) { tmpPlayer = *i; @@ -235,8 +236,9 @@ boost::shared_ptr Game::getPlayerByUniqueId(unsigned id) boost::shared_ptr Game::getPlayerByNumber(int number) { boost::shared_ptr tmpPlayer; - PlayerListIterator i = getSeatsList()->begin(); - PlayerListIterator end = getSeatsList()->end(); + PlayerList tmpList = getSeatsList(); + PlayerListIterator i = tmpList->begin(); + PlayerListIterator end = tmpList->end(); while (i != end) { if ((*i)->getMyID() == number) { tmpPlayer = *i; @@ -258,8 +260,9 @@ boost::shared_ptr Game::getCurrentPlayer() boost::shared_ptr Game::getPlayerByName(const std::string &name) { boost::shared_ptr tmpPlayer; - PlayerListIterator i = getSeatsList()->begin(); - PlayerListIterator end = getSeatsList()->end(); + PlayerList tmpList = getSeatsList(); + PlayerListIterator i = tmpList->begin(); + PlayerListIterator end = tmpList->end(); while (i != end) { if ((*i)->getMyName() == name) { tmpPlayer = *i; diff --git a/src/gamedata.h b/src/gamedata.h index a5e3e0d5..cb5a463a 100644 --- a/src/gamedata.h +++ b/src/gamedata.h @@ -102,6 +102,7 @@ struct GameInfo { unsigned adminPlayerId; PlayerIdList players; PlayerIdList spectators; + PlayerIdList spectatorsDuringGame; bool isPasswordProtected; }; diff --git a/src/net/clientthread.h b/src/net/clientthread.h index ae16614a..39de5407 100644 --- a/src/net/clientthread.h +++ b/src/net/clientthread.h @@ -218,6 +218,9 @@ protected: void ModifyGameInfoRemovePlayer(unsigned gameId, unsigned playerId); void ModifyGameInfoAddSpectator(unsigned gameId, unsigned playerId); void ModifyGameInfoRemoveSpectator(unsigned gameId, unsigned playerId); + void ModifyGameInfoClearSpectatorsDuringGame(); + void ModifyGameInfoAddSpectatorDuringGame(unsigned playerId); + void ModifyGameInfoRemoveSpectatorDuringGame(unsigned playerId, int removeReason); void ClearGameInfoMap(); void StartPetition(unsigned petitionId, unsigned proposingPlayerId, unsigned kickPlayerId, int timeoutSec, int numVotesToKick); diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index 25520963..a22620ce 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -631,7 +631,12 @@ AbstractClientStateReceiving::HandlePacket(boost::shared_ptr clien } else if (tmpPacket->GetMsg()->messagetype() == PokerTHMessage::Type_GameSpectatorJoinedMessage) { // Another spectator joined the network game. const GameSpectatorJoinedMessage &netSpectatorJoined = tmpPacket->GetMsg()->gamespectatorjoinedmessage(); - client->GetCallback().SignalNetClientSpectatorJoined(netSpectatorJoined.playerid(), client->GetPlayerName(netSpectatorJoined.playerid())); + // Request player info if needed. + PlayerInfo info; + if (!client->GetCachedPlayerInfo(netSpectatorJoined.playerid(), info)) { + client->RequestPlayerInfo(netSpectatorJoined.playerid()); + } + client->ModifyGameInfoAddSpectatorDuringGame(netSpectatorJoined.playerid()); } else if (tmpPacket->GetMsg()->messagetype() == PokerTHMessage::Type_GameSpectatorLeftMessage) { // A spectator left the network game. const GameSpectatorLeftMessage &netSpectatorLeft = tmpPacket->GetMsg()->gamespectatorleftmessage(); @@ -645,7 +650,7 @@ AbstractClientStateReceiving::HandlePacket(boost::shared_ptr clien removeReason = NTF_NET_REMOVED_ON_REQUEST; break; } - client->GetCallback().SignalNetClientSpectatorLeft(netSpectatorLeft.playerid(), client->GetPlayerName(netSpectatorLeft.playerid()), removeReason); + client->ModifyGameInfoRemoveSpectatorDuringGame(netSpectatorLeft.playerid(), removeReason); } else if (tmpPacket->GetMsg()->messagetype() == PokerTHMessage::Type_TimeoutWarningMessage) { const TimeoutWarningMessage &tmpTimeout = tmpPacket->GetMsg()->timeoutwarningmessage(); client->GetCallback().SignalNetClientShowTimeoutDialog((NetTimeoutReason)tmpTimeout.timeoutreason(), tmpTimeout.remainingseconds()); @@ -1252,6 +1257,7 @@ ClientStateWaitJoin::InternalHandlePacket(boost::shared_ptr client GameData tmpData; NetPacket::GetGameData(netJoinAck.gameinfo(), tmpData); client->SetGameData(tmpData); + client->ModifyGameInfoClearSpectatorsDuringGame(); // Player number is 0 on init. Will be set when the game starts. boost::shared_ptr playerData( diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index e396fbd0..775bce70 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -1454,6 +1454,48 @@ ClientThread::ModifyGameInfoRemoveSpectator(unsigned gameId, unsigned playerId) GetCallback().SignalNetClientGameListSpectatorLeft(gameId, playerId); } +void +ClientThread::ModifyGameInfoClearSpectatorsDuringGame() +{ + boost::mutex::scoped_lock lock(m_gameInfoMapMutex); + GameInfoMap::iterator pos = m_gameInfoMap.find(GetGameId()); + if (pos != m_gameInfoMap.end()) { + pos->second.spectatorsDuringGame.clear(); + } +} + +void +ClientThread::ModifyGameInfoAddSpectatorDuringGame(unsigned playerId) +{ + bool spectatorAdded = false; + { + boost::mutex::scoped_lock lock(m_gameInfoMapMutex); + GameInfoMap::iterator pos = m_gameInfoMap.find(GetGameId()); + if (pos != m_gameInfoMap.end()) { + pos->second.spectatorsDuringGame.push_back(playerId); + spectatorAdded = true; + } + } + if (spectatorAdded) + GetCallback().SignalNetClientSpectatorJoined(playerId, GetPlayerName(playerId)); +} + +void +ClientThread::ModifyGameInfoRemoveSpectatorDuringGame(unsigned playerId, int removeReason) +{ + bool spectatorRemoved = false; + { + boost::mutex::scoped_lock lock(m_gameInfoMapMutex); + GameInfoMap::iterator pos = m_gameInfoMap.find(GetGameId()); + if (pos != m_gameInfoMap.end()) { + pos->second.spectatorsDuringGame.remove(playerId); + spectatorRemoved = true; + } + } + if (spectatorRemoved) + GetCallback().SignalNetClientSpectatorLeft(playerId, GetPlayerName(playerId), removeReason); +} + void ClientThread::ClearGameInfoMap() {