Trying to add computer players in dedicated server. Not fully functional yet (names are not shown).

This commit is contained in:
lotodore
2007-09-02 20:59:16 +00:00
parent 33fdc95cec
commit dde14654f9
6 changed files with 99 additions and 32 deletions
+7 -8
View File
@@ -226,7 +226,6 @@ ServerGameStateInit::Instance()
} }
ServerGameStateInit::ServerGameStateInit() ServerGameStateInit::ServerGameStateInit()
: m_curUniquePlayerId(0)
{ {
} }
@@ -264,7 +263,7 @@ ServerGameStateInit::HandleNewSession(ServerGameThread &server, SessionWrapper s
server.GetSender().Send(session.sessionData->GetSocket(), joinGameAck); server.GetSender().Send(session.sessionData->GetSocket(), joinGameAck);
// Send notifications for connected players to client. // Send notifications for connected players to client.
PlayerDataList tmpPlayerList = server.GetSessionManager().GetPlayerDataList(); PlayerDataList tmpPlayerList = server.GetFullPlayerDataList();
PlayerDataList::iterator player_i = tmpPlayerList.begin(); PlayerDataList::iterator player_i = tmpPlayerList.begin();
PlayerDataList::iterator player_end = tmpPlayerList.end(); PlayerDataList::iterator player_end = tmpPlayerList.end();
while (player_i != player_end) while (player_i != player_end)
@@ -295,20 +294,20 @@ ServerGameStateInit::InternalProcess(ServerGameThread &server, SessionWrapper se
if (packet->ToNetPacketStartEvent()) if (packet->ToNetPacketStartEvent())
{ {
server.ResetComputerPlayerList(); server.ResetComputerPlayerList();
/* int remainingSlots = server.GetGameData().maxNumberOfPlayers - server.GetCurNumberOfPlayers(); int remainingSlots = server.GetGameData().maxNumberOfPlayers - server.GetCurNumberOfPlayers();
for (int i = 1; i <= remainingSlots; i++) for (int i = 1; i <= remainingSlots; i++)
{ {
boost::shared_ptr<PlayerData> tmpPlayerData( boost::shared_ptr<PlayerData> tmpPlayerData(
new PlayerData(m_curUniquePlayerId++, 0, PLAYER_TYPE_COMPUTER, PLAYER_RIGHTS_NORMAL)); new PlayerData(server.GetLobbyThread().GetNextUniquePlayerId(), 0, PLAYER_TYPE_COMPUTER, PLAYER_RIGHTS_NORMAL));
ostringstream name; ostringstream name;
name << SERVER_COMPUTER_PLAYER_NAME << i; name << SERVER_COMPUTER_PLAYER_NAME << i;
tmpPlayerData->SetName(name.str()); tmpPlayerData->SetName(name.str());
server.AddComputerPlayer(tmpPlayerData);
// Send "Player Joined" to other fully connected clients. // Send "Player Joined" to other fully connected clients.
server.SendToAllPlayers(CreateNetPacketPlayerJoined(*tmpPlayerData)); server.SendToAllPlayers(CreateNetPacketPlayerJoined(*tmpPlayerData), SessionData::Game);
server.AddComputerPlayer(tmpPlayerData); }
}*/
server.InternalStartGame(); server.InternalStartGame();
server.SetState(SERVER_START_GAME_STATE::Instance()); server.SetState(SERVER_START_GAME_STATE::Instance());
@@ -387,7 +386,7 @@ ServerGameStateStartGame::Process(ServerGameThread &server)
// Send player order to clients. // Send player order to clients.
// Assume player list is sorted by number. // Assume player list is sorted by number.
PlayerDataList tmpPlayerList = server.GetSessionManager().GetPlayerDataList(); PlayerDataList tmpPlayerList = server.GetFullPlayerDataList();
PlayerDataList::iterator player_i = tmpPlayerList.begin(); PlayerDataList::iterator player_i = tmpPlayerList.begin();
PlayerDataList::iterator player_end = tmpPlayerList.end(); PlayerDataList::iterator player_end = tmpPlayerList.end();
while (player_i != player_end) while (player_i != player_end)
+68 -9
View File
@@ -146,7 +146,7 @@ ServerGameThread::InternalStartGame()
// Initialize the game. // Initialize the game.
GuiInterface &gui = GetGui(); GuiInterface &gui = GetGui();
PlayerDataList playerData = GetSessionManager().GetPlayerDataList(); PlayerDataList playerData = GetFullPlayerDataList();
// Create EngineFactory // Create EngineFactory
boost::shared_ptr<EngineFactory> factory(new LocalEngineFactory(m_playerConfig)); // LocalEngine erstellen boost::shared_ptr<EngineFactory> factory(new LocalEngineFactory(m_playerConfig)); // LocalEngine erstellen
@@ -185,22 +185,81 @@ ServerGameThread::InternalStartGame()
void void
ServerGameThread::InternalKickPlayer(unsigned playerId) ServerGameThread::InternalKickPlayer(unsigned playerId)
{ {
// TODO SessionWrapper tmpSession = GetSessionManager().GetSessionByUniquePlayerId(playerId);
// SessionWrapper tmpSession = GetSessionByUniquePlayerId(uniqueId); SessionError(tmpSession, ERR_NET_PLAYER_KICKED);
// SessionError(tmpSession, ERR_NET_PLAYER_KICKED); }
PlayerDataList
ServerGameThread::GetFullPlayerDataList() const
{
PlayerDataList playerList(GetSessionManager().GetPlayerDataList());
boost::mutex::scoped_lock lock(m_computerPlayerListMutex);
copy(m_computerPlayerList.begin(), m_computerPlayerList.end(), back_inserter(playerList));
return playerList;
}
boost::shared_ptr<PlayerData>
ServerGameThread::GetPlayerDataByUniqueId(unsigned playerId) const
{
boost::shared_ptr<PlayerData> tmpPlayer;
SessionWrapper session = GetSessionManager().GetSessionByUniquePlayerId(playerId);
if (session.playerData.get())
{
tmpPlayer = session.playerData;
}
else
{
boost::mutex::scoped_lock lock(m_computerPlayerListMutex);
PlayerDataList::const_iterator i = m_computerPlayerList.begin();
PlayerDataList::const_iterator end = m_computerPlayerList.end();
while (i != end)
{
if ((*i)->GetUniqueId() == playerId)
{
tmpPlayer = *i;
break;
}
++i;
}
}
return tmpPlayer;
}
PlayerIdList
ServerGameThread::GetPlayerIdList() const
{
PlayerIdList idList(GetSessionManager().GetPlayerIdList());
boost::mutex::scoped_lock lock(m_computerPlayerListMutex);
PlayerDataList::const_iterator i = m_computerPlayerList.begin();
PlayerDataList::const_iterator end = m_computerPlayerList.end();
while (i != end)
{
idList.push_back((*i)->GetUniqueId());
++i;
}
return idList;
}
bool
ServerGameThread::IsPlayerConnected(const std::string &name) const
{
return GetSessionManager().IsPlayerConnected(name);
} }
void void
ServerGameThread::AddComputerPlayer(boost::shared_ptr<PlayerData> player) ServerGameThread::AddComputerPlayer(boost::shared_ptr<PlayerData> player)
{ {
// TODO boost::mutex::scoped_lock lock(m_computerPlayerListMutex);
m_computerPlayers.push_back(player); m_computerPlayerList.push_back(player);
} }
void void
ServerGameThread::ResetComputerPlayerList() ServerGameThread::ResetComputerPlayerList()
{ {
m_computerPlayers.clear(); boost::mutex::scoped_lock lock(m_computerPlayerListMutex);
m_computerPlayerList.clear();
} }
void void
@@ -271,7 +330,7 @@ ServerGameThread::RemoveDisconnectedPlayers()
size_t size_t
ServerGameThread::GetCurNumberOfPlayers() const ServerGameThread::GetCurNumberOfPlayers() const
{ {
return GetSessionManager().GetPlayerDataList().size(); return GetFullPlayerDataList().size();
} }
void void
@@ -279,7 +338,7 @@ ServerGameThread::AssignPlayerNumbers()
{ {
int playerNumber = 0; int playerNumber = 0;
PlayerDataList playerList = GetSessionManager().GetPlayerDataList(); PlayerDataList playerList = GetFullPlayerDataList();
PlayerDataList::iterator player_i = playerList.begin(); PlayerDataList::iterator player_i = playerList.begin();
PlayerDataList::iterator player_end = playerList.end(); PlayerDataList::iterator player_end = playerList.end();
+11 -10
View File
@@ -127,6 +127,7 @@ ServerLobbyThread::RemoveGame(unsigned id)
u_int32_t u_int32_t
ServerLobbyThread::GetNextUniquePlayerId() ServerLobbyThread::GetNextUniquePlayerId()
{ {
boost::mutex::scoped_lock lock(m_curUniquePlayerIdMutex);
return m_curUniquePlayerId++; return m_curUniquePlayerId++;
} }
@@ -300,28 +301,28 @@ ServerLobbyThread::HandleNetPacketRetrievePlayerInfo(SessionWrapper session, con
tmpPacket.GetData(request); tmpPacket.GetData(request);
// Find player in lobby or in a game. // Find player in lobby or in a game.
SessionWrapper tmpSession = m_sessionManager.GetSessionByUniquePlayerId(request.playerId); boost::shared_ptr<PlayerData> tmpPlayer = m_sessionManager.GetSessionByUniquePlayerId(request.playerId).playerData;
if (!tmpSession.sessionData.get() || !tmpSession.playerData.get()) if (!tmpPlayer.get())
{ {
GameMap::const_iterator game_i = m_gameMap.begin(); GameMap::const_iterator game_i = m_gameMap.begin();
GameMap::const_iterator game_end = m_gameMap.end(); GameMap::const_iterator game_end = m_gameMap.end();
while (game_i != game_end) while (game_i != game_end)
{ {
tmpSession = game_i->second->GetSessionManager().GetSessionByUniquePlayerId(request.playerId); tmpPlayer = game_i->second->GetPlayerDataByUniqueId(request.playerId);
if (tmpSession.sessionData.get() && tmpSession.playerData.get()) if (tmpPlayer.get())
break; break;
++game_i; ++game_i;
} }
} }
if (tmpSession.sessionData.get() && tmpSession.playerData.get()) if (tmpPlayer.get())
{ {
// Send player info to client. // Send player info to client.
boost::shared_ptr<NetPacket> info(new NetPacketPlayerInfo); boost::shared_ptr<NetPacket> info(new NetPacketPlayerInfo);
NetPacketPlayerInfo::Data infoData; NetPacketPlayerInfo::Data infoData;
infoData.playerId = tmpSession.playerData->GetUniqueId(); infoData.playerId = tmpPlayer->GetUniqueId();
infoData.playerInfo.ptype = tmpSession.playerData->GetType(); infoData.playerInfo.ptype = tmpPlayer->GetType();
infoData.playerInfo.playerName = tmpSession.playerData->GetName(); infoData.playerInfo.playerName = tmpPlayer->GetName();
static_cast<NetPacketPlayerInfo *>(info.get())->SetData(infoData); static_cast<NetPacketPlayerInfo *>(info.get())->SetData(infoData);
GetSender().Send(session.sessionData->GetSocket(), info); GetSender().Send(session.sessionData->GetSocket(), info);
} }
@@ -560,7 +561,7 @@ ServerLobbyThread::IsPlayerConnected(const string &name)
GameMap::const_iterator game_end = m_gameMap.end(); GameMap::const_iterator game_end = m_gameMap.end();
while (game_i != game_end) while (game_i != game_end)
{ {
if (game_i->second->GetSessionManager().IsPlayerConnected(name)) if (game_i->second->IsPlayerConnected(name))
{ {
retVal = true; retVal = true;
break; break;
@@ -580,7 +581,7 @@ ServerLobbyThread::CreateNetPacketGameListNew(const ServerGameThread &game)
packetData.gameInfo.mode = GAME_MODE_CREATED; packetData.gameInfo.mode = GAME_MODE_CREATED;
packetData.gameInfo.name = game.GetName(); packetData.gameInfo.name = game.GetName();
packetData.gameInfo.data = game.GetGameData(); packetData.gameInfo.data = game.GetGameData();
packetData.gameInfo.players = game.GetSessionManager().GetPlayerIdList(); packetData.gameInfo.players = game.GetPlayerIdList();
static_cast<NetPacketGameListNew *>(packet.get())->SetData(packetData); static_cast<NetPacketGameListNew *>(packet.get())->SetData(packetData);
return packet; return packet;
} }
-1
View File
@@ -126,7 +126,6 @@ protected:
private: private:
u_int32_t m_curUniquePlayerId;
static boost::thread_specific_ptr<ServerGameStateInit> Ptr; static boost::thread_specific_ptr<ServerGameStateInit> Ptr;
}; };
+10 -3
View File
@@ -60,8 +60,9 @@ public:
bool CheckPassword(const std::string &password) const; bool CheckPassword(const std::string &password) const;
const GameData &GetGameData() const; const GameData &GetGameData() const;
const SessionManager &GetSessionManager() const; boost::shared_ptr<PlayerData> GetPlayerDataByUniqueId(unsigned playerId) const;
SessionManager &GetSessionManager(); PlayerIdList GetPlayerIdList() const;
bool IsPlayerConnected(const std::string &name) const;
protected: protected:
@@ -73,6 +74,8 @@ protected:
void InternalStartGame(); void InternalStartGame();
void InternalKickPlayer(unsigned playerId); void InternalKickPlayer(unsigned playerId);
PlayerDataList GetFullPlayerDataList() const;
void AddComputerPlayer(boost::shared_ptr<PlayerData> player); void AddComputerPlayer(boost::shared_ptr<PlayerData> player);
void ResetComputerPlayerList(); void ResetComputerPlayerList();
@@ -104,13 +107,17 @@ protected:
unsigned GetNextGameNum(); unsigned GetNextGameNum();
const SessionManager &GetSessionManager() const;
SessionManager &GetSessionManager();
private: private:
SessionQueue m_sessionQueue; SessionQueue m_sessionQueue;
mutable boost::mutex m_sessionQueueMutex; mutable boost::mutex m_sessionQueueMutex;
SessionManager m_sessionManager; SessionManager m_sessionManager;
PlayerDataList m_computerPlayers; PlayerDataList m_computerPlayerList;
mutable boost::mutex m_computerPlayerListMutex;
ServerLobbyThread &m_lobbyThread; ServerLobbyThread &m_lobbyThread;
std::auto_ptr<ReceiverHelper> m_receiver; std::auto_ptr<ReceiverHelper> m_receiver;
+3 -1
View File
@@ -130,8 +130,10 @@ private:
std::string m_password; std::string m_password;
ConfigFile *m_playerConfig; ConfigFile *m_playerConfig;
u_int32_t m_curUniquePlayerId;
u_int32_t m_curGameId; u_int32_t m_curGameId;
u_int32_t m_curUniquePlayerId;
mutable boost::mutex m_curUniquePlayerIdMutex;
}; };
#endif #endif