2007-02-16 00:03:30 +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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <net/clientthread.h>
|
2007-02-17 23:44:45 +00:00
|
|
|
#include <net/clientstate.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/clientcontext.h>
|
2007-02-25 23:16:26 +00:00
|
|
|
#include <net/senderthread.h>
|
2007-03-13 02:23:12 +00:00
|
|
|
#include <net/receiverhelper.h>
|
2007-02-18 11:26:57 +00:00
|
|
|
#include <net/clientexception.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
2007-05-20 00:16:29 +00:00
|
|
|
#include <clientenginefactory.h>
|
|
|
|
|
#include <game.h>
|
2007-02-17 23:44:45 +00:00
|
|
|
|
2007-05-20 00:16:29 +00:00
|
|
|
#include <boost/lambda/lambda.hpp>
|
2007-09-02 16:36:52 +00:00
|
|
|
#include <sstream>
|
2007-02-17 23:44:45 +00:00
|
|
|
#include <cassert>
|
2007-02-16 00:03:30 +00:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
class ClientSenderCallback : public SenderCallback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ClientSenderCallback(ClientThread &client) : m_client(client) {}
|
|
|
|
|
virtual ~ClientSenderCallback() {}
|
|
|
|
|
|
2007-09-03 18:05:39 +00:00
|
|
|
virtual void SignalNetError(SOCKET /*sock*/, int errorID, int osErrorID)
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
// For now, we ignore the socket.
|
|
|
|
|
// Just signal the error.
|
|
|
|
|
// We assume that the client thread will be terminated.
|
2007-03-20 02:20:50 +00:00
|
|
|
m_client.GetCallback().SignalNetClientError(errorID, osErrorID);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ClientThread &m_client;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2007-05-20 00:16:29 +00:00
|
|
|
ClientThread::ClientThread(GuiInterface &gui)
|
2007-08-26 12:15:10 +00:00
|
|
|
: m_curState(NULL), m_gui(gui), m_curGameId(1), m_guiPlayerId(0), m_sessionEstablished(false)
|
2007-02-16 00:03:30 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
m_context.reset(new ClientContext);
|
|
|
|
|
m_senderCallback.reset(new ClientSenderCallback(*this));
|
2007-03-20 11:56:30 +00:00
|
|
|
m_sender.reset(new SenderThread(GetSenderCallback()));
|
|
|
|
|
m_receiver.reset(new ReceiverHelper);
|
2007-02-16 00:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClientThread::~ClientThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-04-08 16:18:20 +00:00
|
|
|
ClientThread::Init(
|
2007-06-04 16:49:19 +00:00
|
|
|
const string &serverAddress, unsigned serverPort, bool ipv6, bool sctp, const string &pwd, const string &playerName)
|
2007-02-16 00:03:30 +00:00
|
|
|
{
|
|
|
|
|
if (IsRunning())
|
2007-06-03 13:36:24 +00:00
|
|
|
{
|
|
|
|
|
assert(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-02-18 11:26:57 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ClientContext &context = GetContext();
|
2007-02-18 11:26:57 +00:00
|
|
|
|
2007-06-04 16:49:19 +00:00
|
|
|
context.SetProtocol(sctp ? SOCKET_IPPROTO_SCTP : 0);
|
2007-03-13 23:27:17 +00:00
|
|
|
context.SetAddrFamily(ipv6 ? AF_INET6 : AF_INET);
|
|
|
|
|
context.SetServerAddr(serverAddress);
|
|
|
|
|
context.SetServerPort(serverPort);
|
|
|
|
|
context.SetPassword(pwd);
|
2007-04-08 16:18:20 +00:00
|
|
|
context.SetPlayerName(playerName);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-02 20:52:13 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::SendKickPlayer(const string &playerName)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayer = GetPlayerDataByName(playerName);
|
|
|
|
|
if (tmpPlayer.get())
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> request(new NetPacketKickPlayer);
|
|
|
|
|
NetPacketKickPlayer::Data requestData;
|
|
|
|
|
requestData.playerId = tmpPlayer->GetUniqueId();
|
|
|
|
|
static_cast<NetPacketKickPlayer *>(request.get())->SetData(requestData);
|
2007-08-26 12:15:10 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
|
|
|
|
m_outPacketList.push_back(request);
|
2007-08-02 20:52:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::SendStartEvent()
|
|
|
|
|
{
|
|
|
|
|
// Warning: This function is called in the context of the GUI thread.
|
|
|
|
|
// Create a network packet for the server start event.
|
|
|
|
|
boost::shared_ptr<NetPacket> startEvent(new NetPacketStartEvent);
|
2007-08-26 12:15:10 +00:00
|
|
|
// Just dump the packet.
|
|
|
|
|
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
|
|
|
|
m_outPacketList.push_back(startEvent);
|
2007-08-02 20:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-23 22:31:56 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::SendPlayerAction()
|
|
|
|
|
{
|
2007-05-24 20:26:23 +00:00
|
|
|
// Warning: This function is called in the context of the GUI thread.
|
|
|
|
|
// Create a network packet containing the current player action.
|
2007-05-23 22:31:56 +00:00
|
|
|
boost::shared_ptr<NetPacket> action(new NetPacketPlayersAction);
|
|
|
|
|
NetPacketPlayersAction::Data actionData;
|
2007-05-24 20:26:23 +00:00
|
|
|
actionData.gameState = static_cast<GameState>(GetGame()->getCurrentHand()->getActualRound());
|
|
|
|
|
actionData.playerAction = static_cast<PlayerAction>(GetGame()->getPlayerArray()[0]->getMyAction());
|
2007-06-02 16:35:41 +00:00
|
|
|
// 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();
|
|
|
|
|
else
|
|
|
|
|
actionData.playerBet = 0;
|
2007-08-26 12:15:10 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
static_cast<NetPacketPlayersAction *>(action.get())->SetData(actionData);
|
|
|
|
|
// Just dump the packet.
|
|
|
|
|
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
|
|
|
|
m_outPacketList.push_back(action);
|
|
|
|
|
} catch (const NetException &)
|
|
|
|
|
{
|
|
|
|
|
}
|
2007-05-23 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-25 16:03:12 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::SendChatMessage(const std::string &msg)
|
|
|
|
|
{
|
|
|
|
|
// Warning: This function is called in the context of the GUI thread.
|
|
|
|
|
// Create a network packet containing the chat message.
|
|
|
|
|
boost::shared_ptr<NetPacket> chat(new NetPacketSendChatText);
|
|
|
|
|
NetPacketSendChatText::Data chatData;
|
|
|
|
|
chatData.text = msg;
|
2007-05-27 22:23:48 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
static_cast<NetPacketSendChatText *>(chat.get())->SetData(chatData);
|
2007-08-26 12:15:10 +00:00
|
|
|
// Just dump the packet.
|
|
|
|
|
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
|
|
|
|
m_outPacketList.push_back(chat);
|
2007-06-02 15:53:19 +00:00
|
|
|
} catch (const NetException &)
|
2007-05-27 22:23:48 +00:00
|
|
|
{
|
|
|
|
|
}
|
2007-05-25 16:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-26 12:15:10 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::SendJoinFirstGame(const std::string &password)
|
|
|
|
|
{
|
|
|
|
|
// Warning: This function is called in the context of the GUI thread.
|
|
|
|
|
// Create a network packet to request joining a game.
|
|
|
|
|
boost::shared_ptr<NetPacket> join(new NetPacketJoinGame);
|
|
|
|
|
NetPacketJoinGame::Data joinData;
|
|
|
|
|
joinData.gameId = 0;
|
|
|
|
|
joinData.password = password;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData);
|
|
|
|
|
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
|
|
|
|
m_outPacketList.push_back(join);
|
|
|
|
|
} catch (const NetException &)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 23:25:12 +00:00
|
|
|
void
|
2007-08-21 12:34:33 +00:00
|
|
|
ClientThread::SendJoinGame(const std::string &name, const std::string &password)
|
2007-08-20 23:25:12 +00:00
|
|
|
{
|
2007-08-21 12:34:33 +00:00
|
|
|
// Warning: This function is called in the context of the GUI thread.
|
|
|
|
|
// Create a network packet to request joining a game.
|
|
|
|
|
boost::shared_ptr<NetPacket> join(new NetPacketJoinGame);
|
|
|
|
|
NetPacketJoinGame::Data joinData;
|
|
|
|
|
joinData.password = password;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
joinData.gameId = GetGameIdByName(name);
|
|
|
|
|
static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData);
|
2007-08-26 12:15:10 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
|
|
|
|
m_outPacketList.push_back(join);
|
2007-08-21 12:34:33 +00:00
|
|
|
} catch (const NetException &)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
2007-08-20 23:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-08-21 12:34:33 +00:00
|
|
|
ClientThread::SendCreateGame(const GameData &gameData, const std::string &name, const std::string &password)
|
2007-08-20 23:25:12 +00:00
|
|
|
{
|
2007-08-21 12:34:33 +00:00
|
|
|
// Warning: This function is called in the context of the GUI thread.
|
|
|
|
|
// Create a network packet to request creating a new game.
|
|
|
|
|
boost::shared_ptr<NetPacket> create(new NetPacketCreateGame);
|
|
|
|
|
NetPacketCreateGame::Data createData;
|
|
|
|
|
createData.gameData = gameData;
|
|
|
|
|
createData.gameName = name;
|
|
|
|
|
createData.password = password;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
static_cast<NetPacketCreateGame *>(create.get())->SetData(createData);
|
2007-08-26 12:15:10 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
|
|
|
|
m_outPacketList.push_back(create);
|
2007-08-21 12:34:33 +00:00
|
|
|
} catch (const NetException &)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
2007-08-20 23:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-27 09:57:51 +00:00
|
|
|
GameInfo
|
2007-09-02 16:36:52 +00:00
|
|
|
ClientThread::GetGameInfo(unsigned gameId) const
|
2007-08-27 09:57:51 +00:00
|
|
|
{
|
|
|
|
|
GameInfo tmpInfo;
|
2007-09-02 16:36:52 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
|
|
|
|
|
GameInfoMap::const_iterator pos = m_gameInfoMap.find(gameId);
|
|
|
|
|
if (pos != m_gameInfoMap.end())
|
2007-08-27 09:57:51 +00:00
|
|
|
{
|
2007-09-02 16:36:52 +00:00
|
|
|
tmpInfo = pos->second;
|
2007-08-27 09:57:51 +00:00
|
|
|
}
|
|
|
|
|
return tmpInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
PlayerInfo
|
|
|
|
|
ClientThread::GetPlayerInfo(unsigned playerId) const
|
|
|
|
|
{
|
|
|
|
|
PlayerInfo info;
|
|
|
|
|
if (!GetCachedPlayerInfo(playerId, info))
|
|
|
|
|
{
|
|
|
|
|
ostringstream name;
|
|
|
|
|
name << "#" << playerId;
|
|
|
|
|
|
|
|
|
|
info.playerName = name.str();
|
|
|
|
|
}
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ClientCallback &
|
|
|
|
|
ClientThread::GetCallback()
|
|
|
|
|
{
|
2007-05-20 00:16:29 +00:00
|
|
|
return m_gui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GuiInterface &
|
|
|
|
|
ClientThread::GetGui()
|
|
|
|
|
{
|
|
|
|
|
return m_gui;
|
2007-02-16 00:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::Main()
|
|
|
|
|
{
|
2007-02-18 11:26:57 +00:00
|
|
|
SetState(CLIENT_INITIAL_STATE::Instance());
|
2007-03-13 23:27:17 +00:00
|
|
|
|
|
|
|
|
GetSender().Run();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2007-02-18 11:26:57 +00:00
|
|
|
while (!ShouldTerminate())
|
|
|
|
|
{
|
|
|
|
|
int msg = GetState().Process(*this);
|
|
|
|
|
if (msg != MSG_SOCK_INTERNAL_PENDING)
|
2007-03-20 02:20:50 +00:00
|
|
|
{
|
|
|
|
|
if (msg <= MSG_SOCK_LIMIT_CONNECT)
|
|
|
|
|
GetCallback().SignalNetClientConnect(msg);
|
|
|
|
|
else
|
|
|
|
|
GetCallback().SignalNetClientGameInfo(msg);
|
2007-04-27 21:35:18 +00:00
|
|
|
|
|
|
|
|
// Additionally signal the start of the game.
|
2007-05-13 22:25:59 +00:00
|
|
|
if (msg == MSG_NET_GAME_CLIENT_START)
|
2007-05-20 00:16:29 +00:00
|
|
|
{
|
|
|
|
|
// EngineFactory erstellen
|
|
|
|
|
boost::shared_ptr<EngineFactory> factory(new ClientEngineFactory); // LocalEngine erstellen
|
|
|
|
|
|
2007-05-25 16:03:12 +00:00
|
|
|
MapPlayerDataList();
|
2007-06-11 19:46:18 +00:00
|
|
|
if (GetPlayerDataList().size() != GetStartData().numberOfPlayers)
|
|
|
|
|
throw NetException(ERR_NET_INVALID_PLAYER_COUNT, 0);
|
2007-05-25 16:03:12 +00:00
|
|
|
m_game.reset(new Game(&m_gui, factory, GetPlayerDataList(), GetGameData(), GetStartData(), m_curGameId++));
|
2007-06-02 21:19:19 +00:00
|
|
|
// Initialize GUI speed.
|
|
|
|
|
GetGui().initGui(GetGameData().guiSpeed);
|
|
|
|
|
// Signal start of game to GUI.
|
2007-05-20 00:16:29 +00:00
|
|
|
GetCallback().SignalNetClientGameStart(m_game);
|
|
|
|
|
}
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
2007-08-26 12:15:10 +00:00
|
|
|
if (IsSessionEstablished())
|
|
|
|
|
SendPacketLoop();
|
2007-02-18 11:26:57 +00:00
|
|
|
}
|
2007-03-13 02:23:12 +00:00
|
|
|
} catch (const NetException &e)
|
2007-02-16 00:03:30 +00:00
|
|
|
{
|
2007-03-20 02:20:50 +00:00
|
|
|
GetCallback().SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode());
|
2007-02-16 00:03:30 +00:00
|
|
|
}
|
2007-02-25 23:16:26 +00:00
|
|
|
GetSender().SignalTermination();
|
2007-03-13 23:27:17 +00:00
|
|
|
GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT);
|
2007-02-16 00:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-26 12:15:10 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::AddPacket(boost::shared_ptr<NetPacket> packet)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
|
|
|
|
m_outPacketList.push_back(packet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::SendPacketLoop()
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_outPacketListMutex);
|
|
|
|
|
|
|
|
|
|
if (!m_outPacketList.empty())
|
|
|
|
|
{
|
|
|
|
|
NetPacketList::iterator i = m_outPacketList.begin();
|
|
|
|
|
NetPacketList::iterator end = m_outPacketList.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
GetSender().Send(GetContext().GetSocket(), *i);
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
m_outPacketList.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
bool
|
|
|
|
|
ClientThread::GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const
|
2007-08-26 22:41:37 +00:00
|
|
|
{
|
2007-09-02 16:36:52 +00:00
|
|
|
bool retVal = false;
|
|
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_playerInfoMapMutex);
|
2007-08-26 22:41:37 +00:00
|
|
|
PlayerInfoMap::const_iterator pos = m_playerInfoMap.find(id);
|
2007-09-02 16:36:52 +00:00
|
|
|
if (pos != m_playerInfoMap.end())
|
|
|
|
|
{
|
|
|
|
|
info = pos->second;
|
|
|
|
|
retVal = true;
|
|
|
|
|
}
|
|
|
|
|
return retVal;
|
2007-08-26 22:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-09-02 16:36:52 +00:00
|
|
|
ClientThread::RequestPlayerInfo(unsigned id)
|
2007-08-26 22:41:37 +00:00
|
|
|
{
|
2007-09-02 16:36:52 +00:00
|
|
|
if (find(m_playerInfoRequestList.begin(), m_playerInfoRequestList.end(), id) == m_playerInfoRequestList.end())
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> req(new NetPacketRetrievePlayerInfo);
|
|
|
|
|
NetPacketRetrievePlayerInfo::Data reqData;
|
|
|
|
|
reqData.playerId = id;
|
|
|
|
|
static_cast<NetPacketRetrievePlayerInfo *>(req.get())->SetData(reqData);
|
|
|
|
|
GetSender().Send(GetContext().GetSocket(), req);
|
2007-08-26 22:41:37 +00:00
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
m_playerInfoRequestList.push_back(id);
|
|
|
|
|
}
|
2007-08-26 22:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::SetPlayerInfo(unsigned id, const PlayerInfo &info)
|
|
|
|
|
{
|
|
|
|
|
{
|
2007-09-02 16:36:52 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_playerInfoMapMutex);
|
|
|
|
|
m_playerInfoMap[id] = info;
|
2007-08-26 22:41:37 +00:00
|
|
|
}
|
|
|
|
|
// Update player data for current game.
|
|
|
|
|
boost::shared_ptr<PlayerData> playerData = GetPlayerDataByUniqueId(id);
|
|
|
|
|
if (playerData.get())
|
|
|
|
|
{
|
|
|
|
|
playerData->SetName(info.playerName);
|
|
|
|
|
playerData->SetType(info.ptype);
|
|
|
|
|
}
|
2007-09-02 22:54:33 +00:00
|
|
|
|
|
|
|
|
GetCallback().SignalNetClientPlayerChanged(id, info.playerName);
|
2007-08-26 22:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
const ClientContext &
|
|
|
|
|
ClientThread::GetContext() const
|
2007-02-17 23:44:45 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
2007-02-17 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ClientContext &
|
|
|
|
|
ClientThread::GetContext()
|
2007-02-17 23:44:45 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
2007-02-17 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
2007-02-18 00:10:26 +00:00
|
|
|
ClientState &
|
|
|
|
|
ClientThread::GetState()
|
|
|
|
|
{
|
|
|
|
|
assert(m_curState);
|
|
|
|
|
return *m_curState;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-17 23:44:45 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::SetState(ClientState &newState)
|
|
|
|
|
{
|
|
|
|
|
m_curState = &newState;
|
|
|
|
|
}
|
2007-02-25 23:16:26 +00:00
|
|
|
|
|
|
|
|
SenderThread &
|
|
|
|
|
ClientThread::GetSender()
|
|
|
|
|
{
|
|
|
|
|
assert(m_sender.get());
|
|
|
|
|
return *m_sender;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 02:23:12 +00:00
|
|
|
ReceiverHelper &
|
|
|
|
|
ClientThread::GetReceiver()
|
|
|
|
|
{
|
|
|
|
|
assert(m_receiver.get());
|
|
|
|
|
return *m_receiver;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-27 21:35:18 +00:00
|
|
|
const GameData &
|
|
|
|
|
ClientThread::GetGameData() const
|
|
|
|
|
{
|
2007-05-20 00:16:29 +00:00
|
|
|
return m_gameData;
|
2007-04-27 21:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::SetGameData(const GameData &gameData)
|
|
|
|
|
{
|
2007-05-20 00:16:29 +00:00
|
|
|
m_gameData = gameData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const StartData &
|
|
|
|
|
ClientThread::GetStartData() const
|
|
|
|
|
{
|
|
|
|
|
return m_startData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::SetStartData(const StartData &startData)
|
|
|
|
|
{
|
|
|
|
|
m_startData = startData;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-21 19:00:06 +00:00
|
|
|
int
|
2007-07-24 14:55:14 +00:00
|
|
|
ClientThread::GetGuiPlayerId() const
|
2007-05-21 19:00:06 +00:00
|
|
|
{
|
2007-07-24 14:55:14 +00:00
|
|
|
return m_guiPlayerId;
|
2007-05-21 19:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-07-24 14:55:14 +00:00
|
|
|
ClientThread::SetGuiPlayerId(int guiPlayerId)
|
2007-05-21 19:00:06 +00:00
|
|
|
{
|
2007-07-24 14:55:14 +00:00
|
|
|
m_guiPlayerId = guiPlayerId;
|
2007-05-21 19:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-20 00:16:29 +00:00
|
|
|
boost::shared_ptr<Game>
|
|
|
|
|
ClientThread::GetGame()
|
|
|
|
|
{
|
|
|
|
|
return m_game;
|
2007-04-27 21:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ClientSenderCallback &
|
|
|
|
|
ClientThread::GetSenderCallback()
|
|
|
|
|
{
|
|
|
|
|
assert(m_senderCallback.get());
|
|
|
|
|
return *m_senderCallback;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-20 00:16:29 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::AddPlayerData(boost::shared_ptr<PlayerData> playerData)
|
2007-05-06 23:27:08 +00:00
|
|
|
{
|
2007-05-20 00:16:29 +00:00
|
|
|
if (playerData.get() && !playerData->GetName().empty())
|
|
|
|
|
{
|
|
|
|
|
m_playerDataList.push_back(playerData);
|
2007-08-02 20:52:13 +00:00
|
|
|
if (playerData->GetUniqueId() == GetGuiPlayerId())
|
2007-09-02 16:36:52 +00:00
|
|
|
GetCallback().SignalNetClientSelfJoined(playerData->GetUniqueId(), playerData->GetName(), playerData->GetRights());
|
2007-08-02 20:52:13 +00:00
|
|
|
else
|
2007-09-02 16:36:52 +00:00
|
|
|
GetCallback().SignalNetClientPlayerJoined(playerData->GetUniqueId(), playerData->GetName(), playerData->GetRights());
|
2007-05-20 00:16:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::RemovePlayerData(unsigned playerId)
|
|
|
|
|
{
|
2007-09-02 16:36:52 +00:00
|
|
|
boost::shared_ptr<PlayerData> tmpData;
|
2007-05-20 00:16:29 +00:00
|
|
|
|
|
|
|
|
PlayerDataList::iterator i = m_playerDataList.begin();
|
|
|
|
|
PlayerDataList::iterator end = m_playerDataList.end();
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
if ((*i)->GetUniqueId() == playerId)
|
|
|
|
|
{
|
2007-09-02 16:36:52 +00:00
|
|
|
tmpData = *i;
|
2007-05-20 00:16:29 +00:00
|
|
|
m_playerDataList.erase(i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
if (tmpData.get())
|
2007-08-27 09:57:51 +00:00
|
|
|
{
|
2007-09-02 16:36:52 +00:00
|
|
|
// Remove player from gui.
|
|
|
|
|
GetCallback().SignalNetClientPlayerLeft(tmpData->GetUniqueId(), tmpData->GetName());
|
2007-08-27 09:57:51 +00:00
|
|
|
}
|
2007-05-20 00:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-25 16:03:12 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::MapPlayerDataList()
|
2007-05-20 00:16:29 +00:00
|
|
|
{
|
2007-07-24 14:55:14 +00:00
|
|
|
// Retrieve the GUI player.
|
|
|
|
|
boost::shared_ptr<PlayerData> guiPlayer = GetPlayerDataByUniqueId(GetGuiPlayerId());
|
|
|
|
|
assert(guiPlayer.get());
|
|
|
|
|
int guiPlayerNum = guiPlayer->GetNumber();
|
|
|
|
|
|
|
|
|
|
// Create a copy of the player list so that the GUI player
|
|
|
|
|
// is player 0. This is mapped because the GUI depends on it.
|
2007-05-21 19:00:06 +00:00
|
|
|
PlayerDataList mappedList;
|
|
|
|
|
|
|
|
|
|
PlayerDataList::const_iterator i = m_playerDataList.begin();
|
|
|
|
|
PlayerDataList::const_iterator end = m_playerDataList.end();
|
2007-07-24 14:55:14 +00:00
|
|
|
int numPlayers = GetStartData().numberOfPlayers;
|
2007-05-21 19:00:06 +00:00
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpData(new PlayerData(*(*i)));
|
2007-07-24 14:55:14 +00:00
|
|
|
int numberDiff = tmpData->GetNumber() - guiPlayerNum;
|
2007-05-21 19:00:06 +00:00
|
|
|
if (numberDiff >= 0)
|
|
|
|
|
tmpData->SetNumber(numberDiff);
|
|
|
|
|
else
|
2007-07-24 14:55:14 +00:00
|
|
|
tmpData->SetNumber(numPlayers + numberDiff);
|
2007-05-21 19:00:06 +00:00
|
|
|
mappedList.push_back(tmpData);
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort the list by player number.
|
|
|
|
|
mappedList.sort(*boost::lambda::_1 < *boost::lambda::_2);
|
|
|
|
|
|
2007-05-25 16:03:12 +00:00
|
|
|
m_playerDataList = mappedList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PlayerDataList &
|
|
|
|
|
ClientThread::GetPlayerDataList() const
|
|
|
|
|
{
|
|
|
|
|
return m_playerDataList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr<PlayerData>
|
|
|
|
|
ClientThread::GetPlayerDataByUniqueId(unsigned id)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayer;
|
|
|
|
|
|
|
|
|
|
PlayerDataList::const_iterator i = m_playerDataList.begin();
|
|
|
|
|
PlayerDataList::const_iterator end = m_playerDataList.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
if ((*i)->GetUniqueId() == id)
|
|
|
|
|
{
|
|
|
|
|
tmpPlayer = *i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return tmpPlayer;
|
2007-05-06 23:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-02 20:52:13 +00:00
|
|
|
boost::shared_ptr<PlayerData>
|
|
|
|
|
ClientThread::GetPlayerDataByName(const std::string &name)
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayer;
|
|
|
|
|
|
|
|
|
|
if (!name.empty())
|
|
|
|
|
{
|
|
|
|
|
PlayerDataList::const_iterator i = m_playerDataList.begin();
|
|
|
|
|
PlayerDataList::const_iterator end = m_playerDataList.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
if ((*i)->GetName() == name)
|
|
|
|
|
{
|
|
|
|
|
tmpPlayer = *i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tmpPlayer;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-15 10:38:41 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::RemoveDisconnectedPlayers()
|
|
|
|
|
{
|
|
|
|
|
// This should only be called between hands.
|
|
|
|
|
if (m_game.get())
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < m_game->getStartQuantityPlayers(); i++)
|
|
|
|
|
{
|
2007-08-10 23:34:56 +00:00
|
|
|
boost::shared_ptr<PlayerInterface> tmpPlayer = m_game->getPlayerArray()[i];
|
2007-06-15 10:38:41 +00:00
|
|
|
if (tmpPlayer->getMyActiveStatus())
|
|
|
|
|
{
|
|
|
|
|
// If a player is not in the player data list, it was disconnected.
|
|
|
|
|
if (!GetPlayerDataByUniqueId(tmpPlayer->getMyUniqueID()).get())
|
|
|
|
|
{
|
|
|
|
|
tmpPlayer->setMyCash(0);
|
|
|
|
|
tmpPlayer->setMyActiveStatus(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-21 12:34:33 +00:00
|
|
|
unsigned
|
|
|
|
|
ClientThread::GetGameIdByName(const std::string &name) const
|
|
|
|
|
{
|
|
|
|
|
// Find the game.
|
2007-08-27 09:57:51 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
|
|
|
|
|
GameInfoMap::const_iterator i = m_gameInfoMap.begin();
|
|
|
|
|
GameInfoMap::const_iterator end = m_gameInfoMap.end();
|
2007-08-21 19:50:13 +00:00
|
|
|
while (i != end)
|
|
|
|
|
{
|
2007-08-27 09:57:51 +00:00
|
|
|
if (i->second.name == name)
|
2007-08-21 19:50:13 +00:00
|
|
|
break;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i == end)
|
2007-08-21 12:34:33 +00:00
|
|
|
throw NetException(ERR_NET_UNKNOWN_GAME, 0);
|
2007-08-21 19:50:13 +00:00
|
|
|
return i->first;
|
2007-08-21 12:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-09-02 16:36:52 +00:00
|
|
|
ClientThread::AddGameInfo(unsigned gameId, const GameInfo &info)
|
2007-08-21 12:34:33 +00:00
|
|
|
{
|
|
|
|
|
{
|
2007-08-27 09:57:51 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
|
2007-09-02 16:36:52 +00:00
|
|
|
m_gameInfoMap.insert(GameInfoMap::value_type(gameId, info));
|
2007-08-21 12:34:33 +00:00
|
|
|
}
|
2007-09-02 16:36:52 +00:00
|
|
|
GetCallback().SignalNetClientGameListNew(gameId, info.name);
|
2007-08-21 19:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-09-02 16:36:52 +00:00
|
|
|
ClientThread::RemoveGameInfo(unsigned gameId)
|
2007-08-21 19:50:13 +00:00
|
|
|
{
|
|
|
|
|
string name;
|
|
|
|
|
{
|
2007-08-27 09:57:51 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
|
2007-09-02 16:36:52 +00:00
|
|
|
GameInfoMap::iterator pos = m_gameInfoMap.find(gameId);
|
2007-08-27 09:57:51 +00:00
|
|
|
if (pos != m_gameInfoMap.end())
|
2007-08-21 19:50:13 +00:00
|
|
|
{
|
2007-08-27 09:57:51 +00:00
|
|
|
name = pos->second.name;
|
|
|
|
|
m_gameInfoMap.erase(pos);
|
2007-08-21 19:50:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!name.empty())
|
2007-09-02 16:36:52 +00:00
|
|
|
GetCallback().SignalNetClientGameListRemove(gameId, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::ModifyGameInfoAddPlayer(unsigned gameId, unsigned playerId)
|
|
|
|
|
{
|
|
|
|
|
bool playerAdded = false;
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
|
|
|
|
|
GameInfoMap::iterator pos = m_gameInfoMap.find(gameId);
|
|
|
|
|
if (pos != m_gameInfoMap.end())
|
|
|
|
|
{
|
|
|
|
|
pos->second.players.push_back(playerId);
|
|
|
|
|
playerAdded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (playerAdded)
|
|
|
|
|
GetCallback().SignalNetClientGameListPlayerJoined(gameId, playerId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::ModifyGameInfoRemovePlayer(unsigned gameId, unsigned playerId)
|
|
|
|
|
{
|
|
|
|
|
bool playerRemoved = false;
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
|
|
|
|
|
GameInfoMap::iterator pos = m_gameInfoMap.find(gameId);
|
|
|
|
|
if (pos != m_gameInfoMap.end())
|
|
|
|
|
{
|
|
|
|
|
pos->second.players.remove(playerId);
|
|
|
|
|
playerRemoved = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (playerRemoved)
|
|
|
|
|
GetCallback().SignalNetClientGameListPlayerLeft(gameId, playerId);
|
2007-08-21 12:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-26 12:15:10 +00:00
|
|
|
bool
|
|
|
|
|
ClientThread::IsSessionEstablished() const
|
|
|
|
|
{
|
|
|
|
|
return m_sessionEstablished;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::SetSessionEstablished(bool flag)
|
|
|
|
|
{
|
|
|
|
|
m_sessionEstablished = flag;
|
|
|
|
|
}
|
|
|
|
|
|