1 /***************************************************************************
2  *   Copyright (C) 2008 by Max-Wilhelm Bruker   *
3  *   brukie@laptop   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifndef SERVERGAME_H
21 #define SERVERGAME_H
22 
23 #include "pb/event_leave.pb.h"
24 #include "pb/response.pb.h"
25 #include "pb/serverinfo_game.pb.h"
26 #include "server_response_containers.h"
27 
28 #include <QDateTime>
29 #include <QMap>
30 #include <QMutex>
31 #include <QObject>
32 #include <QSet>
33 #include <QStringList>
34 
35 class QTimer;
36 class GameEventContainer;
37 class GameReplay;
38 class Server_Room;
39 class Server_Player;
40 class ServerInfo_User;
41 class ServerInfo_Player;
42 class ServerInfo_Game;
43 class Server_AbstractUserInterface;
44 class Event_GameStateChanged;
45 
46 class Server_Game : public QObject
47 {
48     Q_OBJECT
49 private:
50     Server_Room *room;
51     int nextPlayerId;
52     int hostId;
53     ServerInfo_User *creatorInfo;
54     QMap<int, Server_Player *> players;
55     QSet<QString> allPlayersEver, allSpectatorsEver;
56     bool gameStarted;
57     bool gameClosed;
58     int gameId;
59     QString description;
60     QString password;
61     int maxPlayers;
62     QList<int> gameTypes;
63     int activePlayer, activePhase;
64     bool onlyBuddies, onlyRegistered;
65     bool spectatorsAllowed;
66     bool spectatorsNeedPassword;
67     bool spectatorsCanTalk;
68     bool spectatorsSeeEverything;
69     int inactivityCounter;
70     int startTimeOfThisGame, secondsElapsed;
71     bool firstGameStarted;
72     bool turnOrderReversed;
73     QDateTime startTime;
74     QTimer *pingClock;
75     QList<GameReplay *> replayList;
76     GameReplay *currentReplay;
77 
78     void createGameStateChangedEvent(Event_GameStateChanged *event,
79                                      Server_Player *playerWhosAsking,
80                                      bool omniscient,
81                                      bool withUserInfo);
82     void storeGameInformation();
83 signals:
84     void sigStartGameIfReady();
85     void gameInfoChanged(ServerInfo_Game gameInfo);
86 private slots:
87     void pingClockTimeout();
88     void doStartGameIfReady();
89 
90 public:
91     mutable QMutex gameMutex;
92     Server_Game(const ServerInfo_User &_creatorInfo,
93                 int _gameId,
94                 const QString &_description,
95                 const QString &_password,
96                 int _maxPlayers,
97                 const QList<int> &_gameTypes,
98                 bool _onlyBuddies,
99                 bool _onlyRegistered,
100                 bool _spectatorsAllowed,
101                 bool _spectatorsNeedPassword,
102                 bool _spectatorsCanTalk,
103                 bool _spectatorsSeeEverything,
104                 Server_Room *parent);
105     ~Server_Game();
getRoom()106     Server_Room *getRoom() const
107     {
108         return room;
109     }
110     void getInfo(ServerInfo_Game &result) const;
getHostId()111     int getHostId() const
112     {
113         return hostId;
114     }
getCreatorInfo()115     ServerInfo_User *getCreatorInfo() const
116     {
117         return creatorInfo;
118     }
getGameStarted()119     bool getGameStarted() const
120     {
121         return gameStarted;
122     }
123     int getPlayerCount() const;
124     int getSpectatorCount() const;
getPlayers()125     const QMap<int, Server_Player *> &getPlayers() const
126     {
127         return players;
128     }
getGameId()129     int getGameId() const
130     {
131         return gameId;
132     }
getDescription()133     QString getDescription() const
134     {
135         return description;
136     }
getPassword()137     QString getPassword() const
138     {
139         return password;
140     }
getMaxPlayers()141     int getMaxPlayers() const
142     {
143         return maxPlayers;
144     }
getSpectatorsAllowed()145     bool getSpectatorsAllowed() const
146     {
147         return spectatorsAllowed;
148     }
getSpectatorsNeedPassword()149     bool getSpectatorsNeedPassword() const
150     {
151         return spectatorsNeedPassword;
152     }
getSpectatorsCanTalk()153     bool getSpectatorsCanTalk() const
154     {
155         return spectatorsCanTalk;
156     }
getSpectatorsSeeEverything()157     bool getSpectatorsSeeEverything() const
158     {
159         return spectatorsSeeEverything;
160     }
161     Response::ResponseCode
162     checkJoin(ServerInfo_User *user, const QString &_password, bool spectator, bool overrideRestrictions, bool asJudge);
163     bool containsUser(const QString &userName) const;
164     void addPlayer(Server_AbstractUserInterface *userInterface,
165                    ResponseContainer &rc,
166                    bool spectator,
167                    bool judge,
168                    bool broadcastUpdate = true);
169     void removePlayer(Server_Player *player, Event_Leave::LeaveReason reason);
170     void removeArrowsRelatedToPlayer(GameEventStorage &ges, Server_Player *player);
171     void unattachCards(GameEventStorage &ges, Server_Player *player);
172     bool kickPlayer(int playerId);
173     void startGameIfReady();
174     void stopGameIfFinished();
getActivePlayer()175     int getActivePlayer() const
176     {
177         return activePlayer;
178     }
getActivePhase()179     int getActivePhase() const
180     {
181         return activePhase;
182     }
183     void setActivePlayer(int _activePlayer);
184     void setActivePhase(int _activePhase);
185     void nextTurn();
getSecondsElapsed()186     int getSecondsElapsed() const
187     {
188         return secondsElapsed;
189     }
reverseTurnOrder()190     bool reverseTurnOrder()
191     {
192         return turnOrderReversed = !turnOrderReversed;
193     }
194 
195     void createGameJoinedEvent(Server_Player *player, ResponseContainer &rc, bool resuming);
196 
197     GameEventContainer *
198     prepareGameEvent(const ::google::protobuf::Message &gameEvent, int playerId, GameEventContext *context = 0);
199     GameEventContext prepareGameEventContext(const ::google::protobuf::Message &gameEventContext);
200 
201     void sendGameStateToPlayers();
202     void sendGameEventContainer(GameEventContainer *cont,
203                                 GameEventStorageItem::EventRecipients recipients = GameEventStorageItem::SendToPrivate |
204                                                                                    GameEventStorageItem::SendToOthers,
205                                 int privatePlayerId = -1);
206 };
207 
208 #endif
209