Keep server bookmarks in save order

This commit is contained in:
Benau 2023-05-26 11:15:44 +08:00
parent 1ff8b0fc9c
commit 3d76bc0108
6 changed files with 63 additions and 2 deletions

View File

@ -849,6 +849,11 @@ namespace UserConfigParams
"Wan server bookmarks",
{{ "server-bookmarks", "server-name", "last-online" }}, {}));
PARAM_PREFIX StringToUIntUserConfigParam m_server_bookmarks_order
PARAM_DEFAULT(StringToUIntUserConfigParam("server-bookmarks-order",
"Wan server bookmarks order",
{{ "server-bookmarks", "server-name", "id" }}, {}));
PARAM_PREFIX StringToUIntUserConfigParam m_address_history
PARAM_DEFAULT(StringToUIntUserConfigParam("address-history",
"Last 5 IP addresses that user entered",

View File

@ -17,6 +17,7 @@
#include "network/server.hpp"
#include "config/player_manager.hpp"
#include "config/user_config.hpp"
#include "io/xml_node.hpp"
#include "online/online_player_profile.hpp"
#include "online/online_profile.hpp"
@ -45,6 +46,7 @@ Server::Server(const XMLNode& server_info) : m_supports_encrytion(true)
m_server_id = 0;
m_current_players = 0;
m_current_ai = 0;
m_bookmark_id = 0;
m_max_players = 0;
m_distance = 0.0f;
m_server_mode = 0;
@ -119,6 +121,30 @@ Server::Server(const XMLNode& server_info) : m_supports_encrytion(true)
return std::get<0>(a) < std::get<0>(b);
});
std::string key = getBookmarkKey();
std::map<std::string, uint32_t>& bookmarks =
UserConfigParams::m_server_bookmarks;
if (bookmarks.find(key) != bookmarks.end())
{
std::map<std::string, uint32_t>& bookmarks_order =
UserConfigParams::m_server_bookmarks_order;
auto it = bookmarks_order.find(key);
if (it == bookmarks_order.end())
{
uint32_t max_id = 0;
for (auto& order : bookmarks_order)
{
if (order.second > max_id)
max_id = order.second;
}
max_id += 1;
bookmarks_order[key] = max_id;
m_bookmark_id = max_id;
}
else
m_bookmark_id = it->second;
}
// Show owner name as Official right now if official server hoster account
m_official = false;
xml.get("official", &m_official);
@ -200,7 +226,7 @@ Server::Server(unsigned server_id, const core::stringw &name, int max_players,
m_official = false;
m_game_started = game_started;
m_current_track = current_track;
m_current_ai = 0;
m_current_ai = m_bookmark_id = 0;
} // server(server_id, ...)
// ----------------------------------------------------------------------------

View File

@ -72,6 +72,8 @@ protected:
int m_current_ai;
uint32_t m_bookmark_id;
/** The public ip address and port of this server. */
std::unique_ptr<SocketAddress> m_address;
@ -213,7 +215,11 @@ public:
// ------------------------------------------------------------------------
std::string getBookmarkKey() const;
// ------------------------------------------------------------------------
const int getCurrentAI() const { return m_current_ai; }
const int getCurrentAI() const { return m_current_ai; }
// ------------------------------------------------------------------------
uint32_t getBookmarkID() const { return m_bookmark_id; }
// ------------------------------------------------------------------------
void setBookmarkID(uint32_t id) { m_bookmark_id = id; }
}; // Server
class UserDefinedServer : public Server

View File

@ -261,12 +261,23 @@ void ServerInfoDialog::updateBookmarkStatus(bool change_bookmark)
const std::string& key = m_server->getBookmarkKey();
std::map<std::string, uint32_t>& bookmarks =
UserConfigParams::m_server_bookmarks;
std::map<std::string, uint32_t>& bookmarks_order =
UserConfigParams::m_server_bookmarks_order;
auto it = bookmarks.find(key);
if (it == bookmarks.end())
{
if (change_bookmark)
{
bookmarks[key] = StkTime::getTimeSinceEpoch();
uint32_t max_id = 0;
for (auto& order : bookmarks_order)
{
if (order.second > max_id)
max_id = order.second;
}
max_id += 1;
bookmarks_order[key] = max_id;
m_server->setBookmarkID(max_id);
m_bookmark_widget->setLabel(_("Remove from bookmarks"));
m_bookmark_widget->setImage(m_remove_icon);
}
@ -276,6 +287,7 @@ void ServerInfoDialog::updateBookmarkStatus(bool change_bookmark)
if (change_bookmark)
{
bookmarks.erase(key);
bookmarks_order.erase(key);
m_bookmark_widget->setLabel(_("Bookmark this server"));
m_bookmark_widget->setImage(m_bookmark_icon);
}

View File

@ -154,6 +154,7 @@ void ServerSelection::init()
Screen::init();
m_last_load_time = -5000;
m_sort_default = true;
updateHeader();
#ifndef ENABLE_IPV6
@ -227,6 +228,8 @@ void ServerSelection::loadList()
{
std::shared_ptr<Server> c = m_sort_desc ? a : b;
std::shared_ptr<Server> d = m_sort_desc ? b : a;
if (g_bookmarks_next && m_sort_default)
return c->getBookmarkID() > d->getBookmarkID();
switch (m_current_column)
{
case 0:
@ -338,12 +341,14 @@ void ServerSelection::onColumnClicked(int column_id, bool sort_desc, bool sort_d
if (sort_default)
{
m_sort_desc = false;
m_sort_default = true;
m_current_column = 5/*distance*/;
loadList();
}
else
{
m_sort_desc = sort_desc;
m_sort_default = false;
m_current_column = column_id;
loadList();
}
@ -458,6 +463,8 @@ void ServerSelection::onUpdate(float dt)
all_possible_keys.insert(server->getBookmarkKey());
std::map<std::string, uint32_t>& bookmarks =
UserConfigParams::m_server_bookmarks;
std::map<std::string, uint32_t>& bookmarks_order =
UserConfigParams::m_server_bookmarks_order;
auto it = bookmarks.begin();
while (it != bookmarks.end())
{
@ -467,7 +474,10 @@ void ServerSelection::onUpdate(float dt)
all_possible_keys.end())
{
if (it->second < limit)
{
it = bookmarks.erase(it);
bookmarks_order.erase(it->first);
}
else
it++;
}

View File

@ -75,6 +75,8 @@ private:
/** \brief To check (and set) if sort order is descending **/
bool m_sort_desc;
bool m_sort_default;
int m_current_column;
bool m_refreshing_server;