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