2007-11-16 15:24:25 +00:00
|
|
|
/***************************************************************************
|
2009-05-31 20:14:46 +00:00
|
|
|
* Copyright (C) 2007-2009 by Lothar May *
|
2007-11-16 15:24:25 +00:00
|
|
|
* *
|
|
|
|
|
* 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. *
|
|
|
|
|
***************************************************************************/
|
2009-05-31 20:14:46 +00:00
|
|
|
/* Network server game. */
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-05-31 20:14:46 +00:00
|
|
|
#ifndef _SERVERGAME_H_
|
|
|
|
|
#define _SERVERGAME_H_
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-12 15:48:34 +00:00
|
|
|
#include <boost/enable_shared_from_this.hpp>
|
2009-05-07 22:36:51 +00:00
|
|
|
#include <third_party/boost/timers.hpp>
|
2010-08-08 10:23:40 +00:00
|
|
|
#include <map>
|
2009-05-07 22:36:51 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <net/sessionmanager.h>
|
2009-10-03 20:19:51 +00:00
|
|
|
#include <db/serverdbcallback.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <gui/guiinterface.h>
|
|
|
|
|
#include <gamedata.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReceiverHelper;
|
|
|
|
|
class ServerLobbyThread;
|
|
|
|
|
class ServerGameState;
|
2010-08-07 23:06:59 +00:00
|
|
|
class ServerDBInterface;
|
2007-11-16 15:24:25 +00:00
|
|
|
class ConfigFile;
|
|
|
|
|
struct GameData;
|
|
|
|
|
class Game;
|
|
|
|
|
|
2009-06-12 15:48:34 +00:00
|
|
|
class ServerGame : public boost::enable_shared_from_this<ServerGame>
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2009-05-31 20:14:46 +00:00
|
|
|
ServerGame(
|
2009-06-12 15:48:34 +00:00
|
|
|
boost::shared_ptr<ServerLobbyThread> lobbyThread, u_int32_t id, const std::string &name, const std::string &pwd, const GameData &gameData, unsigned adminPlayerId, GuiInterface &gui, ConfigFile *playerConfig);
|
2009-05-31 20:14:46 +00:00
|
|
|
virtual ~ServerGame();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-07 23:25:13 +00:00
|
|
|
void Init();
|
|
|
|
|
void Exit();
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
u_int32_t GetId() const;
|
|
|
|
|
const std::string &GetName() const;
|
|
|
|
|
|
2009-10-11 09:20:17 +00:00
|
|
|
DB_id GetDBId() const;
|
|
|
|
|
void SetDBId(DB_id newId);
|
2009-10-03 20:19:51 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
void AddSession(SessionWrapper session);
|
2008-03-06 15:27:31 +00:00
|
|
|
void RemovePlayer(unsigned playerId, unsigned errorCode);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-05-30 22:20:40 +00:00
|
|
|
void HandlePacket(SessionWrapper session, boost::shared_ptr<NetPacket> packet);
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
ServerCallback &GetCallback();
|
|
|
|
|
GameState GetCurRound() const;
|
|
|
|
|
|
|
|
|
|
void SendToAllPlayers(boost::shared_ptr<NetPacket> packet, SessionData::State state);
|
2010-07-17 21:11:12 +00:00
|
|
|
void SendToAllButOnePlayers(boost::shared_ptr<NetPacket> packet, SessionId except, SessionData::State state);
|
2010-08-28 14:34:02 +00:00
|
|
|
void RemoveAllSessions();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
bool IsPasswordProtected() const;
|
|
|
|
|
bool CheckPassword(const std::string &password) const;
|
2010-08-01 20:15:49 +00:00
|
|
|
static bool CheckSettings(const GameData &data);
|
2007-11-16 15:24:25 +00:00
|
|
|
const GameData &GetGameData() const;
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr<PlayerData> GetPlayerDataByUniqueId(unsigned playerId) const;
|
|
|
|
|
PlayerIdList GetPlayerIdList() const;
|
|
|
|
|
bool IsPlayerConnected(const std::string &name) const;
|
2010-03-30 18:03:08 +00:00
|
|
|
bool IsPlayerConnected(unsigned playerId) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
bool IsRunning() const;
|
|
|
|
|
|
|
|
|
|
unsigned GetAdminPlayerId() const;
|
|
|
|
|
void SetAdminPlayerId(unsigned playerId);
|
|
|
|
|
|
2010-03-30 17:10:37 +00:00
|
|
|
void AddPlayerInvitation(unsigned playerId);
|
|
|
|
|
void RemovePlayerInvitation(unsigned playerId);
|
|
|
|
|
bool IsPlayerInvited(unsigned playerId) const;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
// should be protected, but is needed in function.
|
|
|
|
|
const Game &GetGame() const;
|
|
|
|
|
Game &GetGame();
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
2010-08-08 10:23:40 +00:00
|
|
|
struct RankingData
|
|
|
|
|
{
|
|
|
|
|
RankingData() : dbid(DB_ID_INVALID), place(0) {}
|
|
|
|
|
RankingData(DB_id i, int p = 0) : dbid(i), place(p) {}
|
|
|
|
|
DB_id dbid;
|
|
|
|
|
int place;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef std::map<unsigned, RankingData> RankingMap;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-12 15:48:34 +00:00
|
|
|
void TimerVoteKick(const boost::system::error_code &ec);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
void InternalStartGame();
|
2010-08-08 10:23:40 +00:00
|
|
|
void InitRankingMap(const PlayerDataList &playerDataList);
|
2010-08-28 14:34:02 +00:00
|
|
|
void UpdateRankingMap();
|
2010-08-08 10:23:40 +00:00
|
|
|
void SetPlayerPlace(unsigned playerId, int place);
|
|
|
|
|
void InternalEndGame();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
void InternalKickPlayer(unsigned playerId);
|
2008-12-06 00:18:36 +00:00
|
|
|
void InternalAskVoteKick(SessionWrapper byWhom, unsigned playerIdWho, unsigned timeoutSec);
|
|
|
|
|
void InternalDenyAskVoteKick(SessionWrapper byWhom, unsigned playerIdWho, DenyKickPlayerReason reason);
|
2008-12-07 11:04:42 +00:00
|
|
|
void InternalVoteKick(SessionWrapper byWhom, unsigned petitionId, KickVote vote);
|
|
|
|
|
void InternalDenyVoteKick(SessionWrapper byWhom, unsigned petitionId, DenyVoteReason reason);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
PlayerDataList GetFullPlayerDataList() const;
|
|
|
|
|
|
|
|
|
|
void AddComputerPlayer(boost::shared_ptr<PlayerData> player);
|
2008-12-08 23:06:11 +00:00
|
|
|
boost::shared_ptr<PlayerData> RemoveComputerPlayer(unsigned playerId);
|
|
|
|
|
bool IsComputerPlayerActive(unsigned playerId) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
void ResetComputerPlayerList();
|
|
|
|
|
|
2008-11-25 23:19:34 +00:00
|
|
|
void GracefulRemoveSession(SessionWrapper session, int reason);
|
|
|
|
|
void RemovePlayerData(boost::shared_ptr<PlayerData> player, int reason);
|
2007-11-16 15:24:25 +00:00
|
|
|
void ErrorRemoveSession(SessionWrapper session);
|
|
|
|
|
void SessionError(SessionWrapper session, int errorCode);
|
|
|
|
|
void MoveSessionToLobby(SessionWrapper session, int reason);
|
|
|
|
|
|
|
|
|
|
void RemoveDisconnectedPlayers();
|
|
|
|
|
size_t GetCurNumberOfPlayers() const;
|
|
|
|
|
void AssignPlayerNumbers();
|
2008-12-12 22:38:35 +00:00
|
|
|
bool IsValidPlayer(unsigned playerId) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
ServerLobbyThread &GetLobbyThread();
|
|
|
|
|
|
|
|
|
|
ServerGameState &GetState();
|
2009-05-31 13:17:02 +00:00
|
|
|
void SetState(ServerGameState &newState);
|
|
|
|
|
|
2009-06-12 15:48:34 +00:00
|
|
|
boost::asio::deadline_timer &GetStateTimer();
|
2007-11-21 23:34:25 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
ReceiverHelper &GetReceiver();
|
|
|
|
|
|
|
|
|
|
const StartData &GetStartData() const;
|
|
|
|
|
void SetStartData(const StartData &startData);
|
|
|
|
|
|
|
|
|
|
GuiInterface &GetGui();
|
|
|
|
|
|
|
|
|
|
unsigned GetNextGameNum();
|
|
|
|
|
|
|
|
|
|
const SessionManager &GetSessionManager() const;
|
|
|
|
|
SessionManager &GetSessionManager();
|
2010-08-07 23:06:59 +00:00
|
|
|
ServerDBInterface &GetDatabase();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
private:
|
2009-05-31 20:14:46 +00:00
|
|
|
ServerGame(const ServerGame &other);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
SessionManager m_sessionManager;
|
|
|
|
|
PlayerDataList m_computerPlayerList;
|
|
|
|
|
mutable boost::mutex m_computerPlayerListMutex;
|
|
|
|
|
|
2010-03-30 17:10:37 +00:00
|
|
|
PlayerIdList m_playerInvitationList;
|
|
|
|
|
mutable boost::mutex m_playerInvitationListMutex;
|
|
|
|
|
|
2010-08-08 10:23:40 +00:00
|
|
|
RankingMap m_rankingMap;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
unsigned m_adminPlayerId;
|
|
|
|
|
|
2008-12-06 21:10:46 +00:00
|
|
|
boost::shared_ptr<VoteKickData> m_voteKickData;
|
2008-11-16 22:57:11 +00:00
|
|
|
|
2009-06-12 15:48:34 +00:00
|
|
|
boost::shared_ptr<ServerLobbyThread> m_lobbyThread;
|
2007-11-16 15:24:25 +00:00
|
|
|
boost::shared_ptr<ReceiverHelper> m_receiver;
|
2010-08-07 23:06:59 +00:00
|
|
|
boost::shared_ptr<ServerDBInterface> m_database;
|
2007-11-16 15:24:25 +00:00
|
|
|
GuiInterface &m_gui;
|
|
|
|
|
|
|
|
|
|
const GameData m_gameData;
|
|
|
|
|
StartData m_startData;
|
2009-05-31 13:17:02 +00:00
|
|
|
boost::shared_ptr<Game> m_game;
|
|
|
|
|
ServerGameState *m_curState;
|
2009-05-30 22:20:40 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
const u_int32_t m_id;
|
2009-10-11 09:20:17 +00:00
|
|
|
DB_id m_dbId;
|
2007-11-16 15:24:25 +00:00
|
|
|
const std::string m_name;
|
|
|
|
|
const std::string m_password;
|
|
|
|
|
ConfigFile *m_playerConfig;
|
|
|
|
|
unsigned m_gameNum;
|
2008-11-16 22:57:11 +00:00
|
|
|
unsigned m_curPetitionId;
|
2009-06-12 15:48:34 +00:00
|
|
|
boost::asio::deadline_timer m_voteKickTimer;
|
|
|
|
|
boost::asio::deadline_timer m_stateTimer;
|
2008-12-06 21:10:46 +00:00
|
|
|
|
2009-05-06 21:09:58 +00:00
|
|
|
friend class ServerLobbyThread;
|
2007-11-16 15:24:25 +00:00
|
|
|
friend class AbstractServerGameStateReceiving;
|
|
|
|
|
friend class ServerGameStateInit;
|
|
|
|
|
friend class ServerGameStateWaitAck;
|
|
|
|
|
friend class ServerGameStateStartGame;
|
2009-05-30 22:20:40 +00:00
|
|
|
friend class ServerGameStateHand;
|
2007-11-16 15:24:25 +00:00
|
|
|
friend class ServerGameStateWaitPlayerAction;
|
2010-07-15 21:55:52 +00:00
|
|
|
friend class ServerGameStateWaitNextHand;
|
2007-11-16 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|