1
/***************************************************************************
2
* Copyright (C) 2007 by Lothar May *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18
***************************************************************************/
19
/* Network server game thread. */
21
#ifndef _SERVERGAMETHREAD_H_
22
#define _SERVERGAMETHREAD_H_
24
#include <net/sessionmanager.h>
25
#include <gui/guiinterface.h>
27
#include <third_party/boost/timers.hpp>
31
#define GAME_THREAD_TERMINATE_TIMEOUT THREAD_WAIT_INFINITE
35
class ServerLobbyThread;
36
class ServerGameState;
41
class ServerGameThread : public Thread
45
ServerLobbyThread &lobbyThread, u_int32_t id, const std::string &name, const std::string &pwd, const GameData &gameData, unsigned adminPlayerId, GuiInterface &gui, ConfigFile *playerConfig);
46
virtual ~ServerGameThread();
48
u_int32_t GetId() const;
49
const std::string &GetName() const;
51
void AddSession(SessionWrapper session);
52
void RemovePlayer(unsigned playerId, unsigned errorCode);
54
ServerCallback &GetCallback();
55
GameState GetCurRound() const;
57
void SendToAllPlayers(boost::shared_ptr<NetPacket> packet, SessionData::State state);
58
void RemoveAllSessions();
60
bool IsPasswordProtected() const;
61
bool CheckPassword(const std::string &password) const;
62
const GameData &GetGameData() const;
64
boost::shared_ptr<PlayerData> GetPlayerDataByUniqueId(unsigned playerId) const;
65
PlayerIdList GetPlayerIdList() const;
66
bool IsPlayerConnected(const std::string &name) const;
68
bool IsRunning() const;
70
unsigned GetAdminPlayerId() const;
71
void SetAdminPlayerId(unsigned playerId);
73
// should be protected, but is needed in function.
74
const Game &GetGame() const;
79
typedef std::deque<SessionWrapper> SessionQueue;
81
// Main function of the thread.
83
void RemovePlayerLoop();
85
void VoteKickAction();
87
void InternalStartGame();
90
void InternalKickPlayer(unsigned playerId);
91
void InternalAskVoteKick(SessionWrapper byWhom, unsigned playerIdWho, unsigned timeoutSec);
92
void InternalDenyAskVoteKick(SessionWrapper byWhom, unsigned playerIdWho, DenyKickPlayerReason reason);
93
void InternalVoteKick(SessionWrapper byWhom, unsigned petitionId, KickVote vote);
94
void InternalDenyVoteKick(SessionWrapper byWhom, unsigned petitionId, DenyVoteReason reason);
96
PlayerDataList GetFullPlayerDataList() const;
98
void AddComputerPlayer(boost::shared_ptr<PlayerData> player);
99
boost::shared_ptr<PlayerData> RemoveComputerPlayer(unsigned playerId);
100
bool IsComputerPlayerActive(unsigned playerId) const;
101
void ResetComputerPlayerList();
103
void GracefulRemoveSession(SessionWrapper session, int reason);
104
void RemovePlayerData(boost::shared_ptr<PlayerData> player, int reason);
105
void ErrorRemoveSession(SessionWrapper session);
106
void SessionError(SessionWrapper session, int errorCode);
107
void MoveSessionToLobby(SessionWrapper session, int reason);
109
void RemoveDisconnectedPlayers();
110
size_t GetCurNumberOfPlayers() const;
111
void AssignPlayerNumbers();
112
bool IsValidPlayer(unsigned playerId) const;
114
ServerLobbyThread &GetLobbyThread();
116
ServerGameState &GetState();
117
void SetState(ServerGameState &newState);
119
const boost::timers::portable::microsec_timer &GetStateTimer() const;
120
boost::timers::portable::microsec_timer &GetStateTimer();
121
unsigned GetStateTimerFlag() const;
122
void SetStateTimerFlag(unsigned flag);
124
ReceiverHelper &GetReceiver();
126
const StartData &GetStartData() const;
127
void SetStartData(const StartData &startData);
129
GuiInterface &GetGui();
131
unsigned GetNextGameNum();
133
const SessionManager &GetSessionManager() const;
134
SessionManager &GetSessionManager();
138
SessionQueue m_sessionQueue;
139
mutable boost::mutex m_sessionQueueMutex;
141
SessionManager m_sessionManager;
142
PlayerDataList m_computerPlayerList;
143
mutable boost::mutex m_computerPlayerListMutex;
145
RemovePlayerList m_removePlayerList;
146
mutable boost::mutex m_removePlayerListMutex;
148
unsigned m_adminPlayerId;
149
mutable boost::mutex m_adminPlayerIdMutex;
151
boost::shared_ptr<VoteKickData> m_voteKickData;
152
mutable boost::mutex m_voteKickDataMutex;
154
ServerLobbyThread &m_lobbyThread;
155
boost::shared_ptr<ReceiverHelper> m_receiver;
158
const GameData m_gameData;
159
StartData m_startData;
160
boost::shared_ptr<Game> m_game;
161
const u_int32_t m_id;
162
const std::string m_name;
163
const std::string m_password;
164
ConfigFile *m_playerConfig;
165
ServerGameState *m_curState;
167
unsigned m_curPetitionId;
169
boost::timers::portable::microsec_timer m_stateTimer;
170
unsigned m_stateTimerFlag;
172
boost::timers::portable::microsec_timer m_voteKickActionTimer;
174
friend class AbstractServerGameStateReceiving;
175
friend class AbstractServerGameStateRunning;
176
friend class AbstractServerGameStateTimer;
177
friend class ServerGameStateInit;
178
friend class ServerGameStateWaitAck;
179
friend class ServerGameStateStartGame;
180
friend class ServerGameStateStartHand;
181
friend class ServerGameStateStartRound;
182
friend class ServerGameStateWaitPlayerAction;
183
friend class ServerGameStateComputerAction;
184
friend class ServerGameStateDealCardsDelay;
185
friend class ServerGameStateShowCardsDelay;
186
friend class ServerGameStateNextHandDelay;
187
friend class ServerGameStateNextGameDelay;