Exclude DDNet servery by type

This commit is contained in:
def 2014-12-14 16:45:18 +01:00
parent 3b0f768478
commit 8d945fe968
5 changed files with 248 additions and 94 deletions

View file

@ -561,20 +561,24 @@ void CServerBrowser::Refresh(int Type)
// remove unknown elements of exclude list // remove unknown elements of exclude list
DDNetCountryFilterClean(); DDNetCountryFilterClean();
DDNetTypeFilterClean();
for(int i = 0; i < m_NumDDNetCountries; i++) for(int i = 0; i < m_NumDDNetCountries; i++)
{ {
CDDNetCountry *pCntr = &m_aDDNetCountries[i]; CDDNetCountry *pCntr = &m_aDDNetCountries[i];
// check for filter // check for filter
if (DDNetCountryFiltered(pCntr->m_aName)) if(DDNetFiltered(g_Config.m_BrFilterExcludeCountries, pCntr->m_aName))
continue; continue;
for(int g = 0; g < pCntr->m_NumServers; g++) for(int g = 0; g < pCntr->m_NumServers; g++)
{
if(!DDNetFiltered(g_Config.m_BrFilterExcludeTypes, pCntr->m_aTypes[g]))
Set(pCntr->m_aServers[g], IServerBrowser::SET_DDNET_ADD, -1, 0); Set(pCntr->m_aServers[g], IServerBrowser::SET_DDNET_ADD, -1, 0);
} }
} }
} }
}
void CServerBrowser::RequestImpl(const NETADDR &Addr, CServerEntry *pEntry) const void CServerBrowser::RequestImpl(const NETADDR &Addr, CServerEntry *pEntry) const
{ {
@ -883,6 +887,7 @@ void CServerBrowser::LoadDDNet()
{ {
// reset servers / countries // reset servers / countries
m_NumDDNetCountries = 0; m_NumDDNetCountries = 0;
m_NumDDNetTypes = 0;
// load ddnet server list // load ddnet server list
IStorage *pStorage = Kernel()->RequestInterface<IStorage>(); IStorage *pStorage = Kernel()->RequestInterface<IStorage>();
@ -906,12 +911,15 @@ void CServerBrowser::LoadDDNet()
{ {
// pSrv - { name, flagId, servers } // pSrv - { name, flagId, servers }
const json_value *pSrv = json_array_get(pCountries, i); const json_value *pSrv = json_array_get(pCountries, i);
const json_value *pAddrs = json_object_get(pSrv, "servers"); const json_value *pTypes = json_object_get(pSrv, "servers");
const json_value *pName = json_object_get(pSrv, "name"); const json_value *pName = json_object_get(pSrv, "name");
const json_value *pFlagID = json_object_get(pSrv, "flagId"); const json_value *pFlagID = json_object_get(pSrv, "flagId");
if (pSrv->type != json_object || pAddrs->type != json_array || pName->type != json_string || pFlagID->type != json_integer) if (pSrv->type != json_object || pTypes->type != json_object || pName->type != json_string || pFlagID->type != json_integer)
continue; // invalid attributes {
dbg_msg("client_srvbrowse", "Invalid attributes");
continue;
}
// build structure // build structure
CDDNetCountry *pCntr = &m_aDDNetCountries[m_NumDDNetCountries]; CDDNetCountry *pCntr = &m_aDDNetCountries[m_NumDDNetCountries];
@ -921,14 +929,36 @@ void CServerBrowser::LoadDDNet()
str_copy(pCntr->m_aName, json_string_get(pName), sizeof(pCntr->m_aName)); str_copy(pCntr->m_aName, json_string_get(pName), sizeof(pCntr->m_aName));
pCntr->m_FlagID = json_int_get(pFlagID); pCntr->m_FlagID = json_int_get(pFlagID);
// add country
for (unsigned int t = 0; t < pTypes->u.object.length; t++)
{
const char *pType = pTypes->u.object.values[t].name;
const json_value *pAddrs = pTypes->u.object.values[t].value;
// add type
if(json_array_length(pAddrs) > 0 && m_NumDDNetTypes < MAX_DDNET_TYPES)
{
int pos;
for(pos = 0; pos < m_NumDDNetTypes; pos++)
{
if(!str_comp(m_aDDNetTypes[pos], pType))
break;
}
if(pos == m_NumDDNetTypes)
{
str_copy(m_aDDNetTypes[m_NumDDNetTypes], pType, sizeof(m_aDDNetTypes[m_NumDDNetTypes]));
m_NumDDNetTypes++;
}
}
// add addresses // add addresses
for (int g = 0; g < json_array_length(pAddrs); g++) for (int g = 0; g < json_array_length(pAddrs); g++, pCntr->m_NumServers++)
{ {
const json_value *pAddr = json_array_get(pAddrs, g); const json_value *pAddr = json_array_get(pAddrs, g);
const char* pStr = json_string_get(pAddr); const char* pStr = json_string_get(pAddr);
net_addr_from_str(&pCntr->m_aServers[pCntr->m_NumServers], pStr);
net_addr_from_str(&pCntr->m_aServers[g], pStr); str_copy(pCntr->m_aTypes[pCntr->m_NumServers], pType, sizeof(pCntr->m_aTypes[pCntr->m_NumServers]));
pCntr->m_NumServers++; }
} }
m_NumDDNetCountries++; m_NumDDNetCountries++;
@ -976,27 +1006,27 @@ void CServerBrowser::ConfigSaveCallback(IConfig *pConfig, void *pUserData)
} }
} }
void CServerBrowser::DDNetCountryFilterAdd(const char *pName) void CServerBrowser::DDNetFilterAdd(char *pFilter, const char *pName)
{ {
if (DDNetCountryFiltered(pName)) if (DDNetFiltered(pFilter, pName))
return; return;
char aBuf[256]; char aBuf[128];
str_format(aBuf, sizeof(aBuf), ",%s", pName); str_format(aBuf, sizeof(aBuf), ",%s", pName);
str_append(g_Config.m_BrFilterExcludeCountries, aBuf, sizeof(g_Config.m_BrFilterExcludeCountries)); str_append(pFilter, aBuf, 128);
} }
void CServerBrowser::DDNetCountryFilterRem(const char *pName) void CServerBrowser::DDNetFilterRem(char *pFilter, const char *pName)
{ {
if (!DDNetCountryFiltered(pName)) if (!DDNetFiltered(pFilter, pName))
return; return;
// rewrite exclude/filter list // rewrite exclude/filter list
char aBuf[256]; char aBuf[128];
char *p; char *p;
str_copy(aBuf, g_Config.m_BrFilterExcludeCountries, sizeof(aBuf)); str_copy(aBuf, pFilter, sizeof(aBuf));
g_Config.m_BrFilterExcludeCountries[0] = '\0'; pFilter[0] = '\0';
p = strtok(aBuf, ","); p = strtok(aBuf, ",");
@ -1004,21 +1034,21 @@ void CServerBrowser::DDNetCountryFilterRem(const char *pName)
{ {
if(str_comp_nocase(pName, p) != 0) if(str_comp_nocase(pName, p) != 0)
{ {
char aBuf2[256]; char aBuf2[128];
str_format(aBuf2, sizeof(aBuf2), ",%s", p); str_format(aBuf2, sizeof(aBuf2), ",%s", p);
str_append(g_Config.m_BrFilterExcludeCountries, aBuf2, sizeof(g_Config.m_BrFilterExcludeCountries)); str_append(pFilter, aBuf2, 128);
} }
p = strtok(NULL, ","); p = strtok(NULL, ",");
} }
} }
bool CServerBrowser::DDNetCountryFiltered(const char *pName) bool CServerBrowser::DDNetFiltered(char *pFilter, const char *pName)
{ {
char aBuf[256]; char aBuf[128];
char *p; char *p;
str_copy(aBuf, g_Config.m_BrFilterExcludeCountries, sizeof(aBuf)); str_copy(aBuf, pFilter, sizeof(aBuf));
p = strtok(aBuf, ","); p = strtok(aBuf, ",");
@ -1035,14 +1065,14 @@ bool CServerBrowser::DDNetCountryFiltered(const char *pName)
void CServerBrowser::DDNetCountryFilterClean() void CServerBrowser::DDNetCountryFilterClean()
{ {
char aNewList[256]; char aNewList[128];
for(int i = 0; i < m_NumDDNetCountries; i++) for(int i = 0; i < m_NumDDNetCountries; i++)
{ {
const char *pName = m_aDDNetCountries[i].m_aName; const char *pName = m_aDDNetCountries[i].m_aName;
if(DDNetCountryFiltered(pName)) if(DDNetFiltered(g_Config.m_BrFilterExcludeCountries, pName))
{ {
char aBuf[256]; char aBuf[128];
str_format(aBuf, sizeof(aBuf), ",%s", pName); str_format(aBuf, sizeof(aBuf), ",%s", pName);
str_append(aNewList, aBuf, sizeof(aNewList)); str_append(aNewList, aBuf, sizeof(aNewList));
} }
@ -1050,3 +1080,21 @@ void CServerBrowser::DDNetCountryFilterClean()
str_copy(g_Config.m_BrFilterExcludeCountries, aNewList, sizeof(g_Config.m_BrFilterExcludeCountries)); str_copy(g_Config.m_BrFilterExcludeCountries, aNewList, sizeof(g_Config.m_BrFilterExcludeCountries));
} }
void CServerBrowser::DDNetTypeFilterClean()
{
char aNewList[128];
for(int i = 0; i < m_NumDDNetTypes; i++)
{
const char *pName = m_aDDNetTypes[i];
if(DDNetFiltered(g_Config.m_BrFilterExcludeTypes, pName))
{
char aBuf[128];
str_format(aBuf, sizeof(aBuf), ",%s", pName);
str_append(aNewList, aBuf, sizeof(aNewList));
}
}
str_copy(g_Config.m_BrFilterExcludeTypes, aNewList, sizeof(g_Config.m_BrFilterExcludeTypes));
}

View file

@ -34,16 +34,25 @@ public:
char m_aName[256]; char m_aName[256];
int m_FlagID; int m_FlagID;
NETADDR m_aServers[MAX_SERVERS]; NETADDR m_aServers[MAX_SERVERS];
char m_aTypes[MAX_SERVERS][32];
int m_NumServers; int m_NumServers;
void Reset() { m_NumServers = 0; m_FlagID = -1; m_aName[0] = '\0'; }; void Reset() { m_NumServers = 0; m_FlagID = -1; m_aName[0] = '\0'; };
void Add(NETADDR Addr) { if (m_NumServers < MAX_SERVERS) m_aServers[m_NumServers++] = Addr; }; /*void Add(NETADDR Addr, char* pType) {
if (m_NumServers < MAX_SERVERS)
{
m_aServers[m_NumServers] = Addr;
str_copy(m_aTypes[m_NumServers], pType, sizeof(m_aTypes[0]));
m_NumServers++;
}
};*/
}; };
enum enum
{ {
MAX_FAVORITES=2048, MAX_FAVORITES=2048,
MAX_DDNET_COUNTRIES=16, MAX_DDNET_COUNTRIES=16,
MAX_DDNET_TYPES=32,
}; };
CServerBrowser(); CServerBrowser();
@ -67,10 +76,15 @@ public:
int NumDDNetCountries() { return m_NumDDNetCountries; }; int NumDDNetCountries() { return m_NumDDNetCountries; };
int GetDDNetCountryFlag(int Index) { return m_aDDNetCountries[Index].m_FlagID; }; int GetDDNetCountryFlag(int Index) { return m_aDDNetCountries[Index].m_FlagID; };
const char *GetDDNetCountryName(int Index) { return m_aDDNetCountries[Index].m_aName; }; const char *GetDDNetCountryName(int Index) { return m_aDDNetCountries[Index].m_aName; };
void DDNetCountryFilterAdd(const char *pName);
void DDNetCountryFilterRem(const char *pName); int NumDDNetTypes() { return m_NumDDNetTypes; };
bool DDNetCountryFiltered(const char *pName); const char *GetDDNetType(int Index) { return m_aDDNetTypes[Index]; };
void DDNetFilterAdd(char *pFilter, const char *pName);
void DDNetFilterRem(char *pFilter, const char *pName);
bool DDNetFiltered(char *pFilter, const char *pName);
void DDNetCountryFilterClean(); void DDNetCountryFilterClean();
void DDNetTypeFilterClean();
// //
void Update(bool ForceResort); void Update(bool ForceResort);
@ -100,6 +114,9 @@ private:
CDDNetCountry m_aDDNetCountries[MAX_DDNET_COUNTRIES]; CDDNetCountry m_aDDNetCountries[MAX_DDNET_COUNTRIES];
int m_NumDDNetCountries; int m_NumDDNetCountries;
char m_aDDNetTypes[MAX_DDNET_TYPES][32];
int m_NumDDNetTypes;
CServerEntry *m_aServerlistIp[256]; // ip hash list CServerEntry *m_aServerlistIp[256]; // ip hash list
CServerEntry *m_pFirstReqServer; // request list CServerEntry *m_pFirstReqServer; // request list

View file

@ -107,9 +107,13 @@ public:
virtual int NumDDNetCountries() = 0; virtual int NumDDNetCountries() = 0;
virtual int GetDDNetCountryFlag(int Index) = 0; virtual int GetDDNetCountryFlag(int Index) = 0;
virtual const char *GetDDNetCountryName(int Index) = 0; virtual const char *GetDDNetCountryName(int Index) = 0;
virtual void DDNetCountryFilterAdd(const char *pName) = 0;
virtual void DDNetCountryFilterRem(const char *pName) = 0; virtual int NumDDNetTypes() = 0;
virtual bool DDNetCountryFiltered(const char *pName) = 0; virtual const char *GetDDNetType(int Index) = 0;
virtual void DDNetFilterAdd(char *pFilter, const char *pName) = 0;
virtual void DDNetFilterRem(char *pFilter, const char *pName) = 0;
virtual bool DDNetFiltered(char *pFilter, const char *pName) = 0;
}; };
#endif #endif

View file

@ -55,6 +55,7 @@ MACRO_CONFIG_INT(BrFilterPureMap, br_filter_pure_map, 0, 0, 1, CFGFLAG_SAVE|CFGF
MACRO_CONFIG_INT(BrFilterCompatversion, br_filter_compatversion, 0, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Filter out non-compatible servers in browser") MACRO_CONFIG_INT(BrFilterCompatversion, br_filter_compatversion, 0, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Filter out non-compatible servers in browser")
MACRO_CONFIG_STR(BrFilterExcludeCountries, br_filter_exclude_countries, 128, "", CFGFLAG_SAVE|CFGFLAG_CLIENT, "Filter out ddnet servers by country") 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(BrSort, br_sort, 1, 0, 256, CFGFLAG_SAVE|CFGFLAG_CLIENT, "") MACRO_CONFIG_INT(BrSort, br_sort, 1, 0, 256, CFGFLAG_SAVE|CFGFLAG_CLIENT, "")
MACRO_CONFIG_INT(BrSortOrder, br_sort_order, 0, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "") MACRO_CONFIG_INT(BrSortOrder, br_sort_order, 0, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "")

View file

@ -598,7 +598,7 @@ void CMenus::RenderServerbrowserFilters(CUIRect View)
RenderTools()->DrawUIRect(&FilterHeader, vec4(1,1,1,0.25f), CUI::CORNER_T, 4.0f); RenderTools()->DrawUIRect(&FilterHeader, vec4(1,1,1,0.25f), CUI::CORNER_T, 4.0f);
RenderTools()->DrawUIRect(&ServerFilter, vec4(0,0,0,0.15f), CUI::CORNER_B, 4.0f); RenderTools()->DrawUIRect(&ServerFilter, vec4(0,0,0,0.15f), CUI::CORNER_B, 4.0f);
UI()->DoLabelScaled(&FilterHeader, Localize("Server filter"), FontSize+2.0f, 0); UI()->DoLabelScaled(&FilterHeader, Localize("Server filter"), FontSize+2.0f, 0);
CUIRect Button; CUIRect Button, Button2;
ServerFilter.VSplitLeft(5.0f, 0, &ServerFilter); ServerFilter.VSplitLeft(5.0f, 0, &ServerFilter);
ServerFilter.Margin(3.0f, &ServerFilter); ServerFilter.Margin(3.0f, &ServerFilter);
@ -701,14 +701,101 @@ void CMenus::RenderServerbrowserFilters(CUIRect View)
if (DoButton_CheckBox((char *)&g_Config.m_UiColorizePing, Localize("Colorize ping"), g_Config.m_UiColorizePing, &Button)) if (DoButton_CheckBox((char *)&g_Config.m_UiColorizePing, Localize("Colorize ping"), g_Config.m_UiColorizePing, &Button))
g_Config.m_UiColorizePing ^= 1; g_Config.m_UiColorizePing ^= 1;
CUIRect ResetButton;
//ServerFilter.HSplitBottom(5.0f, &ServerFilter, 0);
ServerFilter.HSplitBottom(ms_ButtonHeight-2.0f, &ServerFilter, &ResetButton);
// ddnet country filters // ddnet country filters
if(g_Config.m_UiPage == PAGE_DDNET) if(g_Config.m_UiPage == PAGE_DDNET)
{ {
// add more space // add more space
ServerFilter.HSplitTop(10.0f, &Button, &ServerFilter); ServerFilter.HSplitTop(10.0f, 0, &ServerFilter);
ServerFilter.HSplitTop(20.0f, &Button, &ServerFilter);
ServerFilter.HSplitTop(95.0f, &ServerFilter, 0);
ServerFilter.HSplitTop(30.0f, &Button, &ServerFilter); RenderTools()->DrawUIRect(&ServerFilter, ms_ColorTabbarActive, CUI::CORNER_B, 10.0f);
UI()->DoLabelScaled(&Button, Localize("DDNet Countries:"), FontSize, -1);
Button.VSplitMid(&Button, &Button2);
static int s_ActivePage = 0;
static int s_CountriesButton = 0;
if(DoButton_MenuTab(&s_CountriesButton, Localize("Countries"), s_ActivePage == 0, &Button, CUI::CORNER_TL))
{
s_ActivePage = 0;
}
static int s_TypesButton = 0;
if(DoButton_MenuTab(&s_TypesButton, Localize("Types"), s_ActivePage == 1, &Button2, CUI::CORNER_TR))
{
s_ActivePage = 1;
}
if(s_ActivePage == 1)
{
int MaxTypes = ServerBrowser()->NumDDNetTypes();
int NumTypes = ServerBrowser()->NumDDNetTypes();
int PerLine = 3;
if(MaxTypes <= 12)
ServerFilter.HSplitTop(8.0f, 0, &ServerFilter);
const float TypesWidth = 40.0f;
const float TypesHeight = MaxTypes > 12 ? 15.0f : 20.0f;
CUIRect TypesRect, Left, Right;
static int s_aTypeButtons[64];
while(NumTypes > 0)
{
ServerFilter.HSplitTop(TypesHeight, &TypesRect, &ServerFilter);
TypesRect.VSplitMid(&Left, &Right);
for(int i = 0; i < PerLine && NumTypes > 0; i++, NumTypes--)
{
int TypeIndex = MaxTypes - NumTypes;
const char *pName = ServerBrowser()->GetDDNetType(TypeIndex);
bool Active = !ServerBrowser()->DDNetFiltered(g_Config.m_BrFilterExcludeTypes, pName);
vec2 Pos = vec2(TypesRect.x+TypesRect.w*((i+0.5f)/(float) PerLine), TypesRect.y);
// correct pos
Pos.x -= TypesWidth / 2.0f;
// create button logic
CUIRect Rect;
Rect.x = Pos.x;
Rect.y = Pos.y;
Rect.w = TypesWidth;
Rect.h = TypesHeight;
if (UI()->DoButtonLogic(&s_aTypeButtons[TypeIndex], "", 0, &Rect))
{
// toggle flag filter
if (Active)
ServerBrowser()->DDNetFilterAdd(g_Config.m_BrFilterExcludeTypes, pName);
else
ServerBrowser()->DDNetFilterRem(g_Config.m_BrFilterExcludeTypes, pName);
ServerBrowser()->Refresh(IServerBrowser::TYPE_DDNET);
}
vec4 Color(1.0f, 1.0f, 1.0f, 1.0f);
if (!Active)
Color.a = 0.2f;
TextRender()->TextColor(Color.r, Color.g, Color.b, Color.a);
UI()->DoLabelScaled(&Rect, pName, FontSize, 0);
TextRender()->TextColor(1.0, 1.0, 1.0, 1.0f);
}
}
}
else
{
ServerFilter.HSplitTop(17.0f, &ServerFilter, &ServerFilter);
vec4 Color(1.0f, 1.0f, 1.0f, 1.0f); vec4 Color(1.0f, 1.0f, 1.0f, 1.0f);
@ -727,11 +814,11 @@ void CMenus::RenderServerbrowserFilters(CUIRect View)
{ {
ServerFilter.HSplitTop(30.0f, &FlagsRect, &ServerFilter); ServerFilter.HSplitTop(30.0f, &FlagsRect, &ServerFilter);
for(int i = 0; i < PerLine && NumFlags > 0; i++) for(int i = 0; i < PerLine && NumFlags > 0; i++, NumFlags--)
{ {
int CountryIndex = MaxFlags - NumFlags; int CountryIndex = MaxFlags - NumFlags;
const char *pName = ServerBrowser()->GetDDNetCountryName(CountryIndex); const char *pName = ServerBrowser()->GetDDNetCountryName(CountryIndex);
bool Active = !ServerBrowser()->DDNetCountryFiltered(pName); bool Active = !ServerBrowser()->DDNetFiltered(g_Config.m_BrFilterExcludeCountries, pName);
int FlagID = ServerBrowser()->GetDDNetCountryFlag(CountryIndex); int FlagID = ServerBrowser()->GetDDNetCountryFlag(CountryIndex);
vec2 Pos = vec2(FlagsRect.x+FlagsRect.w*((i+0.5f)/(float) PerLine), FlagsRect.y); vec2 Pos = vec2(FlagsRect.x+FlagsRect.w*((i+0.5f)/(float) PerLine), FlagsRect.y);
@ -752,9 +839,9 @@ void CMenus::RenderServerbrowserFilters(CUIRect View)
{ {
// toggle flag filter // toggle flag filter
if (Active) if (Active)
ServerBrowser()->DDNetCountryFilterAdd(pName); ServerBrowser()->DDNetFilterAdd(g_Config.m_BrFilterExcludeCountries, pName);
else else
ServerBrowser()->DDNetCountryFilterRem(pName); ServerBrowser()->DDNetFilterRem(g_Config.m_BrFilterExcludeCountries, pName);
ServerBrowser()->Refresh(IServerBrowser::TYPE_DDNET); ServerBrowser()->Refresh(IServerBrowser::TYPE_DDNET);
} }
@ -765,16 +852,13 @@ void CMenus::RenderServerbrowserFilters(CUIRect View)
Color.a = 0.2f; Color.a = 0.2f;
m_pClient->m_pCountryFlags->Render(FlagID, &Color, Pos.x, Pos.y, FlagWidth, FlagHeight); m_pClient->m_pCountryFlags->Render(FlagID, &Color, Pos.x, Pos.y, FlagWidth, FlagHeight);
}
NumFlags--;
} }
} }
} }
ServerFilter.HSplitBottom(5.0f, &ServerFilter, 0);
ServerFilter.HSplitBottom(ms_ButtonHeight-2.0f, &ServerFilter, &Button);
static int s_ClearButton = 0; static int s_ClearButton = 0;
if(DoButton_Menu(&s_ClearButton, Localize("Reset filter"), 0, &Button)) if(DoButton_Menu(&s_ClearButton, Localize("Reset filter"), 0, &ResetButton))
{ {
g_Config.m_BrFilterString[0] = 0; g_Config.m_BrFilterString[0] = 0;
g_Config.m_BrExcludeString[0] = 0; g_Config.m_BrExcludeString[0] = 0;