Delay after showing cards when all in condition is true. Added option TCP_NODELAY. Round is now shown before dealing cards.

This commit is contained in:
lotodore
2007-06-07 14:06:22 +00:00
parent cfdc52a8d5
commit 05a86be4fe
16 changed files with 223 additions and 126 deletions
+28 -17
View File
@@ -66,8 +66,8 @@ ClientStateInit::Process(ClientThread &client)
if (context.GetServerAddr().empty())
throw ClientException(ERR_SOCK_SERVERADDR_NOT_SET, 0);
// if (context.GetServerPort() < 1024)
// throw ClientException(ERR_SOCK_INVALID_PORT, 0);
if (context.GetServerPort() < 1024)
throw ClientException(ERR_SOCK_INVALID_PORT, 0);
context.SetSocket(socket(context.GetAddrFamily(), SOCK_STREAM, context.GetProtocol()));
if (!IS_VALID_SOCKET(context.GetSocket()))
@@ -77,6 +77,10 @@ ClientStateInit::Process(ClientThread &client)
if (IOCTLSOCKET(context.GetSocket(), FIONBIO, &mode) == SOCKET_ERROR)
throw ClientException(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
// The following call is optional - the return value is not checked.
int nodelay = 1;
setsockopt(context.GetSocket(), SOL_SOCKET, TCP_NODELAY, (char *)&nodelay, sizeof(nodelay));
client.SetState(ClientStateStartResolve::Instance());
return MSG_SOCK_INIT_DONE;
@@ -558,7 +562,7 @@ ClientStateWaitHand::InternalProcess(ClientThread &client, boost::shared_ptr<Net
client.GetGame()->initHand();
client.GetGame()->startHand();
client.GetGui().dealHoleCards();
client.GetGui().refreshGameLabels();
client.GetGui().refreshGameLabels(GAME_STATE_PREFLOP);
client.SetState(ClientStateRunHand::Instance());
retVal = MSG_NET_GAME_CLIENT_HAND_START;
@@ -637,22 +641,10 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr<NetP
// Set round.
if (curGame->getCurrentHand()->getActualRound() != turnData.gameState)
{
// Reset player actions
for (int i = 0; i < MAX_NUMBER_OF_PLAYERS; i++)
{
int action = curGame->getPlayerArray()[i]->getMyAction();
if (action != 1 && action != 6)
curGame->getPlayerArray()[i]->setMyAction(0);
curGame->getPlayerArray()[i]->setMySetNull();
}
ResetPlayerActions(*curGame);
curGame->getCurrentHand()->setActualRound(turnData.gameState);
client.GetGui().refreshPot();
client.GetGui().refreshSet();
// Refresh actions.
client.GetGui().refreshAction();
client.GetGui().refreshCash();
client.GetGui().refreshGameLabels();
}
// Next player's turn.
@@ -678,6 +670,8 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr<NetP
tmpCards[num] = static_cast<int>(cardsData.flopCards[num]);
tmpCards[3] = tmpCards[4] = 0;
curGame->getCurrentHand()->getBoard()->setMyCards(tmpCards);
client.GetGui().refreshGameLabels(GAME_STATE_FLOP);
client.GetGui().dealFlopCards();
}
else if (packet->ToNetPacketDealTurnCard())
@@ -688,6 +682,8 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr<NetP
curGame->getCurrentHand()->getBoard()->getMyCards(tmpCards);
tmpCards[3] = static_cast<int>(cardsData.turnCard);
curGame->getCurrentHand()->getBoard()->setMyCards(tmpCards);
client.GetGui().refreshGameLabels(GAME_STATE_TURN);
client.GetGui().dealTurnCard();
}
else if (packet->ToNetPacketDealRiverCard())
@@ -698,6 +694,8 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr<NetP
curGame->getCurrentHand()->getBoard()->getMyCards(tmpCards);
tmpCards[4] = static_cast<int>(cardsData.riverCard);
curGame->getCurrentHand()->getBoard()->setMyCards(tmpCards);
client.GetGui().refreshGameLabels(GAME_STATE_RIVER);
client.GetGui().dealRiverCard();
}
else if (packet->ToNetPacketAllInShowCards())
@@ -888,6 +886,19 @@ ClientStateRunHand::SetPlayersTurn(Game &curGame, int playersTurn)
}
}
void
ClientStateRunHand::ResetPlayerActions(Game &curGame)
{
// Reset player actions
for (int i = 0; i < MAX_NUMBER_OF_PLAYERS; i++)
{
int action = curGame.getPlayerArray()[i]->getMyAction();
if (action != 1 && action != 6)
curGame.getPlayerArray()[i]->setMyAction(0);
curGame.getPlayerArray()[i]->setMySetNull();
}
}
//-----------------------------------------------------------------------------
ClientStateFinal &
+145 -89
View File
@@ -32,13 +32,16 @@
using namespace std;
#define SERVER_WAIT_TIMEOUT_MSEC 50
#define SERVER_NEXT_HAND_DELAY_SEC 10
#define SERVER_DEAL_FLOP_CARDS_SEC 5
#define SERVER_DEAL_TURN_CARD_SEC 3
#define SERVER_DEAL_RIVER_CARD_SEC 3
#define SERVER_WAIT_TIMEOUT_MSEC 50
#define SERVER_DELAY_NEXT_HAND_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
#define SERVER_DEAL_ADD_ALL_IN_DELAY_SEC 1
#define SERVER_SHOW_CARDS_DELAY_SEC 2
// Helper functions
// TODO: these are hacks.
static PlayerInterface *GetCurrentPlayer(Game &curGame)
{
@@ -65,6 +68,50 @@ static PlayerInterface *GetCurrentPlayer(Game &curGame)
return curGame.getPlayerArray()[curPlayerNum];
}
static void SendNewRoundCards(ServerRecvThread &server, Game &curGame, int state)
{
// TODO: no switch needed here if game states are polymorphic
switch(state) {
case GAME_STATE_PREFLOP: {
// nothing to do
} break;
case GAME_STATE_FLOP: {
// deal flop cards
int cards[5];
curGame.getCurrentHand()->getBoard()->getMyCards(cards);
boost::shared_ptr<NetPacket> notifyCards(new NetPacketDealFlopCards);
NetPacketDealFlopCards::Data notifyCardsData;
for (int num = 0; num < 3; num++)
notifyCardsData.flopCards[num] = static_cast<u_int16_t>(cards[num]);
static_cast<NetPacketDealFlopCards *>(notifyCards.get())->SetData(notifyCardsData);
server.SendToAllPlayers(notifyCards);
} break;
case GAME_STATE_TURN: {
// deal turn card
int cards[5];
curGame.getCurrentHand()->getBoard()->getMyCards(cards);
boost::shared_ptr<NetPacket> notifyCards(new NetPacketDealTurnCard);
NetPacketDealTurnCard::Data notifyCardsData;
notifyCardsData.turnCard = static_cast<u_int16_t>(cards[3]);
static_cast<NetPacketDealTurnCard *>(notifyCards.get())->SetData(notifyCardsData);
server.SendToAllPlayers(notifyCards);
} break;
case GAME_STATE_RIVER: {
// deal river card
int cards[5];
curGame.getCurrentHand()->getBoard()->getMyCards(cards);
boost::shared_ptr<NetPacket> notifyCards(new NetPacketDealRiverCard);
NetPacketDealRiverCard::Data notifyCardsData;
notifyCardsData.riverCard = static_cast<u_int16_t>(cards[4]);
static_cast<NetPacketDealRiverCard *>(notifyCards.get())->SetData(notifyCardsData);
server.SendToAllPlayers(notifyCards);
} break;
default: {
//
}
}
}
//-----------------------------------------------------------------------------
ServerRecvState::~ServerRecvState()
@@ -454,49 +501,53 @@ ServerRecvStateStartRound::Process(ServerRecvThread &server)
if (newRound != curRound)
{
assert(newRound > curRound);
// Retrieve active players. If only one player is left, no cards are shown.
std::list<PlayerInterface *> activePlayers = GetActivePlayers(curGame);
if (curGame.getCurrentHand()->getAllInCondition()
&& !curGame.getCurrentHand()->getCardsShown())
&& !curGame.getCurrentHand()->getCardsShown()
&& activePlayers.size() > 1)
{
// Retrieve active players. If only one player is left, no cards are shown.
std::list<PlayerInterface *> activePlayers = GetActivePlayers(curGame);
assert(!activePlayers.empty());
if (activePlayers.size() > 1)
// Send cards of all active players to all players (all in).
boost::shared_ptr<NetPacket> allIn(new NetPacketAllInShowCards);
NetPacketAllInShowCards::Data allInData;
std::list<PlayerInterface *>::iterator i = activePlayers.begin();
std::list<PlayerInterface *>::iterator end = activePlayers.end();
while (i != end)
{
// Send cards of all active players to all players (all in).
boost::shared_ptr<NetPacket> allIn(new NetPacketAllInShowCards);
NetPacketAllInShowCards::Data allInData;
NetPacketAllInShowCards::PlayerCards tmpPlayerCards;
tmpPlayerCards.playerId = (*i)->getMyUniqueID();
std::list<PlayerInterface *>::iterator i = activePlayers.begin();
std::list<PlayerInterface *>::iterator end = activePlayers.end();
int tmpCards[2];
(*i)->getMyCards(tmpCards);
tmpPlayerCards.cards[0] = static_cast<u_int16_t>(tmpCards[0]);
tmpPlayerCards.cards[1] = static_cast<u_int16_t>(tmpCards[1]);
while (i != end)
{
NetPacketAllInShowCards::PlayerCards tmpPlayerCards;
tmpPlayerCards.playerId = (*i)->getMyUniqueID();
int tmpCards[2];
(*i)->getMyCards(tmpCards);
tmpPlayerCards.cards[0] = static_cast<u_int16_t>(tmpCards[0]);
tmpPlayerCards.cards[1] = static_cast<u_int16_t>(tmpCards[1]);
allInData.playerCards.push_back(tmpPlayerCards);
++i;
}
static_cast<NetPacketAllInShowCards *>(allIn.get())->SetData(allInData);
server.SendToAllPlayers(allIn);
curGame.getCurrentHand()->setCardsShown(true);
allInData.playerCards.push_back(tmpPlayerCards);
++i;
}
static_cast<NetPacketAllInShowCards *>(allIn.get())->SetData(allInData);
server.SendToAllPlayers(allIn);
curGame.getCurrentHand()->setCardsShown(true);
boost::microsec_timer delayTimer;
delayTimer.start();
ServerRecvStateShowCardsDelay::Instance().SetTimer(delayTimer);
server.SetState(ServerRecvStateShowCardsDelay::Instance());
retVal = MSG_NET_GAME_SERVER_CARDS_DELAY;
}
else
{
SendNewRoundCards(server, curGame, newRound);
SendNewRoundCards(server, curGame, newRound);
boost::microsec_timer delayTimer;
delayTimer.start();
ServerRecvStateDealCardsDelay::Instance().SetTimer(delayTimer);
server.SetState(ServerRecvStateDealCardsDelay::Instance());
retVal = MSG_NET_GAME_SERVER_CARDS_DELAY;
boost::microsec_timer delayTimer;
delayTimer.start();
ServerRecvStateDealCardsDelay::Instance().SetTimer(delayTimer);
server.SetState(ServerRecvStateDealCardsDelay::Instance());
retVal = MSG_NET_GAME_SERVER_CARDS_DELAY;
}
}
else
{
@@ -625,51 +676,6 @@ ServerRecvStateStartRound::GameRun(Game &curGame)
}
}
void
ServerRecvStateStartRound::SendNewRoundCards(ServerRecvThread &server, Game &curGame, int state)
{
// TODO: no switch needed here if game states are polymorphic
switch(state) {
case GAME_STATE_PREFLOP: {
// nothing to do
} break;
case GAME_STATE_FLOP: {
// deal flop cards
int cards[5];
curGame.getCurrentHand()->getBoard()->getMyCards(cards);
boost::shared_ptr<NetPacket> notifyCards(new NetPacketDealFlopCards);
NetPacketDealFlopCards::Data notifyCardsData;
for (int num = 0; num < 3; num++)
notifyCardsData.flopCards[num] = static_cast<u_int16_t>(cards[num]);
static_cast<NetPacketDealFlopCards *>(notifyCards.get())->SetData(notifyCardsData);
server.SendToAllPlayers(notifyCards);
} break;
case GAME_STATE_TURN: {
// deal turn card
int cards[5];
curGame.getCurrentHand()->getBoard()->getMyCards(cards);
boost::shared_ptr<NetPacket> notifyCards(new NetPacketDealTurnCard);
NetPacketDealTurnCard::Data notifyCardsData;
notifyCardsData.turnCard = static_cast<u_int16_t>(cards[3]);
static_cast<NetPacketDealTurnCard *>(notifyCards.get())->SetData(notifyCardsData);
server.SendToAllPlayers(notifyCards);
} break;
case GAME_STATE_RIVER: {
// deal river card
int cards[5];
curGame.getCurrentHand()->getBoard()->getMyCards(cards);
boost::shared_ptr<NetPacket> notifyCards(new NetPacketDealRiverCard);
NetPacketDealRiverCard::Data notifyCardsData;
notifyCardsData.riverCard = static_cast<u_int16_t>(cards[4]);
static_cast<NetPacketDealRiverCard *>(notifyCards.get())->SetData(notifyCardsData);
server.SendToAllPlayers(notifyCards);
} break;
default: {
//
}
}
}
std::list<PlayerInterface *>
ServerRecvStateStartRound::GetActivePlayers(Game &curGame)
{
@@ -857,20 +863,20 @@ ServerRecvStateDealCardsDelay::Process(ServerRecvThread &server)
Game &curGame = server.GetGame();
int allInDelay = curGame.getCurrentHand()->getAllInCondition() ? 1 : 0;
int allInDelay = curGame.getCurrentHand()->getAllInCondition() ? SERVER_DEAL_ADD_ALL_IN_DELAY_SEC : 0;
switch(curGame.getCurrentHand()->getActualRound())
{
case GAME_STATE_FLOP:
if (m_delayTimer.elapsed().seconds() >= SERVER_DEAL_FLOP_CARDS_SEC - allInDelay)
if (m_delayTimer.elapsed().seconds() >= SERVER_DEAL_FLOP_CARDS_DELAY_SEC + allInDelay)
server.SetState(ServerRecvStateStartRound::Instance());
break;
case GAME_STATE_TURN:
if (m_delayTimer.elapsed().seconds() >= SERVER_DEAL_TURN_CARD_SEC /*+ allInDelay*/)
if (m_delayTimer.elapsed().seconds() >= SERVER_DEAL_TURN_CARD_DELAY_SEC)
server.SetState(ServerRecvStateStartRound::Instance());
break;
case GAME_STATE_RIVER:
if (m_delayTimer.elapsed().seconds() >= SERVER_DEAL_RIVER_CARD_SEC /*+ allInDelay*/)
if (m_delayTimer.elapsed().seconds() >= SERVER_DEAL_RIVER_CARD_DELAY_SEC)
server.SetState(ServerRecvStateStartRound::Instance());
break;
default:
@@ -887,6 +893,56 @@ ServerRecvStateDealCardsDelay::InternalProcess(ServerRecvThread &server, Session
//-----------------------------------------------------------------------------
ServerRecvStateShowCardsDelay &
ServerRecvStateShowCardsDelay::Instance()
{
static ServerRecvStateShowCardsDelay state;
return state;
}
ServerRecvStateShowCardsDelay::ServerRecvStateShowCardsDelay()
{
}
ServerRecvStateShowCardsDelay::~ServerRecvStateShowCardsDelay()
{
}
void
ServerRecvStateShowCardsDelay::SetTimer(const boost::microsec_timer &timer)
{
m_delayTimer = timer;
}
int
ServerRecvStateShowCardsDelay::Process(ServerRecvThread &server)
{
int retVal = ServerRecvStateReceiving::Process(server);
Game &curGame = server.GetGame();
if (m_delayTimer.elapsed().seconds() >= SERVER_SHOW_CARDS_DELAY_SEC)
{
SendNewRoundCards(server, curGame, curGame.getCurrentHand()->getActualRound());
boost::microsec_timer delayTimer;
delayTimer.start();
ServerRecvStateDealCardsDelay::Instance().SetTimer(delayTimer);
server.SetState(ServerRecvStateDealCardsDelay::Instance());
retVal = MSG_NET_GAME_SERVER_CARDS_DELAY;
}
return retVal;
}
int
ServerRecvStateShowCardsDelay::InternalProcess(ServerRecvThread &server, SessionWrapper session, boost::shared_ptr<NetPacket> packet)
{
return MSG_SOCK_INTERNAL_PENDING;
}
//-----------------------------------------------------------------------------
ServerRecvStateNextHand &
ServerRecvStateNextHand::Instance()
{
@@ -913,7 +969,7 @@ ServerRecvStateNextHand::Process(ServerRecvThread &server)
{
int retVal = ServerRecvStateReceiving::Process(server);
if (m_delayTimer.elapsed().seconds() >= SERVER_NEXT_HAND_DELAY_SEC)
if (m_delayTimer.elapsed().seconds() >= SERVER_DELAY_NEXT_HAND_SEC)
server.SetState(ServerRecvStateStartHand::Instance());
return retVal;
+3 -1
View File
@@ -115,9 +115,11 @@ ServerThread::Listen()
if (IOCTLSOCKET(context.GetSocket(), FIONBIO, &mode) == SOCKET_ERROR)
throw ServerException(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
// The following call is optional. If it fails, we don't care.
// The following two calls are optional. If they fail, we don't care.
int reuse = 1;
setsockopt(context.GetSocket(), SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
int nodelay = 1;
setsockopt(context.GetSocket(), SOL_SOCKET, TCP_NODELAY, (char *)&nodelay, sizeof(nodelay));
context.GetServerSockaddr()->ss_family = context.GetAddrFamily();