From ee42faac1f6df518a28f86828e2868c6f643324f Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Sat, 24 Apr 2021 18:06:42 +0200 Subject: [PATCH] Determine one's own location automatically, but provide override Automatically determine the approximate location of the client by including that info in the already fetched https://info2.ddnet.tw/info. Can be overridden using the the config variable `br_location`. --- src/engine/client/serverbrowser.cpp | 24 +++++++++++++++++++++++- src/engine/client/serverbrowser.h | 1 + src/engine/shared/config_variables.h | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index 893c091f7..ae5f10d20 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -914,7 +914,20 @@ void CServerBrowser::UpdateFromHttp() const IServerBrowserPingCache::CEntry *pPingEntries; int NumPingEntries; m_pPingCache->GetPingCache(&pPingEntries, &NumPingEntries); - int OwnLocation = CServerInfo::LOC_EUROPE; + int OwnLocation; + if(str_comp(g_Config.m_BrLocation, "auto") == 0) + { + OwnLocation = m_OwnLocation; + } + else + { + if(CServerInfo::ParseLocation(&OwnLocation, g_Config.m_BrLocation)) + { + char aBuf[64]; + str_format(aBuf, sizeof(aBuf), "cannot parse br_location: '%s'", g_Config.m_BrLocation); + m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "serverbrowse", aBuf); + } + } int NumServers = m_pHttp->NumServers(); int NumLegacyServers = m_pHttp->NumLegacyServers(); @@ -1410,6 +1423,15 @@ void CServerBrowser::LoadDDNetInfoJson() json_value_free(m_pDDNetInfo); m_pDDNetInfo = 0; } + + m_OwnLocation = CServerInfo::LOC_UNKNOWN; + const json_value &Location = (*m_pDDNetInfo)["location"]; + if(Location.type != json_string || CServerInfo::ParseLocation(&m_OwnLocation, Location)) + { + char aBuf[64]; + str_format(aBuf, sizeof(aBuf), "cannot parse location from info.sjon: '%s'", (const char *)Location); + m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "serverbrowse", aBuf); + } } const json_value *CServerBrowser::LoadDDNetInfo() diff --git a/src/engine/client/serverbrowser.h b/src/engine/client/serverbrowser.h index bff0c1071..0f7acbbfd 100644 --- a/src/engine/client/serverbrowser.h +++ b/src/engine/client/serverbrowser.h @@ -159,6 +159,7 @@ private: int m_NumFavoriteServers; CNetwork m_aNetworks[NUM_NETWORKS]; + int m_OwnLocation = CServerInfo::LOC_UNKNOWN; json_value *m_pDDNetInfo; diff --git a/src/engine/shared/config_variables.h b/src/engine/shared/config_variables.h index 91881f980..e1bed93c0 100644 --- a/src/engine/shared/config_variables.h +++ b/src/engine/shared/config_variables.h @@ -63,6 +63,7 @@ MACRO_CONFIG_INT(BrFilterUnfinishedMap, br_filter_unfinished_map, 0, 0, 1, CFGFL MACRO_CONFIG_STR(BrFilterExcludeCountries, br_filter_exclude_countries, 128, "", CFGFLAG_SAVE | CFGFLAG_CLIENT, "Filter out DDNet servers by country") MACRO_CONFIG_STR(BrFilterExcludeTypes, br_filter_exclude_types, 128, "", CFGFLAG_SAVE | CFGFLAG_CLIENT, "Filter out DDNet servers by type (mod)") MACRO_CONFIG_INT(BrIndicateFinished, br_indicate_finished, 1, 0, 1, CFGFLAG_SAVE | CFGFLAG_CLIENT, "Show whether you have finished a DDNet map (transmits your player name to info2.ddnet.tw/info)") +MACRO_CONFIG_STR(BrLocation, br_location, 16, "auto", CFGFLAG_SAVE | CFGFLAG_CLIENT, "Override location for ping estimation, available: auto, af, as, as:cn, eu, na, oc, sa (Automatic, Africa, Asia, China, Europe, North America, Oceania/Australia, South America") MACRO_CONFIG_STR(BrFilterExcludeCountriesKoG, br_filter_exclude_countries_kog, 128, "", CFGFLAG_SAVE | CFGFLAG_CLIENT, "Filter out kog servers by country") MACRO_CONFIG_STR(BrFilterExcludeTypesKoG, br_filter_exclude_types_kog, 128, "", CFGFLAG_SAVE | CFGFLAG_CLIENT, "Filter out kog servers by type (mod)")