First try: Transferring client action.

This commit is contained in:
lotodore
2007-05-23 22:31:56 +00:00
parent f9ad1b6b75
commit 2556f43340
21 changed files with 243 additions and 115 deletions
+28 -20
View File
@@ -520,27 +520,35 @@ ClientStateWaitHand::Process(ClientThread &client)
}
else if (tmpPacket->ToNetPacketPlayersActionDone())
{
NetPacketPlayersActionDone::Data tmpData;
tmpPacket->ToNetPacketPlayersActionDone()->GetData(tmpData);
PlayerInterface *tmpPlayer = client.GetGame()->getPlayerByUniqueId(tmpData.playerId);
if (tmpPlayer)
NetPacketPlayersActionDone::Data actionDoneData;
tmpPacket->ToNetPacketPlayersActionDone()->GetData(actionDoneData);
PlayerInterface *tmpPlayer = client.GetGame()->getPlayerByUniqueId(actionDoneData.playerId);
assert(tmpPlayer); // TODO: throw exception
if (actionDoneData.gameState == GAME_STATE_PREFLOP_SMALL_BLIND)
tmpPlayer->setMyButton(BUTTON_SMALL_BLIND);
else if (actionDoneData.gameState == GAME_STATE_PREFLOP_BIG_BLIND)
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);
client.GetGui().refreshSet();
client.GetGui().refreshPot();
client.GetGui().refreshAction();
}
else if (tmpPacket->ToNetPacketPlayersTurn())
{
NetPacketPlayersTurn::Data turnData;
tmpPacket->ToNetPacketPlayersTurn()->GetData(turnData);
PlayerInterface *tmpPlayer = client.GetGame()->getPlayerByUniqueId(turnData.playerId);
assert(tmpPlayer); // TODO: throw exception
if (tmpPlayer->getMyID() == 0) // Is this the GUI player?
{
if (tmpData.gameState == GAME_STATE_PREFLOP_SMALL_BLIND)
{
assert(tmpPlayer->getMyButton() == BUTTON_SMALL_BLIND);
tmpPlayer->setMyAction(tmpData.playerAction);
tmpPlayer->setMySet(tmpData.cashValue);
client.GetGui().refreshSet();
client.GetGui().refreshPot();
}
else if (tmpData.gameState == GAME_STATE_PREFLOP_BIG_BLIND)
{
assert(tmpPlayer->getMyButton() == BUTTON_BIG_BLIND);
tmpPlayer->setMyAction(tmpData.playerAction);
tmpPlayer->setMySet(tmpData.cashValue);
client.GetGui().refreshSet();
client.GetGui().refreshPot();
}
client.GetGui().meInAction();
}
}
}
+14
View File
@@ -81,6 +81,20 @@ ClientThread::Init(
context.SetPlayerName(playerName);
}
void
ClientThread::SendPlayerAction()
{
// TODO: ugly hack
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();
static_cast<NetPacketPlayersAction *>(action.get())->SetData(actionData);
GetSender().Send(GetContext().GetSocket(), action);
}
ClientCallback &
ClientThread::GetCallback()
{
+21 -12
View File
@@ -88,7 +88,7 @@ struct GCC_PACKED NetPacketJoinGameAckData
u_int16_t smallBlind;
u_int16_t handsBeforeRaise;
u_int16_t proposedGuiSpeed;
u_int32_t startCash;
u_int32_t startMoney;
};
struct GCC_PACKED NetPacketJoinGameErrorData
@@ -140,7 +140,7 @@ struct GCC_PACKED NetPacketPlayersActionData
NetPacketHeader head;
u_int16_t gameState;
u_int16_t playerAction;
u_int32_t cashValue;
u_int32_t playerBet;
};
struct GCC_PACKED NetPacketPlayersActionDoneData
@@ -150,7 +150,10 @@ struct GCC_PACKED NetPacketPlayersActionDoneData
u_int16_t playerId;
u_int16_t playerAction;
u_int16_t reserved;
u_int32_t cashValue;
u_int32_t playerBet;
u_int32_t curPlayerMoney;
u_int32_t potSize;
u_int32_t curHandBets;
};
struct GCC_PACKED NetPacketPlayersActionRejectedData
@@ -158,7 +161,7 @@ struct GCC_PACKED NetPacketPlayersActionRejectedData
NetPacketHeader head;
u_int16_t gameState;
u_int16_t playerAction;
u_int32_t cashValue;
u_int32_t playerBet;
u_int16_t rejectionReason;
u_int16_t reserved;
};
@@ -554,7 +557,7 @@ NetPacketJoinGameAck::SetData(const NetPacketJoinGameAck::Data &inData)
tmpData->smallBlind = htons(inData.gameData.smallBlind);
tmpData->handsBeforeRaise = htons(inData.gameData.handsBeforeRaise);
tmpData->proposedGuiSpeed = htons(inData.gameData.guiSpeed);
tmpData->startCash = htonl(inData.gameData.startCash);
tmpData->startMoney = htonl(inData.gameData.startMoney);
}
void
@@ -570,7 +573,7 @@ NetPacketJoinGameAck::GetData(NetPacketJoinGameAck::Data &outData) const
outData.gameData.smallBlind = ntohs(tmpData->smallBlind);
outData.gameData.handsBeforeRaise = ntohs(tmpData->handsBeforeRaise);
outData.gameData.guiSpeed = ntohs(tmpData->proposedGuiSpeed);
outData.gameData.startCash = ntohl(tmpData->startCash);
outData.gameData.startMoney = ntohl(tmpData->startMoney);
}
const NetPacketJoinGameAck *
@@ -983,7 +986,7 @@ NetPacketPlayersAction::SetData(const NetPacketPlayersAction::Data &inData)
tmpData->gameState = htons(inData.gameState);
tmpData->playerAction = htons(inData.playerAction);
tmpData->cashValue = htonl(inData.cashValue);
tmpData->playerBet = htonl(inData.playerBet);
}
void
@@ -994,7 +997,7 @@ NetPacketPlayersAction::GetData(NetPacketPlayersAction::Data &outData) const
outData.gameState = static_cast<GameState>(ntohs(tmpData->gameState));
outData.playerAction = static_cast<PlayerAction>(ntohs(tmpData->playerAction));
outData.cashValue = ntohl(tmpData->cashValue);
outData.playerBet = ntohl(tmpData->playerBet);
}
const NetPacketPlayersAction *
@@ -1061,7 +1064,10 @@ NetPacketPlayersActionDone::SetData(const NetPacketPlayersActionDone::Data &inDa
tmpData->gameState = htons(inData.gameState);
tmpData->playerId = htons(inData.playerId);
tmpData->playerAction = htons(inData.playerAction);
tmpData->cashValue = htonl(inData.cashValue);
tmpData->playerBet = htonl(inData.playerBet);
tmpData->curPlayerMoney = htonl(inData.curPlayerMoney);
tmpData->potSize = htonl(inData.potSize);
tmpData->curHandBets = htonl(inData.curHandBets);
}
void
@@ -1073,7 +1079,10 @@ 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.cashValue = ntohl(tmpData->cashValue);
outData.playerBet = ntohl(tmpData->playerBet);
outData.curPlayerMoney = ntohl(tmpData->curPlayerMoney);
outData.potSize = ntohl(tmpData->potSize);
outData.curHandBets = ntohl(tmpData->curHandBets);
}
const NetPacketPlayersActionDone *
@@ -1135,7 +1144,7 @@ NetPacketPlayersActionRejected::SetData(const NetPacketPlayersActionRejected::Da
tmpData->gameState = htons(inData.gameState);
tmpData->playerAction = htons(inData.playerAction);
tmpData->cashValue = htonl(inData.cashValue);
tmpData->playerBet = htonl(inData.playerBet);
// TODO: set rejection reason
tmpData->rejectionReason = htons(0);
@@ -1149,7 +1158,7 @@ NetPacketPlayersActionRejected::GetData(NetPacketPlayersActionRejected::Data &ou
outData.gameState = static_cast<GameState>(ntohs(tmpData->gameState));
outData.playerAction = static_cast<PlayerAction>(ntohs(tmpData->playerAction));
outData.cashValue = ntohl(tmpData->cashValue);
outData.playerBet = ntohl(tmpData->playerBet);
// TODO: set rejection reason
}
+85 -6
View File
@@ -294,6 +294,9 @@ ServerRecvStateStartHand::Process(ServerRecvThread &server)
server.GetSender().Send(playerArray[i]->getNetSessionData()->GetSocket(), notifyCards);
}
// Start hand.
curGame.startHand();
// Auto small blind / big blind at the beginning of hand.
for (int i = 0; i < curGame.getActualQuantityPlayers(); i++)
{
@@ -304,7 +307,10 @@ ServerRecvStateStartHand::Process(ServerRecvThread &server)
actionDoneData.gameState = GAME_STATE_PREFLOP_SMALL_BLIND;
actionDoneData.playerId = playerArray[i]->getMyUniqueID();
actionDoneData.playerAction = (PlayerAction)playerArray[i]->getMyAction();
actionDoneData.cashValue = playerArray[i]->getMySet();
actionDoneData.playerBet = playerArray[i]->getMySet();
actionDoneData.curPlayerMoney = playerArray[i]->getMyCash();
actionDoneData.potSize = curGame.getCurrentHand()->getBoard()->getPot();
actionDoneData.curHandBets = playerArray[i]->getMySet(); // first bet only
static_cast<NetPacketPlayersActionDone *>(notifySmallBlind.get())->SetData(actionDoneData);
server.SendToAllPlayers(notifySmallBlind);
break;
@@ -319,16 +325,16 @@ ServerRecvStateStartHand::Process(ServerRecvThread &server)
actionDoneData.gameState = GAME_STATE_PREFLOP_BIG_BLIND;
actionDoneData.playerId = playerArray[i]->getMyUniqueID();
actionDoneData.playerAction = (PlayerAction)playerArray[i]->getMyAction();
actionDoneData.cashValue = playerArray[i]->getMySet();
actionDoneData.playerBet = playerArray[i]->getMySet();
actionDoneData.curPlayerMoney = playerArray[i]->getMyCash();
actionDoneData.potSize = curGame.getCurrentHand()->getBoard()->getPot();
actionDoneData.curHandBets = curGame.getCurrentHand()->getBoard()->getSets();
static_cast<NetPacketPlayersActionDone *>(notifyBigBlind.get())->SetData(actionDoneData);
server.SendToAllPlayers(notifyBigBlind);
break;
}
}
// Start hand.
curGame.startHand();
server.SetState(ServerRecvStateStartRound::Instance());
return MSG_NET_GAME_SERVER_HAND;
@@ -386,7 +392,7 @@ ServerRecvStateStartRound::Process(ServerRecvThread &server)
server.SendToAllPlayers(notification);
server.SetState(ServerRecvStateFinal::Instance());
server.SetState(ServerRecvStateWaitPlayerAction::Instance());
return MSG_NET_GAME_SERVER_ROUND;
}
@@ -444,6 +450,79 @@ ServerRecvStateStartRound::GetCurrentPlayer(Game &curGame)
return curGame.getPlayerArray()[curPlayerNum];
}
//-----------------------------------------------------------------------------
ServerRecvStateWaitPlayerAction &
ServerRecvStateWaitPlayerAction::Instance()
{
static ServerRecvStateWaitPlayerAction state;
return state;
}
ServerRecvStateWaitPlayerAction::ServerRecvStateWaitPlayerAction()
{
}
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;
SOCKET recvSock = server.Select();
if (recvSock != INVALID_SOCKET)
{
SessionWrapper session = server.GetSession(recvSock);
boost::shared_ptr<NetPacket> packet;
try
{
packet = server.GetReceiver().Recv(recvSock);
} catch (const NetException &)
{
if (session.sessionData.get())
{
server.CloseSessionDelayed(session);
return retVal;
}
}
// Ignore if no session / no packet.
if (packet.get() && session.sessionData.get())
{
if (packet->ToNetPacketPlayersAction())
{
NetPacketPlayersAction::Data actionData;
packet->ToNetPacketPlayersAction()->GetData(actionData);
Game &curGame = server.GetGame();
boost::shared_ptr<NetPacket> notifyActionDone(new NetPacketPlayersActionDone);
NetPacketPlayersActionDone::Data actionDoneData;
actionDoneData.gameState = GAME_STATE_PREFLOP; // TODO
actionDoneData.playerId = session.playerData->GetUniqueId();
actionDoneData.playerAction = actionData.playerAction;
actionDoneData.playerBet = actionData.playerBet;
actionDoneData.curPlayerMoney = 0;
actionDoneData.potSize = curGame.getCurrentHand()->getBoard()->getPot();
actionDoneData.curHandBets = curGame.getCurrentHand()->getBoard()->getSets();
static_cast<NetPacketPlayersActionDone *>(notifyActionDone.get())->SetData(actionDoneData);
server.SendToAllPlayers(notifyActionDone);
}
}
}
return MSG_SOCK_INTERNAL_PENDING;
}
//-----------------------------------------------------------------------------
+1 -1
View File
@@ -54,7 +54,7 @@ private:
ServerRecvThread::ServerRecvThread(GuiInterface &gui, ConfigFile *playerConfig)
: m_curGameId(0), m_gui(gui), m_playerConfig(playerConfig)
: m_curGameId(1), m_gui(gui), m_playerConfig(playerConfig)
{
m_senderCallback.reset(new ServerSenderCallback(*this));
m_sender.reset(new SenderThread(GetSenderCallback()));