Game List is now dynamically updated.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ public:
|
||||
GuiInterface &GetGui();
|
||||
|
||||
protected:
|
||||
typedef std::map<std::string, unsigned> GameMap;
|
||||
typedef std::map<unsigned, std::string> 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:
|
||||
|
||||
|
||||
@@ -476,7 +476,15 @@ ClientStateWaitJoin::InternalProcess(ClientThread &client, boost::shared_ptr<Net
|
||||
// A new game was created on the server.
|
||||
NetPacketGameListNew::Data gameListNewData;
|
||||
packet->ToNetPacketGameListNew()->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())
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ServerGameThread> 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<ServerGameThread> 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<ServerGameThread> 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<NetPacket>
|
||||
ServerLobbyThread::CreateNetPacketGameListUpdate(const ServerGameThread &game, GameMode mode)
|
||||
{
|
||||
boost::shared_ptr<NetPacket> packet(new NetPacketGameListUpdate);
|
||||
NetPacketGameListUpdate::Data packetData;
|
||||
packetData.gameId = game.GetId();
|
||||
packetData.gameMode = mode;
|
||||
static_cast<NetPacketGameListUpdate *>(packet.get())->SetData(packetData);
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
||||
@@ -77,6 +77,9 @@ protected:
|
||||
void CloseSessionLoop();
|
||||
void RemoveGameLoop();
|
||||
|
||||
void InternalAddGame(boost::shared_ptr<ServerGameThread> game);
|
||||
void InternalRemoveGame(boost::shared_ptr<ServerGameThread> game);
|
||||
|
||||
void TerminateGames();
|
||||
|
||||
void HandleNewConnection(boost::shared_ptr<ConnectData> connData);
|
||||
@@ -100,6 +103,7 @@ protected:
|
||||
bool IsPlayerConnected(const std::string &name);
|
||||
|
||||
static boost::shared_ptr<NetPacket> CreateNetPacketGameListNew(const ServerGameThread &game);
|
||||
static boost::shared_ptr<NetPacket> CreateNetPacketGameListUpdate(const ServerGameThread &game, GameMode mode);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user