From 388db7c16a7975e1dd9ff1776d730638d69c0938 Mon Sep 17 00:00:00 2001 From: lotodore Date: Tue, 21 Aug 2007 19:50:13 +0000 Subject: [PATCH] Game List is now dynamically updated. --- src/gui/qt/mainwindow/mainwindowimpl.cpp | 5 ++- src/net/clientthread.h | 5 +-- src/net/common/clientstate.cpp | 10 +++++- src/net/common/clientthread.cpp | 43 +++++++++++++++++++----- src/net/common/serverlobbythread.cpp | 35 +++++++++++++++++-- src/net/serverlobbythread.h | 4 +++ 6 files changed, 85 insertions(+), 17 deletions(-) diff --git a/src/gui/qt/mainwindow/mainwindowimpl.cpp b/src/gui/qt/mainwindow/mainwindowimpl.cpp index 525653fc..e11f24c5 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.cpp +++ b/src/gui/qt/mainwindow/mainwindowimpl.cpp @@ -816,6 +816,8 @@ void mainWindowImpl::callGameLobbyDialog() { myGameLobbyDialog->setSession(&getSession()); myGameLobbyDialog->treeWidget_GameList->clear(); + myStartNetworkGameDialog->treeWidget->clear(); + myStartNetworkGameDialog->setSession(&getSession()); // Just for testing mySession->startNetworkClientForLocalServer(); @@ -824,9 +826,6 @@ void mainWindowImpl::callGameLobbyDialog() { if (myGameLobbyDialog->result() == QDialog::Accepted) { - myStartNetworkGameDialog->setSession(&getSession()); - myStartNetworkGameDialog->treeWidget->clear(); - showNetworkStartDialog(); } } diff --git a/src/net/clientthread.h b/src/net/clientthread.h index f4c133b1..4ce2d99c 100644 --- a/src/net/clientthread.h +++ b/src/net/clientthread.h @@ -63,7 +63,7 @@ public: GuiInterface &GetGui(); protected: - typedef std::map GameMap; + typedef std::map GameMap; // Main function of the thread. virtual void Main(); @@ -98,7 +98,8 @@ protected: void RemoveDisconnectedPlayers(); unsigned GetGameIdByName(const std::string &name) const; - void AddGameInformation(const std::string &name, unsigned id); + void AddGameInformation(unsigned id, const std::string &name); + void RemoveGameInformation(unsigned id); private: diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index 26088aac..6a1b1225 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -476,7 +476,15 @@ ClientStateWaitJoin::InternalProcess(ClientThread &client, boost::shared_ptrToNetPacketGameListNew()->GetData(gameListNewData); - client.AddGameInformation(gameListNewData.gameName, gameListNewData.gameId); + client.AddGameInformation(gameListNewData.gameId, gameListNewData.gameName); + } + else if (packet->ToNetPacketGameListUpdate()) + { + // An existing game was updated on the server. + NetPacketGameListUpdate::Data gameListUpdateData; + packet->ToNetPacketGameListUpdate()->GetData(gameListUpdateData); + if (gameListUpdateData.gameMode == GAME_MODE_CLOSED) + client.RemoveGameInformation(gameListUpdateData.gameId); } else if (packet->ToNetPacketJoinGameAck()) { diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index 0a9bc65a..bf842b3a 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -475,21 +475,48 @@ unsigned ClientThread::GetGameIdByName(const std::string &name) const { // Find the game. - bool retVal = false; boost::mutex::scoped_lock lock(m_gameMapMutex); - GameMap::const_iterator pos = m_gameMap.find(name); - if (pos == m_gameMap.end()) + GameMap::const_iterator i = m_gameMap.begin(); + GameMap::const_iterator end = m_gameMap.end(); + while (i != end) + { + if (i->second == name) + break; + ++i; + } + + if (i == end) throw NetException(ERR_NET_UNKNOWN_GAME, 0); - return pos->second; + return i->first; } void -ClientThread::AddGameInformation(const std::string &name, unsigned id) +ClientThread::AddGameInformation(unsigned id, const std::string &name) { + if (!name.empty()) { - boost::mutex::scoped_lock lock(m_gameMapMutex); - m_gameMap.insert(GameMap::value_type(name, id)); + { + boost::mutex::scoped_lock lock(m_gameMapMutex); + m_gameMap.insert(GameMap::value_type(id, name)); + } + GetCallback().SignalNetClientGameListNew(name); } - GetCallback().SignalNetClientGameListNew(name); +} + +void +ClientThread::RemoveGameInformation(unsigned id) +{ + string name; + { + boost::mutex::scoped_lock lock(m_gameMapMutex); + GameMap::iterator pos = m_gameMap.find(id); + if (pos != m_gameMap.end()) + { + name = pos->second; + m_gameMap.erase(pos); + } + } + if (!name.empty()) + GetCallback().SignalNetClientGameListRemove(name); } diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 3b6d7ba1..3e40c768 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -282,8 +282,8 @@ ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPa // Add session to the game. game->AddSession(session); - // Add game to list. - m_gameMap.insert(GameMap::value_type(game->GetId(), game)); + // Add game to list of games. + InternalAddGame(game); // Start the game. game->Run(); @@ -353,13 +353,31 @@ ServerLobbyThread::RemoveGameLoop() boost::shared_ptr tmpGame = pos->second; tmpGame->SignalTermination(); tmpGame->Join(GAME_THREAD_TERMINATE_TIMEOUT); - m_gameMap.erase(pos); + InternalRemoveGame(tmpGame); } ++i; } m_removeGameList.clear(); } +void +ServerLobbyThread::InternalAddGame(boost::shared_ptr game) +{ + // Add game to list. + m_gameMap.insert(GameMap::value_type(game->GetId(), game)); + // Notify all players. + m_sessionManager.SendToAllSessions(GetSender(), CreateNetPacketGameListNew(*game), SessionData::Established); +} + +void +ServerLobbyThread::InternalRemoveGame(boost::shared_ptr game) +{ + // Remove game from list. + m_gameMap.erase(game->GetId()); + // Notify all players. + m_sessionManager.SendToAllSessions(GetSender(), CreateNetPacketGameListUpdate(*game, GAME_MODE_CLOSED), SessionData::Established); +} + void ServerLobbyThread::TerminateGames() { @@ -503,3 +521,14 @@ ServerLobbyThread::CreateNetPacketGameListNew(const ServerGameThread &game) return packet; } +boost::shared_ptr +ServerLobbyThread::CreateNetPacketGameListUpdate(const ServerGameThread &game, GameMode mode) +{ + boost::shared_ptr packet(new NetPacketGameListUpdate); + NetPacketGameListUpdate::Data packetData; + packetData.gameId = game.GetId(); + packetData.gameMode = mode; + static_cast(packet.get())->SetData(packetData); + return packet; +} + diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index b81a376b..7952f694 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -77,6 +77,9 @@ protected: void CloseSessionLoop(); void RemoveGameLoop(); + void InternalAddGame(boost::shared_ptr game); + void InternalRemoveGame(boost::shared_ptr game); + void TerminateGames(); void HandleNewConnection(boost::shared_ptr connData); @@ -100,6 +103,7 @@ protected: bool IsPlayerConnected(const std::string &name); static boost::shared_ptr CreateNetPacketGameListNew(const ServerGameThread &game); + static boost::shared_ptr CreateNetPacketGameListUpdate(const ServerGameThread &game, GameMode mode); private: