Files
pokerth/src/session.cpp
T

567 lines
14 KiB
C++
Raw Normal View History

2011-07-02 14:45:12 +00:00
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* Copyright (C) 2006-2011 Felix Hammer, Florian Thauer, Lothar May *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#include <boost/asio.hpp>
#include <net/socket_helper.h>
2007-01-13 02:40:33 +00:00
#include "session.h"
#include "game.h"
#include "log.h"
2007-01-13 02:40:33 +00:00
#include "guiinterface.h"
2007-01-21 14:20:15 +00:00
#include "configfile.h"
#include <qttoolsinterface.h>
#include <localenginefactory.h>
#include <clientenginefactory.h>
2007-02-18 13:45:56 +00:00
#include <net/clientthread.h>
#include <core/avatarmanager.h>
#include <net/servermanagerfactory.h>
2007-01-21 14:20:15 +00:00
#include <sstream>
2009-03-21 01:39:00 +00:00
#define NET_CLIENT_TERMINATE_TIMEOUT_MSEC 2000
#define NET_IRC_TERMINATE_TIMEOUT_MSEC 2000
2007-01-13 02:40:33 +00:00
#define NET_DEFAULT_GAME "default"
2007-01-13 02:40:33 +00:00
using namespace std;
2007-05-13 12:35:27 +00:00
Session::Session(GuiInterface *g, ConfigFile *c)
: currentGameNum(0), myGui(g), myConfig(c), myLog(0), myGameType(GAME_TYPE_NONE)
{
myQtToolsInterface = CreateQtToolsWrapper();
2011-10-26 22:29:47 +00:00
myLog = new Log(myConfig);
2007-01-13 02:40:33 +00:00
}
Session::~Session()
{
terminateNetworkClient();
terminateNetworkServer();
delete myQtToolsInterface;
myQtToolsInterface = 0;
delete myLog;
myLog = 0;
2007-01-13 02:40:33 +00:00
}
2007-09-08 21:43:15 +00:00
bool Session::init()
{
myAvatarManager.reset(new AvatarManager(
myConfig->readConfigInt("ServerUsePutAvatars") == 1,
myConfig->readConfigString("ServerPutAvatarsAddress"),
myConfig->readConfigString("ServerPutAvatarsUser"),
myConfig->readConfigString("ServerPutAvatarsPassword")
));
bool retVal = myAvatarManager->Init(
myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("AppDataDir")),
myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("CacheDir")));
addOwnAvatar(myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("MyAvatar")));
myAvatarManager->RemoveOldAvatarCacheEntries();
return retVal;
2007-09-08 21:43:15 +00:00
}
2007-01-13 02:40:33 +00:00
void Session::init(boost::shared_ptr<AvatarManager> manager)
{
myAvatarManager = manager;
}
void Session::addOwnAvatar(const std::string &avatarFile)
{
myAvatarManager->AddSingleAvatar(avatarFile);
}
void Session::startLocalGame(const GameData &gameData, const StartData &startData)
{
2007-05-02 21:20:54 +00:00
myGameType = GAME_TYPE_LOCAL;
currentGame.reset();
currentGameNum++;
2007-01-13 02:40:33 +00:00
myGui->initGui(gameData.guiSpeed);
PlayerDataList playerDataList;
for(int i = 0; i < startData.numberOfPlayers; i++) {
//Namen und Avatarpfad abfragen
ostringstream myName;
if (i==0) {
myName << "MyName";
} else {
myName << "Opponent" << i << "Name";
}
ostringstream myAvatar;
if (i==0) {
myAvatar << "MyAvatar";
} else {
myAvatar << "Opponent" << i << "Avatar";
}
//PlayerData erzeugen
// UniqueId = PlayerNumber for local games.
boost::shared_ptr<PlayerData> playerData(new PlayerData(
i,
i,
i == 0 ? PLAYER_TYPE_HUMAN : PLAYER_TYPE_COMPUTER,
PLAYER_RIGHTS_NORMAL,
i == 0));
playerData->SetName(myConfig->readConfigString(myName.str()));
playerData->SetAvatarFile(myConfig->readConfigString(myAvatar.str()));
playerDataList.push_back(playerData);
}
// EngineFactory erstellen
boost::shared_ptr<EngineFactory> factory(new LocalEngineFactory(myConfig)); // LocalEngine erstellen
currentGame.reset(new Game(myGui, factory, playerDataList, gameData, startData, currentGameNum, myLog));
//// SPIEL-SCHLEIFE
currentGame->initHand();
currentGame->startHand();
// SPIEL-SCHLEIFE
}
void Session::startClientGame(boost::shared_ptr<Game> game)
{
currentGameNum++;
currentGame = game;
2007-01-13 02:40:33 +00:00
}
2007-02-18 13:45:56 +00:00
2011-01-12 22:37:33 +00:00
boost::shared_ptr<Game> Session::getCurrentGame()
{
2011-01-12 22:37:33 +00:00
return currentGame;
}
2007-05-14 12:40:42 +00:00
GuiInterface *Session::getGui()
{
return myGui;
2007-05-14 12:40:42 +00:00
}
Session::GameType Session::getGameType()
{
return myGameType;
}
boost::shared_ptr<AvatarManager> Session::getAvatarManager()
{
return myAvatarManager;
}
void Session::startInternetClient()
{
if (myNetClient || !myGui) {
assert(false);
return;
}
myGameType = GAME_TYPE_INTERNET;
myNetClient.reset(new ClientThread(*myGui, *myAvatarManager, myConfig));
bool useAvatarServer = myConfig->readConfigInt("UseAvatarServer") != 0;
2009-04-05 21:26:36 +00:00
myNetClient->Init(
myConfig->readConfigString("InternetServerAddress"),
myConfig->readConfigString("InternetServerListAddress"),
myConfig->readConfigString("ServerPassword"),
myConfig->readConfigInt("InternetServerConfigMode") == 0,
myConfig->readConfigInt("InternetServerPort"),
myConfig->readConfigInt("InternetServerUseIpv6") == 1,
myConfig->readConfigInt("InternetServerUseSctp") == 1,
useAvatarServer ? myConfig->readConfigString("AvatarServerAddress") : "",
myConfig->readConfigString("MyName"),
myConfig->readConfigString("MyAvatar"),
myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("CacheDir")));
myNetClient->Run();
}
void Session::startNetworkClient(const string &serverAddress, unsigned serverPort, bool ipv6, bool sctp)
2007-02-18 13:45:56 +00:00
{
if (myNetClient || !myGui) {
assert(false);
return;
}
myGameType = GAME_TYPE_NETWORK;
myNetClient.reset(new ClientThread(*myGui, *myAvatarManager, myConfig));
myNetClient->Init(
serverAddress,
"",
myConfig->readConfigString("ServerPassword"),
false,
serverPort,
ipv6,
sctp,
"", // no avatar server
myConfig->readConfigString("MyName"),
myConfig->readConfigString("MyAvatar"),
myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("CacheDir")));
myNetClient->Run();
myNetClient->SendJoinFirstGame(
"",
myConfig->readConfigInt("NetAutoLeaveGameAfterFinish") == 1
);
2007-02-18 13:45:56 +00:00
}
void Session::startNetworkClientForLocalServer(const GameData &gameData)
{
if (myNetClient || !myGui) {
assert(false);
return;
}
myGameType = GAME_TYPE_NETWORK;
myNetClient.reset(new ClientThread(*myGui, *myAvatarManager, myConfig));
bool useIpv6 = myConfig->readConfigInt("ServerUseIpv6") == 1;
const char *loopbackAddr = useIpv6 ? "::1" : "127.0.0.1";
myNetClient->Init(
loopbackAddr,
"",
myConfig->readConfigString("ServerPassword"),
false,
myConfig->readConfigInt("ServerPort"),
useIpv6,
myConfig->readConfigInt("ServerUseSctp") == 1,
"", // no avatar server
myConfig->readConfigString("MyName"),
myConfig->readConfigString("MyAvatar"),
myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("CacheDir")));
myNetClient->Run();
myNetClient->SendCreateGame(
gameData,
NET_DEFAULT_GAME,
"",
myConfig->readConfigInt("NetAutoLeaveGameAfterFinish") == 1
);
}
2007-02-18 13:45:56 +00:00
void Session::terminateNetworkClient()
{
if (!myNetClient)
return; // already terminated
myNetClient->SignalTermination();
// Give the threads some time to terminate.
if (myNetClient->Join(NET_CLIENT_TERMINATE_TIMEOUT_MSEC))
myNetClient.reset();
// If termination fails, leave a memory leak to prevent a crash.
myGameType = GAME_TYPE_NONE;
2007-02-18 13:45:56 +00:00
}
void Session::clientCreateGame(const GameData &gameData, const string &name, const string &password)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendCreateGame(
gameData,
name,
password,
myConfig->readConfigInt("NetAutoLeaveGameAfterFinish") == 1
);
}
void Session::clientJoinGame(unsigned gameId, const std::string &password)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendJoinGame(
gameId,
password,
myConfig->readConfigInt("NetAutoLeaveGameAfterFinish") == 1
);
}
void Session::clientRejoinGame(unsigned gameId)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendRejoinGame(
gameId,
myConfig->readConfigInt("NetAutoLeaveGameAfterFinish") == 1
);
}
void Session::startNetworkServer(bool dedicated)
{
if (myNetServer) {
assert(false);
return;
}
ServerMode mode;
if (dedicated) {
2010-03-30 12:25:30 +00:00
#ifdef POKERTH_OFFICIAL_SERVER
mode = SERVER_MODE_INTERNET_AUTH;
2010-03-30 12:25:30 +00:00
#else
mode = SERVER_MODE_INTERNET_NOAUTH;
2010-03-30 12:25:30 +00:00
#endif
} else {
mode = SERVER_MODE_LAN;
}
myNetServer = ServerManagerFactory::CreateServerManager(*myConfig, *myGui, mode, *myAvatarManager);
myNetServer->Init(
myConfig->readConfigInt("ServerPort"),
myConfig->readConfigInt("ServerUseIpv6") == 1,
myConfig->readConfigInt("ServerUseSctp") == 1 ? TRANSPORT_PROTOCOL_TCP_SCTP : TRANSPORT_PROTOCOL_TCP,
myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("LogDir"))
);
2010-03-30 12:25:30 +00:00
myNetServer->RunAll();
}
void Session::terminateNetworkServer()
{
if (!myNetServer)
return; // already terminated
myNetServer->SignalTerminationAll();
// Give the thread some time to terminate.
if (myNetServer->JoinAll(true))
myNetServer.reset();
// If termination fails, leave a memory leak to prevent a crash.
}
bool Session::pollNetworkServerTerminated()
{
bool retVal = false;
if (!myNetServer)
retVal = true; // already terminated
else {
myNetServer->Process();
if (myNetServer->JoinAll(false))
retVal = true;
}
return retVal;
}
void Session::sendLeaveCurrentGame()
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendLeaveCurrentGame();
}
void Session::sendStartEvent(bool fillUpWithCpuPlayers)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendStartEvent(fillUpWithCpuPlayers);
}
2007-05-23 22:31:56 +00:00
void Session::sendClientPlayerAction()
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendPlayerAction();
2007-05-23 22:31:56 +00:00
}
void Session::sendGameChatMessage(const std::string &message)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendGameChatMessage(message);
}
void Session::sendLobbyChatMessage(const std::string &message)
{
if (!myNetClient)
return;
myNetClient->SendLobbyChatMessage(message);
2007-05-25 00:09:51 +00:00
}
void Session::sendPrivateChatMessage(unsigned targetPlayerId, const std::string &message)
{
if (!myNetClient)
return;
myNetClient->SendPrivateChatMessage(targetPlayerId, message);
}
void Session::kickPlayer(unsigned playerId)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendKickPlayer(playerId);
}
void Session::resetNetworkTimeout()
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendResetTimeout();
}
2007-10-27 18:51:53 +00:00
void Session::kickPlayer(const string &playerName)
{
if (!myNetClient)
return; // only act if client is running.
unsigned playerId;
if (myNetClient->GetPlayerIdFromName(playerName, playerId))
kickPlayer(playerId);
2007-10-27 18:51:53 +00:00
}
void Session::startVoteKickPlayer(unsigned playerId)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendAskKickPlayer(playerId);
}
void Session::selectServer(unsigned serverId)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SelectServer(serverId);
}
void
Session::setLogin(const std::string &userName, const std::string &password, bool isGuest)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SetLogin(userName, password, isGuest);
}
2010-03-29 12:33:12 +00:00
void
Session::invitePlayerToCurrentGame(unsigned playerId)
2010-03-29 12:33:12 +00:00
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendInvitePlayerToCurrentGame(playerId);
2010-03-29 12:33:12 +00:00
}
void
Session::acceptGameInvitation(unsigned gameId)
2010-03-29 12:33:12 +00:00
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendJoinGame(
gameId,
"",
myConfig->readConfigInt("NetAutoLeaveGameAfterFinish") == 1
);
2010-03-29 12:33:12 +00:00
}
void
Session::rejectGameInvitation(unsigned gameId, DenyGameInvitationReason reason)
2010-03-29 12:33:12 +00:00
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendRejectGameInvitation(gameId, reason);
2010-03-29 12:33:12 +00:00
}
void Session::reportBadAvatar(unsigned reportedPlayerId, const std::string &avatarHash)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendReportAvatar(reportedPlayerId, avatarHash);
}
void Session::voteKick(bool doKick)
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendVoteKick(doKick);
}
void Session::showMyCards()
{
if (!myNetClient)
return; // only act if client is running.
myNetClient->SendShowMyCards();
}
bool Session::isNetworkClientRunning() const
{
// This, and every place which calls this, is a HACK.
return myNetClient.get() != NULL;
}
bool Session::isNetworkServerRunning() const
{
// This, and every place which calls this, is a HACK.
return myNetServer.get() != NULL;
}
ServerInfo Session::getClientServerInfo(unsigned serverId) const
{
ServerInfo info;
if (myNetClient)
info = myNetClient->GetServerInfo(serverId);
return info;
}
GameInfo Session::getClientGameInfo(unsigned playerId) const
{
GameInfo info;
if (myNetClient)
info = myNetClient->GetGameInfo(playerId);
return info;
}
PlayerInfo Session::getClientPlayerInfo(unsigned playerId) const
{
PlayerInfo info;
if (myNetClient)
info = myNetClient->GetPlayerInfo(playerId);
return info;
}
unsigned Session::getGameIdOfPlayer(unsigned playerId) const
{
unsigned gameId = 0;
if (myNetClient)
gameId = myNetClient->GetGameIdOfPlayer(playerId);
return gameId;
}
ServerStats Session::getClientStats() const
{
ServerStats stats;
if (myNetClient)
stats = myNetClient->GetStatData();
return stats;
}
unsigned Session::getClientCurrentGameId() const
{
unsigned id = 0;
if (myNetClient)
id = myNetClient->GetGameId();
return id;
}
2010-03-24 20:10:36 +00:00
unsigned Session::getClientUniquePlayerId() const
{
unsigned id = 0;
if (myNetClient)
id = myNetClient->GetGuiPlayerId();
return id;
2010-03-24 20:10:36 +00:00
}
bool Session::getAvatarFile(const MD5Buf &avatarMD5, std::string &fileName)
{
bool retVal = false;
if (myAvatarManager.get()) {
string tmpFileName;
retVal = myAvatarManager->GetAvatarFileName(avatarMD5, tmpFileName);
if (retVal)
fileName = myQtToolsInterface->stringToUtf8(tmpFileName);
}
return retVal;
}