Show and update admin player of a game even when in lobby and not in the game.

This commit is contained in:
lotodore
2007-10-12 14:32:25 +00:00
parent 96d1f34f5c
commit 4f00333599
21 changed files with 227 additions and 22 deletions
+6
View File
@@ -488,6 +488,12 @@ AbstractClientStateReceiving::Process(ClientThread &client)
tmpPacket->ToNetPacketGameListPlayerLeft()->GetData(playerLeftData);
client.ModifyGameInfoRemovePlayer(playerLeftData.gameId, playerLeftData.playerId);
}
else if (tmpPacket->ToNetPacketGameListAdminChanged())
{
NetPacketGameListAdminChanged::Data adminChangedData;
tmpPacket->ToNetPacketGameListAdminChanged()->GetData(adminChangedData);
client.UpdateGameInfoAdmin(adminChangedData.gameId, adminChangedData.newAdminplayerId);
}
else if (tmpPacket->ToNetPacketAvatarHeader())
{
NetPacketAvatarHeader::Data headerData;
+17
View File
@@ -784,6 +784,23 @@ ClientThread::UpdateGameInfoMode(unsigned gameId, GameMode mode)
GetCallback().SignalNetClientGameListUpdateMode(gameId, mode);
}
void
ClientThread::UpdateGameInfoAdmin(unsigned gameId, unsigned adminPlayerId)
{
bool found = false;
{
boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
GameInfoMap::iterator pos = m_gameInfoMap.find(gameId);
if (pos != m_gameInfoMap.end())
{
found = true;
(*pos).second.adminPlayerId = adminPlayerId;
}
}
if (found)
GetCallback().SignalNetClientGameListUpdateAdmin(gameId, adminPlayerId);
}
void
ClientThread::RemoveGameInfo(unsigned gameId)
{
+80
View File
@@ -38,6 +38,7 @@ using namespace std;
#define NET_TYPE_GAME_LIST_UPDATE 0x0011
#define NET_TYPE_GAME_LIST_PLAYER_JOINED 0x0012
#define NET_TYPE_GAME_LIST_PLAYER_LEFT 0x0013
#define NET_TYPE_GAME_LIST_ADMIN_CHANGED 0x0014
#define NET_TYPE_RETRIEVE_PLAYER_INFO 0x0020
#define NET_TYPE_PLAYER_INFO 0x0021
#define NET_TYPE_UNKNOWN_PLAYER_ID 0x0022
@@ -197,6 +198,7 @@ struct GCC_PACKED NetPacketGameListNewData
{
NetPacketHeader head;
u_int32_t gameId;
u_int32_t adminPlayerId;
u_int16_t gameMode;
u_int16_t gameNameLength;
u_int16_t curNumberOfPlayers;
@@ -226,6 +228,13 @@ struct GCC_PACKED NetPacketGameListPlayerLeftData
u_int32_t playerId;
};
struct GCC_PACKED NetPacketGameListAdminChangedData
{
NetPacketHeader head;
u_int32_t gameId;
u_int32_t newAdminPlayerId;
};
struct GCC_PACKED NetPacketRetrievePlayerInfoData
{
NetPacketHeader head;
@@ -638,6 +647,9 @@ NetPacket::Create(char *data, unsigned &dataSize)
case NET_TYPE_GAME_LIST_PLAYER_LEFT:
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketGameListPlayerLeft);
break;
case NET_TYPE_GAME_LIST_ADMIN_CHANGED:
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketGameListAdminChanged);
break;
case NET_TYPE_RETRIEVE_PLAYER_INFO:
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketRetrievePlayerInfo);
break;
@@ -881,6 +893,12 @@ NetPacket::ToNetPacketGameListPlayerLeft() const
return NULL;
}
const NetPacketGameListAdminChanged *
NetPacket::ToNetPacketGameListAdminChanged() const
{
return NULL;
}
const NetPacketRetrievePlayerInfo *
NetPacket::ToNetPacketRetrievePlayerInfo() const
{
@@ -1668,6 +1686,7 @@ NetPacketGameListNew::SetData(const NetPacketGameListNew::Data &inData)
// Set the data.
tmpData->gameId = htonl(inData.gameId);
tmpData->adminPlayerId = htonl(inData.gameInfo.adminPlayerId);
tmpData->gameMode = htons(inData.gameInfo.mode);
tmpData->gameNameLength = htons(gameNameLen);
tmpData->curNumberOfPlayers = htons(curNumPlayers);
@@ -1705,6 +1724,7 @@ NetPacketGameListNew::GetData(NetPacketGameListNew::Data &outData) const
int manualBlindsSize = numManualBlinds * sizeof(u_int32_t);
outData.gameId = ntohl(tmpData->gameId);
outData.gameInfo.adminPlayerId = ntohl(tmpData->adminPlayerId);
outData.gameInfo.mode = static_cast<GameMode>(ntohs(tmpData->gameMode));
u_int16_t gameNameLen = ntohs(tmpData->gameNameLength);
u_int16_t curNumPlayers = ntohs(tmpData->curNumberOfPlayers);
@@ -1949,6 +1969,66 @@ NetPacketGameListPlayerLeft::InternalCheck(const NetPacketHeader*) const
//-----------------------------------------------------------------------------
NetPacketGameListAdminChanged::NetPacketGameListAdminChanged()
: NetPacket(NET_TYPE_GAME_LIST_ADMIN_CHANGED, sizeof(NetPacketGameListAdminChangedData), sizeof(NetPacketGameListAdminChangedData))
{
}
NetPacketGameListAdminChanged::~NetPacketGameListAdminChanged()
{
}
boost::shared_ptr<NetPacket>
NetPacketGameListAdminChanged::Clone() const
{
boost::shared_ptr<NetPacket> newPacket(new NetPacketGameListAdminChanged);
try
{
newPacket->SetRawData(GetRawData());
} catch (const NetException &)
{
// Need to return the new packet anyway.
}
return newPacket;
}
void
NetPacketGameListAdminChanged::SetData(const NetPacketGameListAdminChanged::Data &inData)
{
NetPacketGameListAdminChangedData *tmpData = (NetPacketGameListAdminChangedData *)GetRawData();
// Set the data.
tmpData->gameId = htonl(inData.gameId);
tmpData->newAdminPlayerId = htonl(inData.newAdminplayerId);
// Check the packet - just in case.
Check(GetRawData());
}
void
NetPacketGameListAdminChanged::GetData(NetPacketGameListAdminChanged::Data &outData) const
{
// We assume that the data is valid. Validity has already been checked.
NetPacketGameListAdminChangedData *tmpData = (NetPacketGameListAdminChangedData *)GetRawData();
outData.gameId = ntohl(tmpData->gameId);
outData.newAdminplayerId = ntohl(tmpData->newAdminPlayerId);
}
const NetPacketGameListAdminChanged *
NetPacketGameListAdminChanged::ToNetPacketGameListAdminChanged() const
{
return this;
}
void
NetPacketGameListAdminChanged::InternalCheck(const NetPacketHeader*) const
{
// Nothing to do.
}
//-----------------------------------------------------------------------------
NetPacketRetrievePlayerInfo::NetPacketRetrievePlayerInfo()
: NetPacket(NET_TYPE_RETRIEVE_PLAYER_INFO, sizeof(NetPacketRetrievePlayerInfoData), sizeof(NetPacketRetrievePlayerInfoData))
{
+2 -2
View File
@@ -247,9 +247,9 @@ ServerGameStateInit::HandleNewSession(ServerGameThread &server, SessionWrapper s
}
else
{
if (curNumPlayers == 0)
if (session.playerData->GetUniqueId() == server.GetAdminPlayerId())
{
// First player is admin.
// This is the admin player.
session.playerData->SetRights(PLAYER_RIGHTS_ADMIN);
}
+27 -9
View File
@@ -52,8 +52,9 @@ private:
};
ServerGameThread::ServerGameThread(ServerLobbyThread &lobbyThread, u_int32_t id, const string &name, const string &pwd, GuiInterface &gui, ConfigFile *playerConfig)
: m_lobbyThread(lobbyThread), m_gui(gui), m_id(id), m_name(name), m_password(pwd), m_playerConfig(playerConfig), m_curState(NULL), m_gameNum(1)
ServerGameThread::ServerGameThread(ServerLobbyThread &lobbyThread, u_int32_t id, const string &name, const string &pwd, unsigned adminPlayerId, GuiInterface &gui, ConfigFile *playerConfig)
: m_lobbyThread(lobbyThread), m_adminPlayerId(adminPlayerId), m_gui(gui), m_id(id),
m_name(name), m_password(pwd), m_playerConfig(playerConfig), m_curState(NULL), m_gameNum(1)
{
m_senderCallback.reset(new ServerSenderCallback(*this));
m_sender.reset(new SenderThread(GetSenderCallback()));
@@ -277,6 +278,20 @@ ServerGameThread::IsRunning() const
return m_game.get() != NULL;
}
unsigned
ServerGameThread::GetAdminPlayerId() const
{
boost::mutex::scoped_lock lock(m_adminPlayerIdMutex);
return m_adminPlayerId;
}
void
ServerGameThread::SetAdminPlayerId(unsigned playerId)
{
boost::mutex::scoped_lock lock(m_adminPlayerIdMutex);
m_adminPlayerId = playerId;
}
void
ServerGameThread::AddComputerPlayer(boost::shared_ptr<PlayerData> player)
{
@@ -321,13 +336,6 @@ ServerGameThread::GracefulRemoveSession(SessionWrapper session)
void
ServerGameThread::RemovePlayerData(boost::shared_ptr<PlayerData> player)
{
// Send "Player Left" to clients.
boost::shared_ptr<NetPacket> thisPlayerLeft(new NetPacketPlayerLeft);
NetPacketPlayerLeft::Data thisPlayerLeftData;
thisPlayerLeftData.playerId = player->GetUniqueId();
static_cast<NetPacketPlayerLeft *>(thisPlayerLeft.get())->SetData(thisPlayerLeftData);
GetSessionManager().SendToAllSessions(GetSender(), thisPlayerLeft, SessionData::Game);
if (player->GetRights() == PLAYER_RIGHTS_ADMIN)
{
// Find new admin for the game
@@ -335,6 +343,7 @@ ServerGameThread::RemovePlayerData(boost::shared_ptr<PlayerData> player)
if (!playerList.empty())
{
boost::shared_ptr<PlayerData> newAdmin = playerList.front();
SetAdminPlayerId(newAdmin->GetUniqueId());
newAdmin->SetRights(PLAYER_RIGHTS_ADMIN);
// Send "Game Admin Changed" to clients.
boost::shared_ptr<NetPacket> adminChanged(new NetPacketGameAdminChanged);
@@ -342,11 +351,20 @@ ServerGameThread::RemovePlayerData(boost::shared_ptr<PlayerData> player)
adminChangedData.playerId = newAdmin->GetUniqueId(); // Choose next player as admin.
static_cast<NetPacketGameAdminChanged *>(adminChanged.get())->SetData(adminChangedData);
GetSessionManager().SendToAllSessions(GetSender(), adminChanged, SessionData::Game);
GetLobbyThread().NotifyGameAdminChanged(GetId(), newAdmin->GetUniqueId());
}
}
// Reset player rights.
player->SetRights(PLAYER_RIGHTS_NORMAL);
// Send "Player Left" to clients.
boost::shared_ptr<NetPacket> thisPlayerLeft(new NetPacketPlayerLeft);
NetPacketPlayerLeft::Data thisPlayerLeftData;
thisPlayerLeftData.playerId = player->GetUniqueId();
static_cast<NetPacketPlayerLeft *>(thisPlayerLeft.get())->SetData(thisPlayerLeftData);
GetSessionManager().SendToAllSessions(GetSender(), thisPlayerLeft, SessionData::Game);
GetLobbyThread().NotifyPlayerLeftGame(GetId(), player->GetUniqueId());
}
+17 -1
View File
@@ -154,6 +154,19 @@ ServerLobbyThread::NotifyPlayerLeftGame(unsigned gameId, unsigned playerId)
m_gameSessionManager.SendToAllSessions(GetSender(), packet, SessionData::Game);
}
void
ServerLobbyThread::NotifyGameAdminChanged(unsigned gameId, unsigned newAdminPlayerId)
{
// Send notification to players in lobby.
boost::shared_ptr<NetPacket> packet(new NetPacketGameListAdminChanged);
NetPacketGameListAdminChanged::Data packetData;
packetData.gameId = gameId;
packetData.newAdminplayerId = newAdminPlayerId;
static_cast<NetPacketGameListAdminChanged *>(packet.get())->SetData(packetData);
m_sessionManager.SendToAllSessions(GetSender(), packet, SessionData::Established);
m_gameSessionManager.SendToAllSessions(GetSender(), packet, SessionData::Game);
}
void
ServerLobbyThread::NotifyStartingGame(unsigned gameId)
{
@@ -556,9 +569,11 @@ ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPa
boost::shared_ptr<ServerGameThread> game(
new ServerGameThread(
*this, GetNextGameId(),
*this,
GetNextGameId(),
createGameData.gameName,
createGameData.password,
session.playerData->GetUniqueId(),
GetGui(),
m_playerConfig));
game->Init(createGameData.gameData);
@@ -870,6 +885,7 @@ ServerLobbyThread::CreateNetPacketGameListNew(const ServerGameThread &game)
boost::shared_ptr<NetPacket> packet(new NetPacketGameListNew);
NetPacketGameListNew::Data packetData;
packetData.gameId = game.GetId();
packetData.gameInfo.adminPlayerId = game.GetAdminPlayerId();
packetData.gameInfo.mode = game.IsRunning() ? GAME_MODE_STARTED : GAME_MODE_CREATED;
packetData.gameInfo.name = game.GetName();
packetData.gameInfo.data = game.GetGameData();