1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 19:13:07 +01:00

Improve status messages and prevent duplicates

This commit is contained in:
Ted John
2019-05-05 15:48:35 +00:00
parent 04c04d197e
commit 51117432f0
3 changed files with 50 additions and 13 deletions

View File

@@ -26,6 +26,7 @@
#include <openrct2/platform/platform.h>
#include <openrct2/sprites.h>
#include <openrct2/util/Util.h>
#include <tuple>
#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<std::vector<ServerListEntry>> _fetchFuture;
static std::future<std::tuple<std::vector<ServerListEntry>, 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 = {};

View File

@@ -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> 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<ServerListEntry>& entries)
Sort();
}
void ServerList::Clear()
{
_serverEntries.clear();
}
std::vector<ServerListEntry> ServerList::ReadFavourites()
{
log_verbose("server_list_read(...)");

View File

@@ -51,6 +51,7 @@ public:
size_t GetCount() const;
void Add(const ServerListEntry& entry);
void AddRange(const std::vector<ServerListEntry>& entries);
void Clear();
void ReadAndAddFavourites();
void WriteFavourites();