From baf2c82864eddc4f8c5464512168dd03e29bfcdf Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 10 Jun 2007 23:28:25 +0000 Subject: [PATCH] Added new packet type as end of game notification. Now displaying dialogs at end of game, to start a new game. Server errors are displayed. --- docs/net_protocol.txt | 10 + src/gui/generic/serverguiwrapper.cpp | 236 +++++++++--------- src/gui/generic/serverguiwrapper.h | 2 + src/gui/qt/guiwrapper.cpp | 3 +- src/gui/qt/guiwrapper.h | 5 + src/gui/qt/mainwindow/mainwindowimpl.cpp | 63 +++-- src/gui/qt/mainwindow/mainwindowimpl.h | 6 + .../waitforservertostartgamedialogimpl.cpp | 6 +- src/net/clientcallback.h | 1 + src/net/common/clientstate.cpp | 8 +- src/net/common/netpacket.cpp | 78 ++++++ src/net/common/serverrecvstate.cpp | 85 ++++++- src/net/common/serverrecvthread.cpp | 4 +- src/net/netpacket.h | 26 +- src/net/servercallback.h | 2 + src/net/serverrecvstate.h | 27 +- src/net/serverrecvthread.h | 3 +- src/net/socket_msg.h | 15 +- src/session.cpp | 6 + src/session.h | 1 + 20 files changed, 418 insertions(+), 169 deletions(-) diff --git a/docs/net_protocol.txt b/docs/net_protocol.txt index f40a4533..e90cf6c7 100644 --- a/docs/net_protocol.txt +++ b/docs/net_protocol.txt @@ -335,6 +335,16 @@ Server Notification: End Of Hand Hide Cards +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +Server Notification: End Of Game + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Message Type = 17 | Message Length = 8 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Winner Player Id | Reserved | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + Client Request: Send Chat Text diff --git a/src/gui/generic/serverguiwrapper.cpp b/src/gui/generic/serverguiwrapper.cpp index 1c057e17..f63cc3d7 100644 --- a/src/gui/generic/serverguiwrapper.cpp +++ b/src/gui/generic/serverguiwrapper.cpp @@ -1,117 +1,119 @@ -/*************************************************************************** - * 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 "serverguiwrapper.h" -#include - - -using namespace std; - - -ServerGuiWrapper::ServerGuiWrapper(ConfigFile *config, ClientCallback *clientcb, ServerCallback *servercb) -: myConfig(config), myClientcb(clientcb), myServercb(servercb) -{ -} - -ServerGuiWrapper::~ServerGuiWrapper() -{ -} - -void ServerGuiWrapper::initGui(int speed) {} - -Session &ServerGuiWrapper::getSession() -{ - assert(mySession.get()); - return *mySession; -} - -void ServerGuiWrapper::setSession(boost::shared_ptr session) -{ - mySession = session; -} - -void ServerGuiWrapper::refreshSet() const {} - -void ServerGuiWrapper::refreshCash() const {} - -void ServerGuiWrapper::refreshAction(int playerID, int playerAction) const {} - -void ServerGuiWrapper::refreshChangePlayer() const {} - -void ServerGuiWrapper::refreshAll() const {} - -void ServerGuiWrapper::refreshPot() const {} -void ServerGuiWrapper::refreshGroupbox(int playerID, int status) const {} -void ServerGuiWrapper::refreshPlayerName() const {} -void ServerGuiWrapper::refreshButton() const {} -void ServerGuiWrapper::refreshGameLabels(GameState state) const {} - -void ServerGuiWrapper::waitForGuiUpdateDone() const {} - -void ServerGuiWrapper::dealHoleCards() {} -void ServerGuiWrapper::dealFlopCards() {} -void ServerGuiWrapper::dealTurnCard() {} -void ServerGuiWrapper::dealRiverCard() {} - -void ServerGuiWrapper::nextPlayerAnimation() {} - -void ServerGuiWrapper::preflopAnimation1() {} -void ServerGuiWrapper::preflopAnimation2() {} - -void ServerGuiWrapper::flopAnimation1() {} -void ServerGuiWrapper::flopAnimation2() {} - -void ServerGuiWrapper::turnAnimation1() {} -void ServerGuiWrapper::turnAnimation2() {} - -void ServerGuiWrapper::riverAnimation1() {} -void ServerGuiWrapper::riverAnimation2() {} - -void ServerGuiWrapper::postRiverAnimation1() {} -void ServerGuiWrapper::postRiverRunAnimation1() {} - -void ServerGuiWrapper::flipHolecardsAllIn() {} - -void ServerGuiWrapper::nextRoundCleanGui() {} - -void ServerGuiWrapper::meInAction() {} -void ServerGuiWrapper::disableMyButtons() {} -void ServerGuiWrapper::startTimeoutAnimation(int playerId, int timeoutSec) {} -void ServerGuiWrapper::stopTimeoutAnimation(int playerId) {} - -void ServerGuiWrapper::logPlayerActionMsg(string playerName, int action, int setValue) {} -void ServerGuiWrapper::logNewGameHandMsg(int gameID, int handID) {} -void ServerGuiWrapper::logPlayerWinsMsg(int playerID, int pot) {} -void ServerGuiWrapper::logDealBoardCardsMsg(int roundID, int card1, int card2, int card3, int card4, int card5) {} -void ServerGuiWrapper::logFlipHoleCardsMsg(std::string playerName, int card1, int card2, int cardsValueInt, std::string showHas) {} - -void ServerGuiWrapper::SignalNetClientConnect(int actionID) { if (myClientcb) myClientcb->SignalNetClientConnect(actionID); } -void ServerGuiWrapper::SignalNetClientGameInfo(int actionID) { if (myClientcb) myClientcb->SignalNetClientGameInfo(actionID); } -void ServerGuiWrapper::SignalNetClientError(int errorID, int osErrorID) { if (myClientcb) myClientcb->SignalNetClientError(errorID, osErrorID); } -void ServerGuiWrapper::SignalNetClientPlayerJoined(const string &playerName) { if (myClientcb) myClientcb->SignalNetClientPlayerJoined(playerName); } -void ServerGuiWrapper::SignalNetClientPlayerLeft(const string &playerName) { if (myClientcb) myClientcb->SignalNetClientPlayerLeft(playerName); } -void ServerGuiWrapper::SignalNetClientGameStart(boost::shared_ptr game) { if (myClientcb) myClientcb->SignalNetClientGameStart(game); } -void ServerGuiWrapper::SignalNetClientChatMsg(const string &playerName, const string &msg) { if (myClientcb) myClientcb->SignalNetClientChatMsg(playerName, msg); } - -void ServerGuiWrapper::SignalNetServerSuccess(int actionID) { if (myServercb) myServercb->SignalNetServerSuccess(actionID); } -void ServerGuiWrapper::SignalNetServerError(int errorID, int osErrorID) { if (myServercb) myServercb->SignalNetServerError(errorID, osErrorID); } -void ServerGuiWrapper::SignalNetServerPlayerJoined(const string &playerName) { if (myServercb) myServercb->SignalNetServerPlayerJoined(playerName); } -void ServerGuiWrapper::SignalNetServerPlayerLeft(const string &playerName) { if (myServercb) myServercb->SignalNetServerPlayerLeft(playerName); } - - +/*************************************************************************** + * 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 "serverguiwrapper.h" +#include + + +using namespace std; + + +ServerGuiWrapper::ServerGuiWrapper(ConfigFile *config, ClientCallback *clientcb, ServerCallback *servercb) +: myConfig(config), myClientcb(clientcb), myServercb(servercb) +{ +} + +ServerGuiWrapper::~ServerGuiWrapper() +{ +} + +void ServerGuiWrapper::initGui(int speed) {} + +Session &ServerGuiWrapper::getSession() +{ + assert(mySession.get()); + return *mySession; +} + +void ServerGuiWrapper::setSession(boost::shared_ptr session) +{ + mySession = session; +} + +void ServerGuiWrapper::refreshSet() const {} + +void ServerGuiWrapper::refreshCash() const {} + +void ServerGuiWrapper::refreshAction(int playerID, int playerAction) const {} + +void ServerGuiWrapper::refreshChangePlayer() const {} + +void ServerGuiWrapper::refreshAll() const {} + +void ServerGuiWrapper::refreshPot() const {} +void ServerGuiWrapper::refreshGroupbox(int playerID, int status) const {} +void ServerGuiWrapper::refreshPlayerName() const {} +void ServerGuiWrapper::refreshButton() const {} +void ServerGuiWrapper::refreshGameLabels(GameState state) const {} + +void ServerGuiWrapper::waitForGuiUpdateDone() const {} + +void ServerGuiWrapper::dealHoleCards() {} +void ServerGuiWrapper::dealFlopCards() {} +void ServerGuiWrapper::dealTurnCard() {} +void ServerGuiWrapper::dealRiverCard() {} + +void ServerGuiWrapper::nextPlayerAnimation() {} + +void ServerGuiWrapper::preflopAnimation1() {} +void ServerGuiWrapper::preflopAnimation2() {} + +void ServerGuiWrapper::flopAnimation1() {} +void ServerGuiWrapper::flopAnimation2() {} + +void ServerGuiWrapper::turnAnimation1() {} +void ServerGuiWrapper::turnAnimation2() {} + +void ServerGuiWrapper::riverAnimation1() {} +void ServerGuiWrapper::riverAnimation2() {} + +void ServerGuiWrapper::postRiverAnimation1() {} +void ServerGuiWrapper::postRiverRunAnimation1() {} + +void ServerGuiWrapper::flipHolecardsAllIn() {} + +void ServerGuiWrapper::nextRoundCleanGui() {} + +void ServerGuiWrapper::meInAction() {} +void ServerGuiWrapper::disableMyButtons() {} +void ServerGuiWrapper::startTimeoutAnimation(int playerId, int timeoutSec) {} +void ServerGuiWrapper::stopTimeoutAnimation(int playerId) {} + +void ServerGuiWrapper::logPlayerActionMsg(string playerName, int action, int setValue) {} +void ServerGuiWrapper::logNewGameHandMsg(int gameID, int handID) {} +void ServerGuiWrapper::logPlayerWinsMsg(int playerID, int pot) {} +void ServerGuiWrapper::logDealBoardCardsMsg(int roundID, int card1, int card2, int card3, int card4, int card5) {} +void ServerGuiWrapper::logFlipHoleCardsMsg(std::string playerName, int card1, int card2, int cardsValueInt, std::string showHas) {} + +void ServerGuiWrapper::SignalNetClientConnect(int actionID) { if (myClientcb) myClientcb->SignalNetClientConnect(actionID); } +void ServerGuiWrapper::SignalNetClientGameInfo(int actionID) { if (myClientcb) myClientcb->SignalNetClientGameInfo(actionID); } +void ServerGuiWrapper::SignalNetClientError(int errorID, int osErrorID) { if (myClientcb) myClientcb->SignalNetClientError(errorID, osErrorID); } +void ServerGuiWrapper::SignalNetClientPlayerJoined(const string &playerName) { if (myClientcb) myClientcb->SignalNetClientPlayerJoined(playerName); } +void ServerGuiWrapper::SignalNetClientPlayerLeft(const string &playerName) { if (myClientcb) myClientcb->SignalNetClientPlayerLeft(playerName); } +void ServerGuiWrapper::SignalNetClientGameStart(boost::shared_ptr game) { if (myClientcb) myClientcb->SignalNetClientGameStart(game); } +void ServerGuiWrapper::SignalNetClientChatMsg(const string &playerName, const string &msg) { if (myClientcb) myClientcb->SignalNetClientChatMsg(playerName, msg); } +void ServerGuiWrapper::SignalNetClientWaitDialog() { if (myClientcb) myClientcb->SignalNetClientWaitDialog(); } + +void ServerGuiWrapper::SignalNetServerSuccess(int actionID) { if (myServercb) myServercb->SignalNetServerSuccess(actionID); } +void ServerGuiWrapper::SignalNetServerError(int errorID, int osErrorID) { if (myServercb) myServercb->SignalNetServerError(errorID, osErrorID); } +void ServerGuiWrapper::SignalNetServerPlayerJoined(const string &playerName) { if (myServercb) myServercb->SignalNetServerPlayerJoined(playerName); } +void ServerGuiWrapper::SignalNetServerPlayerLeft(const string &playerName) { if (myServercb) myServercb->SignalNetServerPlayerLeft(playerName); } +void ServerGuiWrapper::SignalNetServerStartDialog() { if (myServercb) myServercb->SignalNetServerStartDialog(); } + +\ \ No newline at end of file diff --git a/src/gui/generic/serverguiwrapper.h b/src/gui/generic/serverguiwrapper.h index 884cc669..f508e1e8 100644 --- a/src/gui/generic/serverguiwrapper.h +++ b/src/gui/generic/serverguiwrapper.h @@ -91,6 +91,7 @@ public: void SignalNetClientPlayerJoined(const std::string &playerName); void SignalNetClientPlayerLeft(const std::string &playerName); void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg); + void SignalNetClientWaitDialog(); void SignalNetClientGameStart(boost::shared_ptr game); @@ -98,6 +99,7 @@ public: void SignalNetServerError(int errorID, int osErrorID); void SignalNetServerPlayerJoined(const std::string &playerName); void SignalNetServerPlayerLeft(const std::string &playerName); + void SignalNetServerStartDialog(); private: diff --git a/src/gui/qt/guiwrapper.cpp b/src/gui/qt/guiwrapper.cpp index 7c449bac..67696a49 100644 --- a/src/gui/qt/guiwrapper.cpp +++ b/src/gui/qt/guiwrapper.cpp @@ -110,6 +110,7 @@ void GuiWrapper::SignalNetClientPlayerJoined(const string &playerName) { myW->si void GuiWrapper::SignalNetClientPlayerLeft(const string &playerName) { myW->signalNetClientPlayerLeft(QString::fromUtf8(playerName.c_str())); } void GuiWrapper::SignalNetClientGameStart(boost::shared_ptr game) { myW->signalNetClientGameStart(game); } void GuiWrapper::SignalNetClientChatMsg(const string &playerName, const string &msg) { myChat->signalChatMessage(QString::fromUtf8(playerName.c_str()), QString::fromUtf8(msg.c_str())); } +void GuiWrapper::SignalNetClientWaitDialog() { myW->signalShowClientWaitDialog(); } void GuiWrapper::SignalNetServerSuccess(int actionID) { } void GuiWrapper::SignalNetServerError(int errorID, int osErrorID) { myW->signalNetServerError(errorID, osErrorID); } @@ -120,5 +121,5 @@ void GuiWrapper::SignalNetServerPlayerLeft(const string &playerName) myW->signalNetServerPlayerLeft(tmpName); myLog->signalLogPlayerLeftMsg(tmpName); } - +void GuiWrapper::SignalNetServerStartDialog() { myW->signalShowServerStartDialog(); } diff --git a/src/gui/qt/guiwrapper.h b/src/gui/qt/guiwrapper.h index d8dc4b9e..077d413b 100644 --- a/src/gui/qt/guiwrapper.h +++ b/src/gui/qt/guiwrapper.h @@ -42,6 +42,9 @@ public: Session &getSession(); void setSession(boost::shared_ptr session); + void showServerStartDialog(); + void showClientWaitDialog(); + void refreshSet() const; void refreshCash() const; void refreshAction(int =-1, int =-1) const; @@ -98,6 +101,7 @@ public: void SignalNetClientPlayerJoined(const std::string &playerName); void SignalNetClientPlayerLeft(const std::string &playerName); void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg); + void SignalNetClientWaitDialog(); void SignalNetClientGameStart(boost::shared_ptr game); @@ -105,6 +109,7 @@ public: void SignalNetServerError(int errorID, int osErrorID); void SignalNetServerPlayerJoined(const std::string &playerName); void SignalNetServerPlayerLeft(const std::string &playerName); + void SignalNetServerStartDialog(); private: diff --git a/src/gui/qt/mainwindow/mainwindowimpl.cpp b/src/gui/qt/mainwindow/mainwindowimpl.cpp index f9612d43..5ee27ce4 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.cpp +++ b/src/gui/qt/mainwindow/mainwindowimpl.cpp @@ -573,6 +573,9 @@ mainWindowImpl::mainWindowImpl(ConfigFile *c, QMainWindow *parent) //Nachrichten Thread-Save connect(this, SIGNAL(signalInitGui(int)), this, SLOT(initGui(int))); + connect(this, SIGNAL(signalShowServerStartDialog()), this, SLOT(showServerStartDialog())); + connect(this, SIGNAL(signalShowClientWaitDialog()), this, SLOT(showClientWaitDialog())); + connect(this, SIGNAL(signalRefreshSet()), this, SLOT(refreshSet())); connect(this, SIGNAL(signalRefreshCash()), this, SLOT(refreshCash())); connect(this, SIGNAL(signalRefreshAction(int, int)), this, SLOT(refreshAction(int, int))); @@ -733,21 +736,8 @@ void mainWindowImpl::callCreateNetworkGameDialog() { mySession->startNetworkClientForLocalServer(); myStartNetworkGameDialog->setMaxPlayerNumber(gameData.numberOfPlayers); - myStartNetworkGameDialog->exec(); - - - if (myStartNetworkGameDialog->result() == QDialog::Accepted ) { - - //some gui modifications - networkGameModification(); - myServerGuiInterface->getSession().initiateNetworkServerGame(); - - } - else { - mySession->terminateNetworkClient(); - myServerGuiInterface->getSession().terminateNetworkServer(); - } + showServerStartDialog(); } } @@ -779,15 +769,7 @@ void mainWindowImpl::callJoinNetworkGameDialog() { actionJoin_network_Game->trigger(); // re-trigger } else { - myWaitingForServerGameDialog->exec(); - - if (myWaitingForServerGameDialog->result() == QDialog::Rejected) { - mySession->terminateNetworkClient(); - } - else { - //some gui modifications - networkGameModification(); - } + showClientWaitDialog(); } } } @@ -890,6 +872,39 @@ void mainWindowImpl::initGui(int speed) setSpeeds(); } +void mainWindowImpl::showServerStartDialog() +{ + myStartNetworkGameDialog->exec(); + + if (myStartNetworkGameDialog->result() == QDialog::Accepted ) { + + //some gui modifications + networkGameModification(); + myServerGuiInterface->getSession().initiateNetworkServerGame(); + + } + else { + mySession->terminateNetworkClient(); + myServerGuiInterface->getSession().terminateNetworkServer(); + } +} + +void mainWindowImpl::showClientWaitDialog() +{ + if (!myServerGuiInterface.get() || !myServerGuiInterface->getSession().isNetworkServerRunning()) + { + myWaitingForServerGameDialog->exec(); + + if (myWaitingForServerGameDialog->result() == QDialog::Accepted) { + //some gui modifications + networkGameModification(); + } + else { + mySession->terminateNetworkClient(); + } + } +} + Session &mainWindowImpl::getSession() { assert(mySession.get()); return *mySession; } void mainWindowImpl::setSession(boost::shared_ptr session) { mySession = session; } @@ -2344,7 +2359,7 @@ void mainWindowImpl::breakButtonClicked() { int width = tempMetrics.width(tr("Stop")); pushButton_break->setMinimumSize(width+10,20); - pushButton_break->setText(tr("Stop")); + pushButton_break->setText(tr("Stop")); startNewHand(); } } diff --git a/src/gui/qt/mainwindow/mainwindowimpl.h b/src/gui/qt/mainwindow/mainwindowimpl.h index c9f1e67b..01eb3c17 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.h +++ b/src/gui/qt/mainwindow/mainwindowimpl.h @@ -74,6 +74,9 @@ public: signals: void signalInitGui(int speed); + void signalShowServerStartDialog(); + void signalShowClientWaitDialog(); + void signalRefreshSet(); void signalRefreshCash(); void signalRefreshAction(int =-1, int=-1); @@ -129,6 +132,9 @@ public slots: void initGui(int speed); + void showServerStartDialog(); + void showClientWaitDialog(); + //refresh-Funktionen void refreshSet(); void refreshCash(); diff --git a/src/gui/qt/waitforservertostartgamedialog/waitforservertostartgamedialogimpl.cpp b/src/gui/qt/waitforservertostartgamedialog/waitforservertostartgamedialogimpl.cpp index 2c757518..e5f52918 100644 --- a/src/gui/qt/waitforservertostartgamedialog/waitforservertostartgamedialogimpl.cpp +++ b/src/gui/qt/waitforservertostartgamedialog/waitforservertostartgamedialogimpl.cpp @@ -19,6 +19,7 @@ ***************************************************************************/ #include "waitforservertostartgamedialogimpl.h" #include "session.h" +#include // #include "configfile.h" waitForServerToStartGameDialogImpl::waitForServerToStartGameDialogImpl(QWidget *parent) @@ -33,7 +34,10 @@ waitForServerToStartGameDialogImpl::waitForServerToStartGameDialogImpl(QWidget * void waitForServerToStartGameDialogImpl::refresh(int actionID) { - QTimer::singleShot(1000, this, SLOT(accept())); + if (actionID == MSG_NET_GAME_CLIENT_START) + { + QTimer::singleShot(500, this, SLOT(accept())); + } } void waitForServerToStartGameDialogImpl::cancel() { diff --git a/src/net/clientcallback.h b/src/net/clientcallback.h index 30a67f97..c25f5647 100644 --- a/src/net/clientcallback.h +++ b/src/net/clientcallback.h @@ -40,6 +40,7 @@ public: virtual void SignalNetClientPlayerLeft(const std::string &playerName) = 0; virtual void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg) = 0; + virtual void SignalNetClientWaitDialog() = 0; }; #endif diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index f6d9ec74..07e41b9f 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -568,6 +568,12 @@ ClientStateWaitHand::InternalProcess(ClientThread &client, boost::shared_ptrToNetPacketEndOfGame()) + { + client.GetCallback().SignalNetClientWaitDialog(); + client.SetState(ClientStateWaitGame::Instance()); + retVal = MSG_NET_GAME_CLIENT_END; + } return MSG_SOCK_INTERNAL_PENDING; } @@ -831,7 +837,7 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr(new NetPacketEndOfHandHideCards); break; + case NET_TYPE_END_OF_GAME: + tmpPacket = boost::shared_ptr(new NetPacketEndOfGame); + break; case NET_TYPE_SEND_CHAT_TEXT: tmpPacket = boost::shared_ptr(new NetPacketSendChatText); break; @@ -526,6 +537,12 @@ NetPacket::ToNetPacketEndOfHandHideCards() const return NULL; } +const NetPacketEndOfGame * +NetPacket::ToNetPacketEndOfGame() const +{ + return NULL; +} + const NetPacketSendChatText * NetPacket::ToNetPacketSendChatText() const { @@ -1873,6 +1890,67 @@ NetPacketEndOfHandHideCards::Check(const NetPacketHeader* data) const //----------------------------------------------------------------------------- +NetPacketEndOfGame::NetPacketEndOfGame() +: NetPacket(NET_TYPE_END_OF_GAME, sizeof(NetPacketEndOfGameData)) +{ +} + +NetPacketEndOfGame::~NetPacketEndOfGame() +{ +} + +boost::shared_ptr +NetPacketEndOfGame::Clone() const +{ + boost::shared_ptr newPacket(new NetPacketEndOfGame); + try + { + newPacket->SetRawData(GetRawData()); + } catch (const NetException &) + { + // Need to return the new packet anyway. + } + return newPacket; +} + +void +NetPacketEndOfGame::SetData(const NetPacketEndOfGame::Data &inData) +{ + NetPacketEndOfGameData *tmpData = (NetPacketEndOfGameData *)GetRawData(); + assert(tmpData); + + tmpData->winnerPlayerId = htons(inData.winnerPlayerId); +} + +void +NetPacketEndOfGame::GetData(NetPacketEndOfGame::Data &outData) const +{ + NetPacketEndOfGameData *tmpData = (NetPacketEndOfGameData *)GetRawData(); + assert(tmpData); + + outData.winnerPlayerId = ntohs(tmpData->winnerPlayerId); +} + +const NetPacketEndOfGame * +NetPacketEndOfGame::ToNetPacketEndOfGame() const +{ + return this; +} + +void +NetPacketEndOfGame::Check(const NetPacketHeader* data) const +{ + assert(data); + + u_int16_t dataLen = ntohs(data->length); + if (dataLen != sizeof(NetPacketEndOfGameData)) + { + throw NetException(ERR_SOCK_INVALID_PACKET, 0); + } +} + +//----------------------------------------------------------------------------- + NetPacketSendChatText::NetPacketSendChatText() : NetPacket(NET_TYPE_SEND_CHAT_TEXT, sizeof(NetPacketSendChatTextData)) { diff --git a/src/net/common/serverrecvstate.cpp b/src/net/common/serverrecvstate.cpp index 758c5572..ed384b64 100644 --- a/src/net/common/serverrecvstate.cpp +++ b/src/net/common/serverrecvstate.cpp @@ -34,6 +34,7 @@ using namespace std; #define SERVER_WAIT_TIMEOUT_MSEC 50 #define SERVER_DELAY_NEXT_HAND_SEC 10 +#define SERVER_DELAY_NEXT_GAME_SEC 10 #define SERVER_DEAL_FLOP_CARDS_DELAY_SEC 5 #define SERVER_DEAL_TURN_CARD_DELAY_SEC 2 #define SERVER_DEAL_RIVER_CARD_DELAY_SEC 2 @@ -642,11 +643,16 @@ ServerRecvStateStartRound::Process(ServerRecvThread &server) if (curGame.getCurrentHand()->getPlayerArray()[i]->getMyCash() > 0) playersPositiveCashCounter++; } + assert(playersPositiveCashCounter); if (playersPositiveCashCounter == 1) - server.SetState(ServerRecvStateFinal::Instance()); // TODO + { + // View a dialog for a new game - delayed. + server.SetState(ServerRecvStateNextGameDelay::Instance()); + retVal = MSG_NET_GAME_SERVER_END; + } else { - server.SetState(ServerRecvStateNextHand::Instance()); + server.SetState(ServerRecvStateNextHandDelay::Instance()); retVal = MSG_NET_GAME_SERVER_HAND_END; } } @@ -922,10 +928,9 @@ ServerRecvStateShowCardsDelay::Process(ServerRecvThread &server) { int retVal = ServerRecvStateReceiving::Process(server); - Game &curGame = server.GetGame(); - if (GetTimer().elapsed().seconds() >= SERVER_SHOW_CARDS_DELAY_SEC) { + Game &curGame = server.GetGame(); SendNewRoundCards(server, curGame, curGame.getCurrentHand()->getActualRound()); server.SetState(ServerRecvStateDealCardsDelay::Instance()); @@ -943,23 +948,23 @@ ServerRecvStateShowCardsDelay::InternalProcess(ServerRecvThread &server, Session //----------------------------------------------------------------------------- -ServerRecvStateNextHand & -ServerRecvStateNextHand::Instance() +ServerRecvStateNextHandDelay & +ServerRecvStateNextHandDelay::Instance() { - static ServerRecvStateNextHand state; + static ServerRecvStateNextHandDelay state; return state; } -ServerRecvStateNextHand::ServerRecvStateNextHand() +ServerRecvStateNextHandDelay::ServerRecvStateNextHandDelay() { } -ServerRecvStateNextHand::~ServerRecvStateNextHand() +ServerRecvStateNextHandDelay::~ServerRecvStateNextHandDelay() { } int -ServerRecvStateNextHand::Process(ServerRecvThread &server) +ServerRecvStateNextHandDelay::Process(ServerRecvThread &server) { int retVal = ServerRecvStateReceiving::Process(server); @@ -970,7 +975,65 @@ ServerRecvStateNextHand::Process(ServerRecvThread &server) } int -ServerRecvStateNextHand::InternalProcess(ServerRecvThread &server, SessionWrapper session, boost::shared_ptr packet) +ServerRecvStateNextHandDelay::InternalProcess(ServerRecvThread &server, SessionWrapper session, boost::shared_ptr packet) +{ + return MSG_SOCK_INTERNAL_PENDING; +} + +//----------------------------------------------------------------------------- + +ServerRecvStateNextGameDelay & +ServerRecvStateNextGameDelay::Instance() +{ + static ServerRecvStateNextGameDelay state; + return state; +} + +ServerRecvStateNextGameDelay::ServerRecvStateNextGameDelay() +{ +} + +ServerRecvStateNextGameDelay::~ServerRecvStateNextGameDelay() +{ +} + +int +ServerRecvStateNextGameDelay::Process(ServerRecvThread &server) +{ + int retVal = ServerRecvStateReceiving::Process(server); + + if (GetTimer().elapsed().seconds() >= SERVER_DELAY_NEXT_GAME_SEC) + { + Game &curGame = server.GetGame(); + // The game has ended. Notify all clients. + PlayerInterface *winnerPlayer = NULL; + for (int i = 0; i < curGame.getStartQuantityPlayers(); i++) + { + winnerPlayer = curGame.getCurrentHand()->getPlayerArray()[i]; + if (winnerPlayer->getMyCash() > 0) + break; + } + + boost::shared_ptr endGame(new NetPacketEndOfGame); + NetPacketEndOfGame::Data endGameData; + endGameData.winnerPlayerId = winnerPlayer->getMyUniqueID(); + static_cast(endGame.get())->SetData(endGameData); + + server.SendToAllPlayers(endGame); + + // Switch back to the GUI, wait for the start of a new game. + // Luckily, the names are still in the GUI dialog. + // We just need to show the proper dialog, and people can + // join or leave as usual. + server.GetCallback().SignalNetServerStartDialog(); + server.SetState(ServerRecvStateInit::Instance()); + } + + return retVal; +} + +int +ServerRecvStateNextGameDelay::InternalProcess(ServerRecvThread &server, SessionWrapper session, boost::shared_ptr packet) { return MSG_SOCK_INTERNAL_PENDING; } diff --git a/src/net/common/serverrecvthread.cpp b/src/net/common/serverrecvthread.cpp index 15df7e3f..36814de7 100644 --- a/src/net/common/serverrecvthread.cpp +++ b/src/net/common/serverrecvthread.cpp @@ -119,9 +119,9 @@ ServerRecvThread::Main() // Close sessions. CloseSessionLoop(); } - } catch (const NetException &) + } catch (const NetException &e) { - // TODO + GetCallback().SignalNetServerError(e.GetErrorId(), e.GetOsErrorCode()); } GetSender().SignalTermination(); GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT); diff --git a/src/net/netpacket.h b/src/net/netpacket.h index bcea081e..d388d477 100644 --- a/src/net/netpacket.h +++ b/src/net/netpacket.h @@ -56,6 +56,7 @@ class NetPacketDealRiverCard; class NetPacketAllInShowCards; class NetPacketEndOfHandShowCards; class NetPacketEndOfHandHideCards; +class NetPacketEndOfGame; class NetPacketSendChatText; class NetPacketChatText; class NetPacketError; @@ -93,6 +94,7 @@ public: virtual const NetPacketAllInShowCards *ToNetPacketAllInShowCards() const; virtual const NetPacketEndOfHandShowCards *ToNetPacketEndOfHandShowCards() const; virtual const NetPacketEndOfHandHideCards *ToNetPacketEndOfHandHideCards() const; + virtual const NetPacketEndOfGame *ToNetPacketEndOfGame() const; virtual const NetPacketSendChatText *ToNetPacketSendChatText() const; virtual const NetPacketChatText *ToNetPacketChatText() const; virtual const NetPacketError *ToNetPacketError() const; @@ -216,7 +218,6 @@ public: struct Data { StartData startData; - u_int16_t reserved; }; NetPacketGameStart(); @@ -520,6 +521,29 @@ protected: virtual void Check(const NetPacketHeader* data) const; }; +class NetPacketEndOfGame : public NetPacket +{ +public: + struct Data + { + u_int16_t winnerPlayerId; + }; + + NetPacketEndOfGame(); + virtual ~NetPacketEndOfGame(); + + virtual boost::shared_ptr Clone() const; + + void SetData(const Data &inData); + void GetData(Data &outData) const; + + virtual const NetPacketEndOfGame *ToNetPacketEndOfGame() const; + +protected: + + virtual void Check(const NetPacketHeader* data) const; +}; + class NetPacketSendChatText : public NetPacket { public: diff --git a/src/net/servercallback.h b/src/net/servercallback.h index 4278585f..afb2064a 100644 --- a/src/net/servercallback.h +++ b/src/net/servercallback.h @@ -33,6 +33,8 @@ public: virtual void SignalNetServerPlayerJoined(const std::string &playerName) = 0; virtual void SignalNetServerPlayerLeft(const std::string &playerName) = 0; + + virtual void SignalNetServerStartDialog() = 0; }; #endif diff --git a/src/net/serverrecvstate.h b/src/net/serverrecvstate.h index ebd516d1..ff93d354 100644 --- a/src/net/serverrecvstate.h +++ b/src/net/serverrecvstate.h @@ -253,13 +253,13 @@ protected: }; // State: Delay before next hand. -class ServerRecvStateNextHand : public ServerRecvStateReceiving, public ServerRecvStateRunning, public ServerRecvStateTimer +class ServerRecvStateNextHandDelay : public ServerRecvStateReceiving, public ServerRecvStateRunning, public ServerRecvStateTimer { public: // Access the state singleton. - static ServerRecvStateNextHand &Instance(); + static ServerRecvStateNextHandDelay &Instance(); - virtual ~ServerRecvStateNextHand(); + virtual ~ServerRecvStateNextHandDelay(); // Overwrite default processing virtual int Process(ServerRecvThread &server); @@ -267,11 +267,30 @@ public: protected: // Protected constructor - this is a singleton. - ServerRecvStateNextHand(); + ServerRecvStateNextHandDelay(); virtual int InternalProcess(ServerRecvThread &server, SessionWrapper session, boost::shared_ptr packet); }; +// State: Delay before next hand. +class ServerRecvStateNextGameDelay : public ServerRecvStateReceiving, public ServerRecvStateRunning, public ServerRecvStateTimer +{ +public: + // Access the state singleton. + static ServerRecvStateNextGameDelay &Instance(); + + virtual ~ServerRecvStateNextGameDelay(); + + // Overwrite default processing + virtual int Process(ServerRecvThread &server); + +protected: + + // Protected constructor - this is a singleton. + ServerRecvStateNextGameDelay(); + + virtual int InternalProcess(ServerRecvThread &server, SessionWrapper session, boost::shared_ptr packet); +}; // State: Final. class ServerRecvStateFinal : public ServerRecvStateReceiving, public ServerRecvStateRunning diff --git a/src/net/serverrecvthread.h b/src/net/serverrecvthread.h index 90cf52ff..31350b08 100644 --- a/src/net/serverrecvthread.h +++ b/src/net/serverrecvthread.h @@ -173,7 +173,8 @@ friend class ServerRecvStateStartRound; friend class ServerRecvStateWaitPlayerAction; friend class ServerRecvStateShowCardsDelay; friend class ServerRecvStateDealCardsDelay; -friend class ServerRecvStateNextHand; +friend class ServerRecvStateNextHandDelay; +friend class ServerRecvStateNextGameDelay; }; #endif diff --git a/src/net/socket_msg.h b/src/net/socket_msg.h index 53a4c8c9..39b4e6f3 100644 --- a/src/net/socket_msg.h +++ b/src/net/socket_msg.h @@ -69,14 +69,17 @@ // The following messages are game messages. #define MSG_NET_GAME_CLIENT_START 5 -#define MSG_NET_GAME_SERVER_START 6 #define MSG_NET_GAME_CLIENT_HAND_START 7 #define MSG_NET_GAME_CLIENT_HAND_END 8 -#define MSG_NET_GAME_SERVER_HAND_START 9 -#define MSG_NET_GAME_SERVER_HAND_END 10 -#define MSG_NET_GAME_SERVER_ROUND 11 -#define MSG_NET_GAME_SERVER_ACTION 12 -#define MSG_NET_GAME_SERVER_CARDS_DELAY 13 +#define MSG_NET_GAME_CLIENT_END 9 + +#define MSG_NET_GAME_SERVER_START 10 +#define MSG_NET_GAME_SERVER_HAND_START 11 +#define MSG_NET_GAME_SERVER_HAND_END 12 +#define MSG_NET_GAME_SERVER_ROUND 13 +#define MSG_NET_GAME_SERVER_ACTION 14 +#define MSG_NET_GAME_SERVER_CARDS_DELAY 15 +#define MSG_NET_GAME_SERVER_END 16 #endif diff --git a/src/session.cpp b/src/session.cpp index d5ab10f9..ea9cbe9a 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -227,3 +227,9 @@ bool Session::isNetworkClientRunning() const return myNetClient != NULL; } +bool Session::isNetworkServerRunning() const +{ + // This, and every place which calls this, is a HACK. + // TODO + return myNetServer != NULL; +} diff --git a/src/session.h b/src/session.h index 21fe595b..173c6d84 100755 --- a/src/session.h +++ b/src/session.h @@ -62,6 +62,7 @@ public: void kickPlayer(const std::string &playerName); bool isNetworkClientRunning() const; // TODO hack + bool isNetworkServerRunning() const; // TODO hack private: