diff --git a/docs/net_protocol.txt b/docs/net_protocol.txt index 3c98204a..dcfdcba5 100644 --- a/docs/net_protocol.txt +++ b/docs/net_protocol.txt @@ -147,3 +147,5 @@ Server Notification: Player's Turn +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +2007 by Lothar May diff --git a/pokerth.pro b/pokerth.pro index b927d01f..2fc634c6 100755 --- a/pokerth.pro +++ b/pokerth.pro @@ -55,6 +55,7 @@ INCLUDEPATH += . \ HEADERS += src/game.h \ src/session.h \ src/playerdata.h \ + src/gamedata.h \ src/config/configfile.h \ src/core/rand.h \ src/core/thread.h \ diff --git a/src/game.cpp b/src/game.cpp index 6b9d827c..52ad92a6 100755 --- a/src/game.cpp +++ b/src/game.cpp @@ -18,6 +18,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "game.h" +#include "gamedata.h" #include "enginefactory.h" #include "localenginefactory.h" @@ -29,14 +30,15 @@ using namespace std; -Game::Game(GuiInterface* gui, const PlayerDataList &playerData, int sC, int sB, int hbrsB, int gameId) -: myGui(gui), actualHand(0), actualBoard(0), startCash(sC), - startSmallBlind(sB), startHandsBeforeRaiseSmallBlind(hbrsB),myGameID(gameId), - actualSmallBlind(sB), actualHandID(0), dealerPosition(0) +Game::Game(GuiInterface* gui, const PlayerDataList &playerDataList, const GameData &gameData, int gameId) +: myGui(gui), actualHand(0), actualBoard(0), startQuantityPlayers(gameData.numberOfPlayers), + startCash(gameData.startCash), startSmallBlind(gameData.smallBlind), + startHandsBeforeRaiseSmallBlind(gameData.handsBeforeRaise), + myGameID(gameId), actualQuantityPlayers(gameData.numberOfPlayers), + actualSmallBlind(gameData.smallBlind), actualHandID(0), dealerPosition(0) { // cout << "Create Game Object" << "\n"; int i; - startQuantityPlayers = actualQuantityPlayers = playerData.size(); actualHandID = 0; @@ -59,8 +61,8 @@ Game::Game(GuiInterface* gui, const PlayerDataList &playerData, int sC, int sB, myTool.getRandNumber(0, startQuantityPlayers-1, 1, &dealerPosition, 0); // Player erstellen - PlayerDataList::const_iterator player_i = playerData.begin(); - PlayerDataList::const_iterator player_end = playerData.end(); + PlayerDataList::const_iterator player_i = playerDataList.begin(); + PlayerDataList::const_iterator player_end = playerDataList.end(); for(i=0; ispinBox_gameSpeed->value(); setSpeeds(); //positioning Slider horizontalSlider_speed->setValue(guiGameSpeed); - //Start Game!!! - mySession->startGame(v->spinBox_quantityPlayers->value(), v->spinBox_startCash->value(), v->spinBox_smallBlind->value(), v->spinBox_handsBeforeRaiseSmallBlind->value()); + // Set Game Data + gameData.numberOfPlayers = v->spinBox_quantityPlayers->value(); + gameData.startCash = v->spinBox_startCash->value(); + gameData.smallBlind = v->spinBox_smallBlind->value(); + gameData.handsBeforeRaise = v->spinBox_handsBeforeRaiseSmallBlind->value(); } // start with default values else { @@ -617,9 +621,14 @@ void mainWindowImpl::startNewLocalGame(newGameDialogImpl *v) { setSpeeds(); //positioning Slider horizontalSlider_speed->setValue(guiGameSpeed); - //Start Game!!! - mySession->startGame(myConfig->readConfigInt("NumberOfPlayers"), myConfig->readConfigInt("StartCash"), myConfig->readConfigInt("SmallBlind"), myConfig->readConfigInt("HandsBeforeRaiseSmallBlind")); + // Set Game Data + gameData.numberOfPlayers = myConfig->readConfigInt("NumberOfPlayers"); + gameData.startCash = myConfig->readConfigInt("StartCash"); + gameData.smallBlind = myConfig->readConfigInt("SmallBlind"); + gameData.handsBeforeRaise = myConfig->readConfigInt("HandsBeforeRaiseSmallBlind"); } + //Start Game!!! + mySession->startGame(gameData); } @@ -638,7 +647,13 @@ void mainWindowImpl::callCreateNetworkGameDialog() { mySession->terminateNetworkClient(); mySession->terminateNetworkServer(); - mySession->startNetworkServer(); + GameData gameData; + gameData.numberOfPlayers = myCreateNetworkGameDialog->spinBox_quantityPlayers->value(); + gameData.startCash = myCreateNetworkGameDialog->spinBox_startCash->value(); + gameData.smallBlind = myCreateNetworkGameDialog->spinBox_smallBlind->value(); + gameData.handsBeforeRaise = myCreateNetworkGameDialog->spinBox_handsBeforeRaiseSmallBlind->value(); + + mySession->startNetworkServer(gameData); mySession->startNetworkClientForLocalServer(); myStartNetworkGameDialog->exec(); diff --git a/src/net/common/serverrecvstate.cpp b/src/net/common/serverrecvstate.cpp index 543288d7..9408e561 100644 --- a/src/net/common/serverrecvstate.cpp +++ b/src/net/common/serverrecvstate.cpp @@ -81,6 +81,9 @@ ServerRecvStateInit::Process(ServerRecvThread &server) tmpPacket->GetData(playerData); server.GetCallback().SignalNetServerPlayerJoined(playerData.playerName); boost::shared_ptr answer(new NetPacketJoinGameAck); + NetPacketJoinGameAck::Data joinGameAckData; + // TODO: set data. + static_cast(answer.get())->SetData(joinGameAckData); server.GetSender().Send(answer, recvSock); session->SetState(SessionData::Established); } diff --git a/src/net/common/serverrecvthread.cpp b/src/net/common/serverrecvthread.cpp index 7c3c58e8..3eab0806 100644 --- a/src/net/common/serverrecvthread.cpp +++ b/src/net/common/serverrecvthread.cpp @@ -24,6 +24,9 @@ #include #include #include +#include + +using namespace std; class ServerSenderCallback : public SenderCallback { @@ -47,6 +50,7 @@ ServerRecvThread::ServerRecvThread(ServerCallback &cb) m_senderCallback.reset(new ServerSenderCallback(*this)); m_sender.reset(new SenderThread(GetSenderCallback())); m_receiver.reset(new ReceiverHelper); + m_gameData.reset(new GameData); } ServerRecvThread::~ServerRecvThread() @@ -55,6 +59,13 @@ ServerRecvThread::~ServerRecvThread() CleanupSessionMap(); } +void +ServerRecvThread::Init(const string &pwd, const GameData &gameData) +{ + m_password = pwd; + *m_gameData = gameData; +} + void ServerRecvThread::SendToAllPlayers(boost::shared_ptr packet) { @@ -297,6 +308,19 @@ ServerRecvThread::GetReceiver() return *m_receiver; } +const GameData & +ServerRecvThread::GetGameData() +{ + assert(m_gameData.get()); + return *m_gameData; +} + +bool +ServerRecvThread::CheckPassword(const std::string &password) +{ + return (password == m_password); +} + ServerSenderCallback & ServerRecvThread::GetSenderCallback() { diff --git a/src/net/common/serverthread.cpp b/src/net/common/serverthread.cpp index 932eb252..974a3f1c 100644 --- a/src/net/common/serverthread.cpp +++ b/src/net/common/serverthread.cpp @@ -41,7 +41,8 @@ ServerThread::~ServerThread() } void -ServerThread::Init(unsigned serverPort, bool ipv6, const std::string &pwd) +ServerThread::Init(unsigned serverPort, bool ipv6, const std::string &pwd, + const GameData &gameData) { if (IsRunning()) return; // TODO: throw exception @@ -50,7 +51,8 @@ ServerThread::Init(unsigned serverPort, bool ipv6, const std::string &pwd) context.SetAddrFamily(ipv6 ? AF_INET6 : AF_INET); context.SetServerPort(serverPort); - context.SetPassword(pwd); + + GetRecvThread().Init(pwd, gameData); } void diff --git a/src/net/servercontext.h b/src/net/servercontext.h index f79ef1da..2d9d612b 100644 --- a/src/net/servercontext.h +++ b/src/net/servercontext.h @@ -43,10 +43,6 @@ public: {return m_serverPort;} void SetServerPort(unsigned serverPort) {m_serverPort = serverPort;} - const std::string &GetPassword() const - {return m_password;} - void SetPassword(const std::string &password) - {m_password = password;} const sockaddr_storage *GetServerSockaddr() const {return &m_serverSockaddr;} sockaddr_storage *GetServerSockaddr() @@ -59,7 +55,6 @@ private: SOCKET m_sockfd; int m_addrFamily; unsigned m_serverPort; - std::string m_password; sockaddr_storage m_serverSockaddr; }; diff --git a/src/net/serverrecvthread.h b/src/net/serverrecvthread.h index f9a45318..81901f9f 100644 --- a/src/net/serverrecvthread.h +++ b/src/net/serverrecvthread.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -40,6 +41,7 @@ class SenderThread; class ReceiverHelper; class ServerSenderCallback; class NetPacket; +struct GameData; class ServerRecvThread : public Thread { @@ -47,6 +49,8 @@ public: ServerRecvThread(ServerCallback &gui); virtual ~ServerRecvThread(); + void Init(const std::string &pwd, const GameData &gameData); + void SendToAllPlayers(boost::shared_ptr packet); void AddConnection(boost::shared_ptr data); void AddNotification(unsigned notification); @@ -78,6 +82,9 @@ protected: SenderThread &GetSender(); ReceiverHelper &GetReceiver(); + const GameData &GetGameData(); + bool CheckPassword(const std::string &password); + ServerSenderCallback &GetSenderCallback(); private: @@ -96,6 +103,9 @@ private: std::auto_ptr m_sender; std::auto_ptr m_senderCallback; + std::auto_ptr m_gameData; + + std::string m_password; ServerCallback &m_callback; diff --git a/src/net/serverthread.h b/src/net/serverthread.h index bc0d0406..459aaa2e 100644 --- a/src/net/serverthread.h +++ b/src/net/serverthread.h @@ -30,6 +30,7 @@ class ServerContext; class ServerRecvThread; class ServerSenderCallback; class SenderThread; +struct GameData; class ServerThread : public Thread { @@ -38,7 +39,8 @@ public: virtual ~ServerThread(); // Set the parameters. - void Init(unsigned serverPort, bool ipv6, const std::string &pwd); + void Init(unsigned serverPort, bool ipv6, const std::string &pwd, + const GameData &gameData); void StartGame(); ServerCallback &GetCallback(); diff --git a/src/playerdata.cpp b/src/playerdata.cpp index 9a01f7a1..79af9620 100644 --- a/src/playerdata.cpp +++ b/src/playerdata.cpp @@ -19,8 +19,8 @@ #include -PlayerData::PlayerData() -: m_type(PLAYER_TYPE_COMPUTER) +PlayerData::PlayerData(unsigned uniqueId) +: m_uniqueId(uniqueId), m_type(PLAYER_TYPE_COMPUTER) { } diff --git a/src/playerdata.h b/src/playerdata.h index 0c7d7d56..0f384e9a 100644 --- a/src/playerdata.h +++ b/src/playerdata.h @@ -34,7 +34,7 @@ enum PlayerType class PlayerData { public: - PlayerData(); + PlayerData(unsigned uniqueId); ~PlayerData(); const std::string &GetName() const @@ -49,8 +49,11 @@ public: {return m_type;} void SetPlayerType(PlayerType type) {m_type = type;} + unsigned GetUniqueId() const + {return m_uniqueId;} private: + unsigned m_uniqueId; std::string m_name; std::string m_avatarFile; PlayerType m_type; diff --git a/src/session.cpp b/src/session.cpp index 5e8f682d..d70cf343 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -46,12 +46,12 @@ Session::~Session() } -void Session::startGame(int qP, int sC, int sB, int hbrsB) { +void Session::startGame(const GameData &gameData) { currentGameID++; PlayerDataList playerDataList; - for(int i=0; i playerData(new PlayerData); + // UniqueId = PlayerNumber for local games. + boost::shared_ptr playerData(new PlayerData(i)); playerData->SetName(myConfig->readConfigString(myName.str())); playerData->SetAvatarFile(myConfig->readConfigString(myAvatar.str())); @@ -71,7 +72,7 @@ void Session::startGame(int qP, int sC, int sB, int hbrsB) { } - actualGame = new Game(myGui, playerDataList, sC, sB, hbrsB, currentGameID); + actualGame = new Game(myGui, playerDataList, gameData, currentGameID); } void Session::deleteGame() { @@ -123,7 +124,7 @@ void Session::terminateNetworkClient() myNetClient = 0; } -void Session::startNetworkServer() +void Session::startNetworkServer(const GameData &gameData) { if (myNetServer) return; // TODO: throw exception @@ -131,7 +132,8 @@ void Session::startNetworkServer() myNetServer->Init( myConfig->readConfigInt("ServerPort"), myConfig->readConfigInt("ServerUseIpv6") == 1, - myConfig->readConfigString("ServerPassword")); + myConfig->readConfigString("ServerPassword"), + gameData); myNetServer->Run(); } diff --git a/src/session.h b/src/session.h index f956ed5f..5be1e93a 100755 --- a/src/session.h +++ b/src/session.h @@ -19,6 +19,7 @@ ***************************************************************************/ #ifndef STDSESSION_H #define STDSESSION_H +#include "gamedata.h" #include class GuiInterface; @@ -30,18 +31,18 @@ class ServerThread; class Session{ public: - Session(GuiInterface*); + Session(GuiInterface*); - ~Session(); + ~Session(); - void startGame(int qP, int sC, int sB, int hbrsB); + void startGame(const GameData &gameData); void deleteGame(); void startNetworkClient(const std::string &serverAddress, unsigned serverPort, bool ipv6, const std::string &pwd); void startNetworkClientForLocalServer(); void terminateNetworkClient(); - void startNetworkServer(); + void startNetworkServer(const GameData &gameData); void initiateNetworkServerGame(); void terminateNetworkServer();