Keep empty servers at bottom of list (#14511)

This commit is contained in:
sfan5 2024-04-05 13:00:34 +02:00 committed by GitHub
parent fd8e02195e
commit 4e1679d2a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 11 deletions

@ -490,6 +490,9 @@ end
function table.insert_all(t, other) 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 for i=1, #other do
t[#t + 1] = other[i] t[#t + 1] = other[i]
end end

@ -80,7 +80,8 @@ local WEIGHT_SORT = 2
-- how much the estimated latency contributes to the final ranking -- how much the estimated latency contributes to the final ranking
local WEIGHT_LATENCY = 1 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 -- calculate the scores
local s1 = Normalizer:new() local s1 = Normalizer:new()
local s2 = Normalizer:new() local s2 = Normalizer:new()
@ -99,22 +100,36 @@ local function order_server_list(list)
s1 = s1:calc() s1 = s1:calc()
s2 = s2:calc() s2 = s2:calc()
-- make a shallow copy and pre-calculate ordering -- pre-calculate ordering
local res, order = {}, {} local order = {}
for i = 1, #list do for _, fav in ipairs(list) do
local fav = list[i] order[fav] = s1[fav] * WEIGHT_SORT + s2[fav] * WEIGHT_LATENCY
res[i] = fav
local n = s1[fav] * WEIGHT_SORT + s2[fav] * WEIGHT_LATENCY
order[fav] = n
end end
-- now sort the list -- now sort the list
table.sort(res, function(fav1, fav2) table.sort(list, function(fav1, fav2)
return order[fav1] > order[fav2] return order[fav1] > order[fav2]
end) end)
end
return res 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)
table.insert_all(nonempty, empty)
return nonempty
end end
local public_downloading = false local public_downloading = false