Improve sorting algorithm

Fixes #28
This commit is contained in:
rubenwardy 2018-04-05 16:44:22 +01:00
parent 4764568f6d
commit d67d19ec50

98
gui.lua

@ -3,31 +3,70 @@
local S = awards.gettext local S = awards.gettext
local function order_awards(name) local function order_awards(name)
local done = {} local hash_is_unlocked = {}
local retval = {} local retval = {}
local player = awards.player(name)
if player and player.unlocked then local data = awards.player(name)
for _,got in pairs(player.unlocked) do if data and data.unlocked then
if awards.registered_awards[got] then for awardname, _ in pairs(data.unlocked) do
done[got] = true local def = awards.registered_awards[awardname]
table.insert(retval,{name=got,got=true}) if def then
hash_is_unlocked[awardname] = true
local score = -100000
if def.trigger and def.trigger.target then
score = score + def.trigger.target
end
retval[#retval + 1] = {
name = awardname,
def = def,
unlocked = true,
started = true,
score = score,
}
end end
end end
end end
for _,def in pairs(awards.registered_awards) do
if not done[def.name] then for _, def in pairs(awards.registered_awards) do
table.insert(retval,{name=def.name,got=false}) if not hash_is_unlocked[def.name] then
local started = false
local score
if def.secret then
score = 1000000
elseif def.trigger and def.trigger.target and def.getProgress then
local progress = def:getProgress(data).perc
score = (1 - progress) * def.trigger.target
if progress < 0.001 then
score = score + 100
else
started = true
end
else
score = 100
end
retval[#retval + 1] = {
name = def.name,
def = def,
unlocked = false,
started = started,
score = score,
}
end end
end end
table.sort(retval, function(a, b)
return a.score < b.score
end)
return retval return retval
end end
function awards.get_formspec(name, to, sid) function awards.get_formspec(name, to, sid)
local formspec = "" local formspec = ""
local listofawards = order_awards(name) local awards_list = order_awards(name)
local playerdata = awards.player(name) local data = awards.player(name)
if #listofawards == 0 then if #awards_list == 0 then
formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No awards available.")).."]" formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No awards available.")).."]"
formspec = formspec .. "button_exit[4.2,2.3;3,1;close;"..minetest.formspec_escape(S("OK")).."]" formspec = formspec .. "button_exit[4.2,2.3;3,1;close;"..minetest.formspec_escape(S("OK")).."]"
return formspec return formspec
@ -35,10 +74,10 @@ function awards.get_formspec(name, to, sid)
-- Sidebar -- Sidebar
if sid then if sid then
local item = listofawards[sid+0] local item = awards_list[sid+0]
local def = awards.registered_awards[item.name] local def = item.def
if def and def.secret and not item.got then if def and def.secret and not item.unlocked then
formspec = formspec .. "label[1,2.75;".. formspec = formspec .. "label[1,2.75;"..
minetest.formspec_escape(S("(Secret Award)")).."]".. minetest.formspec_escape(S("(Secret Award)")).."]"..
"image[1,0;3,3;awards_unknown.png]" "image[1,0;3,3;awards_unknown.png]"
@ -53,11 +92,11 @@ function awards.get_formspec(name, to, sid)
title = def.title title = def.title
end end
local status = "%s" local status = "%s"
if item.got then if item.unlocked then
status = S("%s (got)") status = S("%s (unlocked)")
end end
formspec = formspec .. "textarea[0.5,2.7;4.8,1.45;;" .. formspec = formspec .. "textarea[0.5,2.7;4.8,1.45;;" ..
string.format(status, minetest.formspec_escape(title)) .. string.format(status, minetest.formspec_escape(title)) ..
";]" ";]"
@ -67,8 +106,8 @@ function awards.get_formspec(name, to, sid)
local barwidth = 4.6 local barwidth = 4.6
local perc = nil local perc = nil
local label = nil local label = nil
if def.getProgress and playerdata then if def.getProgress and data then
local res = def:getProgress(playerdata) local res = def:getProgress(data)
perc = res.perc perc = res.perc
label = res.label label = res.label
end end
@ -91,23 +130,26 @@ function awards.get_formspec(name, to, sid)
-- Create list box -- Create list box
formspec = formspec .. "textlist[4.75,0;6,5;awards;" formspec = formspec .. "textlist[4.75,0;6,5;awards;"
local first = true local first = true
for _,award in pairs(listofawards) do for _, award in pairs(awards_list) do
local def = awards.registered_awards[award.name] local def = award.def
if def then if def then
if not first then if not first then
formspec = formspec .. "," formspec = formspec .. ","
end end
first = false first = false
if def.secret and not award.got then if def.secret and not award.unlocked then
formspec = formspec .. "#707070"..minetest.formspec_escape(S("(Secret Award)")) formspec = formspec .. "#707070"..minetest.formspec_escape(S("(Secret Award)"))
else else
local title = award.name local title = award.name
if def and def.title then if def and def.title then
title = def.title title = def.title
end end
if award.got then -- title = title .. " [" .. award.score .. "]"
if award.unlocked then
formspec = formspec .. minetest.formspec_escape(title) formspec = formspec .. minetest.formspec_escape(title)
elseif awards.started then
formspec = formspec .. "#BEBEBE".. minetest.formspec_escape(title)
else else
formspec = formspec .. "#ACACAC".. minetest.formspec_escape(title) formspec = formspec .. "#ACACAC".. minetest.formspec_escape(title)
end end
@ -128,8 +170,8 @@ function awards.show_to(name, to, sid, text)
return return
end end
if text then if text then
local listofawards = order_awards(name) local awards_list = order_awards(name)
if #listofawards == 0 then if #awards_list == 0 then
minetest.chat_send_player(to, S("Error: No awards available.")) minetest.chat_send_player(to, S("Error: No awards available."))
return return
elseif not data or not data.unlocked then elseif not data or not data.unlocked then
@ -138,7 +180,7 @@ function awards.show_to(name, to, sid, text)
end end
minetest.chat_send_player(to, string.format(S("%ss awards:"), name)) minetest.chat_send_player(to, string.format(S("%ss awards:"), name))
for _, str in pairs(data.unlocked) do for str, _ in pairs(data.unlocked) do
local def = awards.registered_awards[str] local def = awards.registered_awards[str]
if def then if def then
if def.title then if def.title then