Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep empty servers at bottom of list #14511

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions builtin/common/misc_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@ end


function table.insert_all(t, other)
if table.move then -- LuaJIT
return table.move(other, 1, #other, #t + 1, t)
end
for i=1, #other do
t[#t + 1] = other[i]
end
Expand Down
37 changes: 26 additions & 11 deletions builtin/mainmenu/serverlistmgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ local WEIGHT_SORT = 2
-- how much the estimated latency contributes to the final ranking
local WEIGHT_LATENCY = 1

local function order_server_list(list)
--- @param list of servers, will be modified.
local function order_server_list_internal(list)
-- calculate the scores
local s1 = Normalizer:new()
local s2 = Normalizer:new()
Expand All @@ -99,22 +100,36 @@ local function order_server_list(list)
s1 = s1:calc()
s2 = s2:calc()

-- make a shallow copy and pre-calculate ordering
local res, order = {}, {}
for i = 1, #list do
local fav = list[i]
res[i] = fav

local n = s1[fav] * WEIGHT_SORT + s2[fav] * WEIGHT_LATENCY
order[fav] = n
-- pre-calculate ordering
local order = {}
for _, fav in ipairs(list) do
order[fav] = s1[fav] * WEIGHT_SORT + s2[fav] * WEIGHT_LATENCY
end

-- now sort the list
table.sort(res, function(fav1, fav2)
table.sort(list, function(fav1, fav2)
return order[fav1] > order[fav2]
end)
end

local function order_server_list(list)
-- split the list into two parts and sort them separately, to keep empty
-- servers at the bottom.
local nonempty, empty = {}, {}

for _, fav in ipairs(list) do
if (fav.clients or 0) > 0 then
table.insert(nonempty, fav)
else
table.insert(empty, fav)
end
end

order_server_list_internal(nonempty)
order_server_list_internal(empty)

return res
table.insert_all(nonempty, empty)
return nonempty
end

local public_downloading = false
Expand Down