1 //
2 //  SuperTuxKart - a fun racing game with go-kart
3 //  Copyright (C) 2013-2015 Glenn De Jonghe
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License
7 //  as published by the Free Software Foundation; either version 3
8 //  of the License, or (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 Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 #ifndef HEADER_SERVER_HPP
20 #define HEADER_SERVER_HPP
21 
22 /**
23   * \defgroup onlinegroup Online
24   * Represents a server that is joinable
25   */
26 
27 #include "race/race_manager.hpp"
28 #include "utils/types.hpp"
29 
30 #include <irrString.h>
31 
32 #include <map>
33 #include <string>
34 #include <tuple>
35 
36 class Track;
37 class XMLNode;
38 class SocketAddress;
39 
40 /**
41  * \ingroup online
42  */
43 class Server
44 {
45 public:
46 
47 protected:
48     /** The server name to be displayed. */
49     irr::core::stringw m_name;
50 
51     /** Name in lower case for comparisons. */
52     std::string m_lower_case_name;
53 
54     std::string m_server_owner_lower_case_name;
55 
56     std::string m_lower_case_player_names;
57 
58     /** We need to use full socket address structure instead of string to hold
59      *  it, because for local link address the scope id matters for
60      *  multicasting.
61      */
62     std::unique_ptr<SocketAddress> m_ipv6_address;
63 
64     uint32_t m_server_id;
65     uint32_t m_server_owner;
66 
67     /** The maximum number of players that the server supports */
68     int m_max_players;
69 
70     /** The number of players currently on the server */
71     int m_current_players;
72 
73     /** The public ip address and port of this server. */
74     std::unique_ptr<SocketAddress> m_address;
75 
76     /** This is the private port of the server. This is used if a WAN game
77      *  is started, but one client is discovered on the same LAN, so a direct
78      *  connection using the private port with a broadcast is possible. */
79     uint16_t m_private_port;
80 
81     unsigned m_server_mode;
82 
83     RaceManager::Difficulty m_difficulty;
84 
85     bool m_password_protected;
86 
87     /* WAN server only, show the owner name of server, can only be seen
88      * for localhost or if you are friend with the server owner. */
89     core::stringw m_server_owner_name;
90 
91     /* WAN server only, distance based on IP latitude and longitude. */
92     float m_distance;
93 
94     /* WAN server only, true if hosted officially by stk team. */
95     bool m_official;
96 
97     bool m_supports_encrytion;
98 
99     bool m_game_started;
100 
101     bool m_ipv6_connection;
102 
103     bool m_reconnect_when_quit_lobby;
104 
105     std::vector<std::tuple<
106         /*rank*/int, core::stringw, /*scores*/double, /*playing time*/float
107         > > m_players;
108 
109     std::string m_current_track;
110 
111     std::string m_country_code;
112 public:
113 
114          /** Initialises the object from an XML node. */
115          Server(const XMLNode& server_info);
116          Server(unsigned server_id, const irr::core::stringw &name,
117                 int max_players, int current_players, unsigned difficulty,
118                 unsigned server_mode, const SocketAddress &address,
119                 bool password_protected, bool game_started,
120                 const std::string& current_track = "");
121     // ------------------------------------------------------------------------
122     virtual ~Server();
123     // ------------------------------------------------------------------------
124     /** Returns IPv4 address and port of this server. */
getAddress() const125     const SocketAddress& getAddress() const { return *m_address.get(); }
126     // ------------------------------------------------------------------------
127     void setAddress(const SocketAddress& addr);
128     // ------------------------------------------------------------------------
129     /** Returns the lower case name of the server. */
getLowerCaseName() const130     const std::string& getLowerCaseName() const { return m_lower_case_name; }
131     // ------------------------------------------------------------------------
132     /** Returns the name of the server. */
getName() const133     const irr::core::stringw& getName() const { return m_name; }
134     // ------------------------------------------------------------------------
135     /** Returns the ID of this server. */
getServerId() const136     const uint32_t getServerId() const { return m_server_id; }
137     // ------------------------------------------------------------------------
138     /** Returns the user id in STK addon server of the server owner (WAN). */
getServerOwner() const139     const uint32_t getServerOwner() const { return m_server_owner; }
140     // ------------------------------------------------------------------------
getPrivatePort() const141     uint16_t getPrivatePort() const { return m_private_port; }
142     // ------------------------------------------------------------------------
143     /** Returns the maximum number of players allowed on this server. */
getMaxPlayers() const144     const int getMaxPlayers() const { return m_max_players; }
145     // ------------------------------------------------------------------------
146     /** Returns the number of currently connected players. */
getCurrentPlayers() const147     const int getCurrentPlayers() const { return m_current_players; }
148     // ------------------------------------------------------------------------
getServerMode() const149     unsigned getServerMode() const                    { return m_server_mode; }
150     // ------------------------------------------------------------------------
getDifficulty() const151     RaceManager::Difficulty getDifficulty() const      { return m_difficulty; }
152     // ------------------------------------------------------------------------
isPasswordProtected() const153     bool isPasswordProtected() const           { return m_password_protected; }
154     // ------------------------------------------------------------------------
getServerOwnerName() const155     const core::stringw& getServerOwnerName() const
156                                                 { return m_server_owner_name; }
157     // ------------------------------------------------------------------------
getServerOwnerLowerCaseName() const158     const std::string& getServerOwnerLowerCaseName() const
159                                      { return m_server_owner_lower_case_name; }
160     // ------------------------------------------------------------------------
getDistance() const161     float getDistance() const                            { return m_distance; }
162     // ------------------------------------------------------------------------
supportsEncryption() const163     bool supportsEncryption() const            { return m_supports_encrytion; }
164     // ------------------------------------------------------------------------
isOfficial() const165     bool isOfficial() const                              { return m_official; }
166     // ------------------------------------------------------------------------
isGameStarted() const167     bool isGameStarted() const                       { return m_game_started; }
168     // ------------------------------------------------------------------------
169     const std::vector<std::tuple<int, core::stringw, double, float> >&
getPlayers() const170         getPlayers() const                                { return m_players; }
171     // ------------------------------------------------------------------------
setServerId(unsigned id)172     void setServerId(unsigned id)                         { m_server_id = id; }
173     // ------------------------------------------------------------------------
setPrivatePort(uint16_t port)174     void setPrivatePort(uint16_t port)               { m_private_port = port; }
175     // ------------------------------------------------------------------------
setSupportsEncryption(bool val)176     void setSupportsEncryption(bool val)        { m_supports_encrytion = val; }
177     // ------------------------------------------------------------------------
178     bool searchByName(const std::string& lower_case_word);
179     // ------------------------------------------------------------------------
180     Track* getCurrentTrack() const;
181     // ------------------------------------------------------------------------
getCountryCode() const182     const std::string& getCountryCode() const        { return m_country_code; }
183     // ------------------------------------------------------------------------
setIPV6Connection(bool val)184     void setIPV6Connection(bool val)
185     {
186         if (!m_ipv6_address)
187             m_ipv6_connection = false;
188         else
189             m_ipv6_connection = val;
190     }
191     // ------------------------------------------------------------------------
useIPV6Connection() const192     bool useIPV6Connection() const                { return m_ipv6_connection; }
193     // ------------------------------------------------------------------------
194     void setIPV6Address(const SocketAddress& addr);
195     // ------------------------------------------------------------------------
getIPV6Address() const196     SocketAddress* getIPV6Address() const
197     {
198         if (!m_ipv6_address)
199             return NULL;
200         return m_ipv6_address.get();
201     }
202     // ------------------------------------------------------------------------
saveServer() const203     virtual void saveServer() const {}
204     // ------------------------------------------------------------------------
reconnectWhenQuitLobby() const205     bool reconnectWhenQuitLobby() const { return m_reconnect_when_quit_lobby; }
206     // ------------------------------------------------------------------------
setReconnectWhenQuitLobby(bool val)207     void setReconnectWhenQuitLobby(bool val)
208                                          { m_reconnect_when_quit_lobby = val; }
209 };   // Server
210 
211 class UserDefinedServer : public Server
212 {
213 public:
UserDefinedServer(const core::stringw & name,const SocketAddress & ipv4,bool password_protected=false)214     UserDefinedServer(const core::stringw& name, const SocketAddress& ipv4,
215                       bool password_protected = false)
216         : Server(0, name, 0, 0, 0, 0, ipv4, password_protected, false) {}
217     // ------------------------------------------------------------------------
218     virtual void saveServer() const;
219 };   // UserDefinedServer
220 
221 #endif // HEADER_SERVER_HPP
222