1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SERVERBROWSER_H
4#define ENGINE_SERVERBROWSER_H
5
6#include "kernel.h"
7
8#include <base/color.h>
9#include <base/hash.h>
10#include <base/str.h>
11
12#include <engine/map.h>
13#include <engine/shared/protocol.h>
14
15#include <generated/protocol7.h>
16
17#include <optional>
18#include <unordered_set>
19#include <vector>
20
21static constexpr const char *DDNET_INFO_FILE = "ddnet-info.json";
22static constexpr const char *DDNET_INFO_URL = "https://info.ddnet.org/info";
23
24class CUIElement;
25
26class CServerInfo
27{
28public:
29 enum
30 {
31 LOC_UNKNOWN = 0,
32 LOC_AFRICA,
33 LOC_ASIA,
34 LOC_AUSTRALIA,
35 LOC_EUROPE,
36 LOC_NORTH_AMERICA,
37 LOC_SOUTH_AMERICA,
38 // Special case China because it has an exceptionally bad
39 // connection to the outside due to the Great Firewall of
40 // China:
41 // https://en.wikipedia.org/w/index.php?title=Great_Firewall&oldid=1019589632
42 LOC_CHINA,
43 NUM_LOCS,
44 };
45
46 enum EClientScoreKind
47 {
48 CLIENT_SCORE_KIND_UNSPECIFIED,
49 CLIENT_SCORE_KIND_POINTS,
50 CLIENT_SCORE_KIND_TIME,
51 CLIENT_SCORE_KIND_TIME_BACKCOMPAT,
52 };
53
54 enum ERankState
55 {
56 RANK_UNAVAILABLE,
57 RANK_RANKED,
58 RANK_UNRANKED,
59 };
60
61 enum
62 {
63 MAX_COMMUNITY_ID_LENGTH = 32,
64 MAX_COMMUNITY_COUNTRY_LENGTH = 32,
65 MAX_COMMUNITY_TYPE_LENGTH = 32,
66 };
67
68 class CClient
69 {
70 public:
71 char m_aName[MAX_NAME_LENGTH];
72 char m_aClan[MAX_CLAN_LENGTH];
73 /**
74 * Country code in ISO 3166-1 numeric.
75 */
76 int m_Country;
77 int m_Score;
78 bool m_Player;
79 bool m_Afk;
80 int m_FriendState;
81 // skin info 0.6
82 char m_aSkin[MAX_SKIN_LENGTH];
83 bool m_CustomSkinColors;
84 int m_CustomSkinColorBody;
85 int m_CustomSkinColorFeet;
86 // skin info 0.7
87 char m_aaSkin7[protocol7::NUM_SKINPARTS][protocol7::MAX_SKIN_LENGTH];
88 bool m_aUseCustomSkinColor7[protocol7::NUM_SKINPARTS];
89 int m_aCustomSkinColor7[protocol7::NUM_SKINPARTS];
90 };
91
92 int m_ServerIndex;
93
94 int m_Type;
95 uint64_t m_ReceivedPackets;
96
97 int m_NumAddresses;
98 NETADDR m_aAddresses[MAX_SERVER_ADDRESSES];
99
100 int m_QuickSearchHit;
101 int m_FriendState;
102 int m_FriendNum;
103
104 int m_MaxClients;
105 int m_NumClients;
106 int m_MaxPlayers;
107 int m_NumPlayers;
108 int m_Flags;
109 EClientScoreKind m_ClientScoreKind;
110 TRISTATE m_Favorite;
111 TRISTATE m_FavoriteAllowPing;
112 char m_aCommunityId[MAX_COMMUNITY_ID_LENGTH];
113 char m_aCommunityCountry[MAX_COMMUNITY_COUNTRY_LENGTH];
114 char m_aCommunityType[MAX_COMMUNITY_TYPE_LENGTH];
115 int m_Location;
116 bool m_LatencyIsEstimated;
117 int m_Latency; // in ms
118 ERankState m_HasRank;
119 char m_aGameType[16];
120 ColorRGBA m_GametypeColor;
121 char m_aName[64];
122 char m_aMap[MAX_MAP_LENGTH];
123 int m_MapCrc;
124 int m_MapSize;
125 char m_aVersion[32];
126 char m_aAddress[MAX_SERVER_ADDRESSES * NETADDR_MAXSTRSIZE];
127 std::vector<CClient> m_vClients;
128 int m_NumFilteredPlayers;
129 bool m_RequiresLogin;
130
131 static int EstimateLatency(int Loc1, int Loc2);
132 static bool ParseLocation(int *pResult, const char *pString);
133 static ColorRGBA GametypeColor(const char *pGametype);
134};
135
136class CCommunityCountryServer
137{
138 NETADDR m_Address;
139 char m_aTypeName[CServerInfo::MAX_COMMUNITY_TYPE_LENGTH];
140
141public:
142 CCommunityCountryServer(NETADDR Address, const char *pTypeName) :
143 m_Address(Address)
144 {
145 str_copy(dst&: m_aTypeName, src: pTypeName);
146 }
147
148 NETADDR Address() const { return m_Address; }
149 const char *TypeName() const { return m_aTypeName; }
150};
151
152class CCommunityCountry
153{
154 friend class CServerBrowser;
155
156 char m_aName[CServerInfo::MAX_COMMUNITY_COUNTRY_LENGTH];
157 /**
158 * Country code in ISO 3166-1 numeric.
159 */
160 int m_FlagId;
161 std::vector<CCommunityCountryServer> m_vServers;
162
163public:
164 CCommunityCountry(const char *pName, int FlagId) :
165 m_FlagId(FlagId)
166 {
167 str_copy(dst&: m_aName, src: pName);
168 }
169
170 const char *Name() const { return m_aName; }
171 int FlagId() const { return m_FlagId; }
172 const std::vector<CCommunityCountryServer> &Servers() const { return m_vServers; }
173};
174
175class CCommunityType
176{
177 char m_aName[CServerInfo::MAX_COMMUNITY_TYPE_LENGTH];
178
179public:
180 CCommunityType(const char *pName)
181 {
182 str_copy(dst&: m_aName, src: pName);
183 }
184
185 const char *Name() const { return m_aName; }
186};
187
188class CCommunityMap
189{
190 char m_aName[MAX_MAP_LENGTH];
191
192public:
193 CCommunityMap(const char *pName)
194 {
195 str_copy(dst&: m_aName, src: pName);
196 }
197
198 const char *Name() const { return m_aName; }
199
200 bool operator==(const CCommunityMap &Other) const
201 {
202 return str_comp(a: Name(), b: Other.Name()) == 0;
203 }
204
205 bool operator!=(const CCommunityMap &Other) const
206 {
207 return !(*this == Other);
208 }
209
210 struct SHash
211 {
212 size_t operator()(const CCommunityMap &Map) const
213 {
214 return str_quickhash(str: Map.Name());
215 }
216 };
217};
218
219class CCommunity
220{
221 friend class CServerBrowser;
222
223 char m_aId[CServerInfo::MAX_COMMUNITY_ID_LENGTH];
224 char m_aName[64];
225 std::optional<SHA256_DIGEST> m_IconSha256;
226 char m_aIconUrl[128];
227 std::vector<CCommunityCountry> m_vCountries;
228 std::vector<CCommunityType> m_vTypes;
229 int m_NumPlayers = 0;
230 bool m_HasFinishes = false;
231 std::unordered_set<CCommunityMap, CCommunityMap::SHash> m_FinishedMaps;
232
233public:
234 CCommunity(const char *pId, const char *pName, std::optional<SHA256_DIGEST> IconSha256, const char *pIconUrl) :
235 m_IconSha256(IconSha256)
236 {
237 str_copy(dst&: m_aId, src: pId);
238 str_copy(dst&: m_aName, src: pName);
239 str_copy(dst&: m_aIconUrl, src: pIconUrl);
240 }
241
242 const char *Id() const { return m_aId; }
243 const char *Name() const { return m_aName; }
244 const char *IconUrl() const { return m_aIconUrl; }
245 const std::optional<SHA256_DIGEST> &IconSha256() const { return m_IconSha256; }
246 const std::vector<CCommunityCountry> &Countries() const { return m_vCountries; }
247 const std::vector<CCommunityType> &Types() const { return m_vTypes; }
248 bool HasCountry(const char *pCountryName) const;
249 bool HasType(const char *pTypeName) const;
250 bool HasRanks() const { return m_HasFinishes; }
251 CServerInfo::ERankState HasRank(const char *pMap) const;
252 int NumPlayers() const { return m_NumPlayers; }
253};
254
255class IFilterList
256{
257public:
258 virtual ~IFilterList() = default;
259 virtual void Add(const char *pElement) = 0;
260 virtual void Remove(const char *pElement) = 0;
261 virtual void Clear() = 0;
262 virtual bool Empty() const = 0;
263 virtual bool Filtered(const char *pElement) const = 0;
264};
265
266class ICommunityCache
267{
268public:
269 virtual ~ICommunityCache() = default;
270 virtual void Update(bool Force) = 0;
271 virtual const std::vector<const CCommunity *> &SelectedCommunities() const = 0;
272 virtual const std::vector<const CCommunityCountry *> &SelectableCountries() const = 0;
273 virtual const std::vector<const CCommunityType *> &SelectableTypes() const = 0;
274 virtual bool AnyRanksAvailable() const = 0;
275 virtual bool CountriesTypesFilterAvailable() const = 0;
276 virtual const char *CountryTypeFilterKey() const = 0;
277};
278
279class IServerBrowser : public IInterface
280{
281 MACRO_INTERFACE("serverbrowser")
282public:
283 /* Constants: Server Browser Sorting
284 SORT_NAME - Sort by name.
285 SORT_PING - Sort by ping.
286 SORT_MAP - Sort by map.
287 SORT_GAMETYPE - Sort by game type. DM, TDM etc.
288 SORT_NUMPLAYERS - Sort after how many players there are on the server.
289 SORT_NUMFRIENDS - Sort after how many friends there are on the server.
290 SORT_FAVORITES - Sort by favorite status, number of players and then ping.
291 */
292 enum
293 {
294 SORT_NAME = 0,
295 SORT_PING,
296 SORT_MAP,
297 SORT_GAMETYPE,
298 SORT_NUMPLAYERS,
299 SORT_NUMFRIENDS,
300 SORT_FAVORITES,
301 };
302
303 enum
304 {
305 QUICK_SERVERNAME = 1,
306 QUICK_PLAYER = 2,
307 QUICK_MAPNAME = 4,
308 };
309
310 enum
311 {
312 TYPE_INTERNET = 0,
313 TYPE_LAN,
314 TYPE_FAVORITES,
315 TYPE_FAVORITE_COMMUNITY_1,
316 TYPE_FAVORITE_COMMUNITY_2,
317 TYPE_FAVORITE_COMMUNITY_3,
318 TYPE_FAVORITE_COMMUNITY_4,
319 TYPE_FAVORITE_COMMUNITY_5,
320 NUM_TYPES,
321 };
322
323 enum
324 {
325 LAN_PORT_BEGIN = 8303,
326 LAN_PORT_END = 8310,
327 };
328
329 class CServerEntry
330 {
331 public:
332 int64_t m_RequestTime;
333 bool m_RequestIgnoreInfo;
334 int m_GotInfo;
335 CServerInfo m_Info;
336
337 CServerEntry *m_pPrevReq; // request list
338 CServerEntry *m_pNextReq;
339 };
340
341 static constexpr const char *COMMUNITY_DDNET = "ddnet";
342 static constexpr const char *COMMUNITY_NONE = "none";
343
344 static constexpr const char *COMMUNITY_COUNTRY_NONE = "none";
345 static constexpr const char *COMMUNITY_TYPE_NONE = "None";
346 /**
347 * Special community value for country/type filters that
348 * affect all communities.
349 */
350 static constexpr const char *COMMUNITY_ALL = "all";
351
352 static constexpr const char *SEARCH_EXCLUDE_TOKEN = ";";
353
354 virtual void Refresh(int Type, bool Force = false) = 0;
355 virtual bool IsRefreshing() const = 0;
356 virtual bool IsGettingServerlist() const = 0;
357 virtual bool IsServerlistError() const = 0;
358 virtual int LoadingProgression() const = 0;
359
360 virtual int NumServers() const = 0;
361 virtual const CServerInfo *Get(int Index) const = 0;
362
363 virtual int Players(const CServerInfo &Item) const = 0;
364 virtual int Max(const CServerInfo &Item) const = 0;
365
366 virtual int NumSortedServers() const = 0;
367 virtual int NumSortedPlayers() const = 0;
368 virtual const CServerInfo *SortedGet(int Index) const = 0;
369
370 virtual const std::vector<CCommunity> &Communities() const = 0;
371 virtual const CCommunity *Community(const char *pCommunityId) const = 0;
372 virtual std::vector<const CCommunity *> SelectedCommunities() const = 0;
373 virtual std::vector<const CCommunity *> FavoriteCommunities() const = 0;
374 virtual std::vector<const CCommunity *> CurrentCommunities() const = 0;
375 virtual unsigned CurrentCommunitiesHash() const = 0;
376
377 virtual bool DDNetInfoAvailable() const = 0;
378 virtual std::optional<SHA256_DIGEST> DDNetInfoSha256() const = 0;
379
380 virtual ICommunityCache &CommunityCache() = 0;
381 virtual const ICommunityCache &CommunityCache() const = 0;
382 virtual IFilterList &FavoriteCommunitiesFilter() = 0;
383 virtual IFilterList &CommunitiesFilter() = 0;
384 virtual IFilterList &CountriesFilter() = 0;
385 virtual IFilterList &TypesFilter() = 0;
386 virtual const IFilterList &FavoriteCommunitiesFilter() const = 0;
387 virtual const IFilterList &CommunitiesFilter() const = 0;
388 virtual const IFilterList &CountriesFilter() const = 0;
389 virtual const IFilterList &TypesFilter() const = 0;
390 virtual void CleanFilters() = 0;
391
392 virtual CServerEntry *Find(const NETADDR &Addr) = 0;
393 virtual int GetCurrentType() = 0;
394 virtual const char *GetTutorialServer() = 0;
395};
396
397#endif
398