2007-02-18 11:26:57 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* 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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/serverrecvstate.h>
|
|
|
|
|
#include <net/serverrecvthread.h>
|
2007-03-20 02:20:50 +00:00
|
|
|
#include <net/receiverhelper.h>
|
|
|
|
|
#include <net/senderthread.h>
|
|
|
|
|
#include <net/netpacket.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/socket_msg.h>
|
2007-05-05 21:14:23 +00:00
|
|
|
#include <net/netexception.h>
|
2007-04-28 13:07:06 +00:00
|
|
|
#include <core/rand.h>
|
2007-04-27 21:35:18 +00:00
|
|
|
#include <gamedata.h>
|
2007-05-13 22:25:59 +00:00
|
|
|
#include <game.h>
|
|
|
|
|
#include <playerinterface.h>
|
2007-05-14 12:40:42 +00:00
|
|
|
#include <handinterface.h>
|
2007-02-18 11:26:57 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
#define SERVER_WAIT_TIMEOUT_MSEC 50
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ServerRecvState::~ServerRecvState()
|
2007-02-18 11:26:57 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
ServerRecvStateInit &
|
|
|
|
|
ServerRecvStateInit::Instance()
|
2007-02-18 11:26:57 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
static ServerRecvStateInit state;
|
|
|
|
|
return state;
|
2007-02-18 11:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ServerRecvStateInit::ServerRecvStateInit()
|
2007-04-27 21:35:18 +00:00
|
|
|
: m_curUniquePlayerId(0)
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvStateInit::~ServerRecvStateInit()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-03-20 02:20:50 +00:00
|
|
|
ServerRecvStateInit::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
2007-04-28 13:07:06 +00:00
|
|
|
// Create a random session id.
|
|
|
|
|
// This id can be used to reconnect to the server if the connection was lost.
|
|
|
|
|
unsigned sessionId;
|
|
|
|
|
RandomBytes((unsigned char *)&sessionId, sizeof(sessionId)); // TODO: check for collisions.
|
|
|
|
|
|
|
|
|
|
// Create a new session.
|
2007-05-05 21:14:23 +00:00
|
|
|
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), sessionId));
|
|
|
|
|
server.AddSession(sessionData);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ServerRecvStateInit::Process(ServerRecvThread &server)
|
|
|
|
|
{
|
2007-04-28 13:07:06 +00:00
|
|
|
int retVal = MSG_SOCK_INIT_DONE;
|
2007-03-20 02:20:50 +00:00
|
|
|
SOCKET recvSock = server.Select();
|
|
|
|
|
|
|
|
|
|
if (recvSock != INVALID_SOCKET)
|
|
|
|
|
{
|
2007-05-13 22:25:59 +00:00
|
|
|
SessionWrapper session = server.GetSession(recvSock);
|
2007-05-05 21:14:23 +00:00
|
|
|
boost::shared_ptr<NetPacket> packet;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
packet = server.GetReceiver().Recv(recvSock);
|
2007-05-06 23:27:08 +00:00
|
|
|
} catch (const NetException &)
|
2007-05-05 21:14:23 +00:00
|
|
|
{
|
2007-05-13 22:25:59 +00:00
|
|
|
if (session.sessionData.get())
|
2007-05-05 21:14:23 +00:00
|
|
|
{
|
2007-05-06 23:27:08 +00:00
|
|
|
server.CloseSessionDelayed(session);
|
2007-05-05 21:14:23 +00:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-03-20 02:20:50 +00:00
|
|
|
|
|
|
|
|
// Ignore if no session / no packet.
|
2007-05-13 22:25:59 +00:00
|
|
|
if (packet.get() && session.sessionData.get())
|
2007-03-20 02:20:50 +00:00
|
|
|
{
|
2007-04-28 13:07:06 +00:00
|
|
|
// Session should be in initial state.
|
2007-05-13 22:25:59 +00:00
|
|
|
if (session.sessionData->GetState() != SessionData::Init)
|
2007-03-20 02:20:50 +00:00
|
|
|
{
|
2007-05-05 21:40:24 +00:00
|
|
|
server.SessionError(session, ERR_SOCK_INVALID_STATE);
|
2007-04-28 13:07:06 +00:00
|
|
|
return retVal;
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
2007-04-28 13:07:06 +00:00
|
|
|
|
|
|
|
|
// Only accept join game packets.
|
|
|
|
|
const NetPacketJoinGame *tmpPacket = packet->ToNetPacketJoinGame();
|
|
|
|
|
if (!tmpPacket)
|
2007-03-20 02:20:50 +00:00
|
|
|
{
|
2007-05-05 21:40:24 +00:00
|
|
|
server.SessionError(session, ERR_SOCK_INVALID_PACKET);
|
2007-04-28 13:07:06 +00:00
|
|
|
return retVal;
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
2007-04-28 13:07:06 +00:00
|
|
|
|
|
|
|
|
NetPacketJoinGame::Data joinGameData;
|
|
|
|
|
tmpPacket->GetData(joinGameData);
|
|
|
|
|
|
|
|
|
|
// Check the protocol version.
|
|
|
|
|
if (joinGameData.versionMajor != NET_VERSION_MAJOR)
|
|
|
|
|
{
|
2007-05-05 21:40:24 +00:00
|
|
|
server.SessionError(session, ERR_NET_VERSION_NOT_SUPPORTED);
|
2007-04-28 13:07:06 +00:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-23 08:57:34 +00:00
|
|
|
size_t curNumPlayers = server.GetCurNumberOfPlayers();
|
|
|
|
|
|
|
|
|
|
// Check the number of players.
|
|
|
|
|
if (curNumPlayers >= (size_t)server.GetGameData().numberOfPlayers)
|
|
|
|
|
{
|
|
|
|
|
server.SessionError(session, ERR_NET_SERVER_FULL);
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-28 13:07:06 +00:00
|
|
|
// Check the server password.
|
|
|
|
|
if (!server.CheckPassword(joinGameData.password))
|
|
|
|
|
{
|
2007-05-05 21:40:24 +00:00
|
|
|
server.SessionError(session, ERR_NET_INVALID_PASSWORD);
|
2007-04-28 13:07:06 +00:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-23 08:57:34 +00:00
|
|
|
// Check whether the player name is correct.
|
|
|
|
|
// Paranoia check, this is also done in netpacket.
|
|
|
|
|
if (joinGameData.playerName.empty() || joinGameData.playerName.size() > MAX_NAME_SIZE)
|
2007-04-28 13:07:06 +00:00
|
|
|
{
|
2007-05-23 08:57:34 +00:00
|
|
|
server.SessionError(session, ERR_NET_INVALID_PLAYER_NAME);
|
2007-04-28 13:07:06 +00:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check whether this player is already connected.
|
2007-05-05 21:14:23 +00:00
|
|
|
if (server.IsPlayerConnected(joinGameData.playerName))
|
2007-04-28 13:07:06 +00:00
|
|
|
{
|
2007-05-05 21:40:24 +00:00
|
|
|
server.SessionError(session, ERR_NET_PLAYER_NAME_IN_USE);
|
2007-04-28 13:07:06 +00:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create player data object.
|
2007-05-08 06:45:46 +00:00
|
|
|
boost::shared_ptr<PlayerData> tmpPlayerData(
|
|
|
|
|
new PlayerData(m_curUniquePlayerId++, server.GetNextPlayerNumber(), joinGameData.ptype));
|
2007-04-28 13:07:06 +00:00
|
|
|
tmpPlayerData->SetName(joinGameData.playerName);
|
2007-05-13 22:25:59 +00:00
|
|
|
tmpPlayerData->SetNetSessionData(session.sessionData);
|
2007-04-28 13:07:06 +00:00
|
|
|
|
|
|
|
|
// Send ACK to client.
|
|
|
|
|
boost::shared_ptr<NetPacket> answer(new NetPacketJoinGameAck);
|
|
|
|
|
NetPacketJoinGameAck::Data joinGameAckData;
|
2007-05-13 22:25:59 +00:00
|
|
|
joinGameAckData.sessionId = session.sessionData->GetId(); // TODO: currently unused.
|
2007-05-21 19:00:06 +00:00
|
|
|
joinGameAckData.yourPlayerUniqueId = tmpPlayerData->GetUniqueId();
|
|
|
|
|
joinGameAckData.yourPlayerNum = tmpPlayerData->GetNumber();
|
2007-04-28 13:07:06 +00:00
|
|
|
joinGameAckData.gameData = server.GetGameData();
|
|
|
|
|
static_cast<NetPacketJoinGameAck *>(answer.get())->SetData(joinGameAckData);
|
2007-05-06 23:27:08 +00:00
|
|
|
server.GetSender().Send(recvSock, answer);
|
2007-04-28 13:07:06 +00:00
|
|
|
|
2007-05-06 23:27:08 +00:00
|
|
|
// Send notifications for connected players to client.
|
|
|
|
|
PlayerDataList tmpPlayerList = server.GetPlayerDataList();
|
|
|
|
|
PlayerDataList::iterator player_i = tmpPlayerList.begin();
|
|
|
|
|
PlayerDataList::iterator player_end = tmpPlayerList.end();
|
|
|
|
|
while (player_i != player_end)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> otherPlayerJoined(new NetPacketPlayerJoined);
|
|
|
|
|
NetPacketPlayerJoined::Data otherPlayerJoinedData;
|
|
|
|
|
otherPlayerJoinedData.playerId = (*player_i)->GetUniqueId();
|
|
|
|
|
otherPlayerJoinedData.playerName = (*player_i)->GetName();
|
|
|
|
|
otherPlayerJoinedData.playerNumber = (*player_i)->GetNumber();
|
|
|
|
|
otherPlayerJoinedData.ptype = (*player_i)->GetType();
|
|
|
|
|
static_cast<NetPacketPlayerJoined *>(otherPlayerJoined.get())->SetData(otherPlayerJoinedData);
|
2007-05-13 22:25:59 +00:00
|
|
|
server.GetSender().Send(session.sessionData->GetSocket(), otherPlayerJoined);
|
2007-05-06 23:27:08 +00:00
|
|
|
|
|
|
|
|
++player_i;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-23 08:57:34 +00:00
|
|
|
// Send "Player Joined" to other fully connected clients.
|
2007-05-06 23:27:08 +00:00
|
|
|
boost::shared_ptr<NetPacket> thisPlayerJoined(new NetPacketPlayerJoined);
|
|
|
|
|
NetPacketPlayerJoined::Data thisPlayerJoinedData;
|
|
|
|
|
thisPlayerJoinedData.playerId = tmpPlayerData->GetUniqueId();
|
|
|
|
|
thisPlayerJoinedData.playerName = tmpPlayerData->GetName();
|
|
|
|
|
thisPlayerJoinedData.playerNumber = tmpPlayerData->GetNumber();
|
|
|
|
|
thisPlayerJoinedData.ptype = tmpPlayerData->GetType();
|
|
|
|
|
static_cast<NetPacketPlayerJoined *>(thisPlayerJoined.get())->SetData(thisPlayerJoinedData);
|
2007-05-23 08:57:34 +00:00
|
|
|
server.SendToAllPlayers(thisPlayerJoined);
|
2007-05-06 23:27:08 +00:00
|
|
|
|
|
|
|
|
// Set player data for session.
|
2007-05-13 22:25:59 +00:00
|
|
|
server.SetSessionPlayerData(session.sessionData, tmpPlayerData);
|
2007-05-06 23:27:08 +00:00
|
|
|
|
|
|
|
|
// Session is now established.
|
2007-05-13 22:25:59 +00:00
|
|
|
session.sessionData->SetState(SessionData::Established);
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-04-28 13:07:06 +00:00
|
|
|
return retVal;
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
ServerRecvStateStartGame &
|
|
|
|
|
ServerRecvStateStartGame::Instance()
|
|
|
|
|
{
|
|
|
|
|
static ServerRecvStateStartGame state;
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvStateStartGame::ServerRecvStateStartGame()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvStateStartGame::~ServerRecvStateStartGame()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServerRecvStateStartGame::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
|
|
|
|
|
{
|
2007-05-23 08:57:34 +00:00
|
|
|
// Do not accept new connections in this state.
|
|
|
|
|
server.RejectNewConnection(connData);
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ServerRecvStateStartGame::Process(ServerRecvThread &server)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> answer(new NetPacketGameStart);
|
|
|
|
|
|
2007-05-18 17:13:27 +00:00
|
|
|
NetPacketGameStart::Data gameStartData;
|
2007-05-20 00:16:29 +00:00
|
|
|
gameStartData.startData = server.GetStartData();
|
2007-05-18 17:13:27 +00:00
|
|
|
static_cast<NetPacketGameStart *>(answer.get())->SetData(gameStartData);
|
|
|
|
|
|
2007-03-20 11:56:30 +00:00
|
|
|
server.SendToAllPlayers(answer);
|
2007-05-13 22:25:59 +00:00
|
|
|
server.SetState(ServerRecvStateStartHand::Instance());
|
|
|
|
|
|
|
|
|
|
return MSG_NET_GAME_SERVER_START;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
ServerRecvStateStartHand &
|
|
|
|
|
ServerRecvStateStartHand::Instance()
|
|
|
|
|
{
|
|
|
|
|
static ServerRecvStateStartHand state;
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvStateStartHand::ServerRecvStateStartHand()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvStateStartHand::~ServerRecvStateStartHand()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServerRecvStateStartHand::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
|
|
|
|
|
{
|
2007-05-23 08:57:34 +00:00
|
|
|
// Do not accept new connections in this state.
|
|
|
|
|
server.RejectNewConnection(connData);
|
2007-05-13 22:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ServerRecvStateStartHand::Process(ServerRecvThread &server)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> answer(new NetPacketHandStart);
|
|
|
|
|
|
2007-05-14 12:40:42 +00:00
|
|
|
// Initialize hand.
|
2007-05-13 22:25:59 +00:00
|
|
|
Game &curGame = server.GetGame();
|
|
|
|
|
curGame.initHand();
|
|
|
|
|
PlayerInterface **playerArray = curGame.getPlayerArray();
|
|
|
|
|
|
2007-05-14 12:40:42 +00:00
|
|
|
// Send cards to all players.
|
2007-05-13 22:25:59 +00:00
|
|
|
for (int i = 0; i < curGame.getActualQuantityPlayers(); i++)
|
|
|
|
|
{
|
2007-05-23 08:57:34 +00:00
|
|
|
assert(playerArray[i]->getNetSessionData().get()); // TODO throw exception
|
2007-05-13 22:25:59 +00:00
|
|
|
|
2007-05-23 08:57:34 +00:00
|
|
|
int cards[2];
|
|
|
|
|
playerArray[i]->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);
|
2007-05-13 22:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-23 08:57:34 +00:00
|
|
|
// Auto small blind / big blind at the beginning of hand.
|
2007-05-21 22:16:32 +00:00
|
|
|
for (int i = 0; i < curGame.getActualQuantityPlayers(); i++)
|
|
|
|
|
{
|
2007-05-22 23:27:21 +00:00
|
|
|
if(playerArray[i]->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.cashValue = playerArray[i]->getMySet();
|
|
|
|
|
static_cast<NetPacketPlayersActionDone *>(notifySmallBlind.get())->SetData(actionDoneData);
|
|
|
|
|
server.SendToAllPlayers(notifySmallBlind);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-05-21 22:16:32 +00:00
|
|
|
}
|
|
|
|
|
for (int i = 0; i < curGame.getActualQuantityPlayers(); i++)
|
|
|
|
|
{
|
2007-05-22 23:27:21 +00:00
|
|
|
if(playerArray[i]->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.cashValue = playerArray[i]->getMySet();
|
|
|
|
|
static_cast<NetPacketPlayersActionDone *>(notifyBigBlind.get())->SetData(actionDoneData);
|
|
|
|
|
server.SendToAllPlayers(notifyBigBlind);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-05-21 22:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-23 08:57:34 +00:00
|
|
|
// Start hand.
|
|
|
|
|
curGame.startHand();
|
|
|
|
|
|
2007-05-14 12:40:42 +00:00
|
|
|
server.SetState(ServerRecvStateStartRound::Instance());
|
2007-05-13 22:25:59 +00:00
|
|
|
|
|
|
|
|
return MSG_NET_GAME_SERVER_HAND;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2007-05-14 12:40:42 +00:00
|
|
|
ServerRecvStateStartRound &
|
|
|
|
|
ServerRecvStateStartRound::Instance()
|
|
|
|
|
{
|
|
|
|
|
static ServerRecvStateStartRound state;
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvStateStartRound::ServerRecvStateStartRound()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvStateStartRound::~ServerRecvStateStartRound()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServerRecvStateStartRound::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
|
|
|
|
|
{
|
2007-05-23 08:57:34 +00:00
|
|
|
// Do not accept new connections in this state.
|
|
|
|
|
server.RejectNewConnection(connData);
|
2007-05-14 12:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ServerRecvStateStartRound::Process(ServerRecvThread &server)
|
|
|
|
|
{
|
|
|
|
|
Game &curGame = server.GetGame();
|
|
|
|
|
|
|
|
|
|
curGame.getCurrentHand()->switchRounds();
|
2007-05-14 21:18:24 +00:00
|
|
|
|
2007-05-15 20:37:37 +00:00
|
|
|
// Call main loop - if state changes, call again.
|
|
|
|
|
int newRound = curGame.getCurrentHand()->getActualRound();
|
|
|
|
|
int curRound;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
curRound = newRound;
|
|
|
|
|
GameRun(curGame, curRound);
|
|
|
|
|
newRound = curGame.getCurrentHand()->getActualRound();
|
|
|
|
|
} while (newRound != curRound);
|
2007-05-14 21:18:24 +00:00
|
|
|
|
2007-05-15 20:37:37 +00:00
|
|
|
// Retrieve current player.
|
|
|
|
|
PlayerInterface *curPlayer = GetCurrentPlayer(curGame);
|
2007-05-14 21:18:24 +00:00
|
|
|
|
|
|
|
|
boost::shared_ptr<NetPacket> notification(new NetPacketPlayersTurn);
|
|
|
|
|
NetPacketPlayersTurn::Data playersTurnData;
|
|
|
|
|
playersTurnData.gameState = (GameState)curGame.getCurrentHand()->getActualRound();
|
2007-05-15 20:37:37 +00:00
|
|
|
playersTurnData.playerId = curPlayer->getMyUniqueID();
|
2007-05-14 21:18:24 +00:00
|
|
|
static_cast<NetPacketPlayersTurn *>(notification.get())->SetData(playersTurnData);
|
|
|
|
|
|
|
|
|
|
server.SendToAllPlayers(notification);
|
2007-05-14 12:40:42 +00:00
|
|
|
|
|
|
|
|
server.SetState(ServerRecvStateFinal::Instance());
|
|
|
|
|
|
|
|
|
|
return MSG_NET_GAME_SERVER_ROUND;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-15 20:37:37 +00:00
|
|
|
void
|
|
|
|
|
ServerRecvStateStartRound::GameRun(Game &curGame, int state)
|
|
|
|
|
{
|
|
|
|
|
// TODO: no switch needed here if game states are polymorphic
|
|
|
|
|
switch(state) {
|
|
|
|
|
case 0: {
|
|
|
|
|
// Preflop starten
|
|
|
|
|
curGame.getCurrentHand()->getPreflop()->preflopRun();
|
|
|
|
|
} break;
|
|
|
|
|
case 1: {
|
|
|
|
|
// Flop starten
|
|
|
|
|
curGame.getCurrentHand()->getFlop()->flopRun();
|
|
|
|
|
} break;
|
|
|
|
|
case 2: {
|
|
|
|
|
// Turn starten
|
|
|
|
|
curGame.getCurrentHand()->getTurn()->turnRun();
|
|
|
|
|
} break;
|
|
|
|
|
case 3: {
|
|
|
|
|
// River starten
|
|
|
|
|
curGame.getCurrentHand()->getRiver()->riverRun();
|
|
|
|
|
} break;
|
|
|
|
|
default: {
|
2007-05-23 08:57:34 +00:00
|
|
|
//
|
2007-05-15 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlayerInterface *
|
|
|
|
|
ServerRecvStateStartRound::GetCurrentPlayer(Game &curGame)
|
|
|
|
|
{
|
|
|
|
|
int curPlayerNum = 0;
|
|
|
|
|
// TODO: no switch needed here if game states are polymorphic
|
|
|
|
|
switch(curGame.getCurrentHand()->getActualRound()) {
|
|
|
|
|
case 0: {
|
|
|
|
|
curPlayerNum = curGame.getCurrentHand()->getPreflop()->getPlayersTurn();
|
|
|
|
|
} break;
|
|
|
|
|
case 1: {
|
|
|
|
|
curPlayerNum = curGame.getCurrentHand()->getFlop()->getPlayersTurn();
|
|
|
|
|
} break;
|
|
|
|
|
case 2: {
|
|
|
|
|
curPlayerNum = curGame.getCurrentHand()->getTurn()->getPlayersTurn();
|
|
|
|
|
} break;
|
|
|
|
|
case 3: {
|
|
|
|
|
curPlayerNum = curGame.getCurrentHand()->getRiver()->getPlayersTurn();
|
|
|
|
|
} break;
|
|
|
|
|
default: {
|
2007-05-23 08:57:34 +00:00
|
|
|
//
|
2007-05-15 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert(curPlayerNum < curGame.getActualQuantityPlayers());
|
|
|
|
|
return curGame.getPlayerArray()[curPlayerNum];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-14 12:40:42 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2007-05-13 22:25:59 +00:00
|
|
|
ServerRecvStateFinal &
|
|
|
|
|
ServerRecvStateFinal::Instance()
|
|
|
|
|
{
|
|
|
|
|
static ServerRecvStateFinal state;
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvStateFinal::ServerRecvStateFinal()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvStateFinal::~ServerRecvStateFinal()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServerRecvStateFinal::HandleNewConnection(ServerRecvThread &server, boost::shared_ptr<ConnectData> connData)
|
|
|
|
|
{
|
2007-05-23 08:57:34 +00:00
|
|
|
// Do not accept new connections in this state.
|
|
|
|
|
server.RejectNewConnection(connData);
|
2007-05-13 22:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ServerRecvStateFinal::Process(ServerRecvThread &server)
|
|
|
|
|
{
|
|
|
|
|
Thread::Msleep(10);
|
2007-03-20 02:20:50 +00:00
|
|
|
|
2007-03-20 11:56:30 +00:00
|
|
|
return MSG_SOCK_INTERNAL_PENDING;
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|