Randomize player list before starting the game.

This commit is contained in:
lotodore
2010-10-22 19:42:48 +00:00
parent c4fa3243b0
commit 1886d91009
3 changed files with 61 additions and 52 deletions
+19 -8
View File
@@ -19,6 +19,7 @@
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <algorithm>
#include <net/servergame.h> #include <net/servergame.h>
#include <net/servergamestate.h> #include <net/servergamestate.h>
@@ -231,21 +232,29 @@ ServerGame::TimerVoteKick(const boost::system::error_code &ec)
} }
} }
void PlayerDataList
ServerGame::InternalStartGame() ServerGame::InternalStartGame()
{ {
// Initialize the game.
PlayerDataList playerData(GetFullPlayerDataList());
if (playerData.size() >= 2)
{
// Set DB Backend. // Set DB Backend.
if (GetGameData().gameType == GAME_TYPE_RANKING) if (GetGameData().gameType == GAME_TYPE_RANKING)
m_database = GetLobbyThread().GetDatabase(); m_database = GetLobbyThread().GetDatabase();
else else
m_database.reset(new ServerDBNoAction); m_database.reset(new ServerDBNoAction);
// Set order of players. // Randomize player list.
AssignPlayerNumbers(); // Note: This does not use a cryptographically strong
// random number generator.
vector<boost::shared_ptr<PlayerData> > tmpData(playerData.begin(), playerData.end());
random_shuffle(tmpData.begin(), tmpData.end());
copy(tmpData.begin(), tmpData.end(), playerData.begin());
// Initialize the game. // Set order of players.
GuiInterface &gui = GetGui(); AssignPlayerNumbers(playerData);
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
@@ -276,11 +285,14 @@ ServerGame::InternalStartGame()
SetStartData(startData); SetStartData(startData);
GuiInterface &gui = GetGui();
m_game.reset(new Game(&gui, factory, playerData, GetGameData(), GetStartData(), GetNextGameNum())); m_game.reset(new Game(&gui, factory, playerData, GetGameData(), GetStartData(), GetNextGameNum()));
GetLobbyThread().NotifyStartingGame(GetId()); GetLobbyThread().NotifyStartingGame(GetId());
GetDatabase().AsyncCreateGame(GetId(), GetName()); GetDatabase().AsyncCreateGame(GetId(), GetName());
InitRankingMap(playerData); InitRankingMap(playerData);
}
return playerData;
} }
void void
@@ -829,11 +841,10 @@ ServerGame::GetCurNumberOfPlayers() const
} }
void void
ServerGame::AssignPlayerNumbers() ServerGame::AssignPlayerNumbers(PlayerDataList &playerList)
{ {
int playerNumber = 0; int playerNumber = 0;
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();
+2 -4
View File
@@ -687,21 +687,19 @@ ServerGameStateStartGame::TimerTimeout(const boost::system::error_code &ec, boos
void void
ServerGameStateStartGame::DoStart(boost::shared_ptr<ServerGame> server) ServerGameStateStartGame::DoStart(boost::shared_ptr<ServerGame> server)
{ {
PlayerDataList tmpPlayerList = server->GetFullPlayerDataList(); PlayerDataList tmpPlayerList(server->InternalStartGame());
if (tmpPlayerList.size() <= 1) if (tmpPlayerList.size() <= 1)
{ {
if (!tmpPlayerList.empty()) if (!tmpPlayerList.empty())
{ {
boost::shared_ptr<PlayerData> tmpPlayer(tmpPlayerList.front()); boost::shared_ptr<PlayerData> tmpPlayer(tmpPlayerList.front());
SessionWrapper tmpSession = server->GetSessionManager().GetSessionByUniquePlayerId(tmpPlayer->GetUniqueId()); SessionWrapper tmpSession = server->GetSessionManager().GetSessionByUniquePlayerId(tmpPlayer->GetUniqueId());
if (tmpSession.sessionData.get()) if (tmpSession.sessionData)
server->MoveSessionToLobby(tmpSession, NTF_NET_REMOVED_START_FAILED); server->MoveSessionToLobby(tmpSession, NTF_NET_REMOVED_START_FAILED);
} }
} }
else else
{ {
server->InternalStartGame();
boost::shared_ptr<NetPacket> packet(new NetPacket(NetPacket::Alloc)); boost::shared_ptr<NetPacket> packet(new NetPacket(NetPacket::Alloc));
packet->GetMsg()->present = PokerTHMessage_PR_gameStartMessage; packet->GetMsg()->present = PokerTHMessage_PR_gameStartMessage;
GameStartMessage_t *netGameStart = &packet->GetMsg()->choice.gameStartMessage; GameStartMessage_t *netGameStart = &packet->GetMsg()->choice.gameStartMessage;
+2 -2
View File
@@ -107,7 +107,7 @@ protected:
void TimerVoteKick(const boost::system::error_code &ec); void TimerVoteKick(const boost::system::error_code &ec);
void InternalStartGame(); PlayerDataList InternalStartGame();
void InitRankingMap(const PlayerDataList &playerDataList); void InitRankingMap(const PlayerDataList &playerDataList);
void UpdateRankingMap(); void UpdateRankingMap();
void SetPlayerPlace(unsigned playerId, int place); void SetPlayerPlace(unsigned playerId, int place);
@@ -134,7 +134,7 @@ protected:
void RemoveDisconnectedPlayers(); void RemoveDisconnectedPlayers();
size_t GetCurNumberOfPlayers() const; size_t GetCurNumberOfPlayers() const;
void AssignPlayerNumbers(); void AssignPlayerNumbers(PlayerDataList &playerList);
bool IsValidPlayer(unsigned playerId) const; bool IsValidPlayer(unsigned playerId) const;
ServerLobbyThread &GetLobbyThread(); ServerLobbyThread &GetLobbyThread();