Skip to content

Commit c6677bc

Browse files
authored
Render current number of players in community filter, sort communities by current number of players (ddnet#11683)
2 parents 5ad3185 + c9f25c6 commit c6677bc

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

src/engine/client/serverbrowser.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,11 @@ void CServerBrowser::Filter()
424424
m_vSortedServerlist.clear();
425425
m_vSortedServerlist.reserve(m_vpServerlist.size());
426426

427+
for(auto &Community : m_vCommunities)
428+
{
429+
Community.m_NumPlayers = 0;
430+
}
431+
427432
// filter the servers
428433
for(int ServerIndex = 0; ServerIndex < (int)m_vpServerlist.size(); ServerIndex++)
429434
{
@@ -590,7 +595,22 @@ void CServerBrowser::Filter()
590595
m_vSortedServerlist.push_back(ServerIndex);
591596
}
592597
}
598+
599+
if(Info.m_NumClients > 0)
600+
{
601+
auto Community = std::find_if(m_vCommunities.begin(), m_vCommunities.end(), [Info](const auto &Elem) {
602+
return str_comp(Elem.Id(), Info.m_aCommunityId) == 0;
603+
});
604+
if(Community != m_vCommunities.end())
605+
{
606+
Community->m_NumPlayers += Info.m_NumClients;
607+
}
608+
}
593609
}
610+
611+
std::stable_sort(m_vCommunities.begin(), m_vCommunities.end(), [](const CCommunity &Lhs, const CCommunity &Rhs) {
612+
return Lhs.NumPlayers() > Rhs.NumPlayers();
613+
});
594614
}
595615

596616
int CServerBrowser::SortHash() const
@@ -1356,6 +1376,7 @@ const json_value *CServerBrowser::LoadDDNetInfo()
13561376
UpdateServerRank(&pEntry->m_Info);
13571377
}
13581378
ValidateServerlistType();
1379+
RequestResort();
13591380
return m_pDDNetInfo;
13601381
}
13611382

src/engine/serverbrowser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class CCommunity
218218
char m_aIconUrl[128];
219219
std::vector<CCommunityCountry> m_vCountries;
220220
std::vector<CCommunityType> m_vTypes;
221+
int m_NumPlayers = 0;
221222
bool m_HasFinishes = false;
222223
std::unordered_set<CCommunityMap, CCommunityMap::SHash> m_FinishedMaps;
223224

@@ -240,6 +241,7 @@ class CCommunity
240241
bool HasType(const char *pTypeName) const;
241242
bool HasRanks() const { return m_HasFinishes; }
242243
CServerInfo::ERankState HasRank(const char *pMap) const;
244+
int NumPlayers() const { return m_NumPlayers; }
243245
};
244246

245247
class IFilterList

src/engine/textrender.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ namespace FontIcons
9999
[[maybe_unused]] static const char *FONT_ICON_LIST_UL = "\xEF\x83\x8A";
100100
[[maybe_unused]] static const char *FONT_ICON_INFO = "\xEF\x84\xA9";
101101
[[maybe_unused]] static const char *FONT_ICON_TERMINAL = "\xEF\x84\xA0";
102+
[[maybe_unused]] static const char *FONT_ICON_USER = "\xEF\x80\x87";
102103

103104
[[maybe_unused]] static const char *FONT_ICON_SLASH = "\xEF\x9C\x95";
104105
[[maybe_unused]] static const char *FONT_ICON_PLAY = "\xEF\x81\x8B";

src/game/client/components/menus_browser.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,27 +1036,35 @@ void CMenus::RenderServerbrowserCommunitiesFilter(CUIRect View)
10361036
const auto &&GetItemName = [&](int ItemIndex) {
10371037
return ServerBrowser()->Communities()[ItemIndex].Id();
10381038
};
1039-
const auto &&GetItemDisplayName = [&](int ItemIndex) {
1040-
return ServerBrowser()->Communities()[ItemIndex].Name();
1041-
};
10421039
const auto &&RenderItem = [&](int ItemIndex, CUIRect Item, const void *pItemId, bool Active) {
1040+
const auto &Community = ServerBrowser()->Communities()[ItemIndex];
10431041
const float Alpha = (Active ? 0.9f : 0.2f) + (Ui()->HotItem() == pItemId ? 0.1f : 0.0f);
10441042

1045-
CUIRect Icon, Label, FavoriteButton;
1043+
CUIRect Icon, NameLabel, PlayerCountIcon, PlayerCountLabel, FavoriteButton;
10461044
Item.VSplitRight(Item.h, &Item, &FavoriteButton);
1047-
Item.Margin(Spacing, &Item);
1048-
Item.VSplitLeft(Item.h * 2.0f, &Icon, &Label);
1049-
Label.VSplitLeft(Spacing, nullptr, &Label);
1050-
1051-
const char *pItemName = GetItemName(ItemIndex);
1045+
Item.HMargin(Spacing, &Item);
1046+
Item.VSplitLeft(Spacing, nullptr, &Item);
1047+
Item.VSplitRight(1.0f, &Item, nullptr);
1048+
Item.VSplitLeft(Item.h * 2.0f, &Icon, &NameLabel);
1049+
NameLabel.VSplitLeft(Spacing, nullptr, &NameLabel);
1050+
NameLabel.VSplitRight(8.0f, &NameLabel, &PlayerCountIcon);
1051+
NameLabel.VSplitRight(25.0f, &NameLabel, &PlayerCountLabel);
1052+
1053+
const char *pItemName = Community.Id();
10521054
const CCommunityIcon *pIcon = m_CommunityIcons.Find(pItemName);
10531055
if(pIcon != nullptr)
10541056
{
10551057
m_CommunityIcons.Render(pIcon, Icon, Active);
10561058
}
10571059

10581060
TextRender()->TextColor(1.0f, 1.0f, 1.0f, Alpha);
1059-
Ui()->DoLabel(&Label, GetItemDisplayName(ItemIndex), Label.h * CUi::ms_FontmodHeight, TEXTALIGN_ML);
1061+
Ui()->DoLabel(&NameLabel, Community.Name(), NameLabel.h * CUi::ms_FontmodHeight, TEXTALIGN_ML);
1062+
char aNumPlayersLabel[8];
1063+
str_format(aNumPlayersLabel, sizeof(aNumPlayersLabel), "%d", Community.NumPlayers());
1064+
Ui()->DoLabel(&PlayerCountLabel, aNumPlayersLabel, 7.0f, TEXTALIGN_MR);
1065+
TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
1066+
Ui()->DoLabel(&PlayerCountIcon, FONT_ICON_USER, 7.0f, TEXTALIGN_MC);
1067+
TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
10601068
TextRender()->TextColor(TextRender()->DefaultTextColor());
10611069

10621070
const bool Favorite = ServerBrowser()->FavoriteCommunitiesFilter().Filtered(pItemName);
@@ -1071,6 +1079,8 @@ void CMenus::RenderServerbrowserCommunitiesFilter(CUIRect View)
10711079
ServerBrowser()->FavoriteCommunitiesFilter().Add(pItemName);
10721080
}
10731081
}
1082+
GameClient()->m_Tooltips.DoToolTip(&s_vFavoriteButtonIds[ItemIndex], &FavoriteButton,
1083+
Favorite ? Localize("Click to remove this community from your favorites.") : Localize("Click to add this community to your favorites."));
10741084
};
10751085

10761086
s_vFavoriteButtonIds.resize(MaxEntries);

0 commit comments

Comments
 (0)