19 #ifndef SERVER_LOBBY_HPP
20 #define SERVER_LOBBY_HPP
22 #include "network/protocols/lobby_protocol.hpp"
23 #include "utils/cpp2011.hpp"
24 #include "utils/time.hpp"
26 #include "irrString.h"
38 class DatabaseConnector;
55 enum ServerState :
unsigned int
58 REGISTER_SELF_ADDRESS,
59 WAITING_FOR_START_GAME,
62 WAIT_FOR_WORLD_LOADED,
63 WAIT_FOR_RACE_STARTED,
65 WAIT_FOR_RACE_STOPPED,
73 std::string m_aes_key;
75 irr::core::stringw m_name;
76 std::string m_country_code;
81 DatabaseConnector* m_db_connector;
86 std::atomic<ServerState> m_state;
89 enum ResetState :
unsigned int
97 std::atomic<ResetState> m_rs_state;
107 std::atomic<uint32_t> m_server_owner_id;
116 bool m_registered_for_once_only;
118 bool m_save_server_config;
121 std::map<std::weak_ptr<STKPeer>, bool,
124 std::weak_ptr<Online::Request> m_server_registering;
129 std::mutex m_keys_mutex;
131 std::map<uint32_t, KeyData> m_keys;
133 std::map<std::weak_ptr<STKPeer>,
134 std::pair<uint32_t, BareNetworkString>,
135 std::owner_less<std::weak_ptr<STKPeer> > > m_pending_connection;
137 std::map<std::string, uint64_t> m_pending_peer_connection;
139 std::shared_ptr<Ranking> m_ranking;
147 std::atomic<uint32_t> m_server_id_online;
149 std::atomic<uint32_t> m_client_server_host_id;
151 std::atomic<int> m_difficulty;
153 std::atomic<int> m_game_mode;
155 std::atomic<int> m_lobby_players;
157 std::atomic<int> m_current_ai_count;
159 std::atomic<uint64_t> m_last_success_poll_time;
161 uint64_t m_last_unsuccess_poll_time, m_server_started_at, m_server_delay;
167 int m_battle_hit_capture_limit;
169 float m_battle_time_limit;
171 unsigned m_item_seed;
173 uint32_t m_winner_peer_id;
175 uint64_t m_client_starting_time;
182 void connectionRequested(
Event* event);
191 void kickHost(
Event* event);
192 void changeTeam(
Event* event);
193 void handleChat(
Event* event);
195 std::weak_ptr<ServerLobby> sl = std::weak_ptr<ServerLobby>());
196 void updateServerOwner();
199 bool checkPeersReady(
bool ignore_ai_peer)
const;
200 void resetPeersReady()
204 if (it->first.expired())
215 void addPeerConnection(
const std::string& addr_str)
219 void removeExpiredPeerConnection()
223 for (
auto it = m_pending_peer_connection.begin();
224 it != m_pending_peer_connection.end();)
227 it = m_pending_peer_connection.erase(it);
232 void replaceKeys(std::map<uint32_t, KeyData>& new_keys)
234 std::lock_guard<std::mutex> lock(m_keys_mutex);
235 std::swap(m_keys, new_keys);
237 void handlePendingConnection();
238 void handleUnencryptedConnection(std::shared_ptr<STKPeer> peer,
241 const irr::core::stringw& online_name,
242 bool is_pending_connection,
243 std::string country_code =
"");
244 bool decryptConnectionRequest(std::shared_ptr<STKPeer> peer,
246 const std::string& key,
247 const std::string& iv,
249 const irr::core::stringw& online_name,
250 const std::string& country_code);
253 void findMajorityValue(
const std::map<T, unsigned>& choices,
unsigned cur_players,
254 T* best_choice,
float* rate);
255 void getRankingForPlayer(std::shared_ptr<NetworkPlayerProfile> p);
256 void submitRankingsToAddons();
259 void getHitCaptureLimit();
262 void addWaitingPlayersToGame();
266 std::vector<std::shared_ptr<NetworkPlayerProfile> >& players)
const;
268 std::vector<std::shared_ptr<NetworkPlayerProfile> >& players,
269 bool live_join)
const;
271 std::vector<std::shared_ptr<NetworkPlayerProfile> >& players)
const;
272 std::vector<std::shared_ptr<NetworkPlayerProfile> >
getLivePlayers()
const;
280 unsigned local_id)
const;
284 std::set<std::shared_ptr<STKPeer>> getSpectatorsByLimit();
285 void kickPlayerWithReason(
STKPeer* peer,
const char* reason)
const;
286 void testBannedForIP(
STKPeer* peer)
const;
287 void testBannedForIPv6(
STKPeer* peer)
const;
288 void testBannedForOnlineId(
STKPeer* peer, uint32_t online_id)
const;
289 void writePlayerReport(
Event* event);
308 std::pair<std::set<std::string>, std::set<std::string> >
m_addon_kts;
316 std::map<std::weak_ptr<STKPeer>, std::set<irr::core::stringw>,
317 std::owner_less<std::weak_ptr<STKPeer> > > m_peers_muted_players;
324 virtual void setup() OVERRIDE;
325 virtual
void update(
int ticks) OVERRIDE;
332 ServerState getCurrentState()
const {
return m_state.load(); }
333 void updateBanList();
334 bool waitingForPlayers()
const;
335 virtual bool allPlayersReady() const OVERRIDE
336 {
return m_state.load() >= WAIT_FOR_RACE_STARTED; }
337 virtual bool isRacing() const OVERRIDE {
return m_state.load() == RACING; }
338 bool allowJoinedPlayersWaiting()
const;
339 void setSaveServerConfig(
bool val) { m_save_server_config = val; }
340 float getStartupBoostOrPenaltyForKart(uint32_t ping,
unsigned kart_id);
341 int getDifficulty()
const {
return m_difficulty.load(); }
342 int getGameMode()
const {
return m_game_mode.load(); }
343 int getLobbyPlayers()
const {
return m_lobby_players.load(); }
344 void saveInitialItems(std::shared_ptr<NetworkItemManager> nim);
347 void initServerStatsTable();
348 bool isAIProfile(
const std::shared_ptr<NetworkPlayerProfile>& npp)
const
353 uint32_t getServerIdOnline()
const {
return m_server_id_online; }
354 uint32_t getClientServerHostId()
const {
return m_client_server_host_id.load(); }
355 void setClientServerHostId(uint32_t
id) { m_client_server_host_id = id; }
356 static int m_fixed_laps;
357 bool playerReportsTableExists()
const;
359 ProcessType getProcessType() {
return m_process_type; }
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
Class representing an event that need to pass trough the system. This is used to remove ENet dependen...
Definition: event.hpp:73
Used to store the needed data about the players that join a game. This class stores all the possible ...
Definition: game_setup.hpp:43
Base class for both client and server lobby. The lobbies are started when a server opens a game,...
Definition: lobby_protocol.hpp:47
GameSetup * m_game_setup
Stores data about the online game to play.
Definition: lobby_protocol.hpp:141
The network item manager is responsible for handling all network related item manager tasks - synchro...
Definition: network_item_manager.hpp:45
Contains the profile of a player.
Definition: network_player_profile.hpp:42
A new implementation of NetworkString, which has a fixed format: Byte 0: The type of the message,...
Definition: network_string.hpp:422
A simple structure to store a vote from a client: track name, number of laps and reverse or not.
Definition: peer_vote.hpp:30
Definition: ranking.hpp:57
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
Definition: server_lobby.hpp:52
virtual void setup() OVERRIDE
A previous GameSetup is deleted and a new one is created.
Definition: server_lobby.cpp:372
void computeNewRankings()
Compute the new player's rankings used in ranked servers.
Definition: server_lobby.cpp:2373
std::pair< std::set< std::string >, std::set< std::string > > m_available_kts
Available karts and tracks for all clients, this will be initialized with data in server first.
Definition: server_lobby.hpp:111
std::atomic_bool m_server_has_loaded_world
Keeps track of the server state.
Definition: server_lobby.hpp:114
void handlePlayerVote(Event *event)
Called when a player votes for track(s), it will auto correct client data if it sends some invalid da...
Definition: server_lobby.cpp:3177
void finishedLoadingWorldClient(Event *event)
Called when a client notifies the server that it has loaded the world.
Definition: server_lobby.cpp:3487
void updateTracksForMode()
Called whenever server is reset or game mode is changed.
Definition: server_lobby.cpp:293
void kartSelectionRequested(Event *event)
Called when a player asks to select karts.
Definition: server_lobby.cpp:3154
std::pair< std::set< std::string >, std::set< std::string > > m_addon_kts
Addon karts and tracks available in server.
Definition: server_lobby.hpp:308
void finishedLoadingLiveJoinClient(Event *event)
Finally put the kart in the world and inform client the current world status, (including current conf...
Definition: server_lobby.cpp:1403
int getReservedId(std::shared_ptr< NetworkPlayerProfile > &p, unsigned local_id) const
Decide where to put the live join player depends on his team and game mode.
Definition: server_lobby.cpp:1339
void handleServerConfiguration(Event *event)
Called when the server owner request to change game mode or difficulty.
Definition: server_lobby.cpp:3915
std::set< std::string > m_addon_arenas
Addon arenas available in server.
Definition: server_lobby.hpp:311
void changeHandicap(Event *event)
Called when a player want to change his handicap.
Definition: server_lobby.cpp:4023
void registerServer(bool first_time)
Register this server (i.e.
Definition: server_lobby.cpp:1710
void unregisterServer(bool now, std::weak_ptr< ServerLobby > sl=std::weak_ptr< ServerLobby >())
Unregister this server (i.e.
Definition: server_lobby.cpp:1804
std::vector< std::shared_ptr< NetworkPlayerProfile > > getLivePlayers() const
Get a list of current ingame players for live join or spectate.
Definition: server_lobby.cpp:1302
void clientInGameWantsToBackLobby(Event *event)
Client if currently in-game (including spectator) wants to go back to lobby.
Definition: server_lobby.cpp:4264
virtual bool notifyEvent(Event *event) OVERRIDE
Notify a protocol matching the Event type of that event.
Definition: server_lobby.cpp:423
void checkIncomingConnectionRequests()
Query the STK server for connection requests.
Definition: server_lobby.cpp:2165
void rejectLiveJoin(STKPeer *peer, BackLobbyReason blr)
STKPeer peer will be reset back to the lobby with reason BackLobbyReason blr
Definition: server_lobby.cpp:1208
void clientSelectingAssetsWantsToBackLobby(Event *event)
Client if currently select assets wants to go back to lobby.
Definition: server_lobby.cpp:4348
std::weak_ptr< STKPeer > m_ai_peer
AI peer which holds the list of reserved AI for dedicated server.
Definition: server_lobby.hpp:100
void handleKartInfo(Event *event)
Tell the client RemoteKartInfo of a player when some player joining live.
Definition: server_lobby.cpp:4228
void checkRaceFinished()
Checks if the race is finished, and if so informs the clients and switches to state RESULT_DISPLAY,...
Definition: server_lobby.cpp:2296
virtual ~ServerLobby()
Destructor.
Definition: server_lobby.cpp:202
std::pair< std::set< std::string >, std::set< std::string > > m_official_kts
Official karts and tracks available in server.
Definition: server_lobby.hpp:303
void configPeersStartTime()
This function is called when all clients have loaded the world and are therefore ready to start the r...
Definition: server_lobby.cpp:3646
std::vector< std::shared_ptr< NetworkPlayerProfile > > m_ai_profiles
AI profiles for all-in-one graphical client server, this will be a fixed count thorough the live time...
Definition: server_lobby.hpp:105
void startSelection(const Event *event=NULL)
Instructs all clients to start the kart selection.
Definition: server_lobby.cpp:1868
bool handleAllVotes(PeerVote *winner, uint32_t *winner_peer_id)
Select the track to be used based on all votes being received.
Definition: server_lobby.cpp:3279
bool worldIsActive() const
Returns true if world is active for clients to live join, spectate or going back to lobby live.
Definition: server_lobby.cpp:1197
virtual void update(int ticks) OVERRIDE
Simple finite state machine.
Definition: server_lobby.cpp:1503
std::atomic< int64_t > m_timeout
Timeout counter for various state.
Definition: server_lobby.hpp:127
void clientDisconnected(Event *event)
Called when a client disconnects.
Definition: server_lobby.cpp:2421
std::set< std::string > m_addon_soccers
Addon soccers available in server.
Definition: server_lobby.hpp:314
void liveJoinRequest(Event *event)
This message is like kartSelectionRequested, but it will send the peer load world message if he can j...
Definition: server_lobby.cpp:1228
void handlePlayerDisconnection() const
Update and see if any player disconnects, if so eliminate the kart in world, so this function must be...
Definition: server_lobby.cpp:4049
std::weak_ptr< STKPeer > m_server_owner
Hold the next connected peer for server owner if current one expired (disconnected).
Definition: server_lobby.hpp:298
void updatePlayerList(bool update_when_reset_server=false)
Called when any players change their setting (team for example), or connection / disconnection,...
Definition: server_lobby.cpp:2983
ServerLobby()
This is the central game setup protocol running in the server.
Definition: server_lobby.cpp:146
void addLiveJoinPlaceholder(std::vector< std::shared_ptr< NetworkPlayerProfile > > &players) const
Add reserved players for live join later if required.
Definition: server_lobby.cpp:4123
virtual void asynchronousUpdate() OVERRIDE
Find out the public IP server or poll STK server asynchronously.
Definition: server_lobby.cpp:784
void playerFinishedResult(Event *event)
Called when a client clicks on 'ok' on the race result screen.
Definition: server_lobby.cpp:3500
bool canLiveJoinNow() const
Returns true if server can be live joined or spectating.
Definition: server_lobby.cpp:1161
virtual bool notifyEventAsynchronous(Event *event) OVERRIDE
Notify a protocol matching the Event type of that event.
Definition: server_lobby.cpp:604
void finishedLoadingWorld() OVERRIDE
Called from the RaceManager of the server when the world is loaded.
Definition: server_lobby.cpp:3473
std::map< std::weak_ptr< STKPeer >, bool, std::owner_less< std::weak_ptr< STKPeer > > > m_peers_ready
Counts how many peers have finished loading the world.
Definition: server_lobby.hpp:122
Describes a IPv4 or IPv6 address in sockaddr_in(6) format, suitable in using with sendto.
Definition: socket_address.hpp:47
static uint64_t getMonoTimeMs()
Returns a time based since the starting of stk (monotonic clock).
Definition: time.hpp:113
Definition: server_lobby.hpp:72