Main menu: Fix server selection (#15466)

Server selections are now always correct (no more arbitrarily changing selections if the order of the serverlist changes) and consistent with the address + port in the sidebar.
This commit is contained in:
Lars Müller 2024-11-27 18:39:28 +01:00 committed by GitHub
parent 8c56434bd3
commit 6c324cb871
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -55,6 +55,36 @@ local function get_sorted_servers()
return servers return servers
end end
-- Persists the selected server in the "address" and "remote_port" settings
local function set_selected_server(server)
local address = server.address
local port = server.port
gamedata.serverdescription = server.description
gamedata.fav = false
for _, fav in ipairs(serverlistmgr.get_favorites()) do
if address == fav.address and port == fav.port then
gamedata.fav = true
break
end
end
if address and port then
core.settings:set("address", address)
core.settings:set("remote_port", port)
end
end
local function find_selected_server()
local address = core.settings:get("address")
local port = tonumber(core.settings:get("remote_port"))
for _, server in ipairs(serverlistmgr.servers) do
if server.address == address and server.port == port then
return server
end
end
end
local function get_formspec(tabview, name, tabdata) local function get_formspec(tabview, name, tabdata)
-- Update the cached supported proto info, -- Update the cached supported proto info,
-- it may have changed after a change by the settings menu. -- it may have changed after a change by the settings menu.
@ -107,15 +137,15 @@ local function get_formspec(tabview, name, tabdata)
retval = retval .. "button[0.25,6;2.5,0.75;btn_mp_register;" .. fgettext("Register") .. "]" retval = retval .. "button[0.25,6;2.5,0.75;btn_mp_register;" .. fgettext("Register") .. "]"
end end
if tabdata.selected then local selected_server = find_selected_server()
if selected_server then
if gamedata.serverdescription then if gamedata.serverdescription then
retval = retval .. "textarea[0.25,1.85;5.25,2.7;;;" .. retval = retval .. "textarea[0.25,1.85;5.25,2.7;;;" ..
core.formspec_escape(gamedata.serverdescription) .. "]" core.formspec_escape(gamedata.serverdescription) .. "]"
end end
local server = tabdata.lookup[tabdata.selected] local clients_list = selected_server.clients_list
local clients_list = server and server.clients_list
local can_view_clients_list = clients_list and #clients_list > 0 local can_view_clients_list = clients_list and #clients_list > 0
if can_view_clients_list then if can_view_clients_list then
table.sort(clients_list, function(a, b) table.sort(clients_list, function(a, b)
@ -197,11 +227,17 @@ local function get_formspec(tabview, name, tabdata)
retval = retval .. table.concat(rows, ",") retval = retval .. table.concat(rows, ",")
if tabdata.selected then local selected_row_idx = 0
retval = retval .. ";" .. tabdata.selected .. "]" if selected_server then
else for i, server in pairs(tabdata.lookup) do
retval = retval .. ";0]" if selected_server.address == server.address and
selected_server.port == server.port then
selected_row_idx = i
break
end end
end
end
retval = retval .. ";" .. selected_row_idx .. "]"
return retval return retval
end end
@ -253,35 +289,6 @@ local function search_server_list(input)
menudata.search_result = search_result menudata.search_result = search_result
end end
local function set_selected_server(tabdata, idx, server)
-- reset selection
if idx == nil or server == nil then
tabdata.selected = nil
core.settings:set("address", "")
core.settings:set("remote_port", "30000")
return
end
local address = server.address
local port = server.port
gamedata.serverdescription = server.description
gamedata.fav = false
for _, fav in ipairs(serverlistmgr.get_favorites()) do
if address == fav.address and port == fav.port then
gamedata.fav = true
break
end
end
if address and port then
core.settings:set("address", address)
core.settings:set("remote_port", port)
end
tabdata.selected = idx
end
local function main_button_handler(tabview, fields, name, tabdata) local function main_button_handler(tabview, fields, name, tabdata)
if fields.te_name then if fields.te_name then
gamedata.playername = fields.te_name gamedata.playername = fields.te_name
@ -312,14 +319,13 @@ local function main_button_handler(tabview, fields, name, tabdata)
gamedata.serverdescription = server.description gamedata.serverdescription = server.description
if gamedata.address and gamedata.port then if gamedata.address and gamedata.port then
core.settings:set("address", gamedata.address) set_selected_server(server)
core.settings:set("remote_port", gamedata.port)
core.start() core.start()
end end
return true return true
end end
if event.type == "CHG" then if event.type == "CHG" then
set_selected_server(tabdata, event.row, server) set_selected_server(server)
return true return true
end end
end end
@ -332,13 +338,12 @@ local function main_button_handler(tabview, fields, name, tabdata)
if not server then return end if not server then return end
serverlistmgr.delete_favorite(server) serverlistmgr.delete_favorite(server)
-- the server at [idx+1] will be at idx once list is refreshed set_selected_server(server)
set_selected_server(tabdata, idx, tabdata.lookup[idx+1])
return true return true
end end
if fields.btn_view_clients then if fields.btn_view_clients then
local dlg = create_clientslist_dialog(tabdata.lookup[tabdata.selected]) local dlg = create_clientslist_dialog(find_selected_server())
dlg:set_parent(tabview) dlg:set_parent(tabview)
tabview:hide() tabview:hide()
dlg:show() dlg:show()
@ -355,8 +360,7 @@ local function main_button_handler(tabview, fields, name, tabdata)
tabdata.search_for = fields.te_search tabdata.search_for = fields.te_search
search_server_list(fields.te_search:lower()) search_server_list(fields.te_search:lower())
if menudata.search_result then if menudata.search_result then
-- first server in row 2 due to header set_selected_server(menudata.search_result[1])
set_selected_server(tabdata, 2, menudata.search_result[1])
end end
return true return true
@ -383,8 +387,6 @@ local function main_button_handler(tabview, fields, name, tabdata)
local idx = core.get_table_index("servers") local idx = core.get_table_index("servers")
local server = idx and tabdata.lookup[idx] local server = idx and tabdata.lookup[idx]
set_selected_server(tabdata)
if server and server.address == gamedata.address and if server and server.address == gamedata.address and
server.port == gamedata.port then server.port == gamedata.port then