Network: First round seems to work fine now. Pot/sets not yet displayed correctly.

This commit is contained in:
lotodore
2007-05-24 20:26:23 +00:00
parent 69ab45c902
commit 5431f0b93a
19 changed files with 233 additions and 147 deletions
+18
View File
@@ -219,6 +219,24 @@ protected:
ClientStateWaitHand();
};
// State: Hand Loop.
class ClientStateRunHand : public ClientState
{
public:
// Access the state singleton.
static ClientStateRunHand &Instance();
virtual ~ClientStateRunHand();
// select on socket.
virtual int Process(ClientThread &client);
protected:
// Protected constructor - this is a singleton.
ClientStateRunHand();
};
// State: Final (TODO).
class ClientStateFinal : public ClientState
{
+1
View File
@@ -114,6 +114,7 @@ friend class ClientStateStartSession;
friend class ClientStateWaitSession;
friend class ClientStateWaitGame;
friend class ClientStateWaitHand;
friend class ClientStateRunHand;
friend class ClientStateFinal;
};
+73 -12
View File
@@ -498,16 +498,18 @@ ClientStateWaitHand::~ClientStateWaitHand()
int
ClientStateWaitHand::Process(ClientThread &client)
{
int retVal = MSG_SOCK_INTERNAL_PENDING;
ClientContext &context = client.GetContext();
// delegate to receiver helper class
// Delegate to receiver helper class.
boost::shared_ptr<NetPacket> tmpPacket = client.GetReceiver().Recv(context.GetSocket());
if (tmpPacket.get())
{
// TODO: Hack
if (tmpPacket->ToNetPacketHandStart())
{
// Hand was started.
// These are the cards. Good luck.
NetPacketHandStart::Data tmpData;
tmpPacket->ToNetPacketHandStart()->GetData(tmpData);
int myCards[2];
@@ -517,12 +519,48 @@ ClientStateWaitHand::Process(ClientThread &client)
client.GetGame()->initHand();
client.GetGame()->startHand();
client.GetGui().dealHoleCards();
client.SetState(ClientStateRunHand::Instance());
retVal = MSG_NET_GAME_CLIENT_HAND;
}
else if (tmpPacket->ToNetPacketPlayersActionDone())
}
return MSG_SOCK_INTERNAL_PENDING;
}
//-----------------------------------------------------------------------------
ClientStateRunHand &
ClientStateRunHand::Instance()
{
static ClientStateRunHand state;
return state;
}
ClientStateRunHand::ClientStateRunHand()
{
}
ClientStateRunHand::~ClientStateRunHand()
{
}
int
ClientStateRunHand::Process(ClientThread &client)
{
ClientContext &context = client.GetContext();
// Delegate to receiver helper class.
boost::shared_ptr<NetPacket> tmpPacket = client.GetReceiver().Recv(context.GetSocket());
if (tmpPacket.get())
{
boost::shared_ptr<Game> curGame = client.GetGame();
if (tmpPacket->ToNetPacketPlayersActionDone())
{
NetPacketPlayersActionDone::Data actionDoneData;
tmpPacket->ToNetPacketPlayersActionDone()->GetData(actionDoneData);
PlayerInterface *tmpPlayer = client.GetGame()->getPlayerByUniqueId(actionDoneData.playerId);
PlayerInterface *tmpPlayer = curGame->getPlayerByUniqueId(actionDoneData.playerId);
assert(tmpPlayer); // TODO: throw exception
if (actionDoneData.gameState == GAME_STATE_PREFLOP_SMALL_BLIND)
@@ -531,10 +569,10 @@ ClientStateWaitHand::Process(ClientThread &client)
tmpPlayer->setMyButton(BUTTON_BIG_BLIND);
tmpPlayer->setMyAction(actionDoneData.playerAction);
tmpPlayer->setMySet(actionDoneData.playerBet);
//assert(tmpPlayer->getMyCash() == actionDoneData.curPlayerMoney); // TODO: throw exception
client.GetGame()->getCurrentHand()->getBoard()->setPot(actionDoneData.potSize);
client.GetGame()->getCurrentHand()->getBoard()->setSets(actionDoneData.curHandBets);
tmpPlayer->setMySetAbsolute(actionDoneData.totalPlayerBet);
tmpPlayer->setMyCash(actionDoneData.playerMoney);
curGame->getCurrentHand()->getBoard()->setPot(actionDoneData.potSize);
curGame->getCurrentHand()->getBoard()->setSets(actionDoneData.curHandBets);
client.GetGui().refreshSet();
client.GetGui().refreshPot();
client.GetGui().refreshAction();
@@ -543,13 +581,36 @@ ClientStateWaitHand::Process(ClientThread &client)
{
NetPacketPlayersTurn::Data turnData;
tmpPacket->ToNetPacketPlayersTurn()->GetData(turnData);
PlayerInterface *tmpPlayer = client.GetGame()->getPlayerByUniqueId(turnData.playerId);
PlayerInterface *tmpPlayer = curGame->getPlayerByUniqueId(turnData.playerId);
assert(tmpPlayer); // TODO: throw exception
if (tmpPlayer->getMyID() == 0) // Is this the GUI player?
{
client.GetGui().meInAction();
// Set round.
curGame->getCurrentHand()->setActualRound(turnData.gameState);
// Next player's turn.
// TODO: no switch needed here if game states are polymorphic
switch(turnData.gameState) {
case GAME_STATE_PREFLOP: {
curGame->getCurrentHand()->getPreflop()->setPlayersTurn(tmpPlayer->getMyID());
} break;
case GAME_STATE_FLOP: {
curGame->getCurrentHand()->getFlop()->setPlayersTurn(tmpPlayer->getMyID());
} break;
case GAME_STATE_TURN: {
curGame->getCurrentHand()->getTurn()->setPlayersTurn(tmpPlayer->getMyID());
} break;
case GAME_STATE_RIVER: {
curGame->getCurrentHand()->getRiver()->setPlayersTurn(tmpPlayer->getMyID());
} break;
default: {
//
}
}
client.GetGui().nextPlayerAnimation();
if (tmpPlayer->getMyID() == 0) // Is this the GUI player?
client.GetGui().meInAction();
}
}
+6 -5
View File
@@ -84,14 +84,15 @@ ClientThread::Init(
void
ClientThread::SendPlayerAction()
{
// TODO: ugly hack
// Warning: This function is called in the context of the GUI thread.
// Create a network packet containing the current player action.
boost::shared_ptr<NetPacket> action(new NetPacketPlayersAction);
NetPacketPlayersAction::Data actionData;
actionData.gameState = (GameState)GetGame()->getCurrentHand()->getActualRound();
actionData.playerAction = (PlayerAction)GetGame()->getPlayerArray()[0]->getMyAction();
actionData.playerBet = GetGame()->getPlayerArray()[0]->getMySet();
actionData.gameState = static_cast<GameState>(GetGame()->getCurrentHand()->getActualRound());
actionData.playerAction = static_cast<PlayerAction>(GetGame()->getPlayerArray()[0]->getMyAction());
actionData.playerBet = GetGame()->getPlayerArray()[0]->getMyLastRelativeSet();
static_cast<NetPacketPlayersAction *>(action.get())->SetData(actionData);
// The sender is thread-safe, so just dump the packet.
GetSender().Send(GetContext().GetSocket(), action);
}
+6 -6
View File
@@ -150,8 +150,8 @@ struct GCC_PACKED NetPacketPlayersActionDoneData
u_int16_t playerId;
u_int16_t playerAction;
u_int16_t reserved;
u_int32_t playerBet;
u_int32_t curPlayerMoney;
u_int32_t totalPlayerBet;
u_int32_t playerMoney;
u_int32_t potSize;
u_int32_t curHandBets;
};
@@ -1064,8 +1064,8 @@ NetPacketPlayersActionDone::SetData(const NetPacketPlayersActionDone::Data &inDa
tmpData->gameState = htons(inData.gameState);
tmpData->playerId = htons(inData.playerId);
tmpData->playerAction = htons(inData.playerAction);
tmpData->playerBet = htonl(inData.playerBet);
tmpData->curPlayerMoney = htonl(inData.curPlayerMoney);
tmpData->totalPlayerBet = htonl(inData.totalPlayerBet);
tmpData->playerMoney = htonl(inData.playerMoney);
tmpData->potSize = htonl(inData.potSize);
tmpData->curHandBets = htonl(inData.curHandBets);
}
@@ -1079,8 +1079,8 @@ NetPacketPlayersActionDone::GetData(NetPacketPlayersActionDone::Data &outData) c
outData.gameState = static_cast<GameState>(ntohs(tmpData->gameState));
outData.playerId = ntohs(tmpData->playerId);
outData.playerAction = static_cast<PlayerAction>(ntohs(tmpData->playerAction));
outData.playerBet = ntohl(tmpData->playerBet);
outData.curPlayerMoney = ntohl(tmpData->curPlayerMoney);
outData.totalPlayerBet = ntohl(tmpData->totalPlayerBet);
outData.playerMoney = ntohl(tmpData->playerMoney);
outData.potSize = ntohl(tmpData->potSize);
outData.curHandBets = ntohl(tmpData->curHandBets);
}
+42 -52
View File
@@ -207,6 +207,23 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
//-----------------------------------------------------------------------------
ServerRecvStateRunning::ServerRecvStateRunning()
{
}
ServerRecvStateRunning::~ServerRecvStateRunning()
{
}
void
ServerRecvStateRunning::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
{
// Do not accept new connections in this state.
server.RejectNewConnection(connData);
}
//-----------------------------------------------------------------------------
ServerRecvStateStartGame &
ServerRecvStateStartGame::Instance()
{
@@ -222,13 +239,6 @@ ServerRecvStateStartGame::~ServerRecvStateStartGame()
{
}
void
ServerRecvStateStartGame::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
{
// Do not accept new connections in this state.
server.RejectNewConnection(connData);
}
int
ServerRecvStateStartGame::Process(ServerRecvThread &server)
{
@@ -261,13 +271,6 @@ ServerRecvStateStartHand::~ServerRecvStateStartHand()
{
}
void
ServerRecvStateStartHand::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
{
// Do not accept new connections in this state.
server.RejectNewConnection(connData);
}
int
ServerRecvStateStartHand::Process(ServerRecvThread &server)
{
@@ -307,8 +310,8 @@ ServerRecvStateStartHand::Process(ServerRecvThread &server)
actionDoneData.gameState = GAME_STATE_PREFLOP_SMALL_BLIND;
actionDoneData.playerId = playerArray[i]->getMyUniqueID();
actionDoneData.playerAction = (PlayerAction)playerArray[i]->getMyAction();
actionDoneData.playerBet = playerArray[i]->getMySet();
actionDoneData.curPlayerMoney = playerArray[i]->getMyCash();
actionDoneData.totalPlayerBet = playerArray[i]->getMySet();
actionDoneData.playerMoney = playerArray[i]->getMyCash();
actionDoneData.potSize = curGame.getCurrentHand()->getBoard()->getPot();
actionDoneData.curHandBets = playerArray[i]->getMySet(); // first bet only
static_cast<NetPacketPlayersActionDone *>(notifySmallBlind.get())->SetData(actionDoneData);
@@ -325,8 +328,8 @@ ServerRecvStateStartHand::Process(ServerRecvThread &server)
actionDoneData.gameState = GAME_STATE_PREFLOP_BIG_BLIND;
actionDoneData.playerId = playerArray[i]->getMyUniqueID();
actionDoneData.playerAction = (PlayerAction)playerArray[i]->getMyAction();
actionDoneData.playerBet = playerArray[i]->getMySet();
actionDoneData.curPlayerMoney = playerArray[i]->getMyCash();
actionDoneData.totalPlayerBet = playerArray[i]->getMySet();
actionDoneData.playerMoney = playerArray[i]->getMyCash();
actionDoneData.potSize = curGame.getCurrentHand()->getBoard()->getPot();
actionDoneData.curHandBets = curGame.getCurrentHand()->getBoard()->getSets();
static_cast<NetPacketPlayersActionDone *>(notifyBigBlind.get())->SetData(actionDoneData);
@@ -357,13 +360,6 @@ ServerRecvStateStartRound::~ServerRecvStateStartRound()
{
}
void
ServerRecvStateStartRound::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
{
// Do not accept new connections in this state.
server.RejectNewConnection(connData);
}
int
ServerRecvStateStartRound::Process(ServerRecvThread &server)
{
@@ -402,19 +398,19 @@ ServerRecvStateStartRound::GameRun(Game &curGame, int state)
{
// TODO: no switch needed here if game states are polymorphic
switch(state) {
case 0: {
case GAME_STATE_PREFLOP: {
// Preflop starten
curGame.getCurrentHand()->getPreflop()->preflopRun();
} break;
case 1: {
case GAME_STATE_FLOP: {
// Flop starten
curGame.getCurrentHand()->getFlop()->flopRun();
} break;
case 2: {
case GAME_STATE_TURN: {
// Turn starten
curGame.getCurrentHand()->getTurn()->turnRun();
} break;
case 3: {
case GAME_STATE_RIVER: {
// River starten
curGame.getCurrentHand()->getRiver()->riverRun();
} break;
@@ -430,16 +426,16 @@ ServerRecvStateStartRound::GetCurrentPlayer(Game &curGame)
int curPlayerNum = 0;
// TODO: no switch needed here if game states are polymorphic
switch(curGame.getCurrentHand()->getActualRound()) {
case 0: {
case GAME_STATE_PREFLOP: {
curPlayerNum = curGame.getCurrentHand()->getPreflop()->getPlayersTurn();
} break;
case 1: {
case GAME_STATE_FLOP: {
curPlayerNum = curGame.getCurrentHand()->getFlop()->getPlayersTurn();
} break;
case 2: {
case GAME_STATE_TURN: {
curPlayerNum = curGame.getCurrentHand()->getTurn()->getPlayersTurn();
} break;
case 3: {
case GAME_STATE_RIVER: {
curPlayerNum = curGame.getCurrentHand()->getRiver()->getPlayersTurn();
} break;
default: {
@@ -467,17 +463,10 @@ ServerRecvStateWaitPlayerAction::~ServerRecvStateWaitPlayerAction()
{
}
void
ServerRecvStateWaitPlayerAction::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
{
// Do not accept new connections in this state.
server.RejectNewConnection(connData);
}
int
ServerRecvStateWaitPlayerAction::Process(ServerRecvThread &server)
{
int retVal = MSG_SOCK_INIT_DONE;
int retVal = MSG_SOCK_INTERNAL_PENDING;
SOCKET recvSock = server.Select();
if (recvSock != INVALID_SOCKET)
@@ -505,23 +494,31 @@ ServerRecvStateWaitPlayerAction::Process(ServerRecvThread &server)
packet->ToNetPacketPlayersAction()->GetData(actionData);
Game &curGame = server.GetGame();
PlayerInterface *tmpPlayer = curGame.getPlayerByUniqueId(session.playerData->GetUniqueId());
assert(tmpPlayer); // TODO throw exception
tmpPlayer->setMyAction(actionData.playerAction);
tmpPlayer->setMySet(actionData.playerBet);
boost::shared_ptr<NetPacket> notifyActionDone(new NetPacketPlayersActionDone);
NetPacketPlayersActionDone::Data actionDoneData;
actionDoneData.gameState = GAME_STATE_PREFLOP; // TODO
actionDoneData.gameState = static_cast<GameState>(curGame.getCurrentHand()->getActualRound());
actionDoneData.playerId = session.playerData->GetUniqueId();
actionDoneData.playerAction = actionData.playerAction;
actionDoneData.playerBet = actionData.playerBet;
actionDoneData.curPlayerMoney = 0;
actionDoneData.totalPlayerBet = tmpPlayer->getMySet();
actionDoneData.playerMoney = tmpPlayer->getMyCash();
actionDoneData.potSize = curGame.getCurrentHand()->getBoard()->getPot();
actionDoneData.curHandBets = curGame.getCurrentHand()->getBoard()->getSets();
static_cast<NetPacketPlayersActionDone *>(notifyActionDone.get())->SetData(actionDoneData);
server.SendToAllPlayers(notifyActionDone);
server.SetState(ServerRecvStateStartRound::Instance());
retVal = MSG_NET_GAME_SERVER_ACTION;
}
}
}
return MSG_SOCK_INTERNAL_PENDING;
return retVal;
}
//-----------------------------------------------------------------------------
@@ -541,13 +538,6 @@ ServerRecvStateFinal::~ServerRecvStateFinal()
{
}
void
ServerRecvStateFinal::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
{
// Do not accept new connections in this state.
server.RejectNewConnection(connData);
}
int
ServerRecvStateFinal::Process(ServerRecvThread &server)
{
+2 -2
View File
@@ -295,8 +295,8 @@ public:
GameState gameState;
u_int16_t playerId;
PlayerAction playerAction;
u_int32_t playerBet;
u_int32_t curPlayerMoney;
u_int32_t totalPlayerBet;
u_int32_t playerMoney;
u_int32_t potSize;
u_int32_t curHandBets;
};
+18 -20
View File
@@ -69,8 +69,21 @@ private:
unsigned m_curUniquePlayerId;
};
// Abstract State: Game is running.
class ServerRecvStateRunning : public ServerRecvState
{
public:
virtual ~ServerRecvStateRunning();
//
virtual void HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> data);
protected:
ServerRecvStateRunning();
};
// State: Start server game.
class ServerRecvStateStartGame : public ServerRecvState
class ServerRecvStateStartGame : public ServerRecvStateRunning
{
public:
// Access the state singleton.
@@ -78,9 +91,6 @@ public:
virtual ~ServerRecvStateStartGame();
//
virtual void HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> data);
//
virtual int Process(ServerRecvThread &server);
@@ -91,7 +101,7 @@ protected:
};
// State: Start new hand.
class ServerRecvStateStartHand : public ServerRecvState
class ServerRecvStateStartHand : public ServerRecvStateRunning
{
public:
// Access the state singleton.
@@ -99,9 +109,6 @@ public:
virtual ~ServerRecvStateStartHand();
//
virtual void HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> data);
//
virtual int Process(ServerRecvThread &server);
@@ -112,7 +119,7 @@ protected:
};
// State: Start new round.
class ServerRecvStateStartRound : public ServerRecvState
class ServerRecvStateStartRound : public ServerRecvStateRunning
{
public:
// Access the state singleton.
@@ -120,9 +127,6 @@ public:
virtual ~ServerRecvStateStartRound();
//
virtual void HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> data);
//
virtual int Process(ServerRecvThread &server);
@@ -136,7 +140,7 @@ protected:
};
// State: Wait for a player action.
class ServerRecvStateWaitPlayerAction : public ServerRecvState
class ServerRecvStateWaitPlayerAction : public ServerRecvStateRunning
{
public:
// Access the state singleton.
@@ -144,9 +148,6 @@ public:
virtual ~ServerRecvStateWaitPlayerAction();
//
virtual void HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> data);
//
virtual int Process(ServerRecvThread &server);
@@ -158,7 +159,7 @@ protected:
// State: Final.
class ServerRecvStateFinal : public ServerRecvState
class ServerRecvStateFinal : public ServerRecvStateRunning
{
public:
// Access the state singleton.
@@ -166,9 +167,6 @@ public:
virtual ~ServerRecvStateFinal();
//
virtual void HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> data);
//
virtual int Process(ServerRecvThread &server);
+1 -1
View File
@@ -163,11 +163,11 @@ private:
ConfigFile *m_playerConfig;
friend class ServerRecvStateInit;
friend class ServerRecvStateRunning;
friend class ServerRecvStateStartGame;
friend class ServerRecvStateStartHand;
friend class ServerRecvStateStartRound;
friend class ServerRecvStateWaitPlayerAction;
friend class ServerRecvStateFinal;
};
#endif
+1
View File
@@ -67,6 +67,7 @@
#define MSG_NET_GAME_CLIENT_HAND 7
#define MSG_NET_GAME_SERVER_HAND 8
#define MSG_NET_GAME_SERVER_ROUND 10
#define MSG_NET_GAME_SERVER_ACTION 12
#endif