From 1d0b9fc5f2b7939239d2cb2ce9b19d07f8e22658 Mon Sep 17 00:00:00 2001 From: lotodore Date: Mon, 19 Aug 2013 22:50:56 +0200 Subject: [PATCH] Signals/callbacks when spectators join or leave. --- src/gamedata.h | 1 + src/gui/generic/serverguiwrapper.cpp | 16 +++++++ src/gui/generic/serverguiwrapper.h | 4 ++ src/gui/qt/gametable/gametableimpl.cpp | 6 +++ src/gui/qt/gametable/gametableimpl.h | 2 + src/gui/qt/gametable/log/guilog.cpp | 11 +++++ src/gui/qt/gametable/log/guilog.h | 4 ++ src/gui/qt/guiwrapper.cpp | 24 ++++++++++ src/gui/qt/guiwrapper.h | 4 ++ src/gui/qt/startwindow/startwindowimpl.cpp | 4 ++ src/gui/qt/startwindow/startwindowimpl.h | 4 ++ src/net/clientcallback.h | 4 ++ src/net/clientthread.h | 3 ++ src/net/common/clientstate.cpp | 54 +++++++++++++++++----- src/net/common/clientthread.cpp | 46 ++++++++++++++++++ 15 files changed, 176 insertions(+), 11 deletions(-) diff --git a/src/gamedata.h b/src/gamedata.h index 7ba86e5b..e1bf502b 100644 --- a/src/gamedata.h +++ b/src/gamedata.h @@ -100,6 +100,7 @@ struct GameInfo { GameMode mode; unsigned adminPlayerId; PlayerIdList players; + PlayerIdList spectators; bool isPasswordProtected; }; diff --git a/src/gui/generic/serverguiwrapper.cpp b/src/gui/generic/serverguiwrapper.cpp index 84dba5a9..3aeab28b 100644 --- a/src/gui/generic/serverguiwrapper.cpp +++ b/src/gui/generic/serverguiwrapper.cpp @@ -200,6 +200,14 @@ void ServerGuiWrapper::SignalNetClientPlayerLeft(unsigned playerId, const string { if (myClientcb) myClientcb->SignalNetClientPlayerLeft(playerId, playerName, removeReason); } +void ServerGuiWrapper::SignalNetClientSpectatorJoined(unsigned playerId, const string &playerName) +{ + if (myClientcb) myClientcb->SignalNetClientSpectatorJoined(playerId, playerName); +} +void ServerGuiWrapper::SignalNetClientSpectatorLeft(unsigned playerId, const string &playerName, int removeReason) +{ + if (myClientcb) myClientcb->SignalNetClientSpectatorLeft(playerId, playerName, removeReason); +} void ServerGuiWrapper::SignalNetClientNewGameAdmin(unsigned playerId, const string &playerName) { if (myClientcb) myClientcb->SignalNetClientNewGameAdmin(playerId, playerName); @@ -228,6 +236,14 @@ void ServerGuiWrapper::SignalNetClientGameListPlayerLeft(unsigned gameId, unsign { if (myClientcb) myClientcb->SignalNetClientGameListPlayerLeft(gameId, playerId); } +void ServerGuiWrapper::SignalNetClientGameListSpectatorJoined(unsigned gameId, unsigned playerId) +{ + if (myClientcb) myClientcb->SignalNetClientGameListSpectatorJoined(gameId, playerId); +} +void ServerGuiWrapper::SignalNetClientGameListSpectatorLeft(unsigned gameId, unsigned playerId) +{ + if (myClientcb) myClientcb->SignalNetClientGameListSpectatorLeft(gameId, playerId); +} void ServerGuiWrapper::SignalNetClientGameStart(boost::shared_ptr game) { if (myClientcb) myClientcb->SignalNetClientGameStart(game); diff --git a/src/gui/generic/serverguiwrapper.h b/src/gui/generic/serverguiwrapper.h index 343e2c75..ebf23c28 100644 --- a/src/gui/generic/serverguiwrapper.h +++ b/src/gui/generic/serverguiwrapper.h @@ -132,6 +132,8 @@ public: void SignalNetClientPlayerJoined(unsigned playerId, const std::string &playerName, bool isGameAdmin); void SignalNetClientPlayerChanged(unsigned playerId, const std::string &newPlayerName); void SignalNetClientPlayerLeft(unsigned playerId, const std::string &playerName, int removeReason); + void SignalNetClientSpectatorJoined(unsigned playerId, const std::string &playerName); + void SignalNetClientSpectatorLeft(unsigned playerId, const std::string &playerName, int removeReason); void SignalNetClientNewGameAdmin(unsigned playerId, const std::string &playerName); void SignalNetClientGameChatMsg(const std::string &playerName, const std::string &msg); void SignalNetClientLobbyChatMsg(const std::string &playerName, const std::string &msg); @@ -144,6 +146,8 @@ public: void SignalNetClientGameListUpdateAdmin(unsigned gameId, unsigned adminPlayerId); void SignalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId); void SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId); + void SignalNetClientGameListSpectatorJoined(unsigned gameId, unsigned playerId); + void SignalNetClientGameListSpectatorLeft(unsigned gameId, unsigned playerId); void SignalNetClientGameStart(boost::shared_ptr game); void SignalNetClientServerListAdd(unsigned serverId); diff --git a/src/gui/qt/gametable/gametableimpl.cpp b/src/gui/qt/gametable/gametableimpl.cpp index eb149d06..885ddec0 100755 --- a/src/gui/qt/gametable/gametableimpl.cpp +++ b/src/gui/qt/gametable/gametableimpl.cpp @@ -676,6 +676,7 @@ gameTableImpl::gameTableImpl(ConfigFile *c, QMainWindow *parent) connect(this, SIGNAL(signalEndVoteOnKick()), this, SLOT(endVoteOnKick())); connect(this, SIGNAL(signalNetClientPlayerLeft(unsigned)), this, SLOT(netClientPlayerLeft(unsigned))); + connect(this, SIGNAL(signalNetClientSpectatorLeft(unsigned)), this, SLOT(netClientSpectatorLeft(unsigned))); #ifdef GUI_800x480 connect( tabsButton, SIGNAL( clicked() ), this, SLOT( tabsButtonClicked() ) ); @@ -4299,6 +4300,11 @@ void gameTableImpl::netClientPlayerLeft(unsigned /*playerId*/) } } +void gameTableImpl::netClientSpectatorLeft(unsigned /*playerId*/) +{ + // TODO +} + void gameTableImpl::registeredUserMode() { #ifdef GUI_800x480 diff --git a/src/gui/qt/gametable/gametableimpl.h b/src/gui/qt/gametable/gametableimpl.h index b89f6f2b..1f1a00ea 100755 --- a/src/gui/qt/gametable/gametableimpl.h +++ b/src/gui/qt/gametable/gametableimpl.h @@ -166,6 +166,7 @@ signals: void signalChangeVoteOnKickButtonsState(bool showHide); void signalEndVoteOnKick(); void signalNetClientPlayerLeft(unsigned playerId); + void signalNetClientSpectatorLeft(unsigned playerId); public slots: @@ -347,6 +348,7 @@ public slots: void restoreGameTableGeometry(); void netClientPlayerLeft(unsigned playerId); + void netClientSpectatorLeft(unsigned playerId); void registeredUserMode(); void guestUserMode(); diff --git a/src/gui/qt/gametable/log/guilog.cpp b/src/gui/qt/gametable/log/guilog.cpp index 3939c021..b76f9a6d 100644 --- a/src/gui/qt/gametable/log/guilog.cpp +++ b/src/gui/qt/gametable/log/guilog.cpp @@ -58,6 +58,8 @@ guiLog::guiLog(gameTableImpl* w, ConfigFile *c) : myW(w), myConfig(c), myLogDir( connect(this, SIGNAL(signalLogPlayerLeftMsg(QString, int)), this, SLOT(logPlayerLeftMsg(QString, int))); connect(this, SIGNAL(signalLogPlayerJoinedMsg(QString)), this, SLOT(logPlayerJoinedMsg(QString))); connect(this, SIGNAL(signalLogNewGameAdminMsg(QString)), this, SLOT(logNewGameAdminMsg(QString))); + connect(this, SIGNAL(signalLogSpectatorLeftMsg(QString, int)), this, SLOT(logSpectatorLeftMsg(QString, int))); + connect(this, SIGNAL(signalLogSpectatorJoinedMsg(QString)), this, SLOT(logSpectatorJoinedMsg(QString))); connect(this, SIGNAL(signalLogPlayerWinGame(QString, int)), this, SLOT(logPlayerWinGame(QString, int))); connect(this, SIGNAL(signalFlushLogAtGame(int)), this, SLOT(flushLogAtGame(int))); connect(this, SIGNAL(signalFlushLogAtHand()), this, SLOT(flushLogAtHand())); @@ -537,6 +539,15 @@ void guiLog::logPlayerJoinedMsg(QString playerName) #endif } +void guiLog::logSpectatorLeftMsg(QString playerName, int wasKicked) +{ + // TODO +} + +void guiLog::logSpectatorJoinedMsg(QString playerName) +{ + // TODO +} void guiLog::logPlayerWinGame(QString playerName, int gameID) { diff --git a/src/gui/qt/gametable/log/guilog.h b/src/gui/qt/gametable/log/guilog.h index cdc14c74..3b7c338e 100644 --- a/src/gui/qt/gametable/log/guilog.h +++ b/src/gui/qt/gametable/log/guilog.h @@ -75,6 +75,8 @@ public slots: void logPlayerLeftMsg(QString playerName, int wasKicked); void logPlayerJoinedMsg(QString playerName); void logNewGameAdminMsg(QString playerName); + void logSpectatorLeftMsg(QString playerName, int wasKicked); + void logSpectatorJoinedMsg(QString playerName); void logPlayerWinGame(QString playerName, int gameID); void flushLogAtGame(int gameID); void flushLogAtHand(); @@ -106,6 +108,8 @@ signals: void signalLogFlipHoleCardsMsg(QString playerName, int card1, int card2, int cardsValueInt = -1, QString showHas = "shows"); void signalLogPlayerLeftMsg(QString playerName, int wasKicked); void signalLogPlayerJoinedMsg(QString playerName); + void signalLogSpectatorLeftMsg(QString playerName, int wasKicked); + void signalLogSpectatorJoinedMsg(QString playerName); void signalLogNewGameAdminMsg(QString playerName); void signalLogPlayerWinGame(QString playerName, int gameID); void signalFlushLogAtGame(int gameID); diff --git a/src/gui/qt/guiwrapper.cpp b/src/gui/qt/guiwrapper.cpp index 6af3be90..689e4d48 100644 --- a/src/gui/qt/guiwrapper.cpp +++ b/src/gui/qt/guiwrapper.cpp @@ -373,6 +373,22 @@ void GuiWrapper::SignalNetClientPlayerLeft(unsigned playerId, const string &play if (!playerName.empty() && playerName[0] != '#' && myW->isVisible()) myGuiLog->signalLogPlayerLeftMsg(tmpName, removeReason == NTF_NET_REMOVED_KICKED); } +void GuiWrapper::SignalNetClientSpectatorJoined(unsigned playerId, const string &playerName) +{ + myStartWindow->signalNetClientSpectatorJoined(playerId, QString::fromUtf8(playerName.c_str())); + if (!playerName.empty() && playerName[0] != '#' && myW->isVisible()) { + QString tmpName(QString::fromUtf8(playerName.c_str())); + myGuiLog->signalLogSpectatorJoinedMsg(tmpName); + } +} +void GuiWrapper::SignalNetClientSpectatorLeft(unsigned playerId, const string &playerName, int removeReason) +{ + QString tmpName(QString::fromUtf8(playerName.c_str())); + myStartWindow->signalNetClientSpectatorLeft(playerId, tmpName); + myW->signalNetClientSpectatorLeft(playerId); + if (!playerName.empty() && playerName[0] != '#' && myW->isVisible()) + myGuiLog->signalLogSpectatorLeftMsg(tmpName, removeReason == NTF_NET_REMOVED_KICKED); +} void GuiWrapper::SignalNetClientNewGameAdmin(unsigned playerId, const string &playerName) { @@ -405,6 +421,14 @@ void GuiWrapper::SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned pla { myStartWindow->signalNetClientGameListPlayerLeft(gameId, playerId); } +void GuiWrapper::SignalNetClientGameListSpectatorJoined(unsigned gameId, unsigned playerId) +{ + myStartWindow->signalNetClientGameListSpectatorJoined(gameId, playerId); +} +void GuiWrapper::SignalNetClientGameListSpectatorLeft(unsigned gameId, unsigned playerId) +{ + myStartWindow->signalNetClientGameListSpectatorLeft(gameId, playerId); +} void GuiWrapper::SignalNetClientGameStart(boost::shared_ptr game) { myStartWindow->signalNetClientGameStart(game); diff --git a/src/gui/qt/guiwrapper.h b/src/gui/qt/guiwrapper.h index ad8caef0..6a52e331 100644 --- a/src/gui/qt/guiwrapper.h +++ b/src/gui/qt/guiwrapper.h @@ -144,6 +144,8 @@ public: void SignalNetClientPlayerJoined(unsigned playerId, const std::string &playerName, bool isGameAdmin); void SignalNetClientPlayerChanged(unsigned playerId, const std::string &newPlayerName); void SignalNetClientPlayerLeft(unsigned playerId, const std::string &playerName, int removeReason); + void SignalNetClientSpectatorJoined(unsigned playerId, const std::string &playerName); + void SignalNetClientSpectatorLeft(unsigned playerId, const std::string &playerName, int removeReason); void SignalNetClientNewGameAdmin(unsigned playerId, const std::string &playerName); void SignalNetClientGameChatMsg(const std::string &playerName, const std::string &msg); void SignalNetClientLobbyChatMsg(const std::string &playerName, const std::string &msg); @@ -159,6 +161,8 @@ public: void SignalNetClientGameListUpdateAdmin(unsigned gameId, unsigned adminPlayerId); void SignalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId); void SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId); + void SignalNetClientGameListSpectatorJoined(unsigned gameId, unsigned playerId); + void SignalNetClientGameListSpectatorLeft(unsigned gameId, unsigned playerId); void SignalNetClientGameStart(boost::shared_ptr game); diff --git a/src/gui/qt/startwindow/startwindowimpl.cpp b/src/gui/qt/startwindow/startwindowimpl.cpp index 99dbaae7..aca45629 100644 --- a/src/gui/qt/startwindow/startwindowimpl.cpp +++ b/src/gui/qt/startwindow/startwindowimpl.cpp @@ -215,6 +215,8 @@ startWindowImpl::startWindowImpl(ConfigFile *c, Log *l) connect(this, SIGNAL(signalNetClientPlayerJoined(unsigned, QString, bool)), myGameLobbyDialog, SLOT(addConnectedPlayer(unsigned, QString, bool))); connect(this, SIGNAL(signalNetClientPlayerChanged(unsigned, QString)), myGameLobbyDialog, SLOT(updatePlayer(unsigned, QString))); connect(this, SIGNAL(signalNetClientPlayerLeft(unsigned, QString)), myGameLobbyDialog, SLOT(removePlayer(unsigned, QString))); + // TODO connect(this, SIGNAL(signalNetClientSpectatorJoined(unsigned, QString)), myGameLobbyDialog, SLOT(addConnectedSpectator(unsigned, QString))); + // TODO connect(this, SIGNAL(signalNetClientSpectatorLeft(unsigned, QString)), myGameLobbyDialog, SLOT(removeSpectator(unsigned, QString))); connect(this, SIGNAL(signalNetClientNewGameAdmin(unsigned, QString)), myGameLobbyDialog, SLOT(newGameAdmin(unsigned, QString))); connect(this, SIGNAL(signalNetClientGameListNew(unsigned)), myGameLobbyDialog, SLOT(addGame(unsigned))); @@ -223,6 +225,8 @@ startWindowImpl::startWindowImpl(ConfigFile *c, Log *l) connect(this, SIGNAL(signalNetClientGameListUpdateAdmin(unsigned, unsigned)), myGameLobbyDialog, SLOT(updateGameAdmin(unsigned, unsigned))); connect(this, SIGNAL(signalNetClientGameListPlayerJoined(unsigned, unsigned)), myGameLobbyDialog, SLOT(gameAddPlayer(unsigned, unsigned))); connect(this, SIGNAL(signalNetClientGameListPlayerLeft(unsigned, unsigned)), myGameLobbyDialog, SLOT(gameRemovePlayer(unsigned, unsigned))); + // TODO connect(this, SIGNAL(signalNetClientGameListSpectatorJoined(unsigned, unsigned)), myGameLobbyDialog, SLOT(gameAddSpectator(unsigned, unsigned))); + // TODO connect(this, SIGNAL(signalNetClientGameListSpectatorLeft(unsigned, unsigned)), myGameLobbyDialog, SLOT(gameRemoveSpectator(unsigned, unsigned))); connect(this, SIGNAL(signalNetClientRemovedFromGame(int)), myGameLobbyDialog, SLOT(removedFromGame(int))); connect(this, SIGNAL(signalNetClientStatsUpdate(ServerStats)), myGameLobbyDialog, SLOT(updateStats(ServerStats))); diff --git a/src/gui/qt/startwindow/startwindowimpl.h b/src/gui/qt/startwindow/startwindowimpl.h index 8f739f0b..c101dba1 100644 --- a/src/gui/qt/startwindow/startwindowimpl.h +++ b/src/gui/qt/startwindow/startwindowimpl.h @@ -116,6 +116,8 @@ signals: void signalNetClientPlayerJoined(unsigned playerId, QString playerName, bool isGameAdmin); void signalNetClientPlayerChanged(unsigned playerId, QString newPlayerName); void signalNetClientPlayerLeft(unsigned playerId, QString playerName); + void signalNetClientSpectatorJoined(unsigned playerId, QString playerName); + void signalNetClientSpectatorLeft(unsigned playerId, QString playerName); void signalNetClientNewGameAdmin(unsigned playerId, QString playerName); void signalNetClientGameListNew(unsigned gameId); void signalNetClientGameListRemove(unsigned gameId); @@ -123,6 +125,8 @@ signals: void signalNetClientGameListUpdateAdmin(unsigned gameId, unsigned adminPlayerId); void signalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId); void signalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId); + void signalNetClientGameListSpectatorJoined(unsigned gameId, unsigned playerId); + void signalNetClientGameListSpectatorLeft(unsigned gameId, unsigned playerId); void signalNetClientGameStart(boost::shared_ptr game); void signalNetClientGameChatMsg(QString nickName, QString msg); void signalNetClientLobbyChatMsg(QString nickName, QString msg); diff --git a/src/net/clientcallback.h b/src/net/clientcallback.h index 78208c98..cc6d95af 100644 --- a/src/net/clientcallback.h +++ b/src/net/clientcallback.h @@ -60,12 +60,16 @@ public: virtual void SignalNetClientGameListUpdateAdmin(unsigned gameId, unsigned adminPlayerId) = 0; virtual void SignalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId) = 0; virtual void SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId) = 0; + virtual void SignalNetClientGameListSpectatorJoined(unsigned gameId, unsigned playerId) = 0; + virtual void SignalNetClientGameListSpectatorLeft(unsigned gameId, unsigned playerId) = 0; virtual void SignalNetClientGameStart(boost::shared_ptr game) = 0; virtual void SignalNetClientSelfJoined(unsigned playerId, const std::string &playerName, bool isGameAdmin) = 0; virtual void SignalNetClientPlayerJoined(unsigned playerId, const std::string &playerName, bool isGameAdmin) = 0; virtual void SignalNetClientPlayerChanged(unsigned playerId, const std::string &newPlayerName) = 0; virtual void SignalNetClientPlayerLeft(unsigned playerId, const std::string &playerName, int removeReason) = 0; + virtual void SignalNetClientSpectatorJoined(unsigned playerId, const std::string &playerName) = 0; + virtual void SignalNetClientSpectatorLeft(unsigned playerId, const std::string &playerName, int removeReason) = 0; virtual void SignalNetClientNewGameAdmin(unsigned playerId, const std::string &playerName) = 0; virtual void SignalNetClientGameChatMsg(const std::string &playerName, const std::string &msg) = 0; diff --git a/src/net/clientthread.h b/src/net/clientthread.h index 82a1d372..ae16614a 100644 --- a/src/net/clientthread.h +++ b/src/net/clientthread.h @@ -159,6 +159,7 @@ protected: void SetUnknownPlayer(unsigned id); void SetNewGameAdmin(unsigned id); void RetrieveAvatarIfNeeded(unsigned id, const PlayerInfo &info); + std::string GetPlayerName(unsigned id); void AddTempAvatarFile(unsigned playerId, unsigned avatarSize, AvatarFileType type); void StoreInTempAvatarFile(unsigned playerId, const std::vector &data); @@ -215,6 +216,8 @@ protected: void RemoveGameInfo(unsigned gameId); void ModifyGameInfoAddPlayer(unsigned gameId, unsigned playerId); void ModifyGameInfoRemovePlayer(unsigned gameId, unsigned playerId); + void ModifyGameInfoAddSpectator(unsigned gameId, unsigned playerId); + void ModifyGameInfoRemoveSpectator(unsigned gameId, unsigned playerId); 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 425c8bd4..25520963 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -628,6 +628,24 @@ AbstractClientStateReceiving::HandlePacket(boost::shared_ptr clien boost::shared_ptr playerData = client->CreatePlayerData(netPlayerJoined.playerid(), netPlayerJoined.isgameadmin()); client->AddPlayerData(playerData); + } 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())); + } else if (tmpPacket->GetMsg()->messagetype() == PokerTHMessage::Type_GameSpectatorLeftMessage) { + // A spectator left the network game. + const GameSpectatorLeftMessage &netSpectatorLeft = tmpPacket->GetMsg()->gamespectatorleftmessage(); + // Signal to GUI and remove from data list. + int removeReason; + switch (netSpectatorLeft.gamespectatorleftreason()) { + case GamePlayerLeftMessage::leftKicked : + removeReason = NTF_NET_REMOVED_KICKED; + break; + default : + removeReason = NTF_NET_REMOVED_ON_REQUEST; + break; + } + client->GetCallback().SignalNetClientSpectatorLeft(netSpectatorLeft.playerid(), client->GetPlayerName(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()); @@ -672,15 +690,7 @@ AbstractClientStateReceiving::HandlePacket(boost::shared_ptr clien const PlayerListMessage &netPlayerList = tmpPacket->GetMsg()->playerlistmessage(); if (netPlayerList.playerlistnotification() == PlayerListMessage::playerListNew) { - PlayerInfo info; - if (!client->GetCachedPlayerInfo(netPlayerList.playerid(), info)) { - // Request player info. - ostringstream name; - name << "#" << netPlayerList.playerid(); - info.playerName = name.str(); - client->RequestPlayerInfo(netPlayerList.playerid()); - } - client->GetCallback().SignalLobbyPlayerJoined(netPlayerList.playerid(), info.playerName); + client->GetCallback().SignalLobbyPlayerJoined(netPlayerList.playerid(), client->GetPlayerName(netPlayerList.playerid())); } else if (netPlayerList.playerlistnotification() == PlayerListMessage::playerListLeft) { client->GetCallback().SignalLobbyPlayerLeft(netPlayerList.playerid()); } @@ -688,11 +698,11 @@ AbstractClientStateReceiving::HandlePacket(boost::shared_ptr clien // A new game was created on the server. const GameListNewMessage &netListNew = tmpPacket->GetMsg()->gamelistnewmessage(); - unsigned numPlayers = netListNew.playerids_size(); // Request player info for players if needed. GameInfo tmpInfo; list requestList; - for (unsigned i = 0; i < numPlayers; i++) { + // All players. + for (int i = 0; i < netListNew.playerids_size(); i++) { PlayerInfo info; unsigned playerId = netListNew.playerids(i); if (!client->GetCachedPlayerInfo(playerId, info)) { @@ -700,6 +710,15 @@ AbstractClientStateReceiving::HandlePacket(boost::shared_ptr clien } tmpInfo.players.push_back(playerId); } + // All spectators. + for (int i = 0; i < netListNew.spectatorids_size(); i++) { + PlayerInfo info; + unsigned playerId = netListNew.spectatorids(i); + if (!client->GetCachedPlayerInfo(playerId, info)) { + requestList.push_back(playerId); + } + tmpInfo.spectators.push_back(playerId); + } // Send request for multiple players (will only act if list is non-empty). client->RequestPlayerInfo(requestList); @@ -730,6 +749,19 @@ AbstractClientStateReceiving::HandlePacket(boost::shared_ptr clien const GameListPlayerLeftMessage &netListLeft = tmpPacket->GetMsg()->gamelistplayerleftmessage(); client->ModifyGameInfoRemovePlayer(netListLeft.gameid(), netListLeft.playerid()); + } else if (tmpPacket->GetMsg()->messagetype() == PokerTHMessage::Type_GameListSpectatorJoinedMessage) { + const GameListSpectatorJoinedMessage &netListJoined = tmpPacket->GetMsg()->gamelistspectatorjoinedmessage(); + + client->ModifyGameInfoAddSpectator(netListJoined.gameid(), netListJoined.playerid()); + // Request player info if needed. + PlayerInfo info; + if (!client->GetCachedPlayerInfo(netListJoined.playerid(), info)) { + client->RequestPlayerInfo(netListJoined.playerid()); + } + } else if (tmpPacket->GetMsg()->messagetype() == PokerTHMessage::Type_GameListSpectatorLeftMessage) { + const GameListSpectatorLeftMessage &netListLeft = tmpPacket->GetMsg()->gamelistspectatorleftmessage(); + + client->ModifyGameInfoRemoveSpectator(netListLeft.gameid(), netListLeft.playerid()); } else if (tmpPacket->GetMsg()->messagetype() == PokerTHMessage::Type_GameListAdminChangedMessage) { const GameListAdminChangedMessage &netListAdmin = tmpPacket->GetMsg()->gamelistadminchangedmessage(); diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index 1b6a281e..e2530261 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -811,6 +811,20 @@ ClientThread::RetrieveAvatarIfNeeded(unsigned id, const PlayerInfo &info) } } +std::string +ClientThread::GetPlayerName(unsigned id) +{ + PlayerInfo info; + if (!GetCachedPlayerInfo(id, info)) { + // Request player info. + ostringstream name; + name << "#" << id; + info.playerName = name.str(); + RequestPlayerInfo(id); + } + return info.playerName; +} + void ClientThread::AddTempAvatarFile(unsigned playerId, unsigned avatarSize, AvatarFileType type) { @@ -1407,6 +1421,38 @@ ClientThread::ModifyGameInfoRemovePlayer(unsigned gameId, unsigned playerId) GetCallback().SignalNetClientGameListPlayerLeft(gameId, playerId); } +void +ClientThread::ModifyGameInfoAddSpectator(unsigned gameId, unsigned playerId) +{ + bool playerAdded = false; + { + boost::mutex::scoped_lock lock(m_gameInfoMapMutex); + GameInfoMap::iterator pos = m_gameInfoMap.find(gameId); + if (pos != m_gameInfoMap.end()) { + pos->second.spectators.push_back(playerId); + playerAdded = true; + } + } + if (playerAdded) + GetCallback().SignalNetClientGameListSpectatorJoined(gameId, playerId); +} + +void +ClientThread::ModifyGameInfoRemoveSpectator(unsigned gameId, unsigned playerId) +{ + bool playerRemoved = false; + { + boost::mutex::scoped_lock lock(m_gameInfoMapMutex); + GameInfoMap::iterator pos = m_gameInfoMap.find(gameId); + if (pos != m_gameInfoMap.end()) { + pos->second.spectators.remove(playerId); + playerRemoved = true; + } + } + if (playerRemoved) + GetCallback().SignalNetClientGameListSpectatorLeft(gameId, playerId); +} + void ClientThread::ClearGameInfoMap() {