1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 #ifndef __GAMEKEEPER_H__
14 #define __GAMEKEEPER_H__
15 
16 // bzflag global header
17 #include "common.h"
18 
19 // system headers
20 #include <vector>
21 #include <string>
22 
23 // common interface headers
24 #include "PlayerInfo.h"
25 #include "PlayerState.h"
26 #include "TimeKeeper.h"
27 
28 // implementation-specific bzfs-specific headers
29 #include "CmdLineOptions.h"
30 #include "FlagHistory.h"
31 #include "Permissions.h"
32 #include "LagInfo.h"
33 #include "Score.h"
34 #include "RecordReplay.h"
35 #include "NetHandler.h"
36 #include "Authentication.h"
37 #include "messages.h"
38 #include "bzfsAPI.h"
39 #include "FlagInfo.h"
40 #include "ShotUpdate.h"
41 
42 class ShotInfo
43 {
44 public:
ShotInfo()45     ShotInfo() : salt(0), expireTime(0.0), present(false), running(false) {};
46 
47     FiringInfo firingInfo;
48     int   salt;
49     float      expireTime;
50     bool       present;
51     bool       running;
52 };
53 
54 
55 
56 const int PlayerSlot = MaxPlayers + ReplayObservers;
57 
58 typedef void (*tcpCallback)(NetHandler &netPlayer, int i, const RxStatus e);
59 
60 /** This class is meant to be the container of all the global entity that lives
61     into the game and methods to act globally on those.
62     Up to now it contain players. Flag class is only there as a TODO
63 */
64 class GameKeeper
65 {
66 public:
67     class Player
68     {
69     public:
70         Player(int _playerIndex, const struct sockaddr_in &clientAddr, int fd,
71                tcpCallback _clientCallback);
72         Player(int _playerIndex, NetHandler *handler, tcpCallback _clientCallback);
73         Player(int _playerIndex, bz_ServerSidePlayerHandler *handler);
74         ~Player();
75 
76         int        getIndex();
77         static int     getFreeIndex(int min, int max);
78         static Player* getPlayerByIndex(int _playerIndex);
79         static int     count();
80         static void    updateLatency(float &waitTime);
81         static void    dumpScore();
82         static int     anointRabbit(int oldRabbit);
83         static std::vector<int> allowed(PlayerAccessInfo::AccessPerm right,
84                                         int targetPlayer = -1);
85         static int     getPlayerIDByName(const std::string &name);
86         static void    reloadAccessDatabase();
87 
88         bool       loadEnterData(uint16_t& rejectCode,
89                                  char* rejectMsg);
90         void*      packAdminInfo(void* buf);
91         void*      packPlayerInfo(void* buf);
92         void*      packPlayerUpdate(void* buf);
93 
94         void       setPlayerAddMessage ( PlayerAddMessage &msg );
95 
96         void       signingOn(bool ctf);
97         void       close();
98         static bool    clean();
99         void       handleTcpPacket(fd_set *set);
100 
101         // For hostban checking, to avoid check and check again
102         static void    setAllNeedHostbanChecked(bool set);
103         void       setNeedThisHostbanChecked(bool set);
104         bool       needsHostbanChecked();
105 
106         // To handle player State
107         void       setPlayerState(float pos[3], float azimuth);
108         void       getPlayerState(float pos[3], float &azimuth);
109         void       setPlayerState(PlayerState state, float timestamp);
110 
111         void       setBzIdentifier(const std::string& id);
112         const std::string& getBzIdentifier() const;
113 
114         // When is the player's next GameTime?
115         const TimeKeeper&   getNextGameTime() const;
116         void        updateNextGameTime();
117 
118         // To handle Identify
119         void       setLastIdFlag(int _idFlag);
120         int     getLastIdFlag();
121 
122         // To handle shot
123         static void    setMaxShots(int _maxShots);
124         bool       addShot(int id, int salt, FiringInfo &firingInfo);
125         bool       removeShot(int id, int salt);
126         bool       updateShot(int id, int salt);
127 
128 
129         enum LSAState
130         {
131             start,
132             notRequired,
133             required,
134             requesting,
135             checking,
136             timedOut,
137             failed,
138             verified,
139             done
140         } _LSAState;
141 
142         // players
143         PlayerInfo        player;
144         // Net Handler
145         NetHandler        *netHandler;
146         // player lag info
147         LagInfo       lagInfo;
148         // player access
149         PlayerAccessInfo  accessInfo;
150         // Last known position, vel, etc
151         PlayerState       lastState;
152         float         stateTimeStamp;
153         float         serverTimeStamp;
154         // GameTime update
155         float         gameTimeRate;
156         TimeKeeper        gameTimeNext;
157         // FlagHistory
158         FlagHistory       flagHistory;
159         // Score
160         Score         score;
161         // Authentication
162         Authentication    authentication;
163 
164         std::map<std::string, std::string> extraData;
165 
166         // flag to let us know the player is on it's way out
167         bool  isParting;
168 
169         bool hasEntered;
170 
171         // logic class for server side players
172         bz_ServerSidePlayerHandler* playerHandler;
173 
174         bool addWasDelayed;
175         bool hadEnter;
176         double addDelayStartTime;
177 
178         int lastHeldFlagID;
179 
180     private:
181         static Player*    playerList[PlayerSlot];
182         int           playerIndex;
183         bool          closed;
184         tcpCallback       clientCallback;
185         std::string       bzIdentifier;
186         bool          needThisHostbanChecked;
187         // In case you want recheck all condition on all players
188         static bool       allNeedHostbanChecked;
189 
190         static int       maxShots;
191         std::vector<ShotInfo> shotsInfo;
192 
193         int        idFlag;
194 
195     };
196 
197     class Flag
198     {
199     };
200 };
201 
getIndex()202 inline int GameKeeper::Player::getIndex()
203 {
204     return playerIndex;
205 }
206 
getPlayerByIndex(int _playerIndex)207 inline GameKeeper::Player* GameKeeper::Player::getPlayerByIndex(int
208         _playerIndex)
209 {
210     if (_playerIndex < 0 || _playerIndex >= PlayerSlot)
211         return 0;
212     if (!playerList[_playerIndex])
213         return 0;
214     if (playerList[_playerIndex]->closed)
215         return 0;
216     return playerList[_playerIndex];
217 }
218 
219 void* PackPlayerInfo(void* buf, int playerIndex, uint8_t properties );
220 
221 // For hostban checking, to avoid check and check again
setAllNeedHostbanChecked(bool set)222 inline void GameKeeper::Player::setAllNeedHostbanChecked(bool set)
223 {
224     allNeedHostbanChecked = set;
225 }
226 
setNeedThisHostbanChecked(bool set)227 inline void GameKeeper::Player::setNeedThisHostbanChecked(bool set)
228 {
229     needThisHostbanChecked = set;
230 }
231 
needsHostbanChecked()232 inline bool GameKeeper::Player::needsHostbanChecked()
233 {
234     return (allNeedHostbanChecked || needThisHostbanChecked);
235 }
236 
237 
setBzIdentifier(const std::string & id)238 inline void GameKeeper::Player::setBzIdentifier(const std::string& id)
239 {
240     bzIdentifier = id;
241 }
242 
getBzIdentifier()243 inline const std::string& GameKeeper::Player::getBzIdentifier() const
244 {
245     return bzIdentifier;
246 }
247 
248 
getNextGameTime()249 inline const TimeKeeper& GameKeeper::Player::getNextGameTime() const
250 {
251     return gameTimeNext;
252 }
253 
254 
255 #endif
256 
257 // Local Variables: ***
258 // mode: C++ ***
259 // tab-width: 4 ***
260 // c-basic-offset: 4 ***
261 // indent-tabs-mode: nil ***
262 // End: ***
263 // ex: shiftwidth=4 tabstop=4
264