Changed network code for new player lists.

This commit is contained in:
lotodore
2007-09-27 14:17:07 +00:00
parent c469eb5848
commit 78bf4820af
11 changed files with 160 additions and 220 deletions
+28 -109
View File
@@ -694,7 +694,7 @@ ClientStateWaitHand::InternalProcess(ClientThread &client, boost::shared_ptr<Net
int myCards[2];
myCards[0] = (int)tmpData.yourCards[0];
myCards[1] = (int)tmpData.yourCards[1];
client.GetGame()->getPlayerArray()[0]->setMyCards(myCards);
client.GetGame()->getSeatsList()->front()->setMyCards(myCards);
client.GetGame()->initHand();
client.GetGame()->startHand();
client.GetGui().dealHoleCards();
@@ -780,8 +780,8 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr<NetP
curGame->getCurrentHand()->switchRounds();
// Update highest set
if (tmpPlayer->getMySet() > GetHighestSet(*curGame))
SetHighestSet(*curGame, tmpPlayer->getMySet());
if (tmpPlayer->getMySet() > curGame->getCurrentHand()->getCurrentBeRo()->getHighestSet())
curGame->getCurrentHand()->getCurrentBeRo()->setHighestSet(tmpPlayer->getMySet());
// Stop the timeout for the player.
client.GetGui().stopTimeoutAnimation(tmpPlayer->getMyID());
@@ -817,7 +817,9 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr<NetP
}
// Next player's turn.
SetPlayersTurn(*curGame, tmpPlayer->getMyID());
curGame->getCurrentHand()->getCurrentBeRo()->setCurrentPlayersTurnId(tmpPlayer->getMyID());
// TODO: remove this
curGame->getCurrentHand()->getCurrentBeRo()->setPlayersTurn(tmpPlayer->getMyID());
// Mark current player in GUI.
int guiStatus = 2;
@@ -910,8 +912,7 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr<NetP
{
curGame->getCurrentHand()->getBoard()->collectPot();
// Reset player sets
for (int i = 0; i < MAX_NUMBER_OF_PLAYERS; i++)
curGame->getPlayerArray()[i]->setMySetNull();
ResetPlayerSets(*curGame);
client.GetGui().refreshPot();
client.GetGui().refreshSet();
// Synchronize with GUI.
@@ -939,8 +940,7 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr<NetP
{
curGame->getCurrentHand()->getBoard()->collectPot();
// Reset player sets
for (int i = 0; i < MAX_NUMBER_OF_PLAYERS; i++)
curGame->getPlayerArray()[i]->setMySetNull();
ResetPlayerSets(*curGame);
client.GetGui().refreshPot();
client.GetGui().refreshSet();
// Synchronize with GUI.
@@ -994,113 +994,32 @@ ClientStateRunHand::InternalProcess(ClientThread &client, boost::shared_ptr<NetP
return retVal;
}
int
ClientStateRunHand::GetHighestSet(Game &curGame)
{
int highestSet = 0;
// TODO: no switch needed here if game states are polymorphic
switch(curGame.getCurrentHand()->getActualRound()) {
case GAME_STATE_PREFLOP: {
highestSet = curGame.getCurrentHand()->getPreflop()->getHighestSet();
} break;
case GAME_STATE_FLOP: {
highestSet = curGame.getCurrentHand()->getFlop()->getHighestSet();
} break;
case GAME_STATE_TURN: {
highestSet = curGame.getCurrentHand()->getTurn()->getHighestSet();
} break;
case GAME_STATE_RIVER: {
highestSet = curGame.getCurrentHand()->getRiver()->getHighestSet();
} break;
default: {
//
}
}
return highestSet;
}
void
ClientStateRunHand::SetHighestSet(Game &curGame, int highestSet)
{
// TODO: no switch needed here if game states are polymorphic
switch(curGame.getCurrentHand()->getActualRound()) {
case GAME_STATE_PREFLOP: {
curGame.getCurrentHand()->getPreflop()->setHighestSet(highestSet);
} break;
case GAME_STATE_FLOP: {
curGame.getCurrentHand()->getFlop()->setHighestSet(highestSet);
} break;
case GAME_STATE_TURN: {
curGame.getCurrentHand()->getTurn()->setHighestSet(highestSet);
} break;
case GAME_STATE_RIVER: {
curGame.getCurrentHand()->getRiver()->setHighestSet(highestSet);
} break;
default: {
//
}
}
}
int
ClientStateRunHand::GetPlayersTurn(Game &curGame)
{
int playersTurn = 0;
// TODO: no switch needed here if game states are polymorphic
switch(curGame.getCurrentHand()->getActualRound()) {
case GAME_STATE_PREFLOP: {
playersTurn = curGame.getCurrentHand()->getPreflop()->getPlayersTurn();
} break;
case GAME_STATE_FLOP: {
playersTurn = curGame.getCurrentHand()->getFlop()->getPlayersTurn();
} break;
case GAME_STATE_TURN: {
playersTurn = curGame.getCurrentHand()->getTurn()->getPlayersTurn();
} break;
case GAME_STATE_RIVER: {
playersTurn = curGame.getCurrentHand()->getRiver()->getPlayersTurn();
} break;
default: {
//
}
}
return playersTurn;
}
void
ClientStateRunHand::SetPlayersTurn(Game &curGame, int playersTurn)
{
// TODO: no switch needed here if game states are polymorphic
switch(curGame.getCurrentHand()->getActualRound()) {
case GAME_STATE_PREFLOP: {
curGame.getCurrentHand()->getPreflop()->setPlayersTurn(playersTurn);
} break;
case GAME_STATE_FLOP: {
curGame.getCurrentHand()->getFlop()->setPlayersTurn(playersTurn);
} break;
case GAME_STATE_TURN: {
curGame.getCurrentHand()->getTurn()->setPlayersTurn(playersTurn);
} break;
case GAME_STATE_RIVER: {
curGame.getCurrentHand()->getRiver()->setPlayersTurn(playersTurn);
} break;
default: {
//
}
}
}
void
ClientStateRunHand::ResetPlayerActions(Game &curGame)
{
// Reset player actions
for (int i = 0; i < MAX_NUMBER_OF_PLAYERS; i++)
PlayerListIterator i = curGame.getSeatsList()->begin();
PlayerListIterator end = curGame.getSeatsList()->end();
while (i != end)
{
int action = curGame.getPlayerArray()[i]->getMyAction();
int action = (*i)->getMyAction();
if (action != 1 && action != 6)
curGame.getPlayerArray()[i]->setMyAction(0);
curGame.getPlayerArray()[i]->setMySetNull();
(*i)->setMyAction(0);
(*i)->setMySetNull();
++i;
}
}
void
ClientStateRunHand::ResetPlayerSets(Game &curGame)
{
PlayerListIterator i = curGame.getSeatsList()->begin();
PlayerListIterator end = curGame.getSeatsList()->end();
while (i != end)
{
(*i)->setMySetNull();
++i;
}
}
+3 -2
View File
@@ -134,11 +134,12 @@ ClientThread::SendPlayerAction()
// Create a network packet containing the current player action.
boost::shared_ptr<NetPacket> action(new NetPacketPlayersAction);
NetPacketPlayersAction::Data actionData;
boost::shared_ptr<PlayerInterface> myPlayer = GetGame()->getSeatsList()->front();
actionData.gameState = static_cast<GameState>(GetGame()->getCurrentHand()->getActualRound());
actionData.playerAction = static_cast<PlayerAction>(GetGame()->getPlayerArray()[0]->getMyAction());
actionData.playerAction = static_cast<PlayerAction>(myPlayer->getMyAction());
// Only send last bet if not fold/checked.
if (actionData.playerAction != PLAYER_ACTION_FOLD && actionData.playerAction != PLAYER_ACTION_CHECK)
actionData.playerBet = GetGame()->getPlayerArray()[0]->getMyLastRelativeSet();
actionData.playerBet = myPlayer->getMyLastRelativeSet();
else
actionData.playerBet = 0;
try
+55 -48
View File
@@ -455,61 +455,75 @@ ServerGameStateStartHand::Process(ServerGameThread &server)
curGame.getCurrentHand()->getTurn()->resetFirstRun();
curGame.getCurrentHand()->getRiver()->resetFirstRun();
std::vector<boost::shared_ptr<PlayerInterface> >playerArray = curGame.getPlayerArray();
// Consider all players, even inactive.
PlayerListIterator i = curGame.getSeatsList()->begin();
PlayerListIterator end = curGame.getSeatsList()->end();
// Send cards to all players.
for (int i = 0; i < curGame.getStartQuantityPlayers(); i++)
while (i != end)
{
// also send to inactive players, but not to disconnected players.
if (playerArray[i]->getNetSessionData().get())
boost::shared_ptr<PlayerInterface> tmpPlayer = *i;
if (tmpPlayer->getNetSessionData().get())
{
int cards[2];
playerArray[i]->getMyCards(cards);
tmpPlayer->getMyCards(cards);
boost::shared_ptr<NetPacket> notifyCards(new NetPacketHandStart);
NetPacketHandStart::Data handStartData;
handStartData.yourCards[0] = static_cast<unsigned>(cards[0]);
handStartData.yourCards[1] = static_cast<unsigned>(cards[1]);
static_cast<NetPacketHandStart *>(notifyCards.get())->SetData(handStartData);
server.GetSender().Send(playerArray[i]->getNetSessionData()->GetSocket(), notifyCards);
server.GetSender().Send(tmpPlayer->getNetSessionData()->GetSocket(), notifyCards);
}
++i;
}
// Start hand.
curGame.startHand();
// Auto small blind / big blind at the beginning of hand.
for (int i = 0; i < curGame.getStartQuantityPlayers(); i++)
i = curGame.getActivePlayerList()->begin();
end = curGame.getActivePlayerList()->end();
while (i != end)
{
if(playerArray[i]->getMyButton() == BUTTON_SMALL_BLIND)
boost::shared_ptr<PlayerInterface> tmpPlayer = *i;
if (tmpPlayer->getMyButton() == BUTTON_SMALL_BLIND)
{
boost::shared_ptr<NetPacket> notifySmallBlind(new NetPacketPlayersActionDone);
NetPacketPlayersActionDone::Data actionDoneData;
actionDoneData.gameState = GAME_STATE_PREFLOP_SMALL_BLIND;
actionDoneData.playerId = playerArray[i]->getMyUniqueID();
actionDoneData.playerAction = (PlayerAction)playerArray[i]->getMyAction();
actionDoneData.totalPlayerBet = playerArray[i]->getMySet();
actionDoneData.playerMoney = playerArray[i]->getMyCash();
actionDoneData.playerId = tmpPlayer->getMyUniqueID();
actionDoneData.playerAction = (PlayerAction)tmpPlayer->getMyAction();
actionDoneData.totalPlayerBet = tmpPlayer->getMySet();
actionDoneData.playerMoney = tmpPlayer->getMyCash();
static_cast<NetPacketPlayersActionDone *>(notifySmallBlind.get())->SetData(actionDoneData);
server.SendToAllPlayers(notifySmallBlind, SessionData::Game);
break;
}
++i;
}
for (int i = 0; i < curGame.getStartQuantityPlayers(); i++)
i = curGame.getActivePlayerList()->begin();
end = curGame.getActivePlayerList()->end();
while (i != end)
{
if(playerArray[i]->getMyButton() == BUTTON_BIG_BLIND)
boost::shared_ptr<PlayerInterface> tmpPlayer = *i;
if (tmpPlayer->getMyButton() == BUTTON_BIG_BLIND)
{
boost::shared_ptr<NetPacket> notifyBigBlind(new NetPacketPlayersActionDone);
NetPacketPlayersActionDone::Data actionDoneData;
actionDoneData.gameState = GAME_STATE_PREFLOP_BIG_BLIND;
actionDoneData.playerId = playerArray[i]->getMyUniqueID();
actionDoneData.playerAction = (PlayerAction)playerArray[i]->getMyAction();
actionDoneData.totalPlayerBet = playerArray[i]->getMySet();
actionDoneData.playerMoney = playerArray[i]->getMyCash();
actionDoneData.playerId = tmpPlayer->getMyUniqueID();
actionDoneData.playerAction = (PlayerAction)tmpPlayer->getMyAction();
actionDoneData.totalPlayerBet = tmpPlayer->getMySet();
actionDoneData.playerMoney = tmpPlayer->getMyCash();
static_cast<NetPacketPlayersActionDone *>(notifyBigBlind.get())->SetData(actionDoneData);
server.SendToAllPlayers(notifyBigBlind, SessionData::Game);
break;
}
++i;
}
server.SetState(ServerGameStateStartRound::Instance());
@@ -555,19 +569,19 @@ ServerGameStateStartRound::Process(ServerGameThread &server)
if (newRound != curRound && newRound != GAME_STATE_POST_RIVER)
{
assert(newRound > curRound);
// Retrieve active players. If only one player is left, no cards are shown.
std::list<boost::shared_ptr<PlayerInterface> > activePlayers = GetActivePlayers(curGame);
// Retrieve non-fold players. If only one player is left, no cards are shown.
PlayerList runningPlayers = curGame.getRunningPlayerList();
if (curGame.getCurrentHand()->getAllInCondition()
&& !curGame.getCurrentHand()->getCardsShown()
&& activePlayers.size() > 1)
&& runningPlayers->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<boost::shared_ptr<PlayerInterface> >::iterator i = activePlayers.begin();
std::list<boost::shared_ptr<PlayerInterface> >::iterator end = activePlayers.end();
PlayerListConstIterator i = runningPlayers->begin();
PlayerListConstIterator end = runningPlayers->end();
while (i != end)
{
@@ -625,14 +639,14 @@ ServerGameStateStartRound::Process(ServerGameThread &server)
// Engine will find out who won.
curGame.getCurrentHand()->getCurrentBeRo()->postRiverRun();
// Retrieve active players. If only one player is left, no cards are shown.
std::list<boost::shared_ptr<PlayerInterface> > activePlayers = GetActivePlayers(curGame);
// if (activePlayers.empty()) TODO throw exception
// Retrieve non-fold players. If only one player is left, no cards are shown.
PlayerList runningPlayers = curGame.getRunningPlayerList();
// if (runningPlayers.empty()) TODO throw exception
if (activePlayers.size() == 1)
if (runningPlayers->size() == 1)
{
// End of Hand, but keep cards hidden.
boost::shared_ptr<PlayerInterface> player = activePlayers.front();
boost::shared_ptr<PlayerInterface> player = runningPlayers->front();
boost::shared_ptr<NetPacket> endHand(new NetPacketEndOfHandHideCards);
NetPacketEndOfHandHideCards::Data endHandData;
endHandData.playerId = player->getMyUniqueID();
@@ -648,8 +662,8 @@ ServerGameStateStartRound::Process(ServerGameThread &server)
boost::shared_ptr<NetPacket> endHand(new NetPacketEndOfHandShowCards);
NetPacketEndOfHandShowCards::Data endHandData;
std::list<boost::shared_ptr<PlayerInterface> >::iterator i = activePlayers.begin();
std::list<boost::shared_ptr<PlayerInterface> >::iterator end = activePlayers.end();
PlayerListConstIterator i = runningPlayers->begin();
PlayerListConstIterator end = runningPlayers->end();
while (i != end)
{
@@ -683,10 +697,15 @@ ServerGameStateStartRound::Process(ServerGameThread &server)
// Start next hand - if enough players are left.
int playersPositiveCashCounter = 0;
for (int i = 0; i < curGame.getStartQuantityPlayers(); i++)
PlayerListIterator i = curGame.getSeatsList()->begin();
PlayerListIterator end = curGame.getSeatsList()->end();
while (i != end)
{
if (curGame.getCurrentHand()->getPlayerArray()[i]->getMyCash() > 0)
if ((*i)->getMyCash() > 0)
playersPositiveCashCounter++;
++i;
}
if (!playersPositiveCashCounter)
{
@@ -710,21 +729,6 @@ ServerGameStateStartRound::Process(ServerGameThread &server)
return retVal;
}
std::list<boost::shared_ptr<PlayerInterface> >
ServerGameStateStartRound::GetActivePlayers(Game &curGame)
{
std::list<boost::shared_ptr<PlayerInterface> > activePlayers;
for (int i = 0; i < curGame.getStartQuantityPlayers() ; i++)
{
if (curGame.getPlayerArray()[i]->getMyActiveStatus()
&& curGame.getPlayerArray()[i]->getMyAction() != PLAYER_ACTION_FOLD)
{
activePlayers.push_back(curGame.getPlayerArray()[i]);
}
}
return activePlayers;
}
//-----------------------------------------------------------------------------
boost::thread_specific_ptr<ServerGameStateWaitPlayerAction> ServerGameStateWaitPlayerAction::Ptr;
@@ -1045,11 +1049,14 @@ ServerGameStateNextGameDelay::Process(ServerGameThread &server)
Game &curGame = server.GetGame();
// The game has ended. Notify all clients.
boost::shared_ptr<PlayerInterface> winnerPlayer;
for (int i = 0; i < curGame.getStartQuantityPlayers(); i++)
PlayerListIterator i = curGame.getActivePlayerList()->begin();
PlayerListIterator end = curGame.getActivePlayerList()->end();
while (i != end)
{
winnerPlayer = curGame.getCurrentHand()->getPlayerArray()[i];
winnerPlayer = *i;
if (winnerPlayer->getMyCash() > 0)
break;
++i;
}
boost::shared_ptr<NetPacket> endGame(new NetPacketEndOfGame);
+6 -3
View File
@@ -302,15 +302,18 @@ ServerGameThread::RemoveDisconnectedPlayers()
// This should only be called between hands.
if (m_game.get())
{
for (int i = 0; i < m_game->getStartQuantityPlayers(); i++)
PlayerListIterator i = m_game->getSeatsList()->begin();
PlayerListIterator end = m_game->getSeatsList()->end();
while (i != end)
{
boost::shared_ptr<PlayerInterface> tmpPlayer = m_game->getPlayerArray()[i];
boost::shared_ptr<PlayerInterface> tmpPlayer = *i;
if (!GetSessionManager().IsPlayerConnected(tmpPlayer->getMyUniqueID()) && tmpPlayer->getMyType() == PLAYER_TYPE_HUMAN)
{
// Setting player cash to 0 will deactivate the player.
tmpPlayer->setMyCash(0);
tmpPlayer->setMyActiveStatus(false);
tmpPlayer->setNetSessionData(boost::shared_ptr<SessionData>());
}
++i;
}
}
}