From 51117432f0a148e8cc4a7085c360caee013cc610 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 5 May 2019 15:48:35 +0000 Subject: [PATCH] Improve status messages and prevent duplicates --- src/openrct2-ui/windows/ServerList.cpp | 32 ++++++++++++++++++-------- src/openrct2/network/ServerList.cpp | 30 ++++++++++++++++++++---- src/openrct2/network/ServerList.h | 1 + 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/openrct2-ui/windows/ServerList.cpp b/src/openrct2-ui/windows/ServerList.cpp index c39dab3b54..f10826ab62 100644 --- a/src/openrct2-ui/windows/ServerList.cpp +++ b/src/openrct2-ui/windows/ServerList.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #ifndef DISABLE_HTTP using namespace OpenRCT2::Network; @@ -39,9 +40,9 @@ using namespace OpenRCT2::Network; static char _playerName[32 + 1]; static ServerList _serverList; -static std::future> _fetchFuture; +static std::future, rct_string_id>> _fetchFuture; static uint32_t _numPlayersOnline = 0; -static rct_string_id status_text = STR_SERVER_LIST_CONNECTING; +static rct_string_id _statusText = STR_SERVER_LIST_CONNECTING; // clang-format off enum { @@ -416,7 +417,7 @@ static void window_server_list_paint(rct_window* w, rct_drawpixelinfo* dpi) gfx_draw_string_left( dpi, STR_NETWORK_VERSION, (void*)&versionCStr, COLOUR_WHITE, w->x + 324, w->y + w->widgets[WIDX_START_SERVER].top + 1); - gfx_draw_string_left(dpi, status_text, (void*)&_numPlayersOnline, COLOUR_WHITE, w->x + 8, w->y + w->height - 15); + gfx_draw_string_left(dpi, _statusText, (void*)&_numPlayersOnline, COLOUR_WHITE, w->x + 8, w->y + w->height - 15); } static void window_server_list_scrollpaint(rct_window* w, rct_drawpixelinfo* dpi, int32_t scrollIndex) @@ -543,6 +544,10 @@ static void server_list_fetch_servers_begin() return; } + _serverList.Clear(); + _serverList.ReadAndAddFavourites(); + _statusText = STR_SERVER_LIST_CONNECTING; + _fetchFuture = std::async([] { // Spin off background fetches auto lanF = _serverList.FetchLocalServerListAsync(); @@ -559,16 +564,21 @@ static void server_list_fetch_servers_begin() { } + auto status = STR_NONE; try { auto entries = wanF.get(); allEntries.insert(allEntries.end(), entries.begin(), entries.end()); } + catch (const MasterServerException& e) + { + status = e.StatusText; + } catch (const std::exception& e) { + status = STR_SERVER_LIST_NO_CONNECTION; } - - return allEntries; + return std::make_tuple(allEntries, status); }); } @@ -581,18 +591,22 @@ static void server_list_fetch_servers_check(rct_window* w) { try { - auto entries = _fetchFuture.get(); + auto [entries, statusText] = std::move(_fetchFuture.get()); _serverList.AddRange(entries); _numPlayersOnline = _serverList.GetTotalPlayerCount(); - status_text = STR_X_PLAYERS_ONLINE; + _statusText = STR_X_PLAYERS_ONLINE; + if (statusText != STR_NONE) + { + _statusText = statusText; + } } catch (const MasterServerException& e) { - status_text = e.StatusText; + _statusText = e.StatusText; } catch (const std::exception& e) { - status_text = STR_SERVER_LIST_NO_CONNECTION; + _statusText = STR_SERVER_LIST_NO_CONNECTION; log_warning("Unable to connect to master server: %s", e.what()); } _fetchFuture = {}; diff --git a/src/openrct2/network/ServerList.cpp b/src/openrct2/network/ServerList.cpp index a7fe3ca586..d8a0e999cd 100644 --- a/src/openrct2/network/ServerList.cpp +++ b/src/openrct2/network/ServerList.cpp @@ -42,7 +42,7 @@ int32_t ServerListEntry::CompareTo(const ServerListEntry& other) const // Order by local if (a.local != b.local) { - return a.local ? 1 : -1; + return a.local ? -1 : 1; } // Then by version @@ -50,13 +50,19 @@ int32_t ServerListEntry::CompareTo(const ServerListEntry& other) const bool serverBCompatible = b.version == network_get_version(); if (serverACompatible != serverBCompatible) { - return serverACompatible ? 1 : -1; + return serverACompatible ? -1 : 1; } // Then by password protection if (a.requiresPassword != b.requiresPassword) { - return a.requiresPassword ? -1 : 1; + return a.requiresPassword ? 1 : -1; + } + + // Then by number of players + if (a.players != b.players) + { + return a.players > b.players ? -1 : 1; } // Then by name @@ -102,8 +108,19 @@ std::optional ServerListEntry::FromJson(const json_t* server) void ServerList::Sort() { + _serverEntries.erase( + std::unique( + _serverEntries.begin(), _serverEntries.end(), + [](const ServerListEntry& a, const ServerListEntry& b) { + if (a.favourite == b.favourite) + { + return String::Equals(a.address, b.address, true); + } + return false; + }), + _serverEntries.end()); std::sort(_serverEntries.begin(), _serverEntries.end(), [](const ServerListEntry& a, const ServerListEntry& b) { - return a.CompareTo(b) > 0; + return a.CompareTo(b) < 0; }); } @@ -129,6 +146,11 @@ void ServerList::AddRange(const std::vector& entries) Sort(); } +void ServerList::Clear() +{ + _serverEntries.clear(); +} + std::vector ServerList::ReadFavourites() { log_verbose("server_list_read(...)"); diff --git a/src/openrct2/network/ServerList.h b/src/openrct2/network/ServerList.h index 7a1f6b4ced..e6a147cd1b 100644 --- a/src/openrct2/network/ServerList.h +++ b/src/openrct2/network/ServerList.h @@ -51,6 +51,7 @@ public: size_t GetCount() const; void Add(const ServerListEntry& entry); void AddRange(const std::vector& entries); + void Clear(); void ReadAndAddFavourites(); void WriteFavourites();