Fixed player counting bug. Fixed player number. Joining/leaving network game should work now. Added player type to localplayer.

This commit is contained in:
lotodore
2007-05-08 06:45:46 +00:00
parent 5b878d9095
commit 4a10d6188c
14 changed files with 216 additions and 183 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ public:
virtual HandInterface* createHand(EngineFactory *f, GuiInterface *g, BoardInterface *b, PlayerInterface **p, int id, int sP, int aP, int dP, int sB,int sC) =0;
virtual BoardInterface* createBoard() =0;
virtual PlayerInterface* createPlayer(BoardInterface *b, int id, std::string name, std::string avatar, int sC, bool aS, int mB) =0;
virtual PlayerInterface* createPlayer(BoardInterface *b, int id, unsigned uniqueId, PlayerType type, std::string name, std::string avatar, int sC, bool aS, int mB) =0;
virtual PreflopInterface* createPreflop(HandInterface* hi, int id, int aP, int dP, int sB) =0;
virtual FlopInterface* createFlop(HandInterface* hi, int id, int aP, int dP, int sB) =0;
virtual TurnInterface* createTurn(HandInterface* hi, int id, int aP, int dP, int sB) =0;
@@ -35,7 +35,7 @@ HandInterface* LocalEngineFactory::createHand(EngineFactory *f, GuiInterface *g,
BoardInterface* LocalEngineFactory::createBoard() { return new LocalBoard; }
PlayerInterface* LocalEngineFactory::createPlayer(BoardInterface *b, int id, std::string name, std::string avatar, int sC, bool aS, int mB) { return new LocalPlayer(b, id, name, avatar, sC, aS, mB); }
PlayerInterface* LocalEngineFactory::createPlayer(BoardInterface *b, int id, unsigned uniqueId, PlayerType type, std::string name, std::string avatar, int sC, bool aS, int mB) { return new LocalPlayer(b, id, uniqueId, type, name, avatar, sC, aS, mB); }
PreflopInterface* LocalEngineFactory::createPreflop(HandInterface* hi, int id, int aP, int dP, int sB) { return new LocalPreflop(hi, id, aP, dP, sB); }
+1 -1
View File
@@ -39,7 +39,7 @@ public:
HandInterface* createHand(EngineFactory *f, GuiInterface *g, BoardInterface *b, PlayerInterface **p, int id, int sP, int aP, int dP, int sB,int sC);
BoardInterface* createBoard();
PlayerInterface* createPlayer(BoardInterface *b, int id, std::string name, std::string avatar, int sC, bool aS, int mB);
PlayerInterface* createPlayer(BoardInterface *b, int id, unsigned uniqueId, PlayerType type, std::string name, std::string avatar, int sC, bool aS, int mB);
PreflopInterface* createPreflop(HandInterface* hi, int id, int aP, int dP, int sB);
FlopInterface* createFlop(HandInterface* hi, int id, int aP, int dP, int sB);
TurnInterface* createTurn(HandInterface* hi, int id, int aP, int dP, int sB);
+2 -1
View File
@@ -23,7 +23,8 @@
using namespace std;
LocalPlayer::LocalPlayer(BoardInterface *b, int id, std::string name, std::string avatar, int sC, bool aS, int mB) : PlayerInterface(), actualHand(0), actualBoard(b), myCardsValue(0), myID(id), myName(name), myAvatar(avatar), myDude(0), myDude4(0), myCardsValueInt(0), myOdds(-1.0), myCash(sC), mySet(0), myAction(0), myButton(mB), myActiveStatus(aS), myTurn(0), myRoundStartCash(0), sBluff(0), sBluffStatus(0)
LocalPlayer::LocalPlayer(BoardInterface *b, int id, unsigned uniqueId, PlayerType type, std::string name, std::string avatar, int sC, bool aS, int mB)
: PlayerInterface(), actualHand(0), actualBoard(b), myCardsValue(0), myID(id), myUniqueID(uniqueId), myType(type), myName(name), myAvatar(avatar), myDude(0), myDude4(0), myCardsValueInt(0), myOdds(-1.0), myCash(sC), mySet(0), myAction(0), myButton(mB), myActiveStatus(aS), myTurn(0), myRoundStartCash(0), sBluff(0), sBluffStatus(0)
{
// for statistic development
+7 -3
View File
@@ -36,14 +36,16 @@ class BoardInterface;
class LocalPlayer : public PlayerInterface{
public:
LocalPlayer(BoardInterface*, int, std::string, std::string, int, bool, int);
LocalPlayer(
BoardInterface *b, int id, unsigned uniqueId, PlayerType type, std::string name, std::string avatar, int sC, bool aS, int mB);
~LocalPlayer();
~LocalPlayer();
void setHand(HandInterface*);
void setMyID(const int& theValue) { myID = theValue; }
int getMyID() const { return myID; }
unsigned getMyUniqueID() const { return myUniqueID; }
PlayerType getMyType() const { return myType; }
void setMyDude(const int& theValue) { myDude = theValue; }
int getMyDude() const { return myDude; }
@@ -147,6 +149,8 @@ private:
// Konstanten
int myID;
unsigned myUniqueID;
PlayerType myType;
std::string myName;
std::string myAvatar;
int myDude;
+1 -1
View File
@@ -95,7 +95,7 @@ void LocalPreflop::preflopRun() {
else {
// Preflop ist wirklich dran
// nhsten Spieler ermitteln
// naechsten Spieler ermitteln
do {
playersTurn = (playersTurn+1)%(MAX_NUMBER_OF_PLAYERS);
+3 -1
View File
@@ -21,6 +21,7 @@
#define PLAYERINTERFACE_H
#include "handinterface.h"
#include <playerdata.h>
class PlayerInterface{
public:
@@ -29,8 +30,9 @@ public:
virtual void setHand(HandInterface*) =0;
virtual void setMyID(const int& theValue) =0;
virtual int getMyID() const =0;
virtual unsigned getMyUniqueID() const =0;
virtual PlayerType getMyType() const =0;
virtual void setMyDude(const int& theValue) =0;
virtual int getMyDude() const =0;
+5 -1
View File
@@ -65,9 +65,13 @@ Game::Game(GuiInterface* gui, const PlayerDataList &playerDataList, const GameDa
string myName;
string myAvatarFile;
unsigned uniqueId = 0;
PlayerType type = PLAYER_TYPE_COMPUTER;
if (player_i != player_end)
{
uniqueId = (*player_i)->GetUniqueId();
type = (*player_i)->GetType();
myName = (*player_i)->GetName();
myAvatarFile = (*player_i)->GetAvatarFile();
// TODO: set player type
@@ -75,7 +79,7 @@ Game::Game(GuiInterface* gui, const PlayerDataList &playerDataList, const GameDa
}
//PlayerObjekte erzeugen
playerArray[i] = myFactory->createPlayer(actualBoard, i, myName, myAvatarFile, startCash, startQuantityPlayers > i, 0);
playerArray[i] = myFactory->createPlayer(actualBoard, i, uniqueId, type, myName, myAvatarFile, startCash, startQuantityPlayers > i, 0);
}
actualBoard->setPlayer(playerArray);
+2 -8
View File
@@ -96,7 +96,6 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
if (session->GetState() != SessionData::Init)
{
server.SessionError(session, ERR_SOCK_INVALID_STATE);
server.CloseSessionDelayed(session);
return retVal;
}
@@ -105,7 +104,6 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
if (!tmpPacket)
{
server.SessionError(session, ERR_SOCK_INVALID_PACKET);
server.CloseSessionDelayed(session);
return retVal;
}
@@ -116,7 +114,6 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
if (joinGameData.versionMajor != NET_VERSION_MAJOR)
{
server.SessionError(session, ERR_NET_VERSION_NOT_SUPPORTED);
server.CloseSessionDelayed(session);
return retVal;
}
@@ -124,7 +121,6 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
if (!server.CheckPassword(joinGameData.password))
{
server.SessionError(session, ERR_NET_INVALID_PASSWORD);
server.CloseSessionDelayed(session);
return retVal;
}
@@ -134,7 +130,6 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
if (curNumPlayers >= (size_t)server.GetGameData().numberOfPlayers)
{
server.SessionError(session, ERR_NET_SERVER_FULL);
server.CloseSessionDelayed(session);
return retVal;
}
@@ -142,14 +137,13 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
if (server.IsPlayerConnected(joinGameData.playerName))
{
server.SessionError(session, ERR_NET_PLAYER_NAME_IN_USE);
server.CloseSessionDelayed(session);
return retVal;
}
// Create player data object.
boost::shared_ptr<PlayerData> tmpPlayerData(new PlayerData(m_curUniquePlayerId++, 0));
boost::shared_ptr<PlayerData> tmpPlayerData(
new PlayerData(m_curUniquePlayerId++, server.GetNextPlayerNumber(), joinGameData.ptype));
tmpPlayerData->SetName(joinGameData.playerName);
tmpPlayerData->SetPlayerType(joinGameData.ptype);
// Send ACK to client.
boost::shared_ptr<NetPacket> answer(new NetPacketJoinGameAck);
+176 -150
View File
@@ -26,6 +26,8 @@
#include <net/socket_msg.h>
#include <gamedata.h>
#include <boost/lambda/lambda.hpp>
#define SERVER_CLOSE_SESSION_DELAY_SEC 10
using namespace std;
@@ -70,79 +72,6 @@ ServerRecvThread::Init(const string &pwd, const GameData &gameData)
*m_gameData = gameData;
}
void
ServerRecvThread::SendError(SOCKET s, int errorCode)
{
boost::shared_ptr<NetPacket> packet(new NetPacketError);
NetPacketError::Data errorData;
errorData.errorCode = errorCode;
static_cast<NetPacketError *>(packet.get())->SetData(errorData);
GetSender().Send(s, packet);
}
void
ServerRecvThread::SendToAllPlayers(boost::shared_ptr<NetPacket> packet)
{
// This function needs to be thread safe.
boost::mutex::scoped_lock lock(m_sessionMapMutex);
SocketSessionMap::iterator i = m_sessionMap.begin();
SocketSessionMap::iterator end = m_sessionMap.end();
while (i != end)
{
// Send each client a copy of the packet.
GetSender().Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
++i;
}
}
void
ServerRecvThread::SendToAllButOnePlayers(boost::shared_ptr<NetPacket> packet, SOCKET except)
{
// This function needs to be thread safe.
boost::mutex::scoped_lock lock(m_sessionMapMutex);
SocketSessionMap::iterator i = m_sessionMap.begin();
SocketSessionMap::iterator end = m_sessionMap.end();
while (i != end)
{
// Send each client but one a copy of the packet.
if (i->first != except)
GetSender().Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
++i;
}
}
void
ServerRecvThread::CloseSessionDelayed(boost::shared_ptr<SessionData> sessionData)
{
{
boost::mutex::scoped_lock lock(m_sessionMapMutex);
m_sessionMap.erase(sessionData->GetSocket());
}
boost::shared_ptr<PlayerData> tmpPlayerData = sessionData->GetPlayerData();
if (tmpPlayerData.get() && !tmpPlayerData->GetName().empty())
{
GetCallback().SignalNetServerPlayerLeft(tmpPlayerData->GetName());
// Send "Player Left" to clients.
boost::shared_ptr<NetPacket> thisPlayerLeft(new NetPacketPlayerLeft);
NetPacketPlayerLeft::Data thisPlayerLeftData;
thisPlayerLeftData.playerId = tmpPlayerData->GetUniqueId();
static_cast<NetPacketPlayerLeft *>(thisPlayerLeft.get())->SetData(thisPlayerLeftData);
SendToAllPlayers(thisPlayerLeft);
}
boost::microsec_timer closeTimer;
closeTimer.start();
CloseSessionList::value_type closeSessionData(closeTimer, sessionData);
m_closeSessionList.push_back(closeSessionData);
}
void
ServerRecvThread::AddConnection(boost::shared_ptr<ConnectData> data)
{
@@ -157,12 +86,6 @@ ServerRecvThread::AddNotification(unsigned notification)
m_notificationQueue.push_back(notification);
}
ServerCallback &
ServerRecvThread::GetCallback()
{
return m_callback;
}
void
ServerRecvThread::Main()
{
@@ -318,19 +241,6 @@ ServerRecvThread::CleanupSessionMap()
m_sessionMap.clear();
}
ServerRecvState &
ServerRecvThread::GetState()
{
assert(m_curState);
return *m_curState;
}
void
ServerRecvThread::SetState(ServerRecvState &newState)
{
m_curState = &newState;
}
boost::shared_ptr<SessionData>
ServerRecvThread::GetSession(SOCKET sock)
{
@@ -366,6 +276,180 @@ ServerRecvThread::SessionError(boost::shared_ptr<SessionData> sessionData, int e
{
assert(sessionData.get());
SendError(sessionData->GetSocket(), errorCode);
CloseSessionDelayed(sessionData);
}
void
ServerRecvThread::CloseSessionDelayed(boost::shared_ptr<SessionData> sessionData)
{
{
boost::mutex::scoped_lock lock(m_sessionMapMutex);
m_sessionMap.erase(sessionData->GetSocket());
}
boost::shared_ptr<PlayerData> tmpPlayerData = sessionData->GetPlayerData();
if (tmpPlayerData.get() && !tmpPlayerData->GetName().empty())
{
GetCallback().SignalNetServerPlayerLeft(tmpPlayerData->GetName());
// Send "Player Left" to clients.
boost::shared_ptr<NetPacket> thisPlayerLeft(new NetPacketPlayerLeft);
NetPacketPlayerLeft::Data thisPlayerLeftData;
thisPlayerLeftData.playerId = tmpPlayerData->GetUniqueId();
static_cast<NetPacketPlayerLeft *>(thisPlayerLeft.get())->SetData(thisPlayerLeftData);
SendToAllPlayers(thisPlayerLeft);
}
boost::microsec_timer closeTimer;
closeTimer.start();
CloseSessionList::value_type closeSessionData(closeTimer, sessionData);
m_closeSessionList.push_back(closeSessionData);
}
size_t
ServerRecvThread::GetCurNumberOfPlayers() const
{
PlayerDataList playerList = GetPlayerDataList();
return playerList.size();
}
bool
ServerRecvThread::IsPlayerConnected(const std::string &playerName) const
{
bool retVal = false;
PlayerDataList playerList = GetPlayerDataList();
PlayerDataList::const_iterator player_i = playerList.begin();
PlayerDataList::const_iterator player_end = playerList.end();
while (player_i != player_end)
{
if ((*player_i)->GetName() == playerName)
{
retVal = true;
break;
}
++player_i;
}
return retVal;
}
void
ServerRecvThread::SetSessionPlayerData(boost::shared_ptr<SessionData> sessionData, boost::shared_ptr<PlayerData> playerData)
{
sessionData->SetPlayerData(playerData);
// Signal joining player to GUI.
if (playerData.get() && !playerData->GetName().empty())
GetCallback().SignalNetServerPlayerJoined(playerData->GetName());
}
PlayerDataList
ServerRecvThread::GetPlayerDataList() const
{
PlayerDataList playerList;
boost::mutex::scoped_lock lock(m_sessionMapMutex);
SocketSessionMap::const_iterator session_i = m_sessionMap.begin();
SocketSessionMap::const_iterator session_end = m_sessionMap.end();
while (session_i != session_end)
{
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second->GetPlayerData());
if (tmpPlayer.get() && !tmpPlayer->GetName().empty())
playerList.push_back(tmpPlayer);
++session_i;
}
// Sort the list by player number.
playerList.sort(*boost::lambda::_1 < *boost::lambda::_2);
return playerList;
}
int
ServerRecvThread::GetNextPlayerNumber() const
{
int playerNumber = 0;
PlayerDataList playerList = GetPlayerDataList();
PlayerDataList::const_iterator player_i = playerList.begin();
PlayerDataList::const_iterator player_end = playerList.end();
// Assume the player list is sorted by player number.
while (player_i != player_end)
{
if ((*player_i)->GetNumber() == playerNumber)
playerNumber++;
else
break;
++player_i;
}
return playerNumber;
}
void
ServerRecvThread::SendError(SOCKET s, int errorCode)
{
boost::shared_ptr<NetPacket> packet(new NetPacketError);
NetPacketError::Data errorData;
errorData.errorCode = errorCode;
static_cast<NetPacketError *>(packet.get())->SetData(errorData);
GetSender().Send(s, packet);
}
void
ServerRecvThread::SendToAllPlayers(boost::shared_ptr<NetPacket> packet)
{
// This function needs to be thread safe.
boost::mutex::scoped_lock lock(m_sessionMapMutex);
SocketSessionMap::iterator i = m_sessionMap.begin();
SocketSessionMap::iterator end = m_sessionMap.end();
while (i != end)
{
// Send each client a copy of the packet.
GetSender().Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
++i;
}
}
void
ServerRecvThread::SendToAllButOnePlayers(boost::shared_ptr<NetPacket> packet, SOCKET except)
{
// This function needs to be thread safe.
boost::mutex::scoped_lock lock(m_sessionMapMutex);
SocketSessionMap::iterator i = m_sessionMap.begin();
SocketSessionMap::iterator end = m_sessionMap.end();
while (i != end)
{
// Send each client but one a copy of the packet.
if (i->first != except)
GetSender().Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
++i;
}
}
ServerCallback &
ServerRecvThread::GetCallback()
{
return m_callback;
}
ServerRecvState &
ServerRecvThread::GetState()
{
assert(m_curState);
return *m_curState;
}
void
ServerRecvThread::SetState(ServerRecvState &newState)
{
m_curState = &newState;
}
SenderThread &
@@ -395,64 +479,6 @@ ServerRecvThread::CheckPassword(const std::string &password) const
return (password == m_password);
}
size_t
ServerRecvThread::GetCurNumberOfPlayers() const
{
boost::mutex::scoped_lock lock(m_sessionMapMutex);
return m_sessionMap.size();
}
bool
ServerRecvThread::IsPlayerConnected(const std::string &playerName) const
{
bool retVal = false;
boost::mutex::scoped_lock lock(m_sessionMapMutex);
SocketSessionMap::const_iterator session_i = m_sessionMap.begin();
SocketSessionMap::const_iterator session_end = m_sessionMap.end();
while (session_i != session_end)
{
const boost::shared_ptr<PlayerData> tmpPlayerData = session_i->second->GetPlayerData();
if (tmpPlayerData.get() && tmpPlayerData->GetName() == playerName)
{
retVal = true;
break;
}
++session_i;
}
return retVal;
}
void
ServerRecvThread::SetSessionPlayerData(boost::shared_ptr<SessionData> sessionData, boost::shared_ptr<PlayerData> playerData)
{
boost::mutex::scoped_lock lock(m_sessionMapMutex); // Paranoia
sessionData->SetPlayerData(playerData);
// Signal joining player to GUI.
GetCallback().SignalNetServerPlayerJoined(playerData->GetName());
}
PlayerDataList
ServerRecvThread::GetPlayerDataList() const
{
PlayerDataList playerList;
boost::mutex::scoped_lock lock(m_sessionMapMutex);
SocketSessionMap::const_iterator session_i = m_sessionMap.begin();
SocketSessionMap::const_iterator session_end = m_sessionMap.end();
while (session_i != session_end)
{
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second->GetPlayerData());
if (tmpPlayer.get() && !tmpPlayer->GetName().empty())
playerList.push_back(tmpPlayer);
++session_i;
}
return playerList;
}
ServerSenderCallback &
ServerRecvThread::GetSenderCallback()
{
+10 -9
View File
@@ -76,18 +76,24 @@ protected:
void CleanupConnectQueue();
void CleanupSessionMap();
ServerRecvState &GetState();
void SetState(ServerRecvState &newState);
boost::shared_ptr<SessionData> GetSession(SOCKET sock);
void AddSession(boost::shared_ptr<SessionData> sessionData);
void SessionError(boost::shared_ptr<SessionData> sessionData, int errorCode);
void CloseSessionDelayed(boost::shared_ptr<SessionData> sessionData);
size_t GetCurNumberOfPlayers() const;
bool IsPlayerConnected(const std::string &playerName) const;
void SetSessionPlayerData(boost::shared_ptr<SessionData> sessionData, boost::shared_ptr<PlayerData> playerData);
PlayerDataList GetPlayerDataList() const;
int GetNextPlayerNumber() const;
void SendError(SOCKET s, int errorCode);
void SendToAllPlayers(boost::shared_ptr<NetPacket> packet);
void SendToAllButOnePlayers(boost::shared_ptr<NetPacket> packet, SOCKET except);
void CloseSessionDelayed(boost::shared_ptr<SessionData> sessionData);
ServerRecvState &GetState();
void SetState(ServerRecvState &newState);
SenderThread &GetSender();
ReceiverHelper &GetReceiver();
@@ -95,11 +101,6 @@ protected:
const GameData &GetGameData() const;
bool CheckPassword(const std::string &password) const;
size_t GetCurNumberOfPlayers() const;
bool IsPlayerConnected(const std::string &playerName) const;
void SetSessionPlayerData(boost::shared_ptr<SessionData> sessionData, boost::shared_ptr<PlayerData> playerData);
PlayerDataList GetPlayerDataList() const;
ServerSenderCallback &GetSenderCallback();
private:
+2 -2
View File
@@ -19,8 +19,8 @@
#include <playerdata.h>
PlayerData::PlayerData(unsigned uniqueId, int number)
: m_uniqueId(uniqueId), m_number(number), m_type(PLAYER_TYPE_COMPUTER)
PlayerData::PlayerData(unsigned uniqueId, int number, PlayerType type)
: m_uniqueId(uniqueId), m_number(number), m_type(type)
{
}
+4 -3
View File
@@ -35,7 +35,7 @@ enum PlayerType
class PlayerData
{
public:
PlayerData(unsigned uniqueId, int number);
PlayerData(unsigned uniqueId, int number, PlayerType type);
~PlayerData();
const std::string &GetName() const
@@ -48,13 +48,14 @@ public:
{m_avatarFile = avatarFile;}
PlayerType GetType() const
{return m_type;}
void SetPlayerType(PlayerType type)
{m_type = type;}
unsigned GetUniqueId() const
{return m_uniqueId;}
int GetNumber() const
{return m_number;}
bool operator<(const PlayerData &other)
{return m_number < other.GetNumber();}
private:
unsigned m_uniqueId;
int m_number;
+1 -1
View File
@@ -69,7 +69,7 @@ void Session::startGame(const GameData &gameData) {
//PlayerData erzeugen
// TODO: PlayerType setzen
// UniqueId = PlayerNumber for local games.
boost::shared_ptr<PlayerData> playerData(new PlayerData(i, i));
boost::shared_ptr<PlayerData> playerData(new PlayerData(i, i, i == 0 ? PLAYER_TYPE_HUMAN : PLAYER_TYPE_COMPUTER));
playerData->SetName(myConfig->readConfigString(myName.str()));
playerData->SetAvatarFile(myConfig->readConfigString(myAvatar.str()));