From 73a511706633ead8085a206c7a0b50c0322f1e3c Mon Sep 17 00:00:00 2001 From: lotodore Date: Fri, 27 Apr 2007 21:35:18 +0000 Subject: [PATCH] Configuration is transferred. A new game should be started on client side, but signal/slot is not working yet. Oh well. --- docs/net_protocol.txt | 2 +- pokerth.pro | 2 + src/gui/qt/gamedatawrapper.cpp | 45 ++++++ src/gui/qt/gamedatawrapper.h | 43 +++++ src/gui/qt/guiwrapper.cpp | 198 ++++++++++++----------- src/gui/qt/guiwrapper.h | 2 + src/gui/qt/mainwindow/mainwindowimpl.cpp | 6 + src/gui/qt/mainwindow/mainwindowimpl.h | 3 + src/net/clientcallback.h | 4 + src/net/clientthread.h | 7 + src/net/common/clientstate.cpp | 5 + src/net/common/clientthread.cpp | 21 ++- src/net/common/netpacket.cpp | 26 ++- src/net/common/serverrecvstate.cpp | 61 +++++-- src/net/common/serverrecvthread.cpp | 10 +- src/net/netpacket.h | 7 +- src/net/serverrecvstate.h | 6 +- src/net/serverrecvthread.h | 8 +- src/playerdata.h | 2 + 19 files changed, 324 insertions(+), 134 deletions(-) create mode 100644 src/gui/qt/gamedatawrapper.cpp create mode 100644 src/gui/qt/gamedatawrapper.h diff --git a/docs/net_protocol.txt b/docs/net_protocol.txt index dcfdcba5..b965357e 100644 --- a/docs/net_protocol.txt +++ b/docs/net_protocol.txt @@ -62,7 +62,7 @@ Server Reply: Join Game ACK +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Number of Players | Small Blind | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Hands before raise | Game Speed | + | Hands before raise | Reserved | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Start Cash | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ diff --git a/pokerth.pro b/pokerth.pro index 2fc634c6..480240dc 100755 --- a/pokerth.pro +++ b/pokerth.pro @@ -97,6 +97,7 @@ HEADERS += src/game.h \ src/gui/qt/mainwindow/mycardspixmaplabel.h \ src/gui/qt/mainwindow/startsplash/startsplash.h \ src/gui/qt/guiwrapper.h \ + src/gui/qt/gamedatawrapper.h \ src/gui/qt/aboutpokerth/aboutpokerthimpl.h \ src/gui/qt/connecttoserverdialog/connecttoserverdialogimpl.h \ src/gui/qt/createnetworkgamedialog/createnetworkgamedialogimpl.h \ @@ -169,6 +170,7 @@ SOURCES += src/game.cpp \ src/net/common/netexception.cpp \ src/net/common/receiverhelper.cpp \ src/gui/qt/guiwrapper.cpp \ + src/gui/qt/gamedatawrapper.cpp \ src/gui/qt/mainwindow/mainwindowimpl.cpp \ src/gui/qt/mainwindow/mycardspixmaplabel.cpp \ src/gui/qt/mainwindow/startsplash/startsplash.cpp \ diff --git a/src/gui/qt/gamedatawrapper.cpp b/src/gui/qt/gamedatawrapper.cpp new file mode 100644 index 00000000..e054efd3 --- /dev/null +++ b/src/gui/qt/gamedatawrapper.cpp @@ -0,0 +1,45 @@ +/*************************************************************************** + * Copyright (C) 2007 by Lothar May * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "gamedatawrapper.h" + +GameDataWrapper::GameDataWrapper(const GameData &gameData) +: m_gameData(gameData) +{ +} + +GameDataWrapper::GameDataWrapper(const GameDataWrapper &other) +: m_gameData(other.GetGameData()) +{ +} + +const GameData& +GameDataWrapper::GetGameData() const +{ + return m_gameData; +} + +const GameDataWrapper & +GameDataWrapper::operator=(const GameDataWrapper &other) +{ + if (this != &other) + m_gameData = other.GetGameData(); + return *this; +} + diff --git a/src/gui/qt/gamedatawrapper.h b/src/gui/qt/gamedatawrapper.h new file mode 100644 index 00000000..90e920ca --- /dev/null +++ b/src/gui/qt/gamedatawrapper.h @@ -0,0 +1,43 @@ +/*************************************************************************** + * Copyright (C) 2007 by Lothar May * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef GAMEDATAWRAPPER_H +#define GAMEDATAWRAPPER_H + +#include +#include + + +class GameDataWrapper : public QObject +{ +Q_OBJECT + +public: + GameDataWrapper(const GameData &gameData); + GameDataWrapper(const GameDataWrapper &other); + + const GameData &GetGameData() const; + + const GameDataWrapper &operator=(const GameDataWrapper &other); + +private: + GameData m_gameData; +}; + +#endif diff --git a/src/gui/qt/guiwrapper.cpp b/src/gui/qt/guiwrapper.cpp index 2165e602..9508f7d3 100644 --- a/src/gui/qt/guiwrapper.cpp +++ b/src/gui/qt/guiwrapper.cpp @@ -1,98 +1,100 @@ -/*************************************************************************** - * Copyright (C) 2006 by FThauer FHammer * - * f.thauer@web.de * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - -#include "guiwrapper.h" - - -using namespace std; - - -GuiWrapper::GuiWrapper() : myLog(0), myW(0) -{ - - - myW = new mainWindowImpl; - myW->show(); - - myLog = new Log(myW); - myConfig = new ConfigFile; - -} - - -GuiWrapper::~GuiWrapper() -{ -} - -void GuiWrapper::setGame(Game *g) { myW->setGame(g); } -void GuiWrapper::setHand(HandInterface *lh) { myW->setHand(lh); } -void GuiWrapper::setSession(Session *s) { myW->setSession(s); } - -void GuiWrapper::refreshSet() const { myW->refreshSet(); } -void GuiWrapper::refreshCash() const { myW->refreshCash(); } -void GuiWrapper::refreshAction(int playerID, int playerAction) const { myW->refreshAction(playerID, playerAction); } -void GuiWrapper::refreshChangePlayer() const { myW->refreshChangePlayer(); } -void GuiWrapper::refreshAll() const { myW->refreshAll(); } -void GuiWrapper::refreshPot() const { myW->refreshPot(); } -void GuiWrapper::refreshGroupbox(int playerID, int status) const { myW->refreshGroupbox(playerID, status); } -void GuiWrapper::refreshPlayerName() const { myW->refreshPlayerName(); } - -void GuiWrapper::dealHoleCards() const { myW->dealHoleCards(); } -void GuiWrapper::dealFlopCards() const { myW->dealFlopCards0(); } -void GuiWrapper::dealTurnCard() const { myW->dealTurnCards0(); } -void GuiWrapper::dealRiverCard() const { myW->dealRiverCards0(); } - -void GuiWrapper::highlightRoundLabel(string round) const { myW->highlightRoundLabel(round); } - - -void GuiWrapper::nextPlayerAnimation() const { myW->nextPlayerAnimation(); } - -void GuiWrapper::preflopAnimation1() const { myW->preflopAnimation1(); } -void GuiWrapper::preflopAnimation2() const { myW->preflopAnimation2(); } - -void GuiWrapper::flopAnimation1() const { myW->flopAnimation1(); } -void GuiWrapper::flopAnimation2() const { myW->flopAnimation2(); } - -void GuiWrapper::turnAnimation1() const { myW->turnAnimation1(); } -void GuiWrapper::turnAnimation2() const { myW->turnAnimation2(); } - -void GuiWrapper::riverAnimation1() const { myW->riverAnimation1(); } -void GuiWrapper::riverAnimation2() const { myW->riverAnimation2(); } - -void GuiWrapper::postRiverAnimation1() const { myW->postRiverAnimation1(); } -void GuiWrapper::postRiverRunAnimation1() const { myW->postRiverRunAnimation1(); } - -void GuiWrapper::flipHolecardsAllIn() const { myW->flipHolecardsAllIn(); } - -void GuiWrapper::nextRoundCleanGui() const { myW->nextRoundCleanGui(); } - -void GuiWrapper::meInAction() const { myW->meInAction(); } - -void GuiWrapper::logPlayerActionMsg(string playerName, int action, int setValue) { myLog->logPlayerActionMsg(playerName, action, setValue); } -void GuiWrapper::logNewGameHandMsg(int gameID, int handID) { myLog->logNewGameHandMsg(gameID, handID); } - -void GuiWrapper::SignalNetClientConnect(int actionID) { myW->SignalNetClientConnect(actionID); } -void GuiWrapper::SignalNetClientGameInfo(int actionID) { myW->SignalNetClientGameInfo(actionID); } -void GuiWrapper::SignalNetClientError(int errorID, int osErrorID) { myW->SignalNetClientError(errorID, osErrorID); } - -void GuiWrapper::SignalNetServerSuccess(int actionID) { } -void GuiWrapper::SignalNetServerError(int errorID, int osErrorID) { } -void GuiWrapper::SignalNetServerPlayerJoined(const string &playerName) { myW->SignalNetServerPlayerJoined(QString::fromUtf8(playerName.c_str())); } - +/*************************************************************************** + * Copyright (C) 2006 by FThauer FHammer * + * f.thauer@web.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "guiwrapper.h" + + +using namespace std; + + +GuiWrapper::GuiWrapper() : myLog(0), myW(0) +{ + + + myW = new mainWindowImpl; + myW->show(); + + myLog = new Log(myW); + myConfig = new ConfigFile; + +} + + +GuiWrapper::~GuiWrapper() +{ +} + +void GuiWrapper::setGame(Game *g) { myW->setGame(g); } +void GuiWrapper::setHand(HandInterface *lh) { myW->setHand(lh); } +void GuiWrapper::setSession(Session *s) { myW->setSession(s); } + +void GuiWrapper::refreshSet() const { myW->refreshSet(); } +void GuiWrapper::refreshCash() const { myW->refreshCash(); } +void GuiWrapper::refreshAction(int playerID, int playerAction) const { myW->refreshAction(playerID, playerAction); } +void GuiWrapper::refreshChangePlayer() const { myW->refreshChangePlayer(); } +void GuiWrapper::refreshAll() const { myW->refreshAll(); } +void GuiWrapper::refreshPot() const { myW->refreshPot(); } +void GuiWrapper::refreshGroupbox(int playerID, int status) const { myW->refreshGroupbox(playerID, status); } +void GuiWrapper::refreshPlayerName() const { myW->refreshPlayerName(); } + +void GuiWrapper::dealHoleCards() const { myW->dealHoleCards(); } +void GuiWrapper::dealFlopCards() const { myW->dealFlopCards0(); } +void GuiWrapper::dealTurnCard() const { myW->dealTurnCards0(); } +void GuiWrapper::dealRiverCard() const { myW->dealRiverCards0(); } + +void GuiWrapper::highlightRoundLabel(string round) const { myW->highlightRoundLabel(round); } + + +void GuiWrapper::nextPlayerAnimation() const { myW->nextPlayerAnimation(); } + +void GuiWrapper::preflopAnimation1() const { myW->preflopAnimation1(); } +void GuiWrapper::preflopAnimation2() const { myW->preflopAnimation2(); } + +void GuiWrapper::flopAnimation1() const { myW->flopAnimation1(); } +void GuiWrapper::flopAnimation2() const { myW->flopAnimation2(); } + +void GuiWrapper::turnAnimation1() const { myW->turnAnimation1(); } +void GuiWrapper::turnAnimation2() const { myW->turnAnimation2(); } + +void GuiWrapper::riverAnimation1() const { myW->riverAnimation1(); } +void GuiWrapper::riverAnimation2() const { myW->riverAnimation2(); } + +void GuiWrapper::postRiverAnimation1() const { myW->postRiverAnimation1(); } +void GuiWrapper::postRiverRunAnimation1() const { myW->postRiverRunAnimation1(); } + +void GuiWrapper::flipHolecardsAllIn() const { myW->flipHolecardsAllIn(); } + +void GuiWrapper::nextRoundCleanGui() const { myW->nextRoundCleanGui(); } + +void GuiWrapper::meInAction() const { myW->meInAction(); } + +void GuiWrapper::logPlayerActionMsg(string playerName, int action, int setValue) { myLog->logPlayerActionMsg(playerName, action, setValue); } +void GuiWrapper::logNewGameHandMsg(int gameID, int handID) { myLog->logNewGameHandMsg(gameID, handID); } + +void GuiWrapper::SignalNetClientConnect(int actionID) { myW->SignalNetClientConnect(actionID); } +void GuiWrapper::SignalNetClientGameInfo(int actionID) { myW->SignalNetClientGameInfo(actionID); } +void GuiWrapper::SignalNetClientError(int errorID, int osErrorID) { myW->SignalNetClientError(errorID, osErrorID); } + +void GuiWrapper::SignalNetClientGameStart(const GameData &gameData) { myW->SignalNetClientGameStart(GameDataWrapper(gameData)); } + +void GuiWrapper::SignalNetServerSuccess(int actionID) { } +void GuiWrapper::SignalNetServerError(int errorID, int osErrorID) { } +void GuiWrapper::SignalNetServerPlayerJoined(const string &playerName) { myW->SignalNetServerPlayerJoined(QString::fromUtf8(playerName.c_str())); } + diff --git a/src/gui/qt/guiwrapper.h b/src/gui/qt/guiwrapper.h index b89c5081..cc9ef6f5 100644 --- a/src/gui/qt/guiwrapper.h +++ b/src/gui/qt/guiwrapper.h @@ -92,6 +92,8 @@ public: void SignalNetClientGameInfo(int actionID); void SignalNetClientError(int errorID, int osErrorID); + void SignalNetClientGameStart(const GameData &gameData); + void SignalNetServerSuccess(int actionID); void SignalNetServerError(int errorID, int osErrorID); void SignalNetServerPlayerJoined(const std::string &playerName); diff --git a/src/gui/qt/mainwindow/mainwindowimpl.cpp b/src/gui/qt/mainwindow/mainwindowimpl.cpp index d309a7df..a68ee3cf 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.cpp +++ b/src/gui/qt/mainwindow/mainwindowimpl.cpp @@ -538,6 +538,7 @@ mainWindowImpl::mainWindowImpl(QMainWindow *parent) connect(this, SIGNAL(SignalNetClientGameInfo(int)), myWaitingForServerGameDialog, SLOT(refresh(int))); // Errors are handled globally, not within one dialog. connect(this, SIGNAL(SignalNetClientError(int, int)), this, SLOT(networkError(int, int))); + connect(this, SIGNAL(SignalNetClientGameStart(GameDataWrapper)), this, SLOT(networkStart(GameDataWrapper))); connect(this, SIGNAL(SignalNetServerPlayerJoined(QString)), myStartNetworkGameDialog, SLOT(addConnectedPlayer(QString))); @@ -2199,6 +2200,11 @@ void mainWindowImpl::networkError(int errorID, int osErrorID) { myWaitingForServerGameDialog->reject(); } +void mainWindowImpl::networkStart(GameDataWrapper gameData) +{ + mySession->startGame(gameData.GetGameData()); +} + void mainWindowImpl::keyPressEvent ( QKeyEvent * event ) { // cout << event->key() << endl; diff --git a/src/gui/qt/mainwindow/mainwindowimpl.h b/src/gui/qt/mainwindow/mainwindowimpl.h index 9e5b2cee..f4f06795 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.h +++ b/src/gui/qt/mainwindow/mainwindowimpl.h @@ -29,6 +29,7 @@ #include "handinterface.h" #include "game_defs.h" +#include class Session; class Game; @@ -93,6 +94,7 @@ signals: void SignalNetClientConnect(int actionID); void SignalNetClientGameInfo(int actionID); void SignalNetClientError(int errorID, int osErrorID); + void SignalNetClientGameStart(GameDataWrapper gameData); void SignalNetServerPlayerJoined(QString playerName); public slots: @@ -189,6 +191,7 @@ public slots: void paintStartSplash(); void networkError(int, int); + void networkStart(GameDataWrapper); private: diff --git a/src/net/clientcallback.h b/src/net/clientcallback.h index 4383d063..b9464922 100644 --- a/src/net/clientcallback.h +++ b/src/net/clientcallback.h @@ -21,6 +21,8 @@ #ifndef _CLIENTCALLBACK_H_ #define _CLIENTCALLBACK_H_ +struct GameData; + class ClientCallback { public: @@ -29,6 +31,8 @@ public: virtual void SignalNetClientConnect(int actionID) = 0; virtual void SignalNetClientGameInfo(int actionID) = 0; virtual void SignalNetClientError(int errorID, int osErrorID) = 0; + + virtual void SignalNetClientGameStart(const GameData &gameData) = 0; }; #endif diff --git a/src/net/clientthread.h b/src/net/clientthread.h index 4cd8b13f..ef31fb0e 100644 --- a/src/net/clientthread.h +++ b/src/net/clientthread.h @@ -31,6 +31,7 @@ class ClientState; class SenderThread; class ReceiverHelper; class ClientSenderCallback; +struct GameData; class ClientThread : public Thread { @@ -64,6 +65,9 @@ protected: SenderThread &GetSender(); ReceiverHelper &GetReceiver(); + const GameData &GetGameData() const; + void SetGameData(const GameData &gameData); + ClientSenderCallback &GetSenderCallback(); private: @@ -72,9 +76,12 @@ private: std::auto_ptr m_senderCallback; ClientState *m_curState; ClientCallback &m_callback; + std::auto_ptr m_sender; std::auto_ptr m_receiver; + std::auto_ptr m_gameData; + friend class ClientStateInit; friend class ClientStateStartResolve; friend class ClientStateResolving; diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index 435d699b..803f43eb 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -366,6 +366,11 @@ ClientStateWaitSession::Process(ClientThread &client) if (tmpPacket.get() && tmpPacket->ToNetPacketJoinGameAck()) { + // Initialise game configuration. + NetPacketJoinGameAck::Data joinGameAckData; + tmpPacket->ToNetPacketJoinGameAck()->GetData(joinGameAckData); + client.SetGameData(joinGameAckData.gameData); + client.SetState(ClientStateWaitGame::Instance()); retVal = MSG_SOCK_SESSION_DONE; } diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index 84e699a2..9e4c2687 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -24,7 +24,7 @@ #include #include #include - +#include #include @@ -57,6 +57,7 @@ ClientThread::ClientThread(ClientCallback &cb) m_senderCallback.reset(new ClientSenderCallback(*this)); m_sender.reset(new SenderThread(GetSenderCallback())); m_receiver.reset(new ReceiverHelper); + m_gameData.reset(new GameData); } ClientThread::~ClientThread() @@ -103,6 +104,10 @@ ClientThread::Main() GetCallback().SignalNetClientConnect(msg); else GetCallback().SignalNetClientGameInfo(msg); + + // Additionally signal the start of the game. + if (msg == MSG_SOCK_GAME_START) + GetCallback().SignalNetClientGameStart(GetGameData()); } } } catch (const NetException &e) @@ -154,6 +159,20 @@ ClientThread::GetReceiver() return *m_receiver; } +const GameData & +ClientThread::GetGameData() const +{ + assert(m_gameData.get()); + return *m_gameData; +} + +void +ClientThread::SetGameData(const GameData &gameData) +{ + assert(m_gameData.get()); + *m_gameData = gameData; +} + ClientSenderCallback & ClientThread::GetSenderCallback() { diff --git a/src/net/common/netpacket.cpp b/src/net/common/netpacket.cpp index f7332432..8837aed8 100644 --- a/src/net/common/netpacket.cpp +++ b/src/net/common/netpacket.cpp @@ -78,7 +78,7 @@ struct NetPacketJoinGameAckData u_int16_t numberOfPlayers; u_int16_t smallBlind; u_int16_t handsBeforeRaise; - u_int16_t gameSpeed; + u_int16_t reserved; u_int32_t startCash; }; @@ -416,11 +416,10 @@ NetPacketJoinGameAck::SetData(const NetPacketJoinGameAck::Data &inData) tmpData->sessionId = htonl(inData.sessionId); tmpData->playerId = htons(inData.playerId); tmpData->playerNumber = htons(inData.playerNumber); - tmpData->numberOfPlayers = htons(inData.numberOfPlayers); - tmpData->smallBlind = htons(inData.smallBlind); - tmpData->handsBeforeRaise = htons(inData.handsBeforeRaise); - tmpData->gameSpeed = htons(inData.gameSpeed); - tmpData->startCash = htonl(inData.startCash); + tmpData->numberOfPlayers = htons(inData.gameData.numberOfPlayers); + tmpData->smallBlind = htons(inData.gameData.smallBlind); + tmpData->handsBeforeRaise = htons(inData.gameData.handsBeforeRaise); + tmpData->startCash = htonl(inData.gameData.startCash); } void @@ -429,14 +428,13 @@ NetPacketJoinGameAck::GetData(NetPacketJoinGameAck::Data &outData) const NetPacketJoinGameAckData *tmpData = (NetPacketJoinGameAckData *)GetRawData(); assert(tmpData); - outData.sessionId = ntohl(tmpData->sessionId); - outData.playerId = ntohs(tmpData->playerId); - outData.playerNumber = ntohs(tmpData->playerNumber); - outData.numberOfPlayers = ntohs(tmpData->numberOfPlayers); - outData.smallBlind = ntohs(tmpData->smallBlind); - outData.handsBeforeRaise = ntohs(tmpData->handsBeforeRaise); - outData.gameSpeed = ntohs(tmpData->gameSpeed); - outData.startCash = ntohl(tmpData->startCash); + outData.sessionId = ntohl(tmpData->sessionId); + outData.playerId = ntohs(tmpData->playerId); + outData.playerNumber = ntohs(tmpData->playerNumber); + outData.gameData.numberOfPlayers = ntohs(tmpData->numberOfPlayers); + outData.gameData.smallBlind = ntohs(tmpData->smallBlind); + outData.gameData.handsBeforeRaise = ntohs(tmpData->handsBeforeRaise); + outData.gameData.startCash = ntohl(tmpData->startCash); } const NetPacketJoinGameAck * diff --git a/src/net/common/serverrecvstate.cpp b/src/net/common/serverrecvstate.cpp index 9408e561..aea35a11 100644 --- a/src/net/common/serverrecvstate.cpp +++ b/src/net/common/serverrecvstate.cpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include using namespace std; @@ -43,6 +45,7 @@ ServerRecvStateInit::Instance() } ServerRecvStateInit::ServerRecvStateInit() +: m_curUniquePlayerId(0) { } @@ -76,16 +79,54 @@ ServerRecvStateInit::Process(ServerRecvThread &server) const NetPacketJoinGame *tmpPacket = packet->ToNetPacketJoinGame(); if (tmpPacket) { - // TODO: check password - NetPacketJoinGame::Data playerData; - 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); + NetPacketJoinGame::Data joinGameData; + tmpPacket->GetData(joinGameData); + // Check the server password. + if (server.CheckPassword(joinGameData.password)) + { + PlayerDataList &playerDataList = server.GetPlayerDataList(); + // Check whether this player is already connected. + PlayerDataList::const_iterator player_i = playerDataList.begin(); + PlayerDataList::const_iterator player_end = playerDataList.end(); + while (player_i != player_end) + { + if ((*player_i)->GetName() == joinGameData.playerName) + break; + ++player_i; + } + if (player_i == player_end) + { + // Create player data object. + boost::shared_ptr tmpPlayerData(new PlayerData(m_curUniquePlayerId++)); + tmpPlayerData->SetName(joinGameData.playerName); + tmpPlayerData->SetPlayerType(joinGameData.ptype); + + // Signal joining player to GUI. + server.GetCallback().SignalNetServerPlayerJoined(tmpPlayerData->GetName()); + + // Send ACK to client. + boost::shared_ptr answer(new NetPacketJoinGameAck); + NetPacketJoinGameAck::Data joinGameAckData; + joinGameAckData.playerId = tmpPlayerData->GetUniqueId(); + joinGameAckData.playerNumber = playerDataList.size(); + joinGameAckData.sessionId = session->GetId(); // TODO: currently unused. + joinGameAckData.gameData = server.GetGameData(); + static_cast(answer.get())->SetData(joinGameAckData); + server.GetSender().Send(answer, recvSock); + session->SetState(SessionData::Established); + + // Store player data in list. + playerDataList.push_back(tmpPlayerData); + } + else + { + // TODO send error message, duplicate name + } + } + else + { + // TODO send error message, invalid password + } } else { diff --git a/src/net/common/serverrecvthread.cpp b/src/net/common/serverrecvthread.cpp index 3eab0806..bbb1ef72 100644 --- a/src/net/common/serverrecvthread.cpp +++ b/src/net/common/serverrecvthread.cpp @@ -309,18 +309,24 @@ ServerRecvThread::GetReceiver() } const GameData & -ServerRecvThread::GetGameData() +ServerRecvThread::GetGameData() const { assert(m_gameData.get()); return *m_gameData; } bool -ServerRecvThread::CheckPassword(const std::string &password) +ServerRecvThread::CheckPassword(const std::string &password) const { return (password == m_password); } +PlayerDataList & +ServerRecvThread::GetPlayerDataList() +{ + return m_playerDataList; +} + ServerSenderCallback & ServerRecvThread::GetSenderCallback() { diff --git a/src/net/netpacket.h b/src/net/netpacket.h index 949df045..e707134c 100644 --- a/src/net/netpacket.h +++ b/src/net/netpacket.h @@ -22,6 +22,7 @@ #define _NETPACKET_H_ #include +#include #include #define MIN_PACKET_SIZE 4 @@ -112,11 +113,7 @@ public: u_int32_t sessionId; u_int16_t playerId; u_int16_t playerNumber; - u_int16_t numberOfPlayers; - u_int16_t smallBlind; - u_int16_t handsBeforeRaise; - u_int16_t gameSpeed; - u_int32_t startCash; + GameData gameData; }; NetPacketJoinGameAck(); diff --git a/src/net/serverrecvstate.h b/src/net/serverrecvstate.h index 18886406..1d4136f7 100644 --- a/src/net/serverrecvstate.h +++ b/src/net/serverrecvstate.h @@ -21,8 +21,8 @@ #ifndef _SERVERRECVSTATE_H_ #define _SERVERRECVSTATE_H_ -#include #include +#include #define SERVER_INITIAL_STATE ServerRecvStateInit #define SERVER_START_GAME_STATE ServerRecvStateStartGame @@ -61,6 +61,10 @@ protected: // Protected constructor - this is a singleton. ServerRecvStateInit(); + +private: + + unsigned m_curUniquePlayerId; }; // State: Start server game. diff --git a/src/net/serverrecvthread.h b/src/net/serverrecvthread.h index 81901f9f..fd021f12 100644 --- a/src/net/serverrecvthread.h +++ b/src/net/serverrecvthread.h @@ -30,6 +30,7 @@ #include #include #include +#include #define RECEIVER_THREAD_TERMINATE_TIMEOUT 200 @@ -82,8 +83,10 @@ protected: SenderThread &GetSender(); ReceiverHelper &GetReceiver(); - const GameData &GetGameData(); - bool CheckPassword(const std::string &password); + const GameData &GetGameData() const; + bool CheckPassword(const std::string &password) const; + + PlayerDataList &GetPlayerDataList(); ServerSenderCallback &GetSenderCallback(); @@ -105,6 +108,7 @@ private: std::auto_ptr m_senderCallback; std::auto_ptr m_gameData; + PlayerDataList m_playerDataList; std::string m_password; ServerCallback &m_callback; diff --git a/src/playerdata.h b/src/playerdata.h index 0f384e9a..026ff224 100644 --- a/src/playerdata.h +++ b/src/playerdata.h @@ -23,6 +23,7 @@ #include #include +#include #include enum PlayerType @@ -60,6 +61,7 @@ private: }; typedef std::list > PlayerDataList; +typedef std::map > PlayerDataMap; #endif