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,17 +561,21 @@ void CServerBrowser::Refresh(int Type)
// remove unknown elements of exclude list
DDNetCountryFilterClean();
DDNetTypeFilterClean();
for(int i = 0; i < m_NumDDNetCountries; i++)
{
CDDNetCountry *pCntr = &m_aDDNetCountries[i];
// check for filter
if (DDNetCountryFiltered(pCntr->m_aName))
if(DDNetFiltered(g_Config.m_BrFilterExcludeCountries, pCntr->m_aName))
continue;
for(int g = 0; g < pCntr->m_NumServers; g++)
Set(pCntr->m_aServers[g], IServerBrowser::SET_DDNET_ADD, -1, 0);
{
if(!DDNetFiltered(g_Config.m_BrFilterExcludeTypes, pCntr->m_aTypes[g]))
Set(pCntr->m_aServers[g], IServerBrowser::SET_DDNET_ADD, -1, 0);
}
}
}
}
@ -674,12 +678,12 @@ void CServerBrowser::Update(bool ForceResort)
Packet.m_Address = Addr;
m_pNetClient->Send(&Packet);
if(g_Config.m_Debug)
{
{
dbg_msg("client_srvbrowse", "Count-Request sent to %d", i);
}
}
}
}
}
//Check if all server counts arrived
if(m_MasterServerCount == -1)
{
@ -882,7 +886,8 @@ void CServerBrowser::RemoveFavorite(const NETADDR &Addr)
void CServerBrowser::LoadDDNet()
{
// reset servers / countries
m_NumDDNetCountries = 0;
m_NumDDNetCountries = 0;
m_NumDDNetTypes = 0;
// load ddnet server list
IStorage *pStorage = Kernel()->RequestInterface<IStorage>();
@ -906,12 +911,15 @@ void CServerBrowser::LoadDDNet()
{
// pSrv - { name, flagId, servers }
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 *pFlagID = json_object_get(pSrv, "flagId");
if (pSrv->type != json_object || pAddrs->type != json_array || pName->type != json_string || pFlagID->type != json_integer)
continue; // invalid attributes
if (pSrv->type != json_object || pTypes->type != json_object || pName->type != json_string || pFlagID->type != json_integer)
{
dbg_msg("client_srvbrowse", "Invalid attributes");
continue;
}
// build structure
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));
pCntr->m_FlagID = json_int_get(pFlagID);
// add addresses
for (int g = 0; g < json_array_length(pAddrs); g++)
// add country
for (unsigned int t = 0; t < pTypes->u.object.length; t++)
{
const json_value *pAddr = json_array_get(pAddrs, g);
const char *pStr = json_string_get(pAddr);
const char *pType = pTypes->u.object.values[t].name;
const json_value *pAddrs = pTypes->u.object.values[t].value;
net_addr_from_str(&pCntr->m_aServers[g], pStr);
pCntr->m_NumServers++;
// 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
for (int g = 0; g < json_array_length(pAddrs); g++, pCntr->m_NumServers++)
{
const json_value *pAddr = json_array_get(pAddrs, g);
const char* pStr = json_string_get(pAddr);
net_addr_from_str(&pCntr->m_aServers[pCntr->m_NumServers], pStr);
str_copy(pCntr->m_aTypes[pCntr->m_NumServers], pType, sizeof(pCntr->m_aTypes[pCntr->m_NumServers]));
}
}
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;
char aBuf[256];
char aBuf[128];
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;
// rewrite exclude/filter list
char aBuf[256];
char aBuf[128];
char *p;
str_copy(aBuf, g_Config.m_BrFilterExcludeCountries, sizeof(aBuf));
g_Config.m_BrFilterExcludeCountries[0] = '\0';
str_copy(aBuf, pFilter, sizeof(aBuf));
pFilter[0] = '\0';
p = strtok(aBuf, ",");
@ -1004,21 +1034,21 @@ void CServerBrowser::DDNetCountryFilterRem(const char *pName)
{
if(str_comp_nocase(pName, p) != 0)
{
char aBuf2[256];
char aBuf2[128];
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, ",");
}
}
bool CServerBrowser::DDNetCountryFiltered(const char *pName)
bool CServerBrowser::DDNetFiltered(char *pFilter, const char *pName)
{
char aBuf[256];
char aBuf[128];
char *p;
str_copy(aBuf, g_Config.m_BrFilterExcludeCountries, sizeof(aBuf));
str_copy(aBuf, pFilter, sizeof(aBuf));
p = strtok(aBuf, ",");
@ -1035,14 +1065,14 @@ bool CServerBrowser::DDNetCountryFiltered(const char *pName)
void CServerBrowser::DDNetCountryFilterClean()
{
char aNewList[256];
char aNewList[128];
for(int i = 0; i < m_NumDDNetCountries; i++)
{
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_append(aNewList, aBuf, sizeof(aNewList));
}
@ -1050,3 +1080,21 @@ void CServerBrowser::DDNetCountryFilterClean()
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];
int m_FlagID;
NETADDR m_aServers[MAX_SERVERS];
char m_aTypes[MAX_SERVERS][32];
int m_NumServers;
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
{
MAX_FAVORITES=2048,
MAX_DDNET_COUNTRIES=16,
MAX_DDNET_TYPES=32,
};
CServerBrowser();
@ -67,11 +76,16 @@ public:
int NumDDNetCountries() { return m_NumDDNetCountries; };
int GetDDNetCountryFlag(int Index) { return m_aDDNetCountries[Index].m_FlagID; };
const char *GetDDNetCountryName(int Index) { return m_aDDNetCountries[Index].m_aName; };
void DDNetCountryFilterAdd(const char *pName);
void DDNetCountryFilterRem(const char *pName);
bool DDNetCountryFiltered(const char *pName);
int NumDDNetTypes() { return m_NumDDNetTypes; };
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 DDNetTypeFilterClean();
//
void Update(bool ForceResort);
void Set(const NETADDR &Addr, int Type, int Token, const CServerInfo *pInfo);
@ -100,6 +114,9 @@ private:
CDDNetCountry m_aDDNetCountries[MAX_DDNET_COUNTRIES];
int m_NumDDNetCountries;
char m_aDDNetTypes[MAX_DDNET_TYPES][32];
int m_NumDDNetTypes;
CServerEntry *m_aServerlistIp[256]; // ip hash list
CServerEntry *m_pFirstReqServer; // request list

View file

@ -107,9 +107,13 @@ public:
virtual int NumDDNetCountries() = 0;
virtual int GetDDNetCountryFlag(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 bool DDNetCountryFiltered(const char *pName) = 0;
virtual int NumDDNetTypes() = 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

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_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(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(&ServerFilter, vec4(0,0,0,0.15f), CUI::CORNER_B, 4.0f);
UI()->DoLabelScaled(&FilterHeader, Localize("Server filter"), FontSize+2.0f, 0);
CUIRect Button;
CUIRect Button, Button2;
ServerFilter.VSplitLeft(5.0f, 0, &ServerFilter);
ServerFilter.Margin(3.0f, &ServerFilter);
@ -700,81 +700,165 @@ void CMenus::RenderServerbrowserFilters(CUIRect View)
ServerFilter.HSplitTop(20.0f, &Button, &ServerFilter);
if (DoButton_CheckBox((char *)&g_Config.m_UiColorizePing, Localize("Colorize ping"), g_Config.m_UiColorizePing, &Button))
g_Config.m_UiColorizePing ^= 1;
CUIRect ResetButton;
//ServerFilter.HSplitBottom(5.0f, &ServerFilter, 0);
ServerFilter.HSplitBottom(ms_ButtonHeight-2.0f, &ServerFilter, &ResetButton);
// ddnet country filters
if(g_Config.m_UiPage == PAGE_DDNET)
{
// add more space
ServerFilter.HSplitTop(10.0f, &Button, &ServerFilter);
ServerFilter.HSplitTop(30.0f, &Button, &ServerFilter);
UI()->DoLabelScaled(&Button, Localize("DDNet Countries:"), FontSize, -1);
vec4 Color(1.0f, 1.0f, 1.0f, 1.0f);
ServerFilter.HSplitTop(10.0f, 0, &ServerFilter);
ServerFilter.HSplitTop(20.0f, &Button, &ServerFilter);
ServerFilter.HSplitTop(95.0f, &ServerFilter, 0);
const float FlagWidth = 40.0f;
const float FlagHeight = 20.0f;
RenderTools()->DrawUIRect(&ServerFilter, ms_ColorTabbarActive, CUI::CORNER_B, 10.0f);
int MaxFlags = ServerBrowser()->NumDDNetCountries();
int NumFlags = ServerBrowser()->NumDDNetCountries();
int PerLine = MaxFlags > 9 ? 4 : 3;
Button.VSplitMid(&Button, &Button2);
CUIRect FlagsRect;
static int s_ActivePage = 0;
static int s_aFlagButtons[64];
while(NumFlags > 0)
static int s_CountriesButton = 0;
if(DoButton_MenuTab(&s_CountriesButton, Localize("Countries"), s_ActivePage == 0, &Button, CUI::CORNER_TL))
{
ServerFilter.HSplitTop(30.0f, &FlagsRect, &ServerFilter);
s_ActivePage = 0;
}
for(int i = 0; i < PerLine && NumFlags > 0; i++)
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)
{
int CountryIndex = MaxFlags - NumFlags;
const char *pName = ServerBrowser()->GetDDNetCountryName(CountryIndex);
bool Active = !ServerBrowser()->DDNetCountryFiltered(pName);
int FlagID = ServerBrowser()->GetDDNetCountryFlag(CountryIndex);
ServerFilter.HSplitTop(TypesHeight, &TypesRect, &ServerFilter);
TypesRect.VSplitMid(&Left, &Right);
vec2 Pos = vec2(FlagsRect.x+FlagsRect.w*((i+0.5f)/(float) PerLine), FlagsRect.y);
// correct pos
Pos.x -= FlagWidth / 2.0f;
Pos.y -= FlagHeight / 2.0f;
// create button logic
CUIRect Rect;
Rect.x = Pos.x;
Rect.y = Pos.y;
Rect.w = FlagWidth;
Rect.h = FlagHeight;
if (UI()->DoButtonLogic(&s_aFlagButtons[CountryIndex], "", 0, &Rect))
for(int i = 0; i < PerLine && NumTypes > 0; i++, NumTypes--)
{
// toggle flag filter
if (Active)
ServerBrowser()->DDNetCountryFilterAdd(pName);
else
ServerBrowser()->DDNetCountryFilterRem(pName);
int TypeIndex = MaxTypes - NumTypes;
const char *pName = ServerBrowser()->GetDDNetType(TypeIndex);
bool Active = !ServerBrowser()->DDNetFiltered(g_Config.m_BrFilterExcludeTypes, pName);
ServerBrowser()->Refresh(IServerBrowser::TYPE_DDNET);
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);
if (!Active)
Color.a = 0.2f;
const float FlagWidth = 40.0f;
const float FlagHeight = 20.0f;
m_pClient->m_pCountryFlags->Render(FlagID, &Color, Pos.x, Pos.y, FlagWidth, FlagHeight);
int MaxFlags = ServerBrowser()->NumDDNetCountries();
int NumFlags = ServerBrowser()->NumDDNetCountries();
int PerLine = MaxFlags > 9 ? 4 : 3;
NumFlags--;
CUIRect FlagsRect;
static int s_aFlagButtons[64];
while(NumFlags > 0)
{
ServerFilter.HSplitTop(30.0f, &FlagsRect, &ServerFilter);
for(int i = 0; i < PerLine && NumFlags > 0; i++, NumFlags--)
{
int CountryIndex = MaxFlags - NumFlags;
const char *pName = ServerBrowser()->GetDDNetCountryName(CountryIndex);
bool Active = !ServerBrowser()->DDNetFiltered(g_Config.m_BrFilterExcludeCountries, pName);
int FlagID = ServerBrowser()->GetDDNetCountryFlag(CountryIndex);
vec2 Pos = vec2(FlagsRect.x+FlagsRect.w*((i+0.5f)/(float) PerLine), FlagsRect.y);
// correct pos
Pos.x -= FlagWidth / 2.0f;
Pos.y -= FlagHeight / 2.0f;
// create button logic
CUIRect Rect;
Rect.x = Pos.x;
Rect.y = Pos.y;
Rect.w = FlagWidth;
Rect.h = FlagHeight;
if (UI()->DoButtonLogic(&s_aFlagButtons[CountryIndex], "", 0, &Rect))
{
// toggle flag filter
if (Active)
ServerBrowser()->DDNetFilterAdd(g_Config.m_BrFilterExcludeCountries, pName);
else
ServerBrowser()->DDNetFilterRem(g_Config.m_BrFilterExcludeCountries, pName);
ServerBrowser()->Refresh(IServerBrowser::TYPE_DDNET);
}
vec4 Color(1.0f, 1.0f, 1.0f, 1.0f);
if (!Active)
Color.a = 0.2f;
m_pClient->m_pCountryFlags->Render(FlagID, &Color, Pos.x, Pos.y, FlagWidth, FlagHeight);
}
}
}
}
ServerFilter.HSplitBottom(5.0f, &ServerFilter, 0);
ServerFilter.HSplitBottom(ms_ButtonHeight-2.0f, &ServerFilter, &Button);
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_BrExcludeString[0] = 0;