mirror of
https://gitlab.com/rubenwardy/awards.git
synced 2025-07-16 14:50:24 +02:00
Improve API: add awards.get_award_states()
This commit is contained in:
@ -182,3 +182,71 @@ function awards.unlock(name, award)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function awards.get_award_states(name)
|
||||
local hash_is_unlocked = {}
|
||||
local retval = {}
|
||||
|
||||
-- Add all unlocked awards
|
||||
local data = awards.player(name)
|
||||
if data and data.unlocked then
|
||||
for awardname, _ in pairs(data.unlocked) do
|
||||
local def = awards.registered_awards[awardname]
|
||||
if def then
|
||||
hash_is_unlocked[awardname] = true
|
||||
local score = -100000
|
||||
|
||||
local difficulty = def.difficulty or 1
|
||||
if def.trigger and def.trigger.target then
|
||||
difficulty = difficulty * def.trigger.target
|
||||
end
|
||||
score = score + difficulty
|
||||
|
||||
retval[#retval + 1] = {
|
||||
name = awardname,
|
||||
def = def,
|
||||
unlocked = true,
|
||||
started = true,
|
||||
score = score,
|
||||
progress = nil,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Add all locked awards
|
||||
for _, def in pairs(awards.registered_awards) do
|
||||
if not hash_is_unlocked[def.name] and def:can_unlock(data) then
|
||||
local progress = def.get_progress and def:get_progress(data)
|
||||
local started = false
|
||||
local score = def.difficulty or 1
|
||||
if def.secret then
|
||||
score = 1000000
|
||||
elseif def.trigger and def.trigger.target and progress then
|
||||
local perc = progress.current / progress.target
|
||||
score = score * (1 - perc) * def.trigger.target
|
||||
if perc < 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,
|
||||
progress = progress,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
table.sort(retval, function(a, b)
|
||||
return a.score < b.score
|
||||
end)
|
||||
return retval
|
||||
end
|
||||
|
@ -2,9 +2,7 @@
|
||||
|
||||
local S, NS = awards.gettext, awards.ngettext
|
||||
|
||||
awards.registered_awards = {}
|
||||
awards.on = {}
|
||||
awards.on_unlock = {}
|
||||
|
||||
local default_def = {}
|
||||
|
||||
@ -43,11 +41,12 @@ function awards.register_trigger(tname, tdef)
|
||||
}
|
||||
tdef.register(tmp)
|
||||
|
||||
function def.getProgress(_, data)
|
||||
local done = math.min(data[tname] or 0, tmp.target)
|
||||
function def.get_progress(_, data)
|
||||
local current = math.min(data[tname] or 0, tmp.target)
|
||||
return {
|
||||
perc = done / tmp.target,
|
||||
label = S(tdef.progress, done, tmp.target),
|
||||
current = current,
|
||||
target = tmp.target,
|
||||
label = S(tdef.progress, current, tmp.target),
|
||||
}
|
||||
end
|
||||
|
||||
@ -102,7 +101,7 @@ function awards.register_trigger(tname, tdef)
|
||||
end
|
||||
|
||||
-- Called to get progress values and labels
|
||||
function def.getProgress(_, data)
|
||||
function def.get_progress(_, data)
|
||||
data[tname] = data[tname] or {}
|
||||
|
||||
local done
|
||||
@ -114,7 +113,8 @@ function awards.register_trigger(tname, tdef)
|
||||
done = math.min(done, tmp.target)
|
||||
|
||||
return {
|
||||
perc = done / tmp.target,
|
||||
current = done,
|
||||
target = tmp.target,
|
||||
label = S(tdef.progress, done, tmp.target),
|
||||
}
|
||||
end
|
||||
|
83
src/gui.lua
83
src/gui.lua
@ -2,73 +2,9 @@
|
||||
|
||||
local S = awards.gettext
|
||||
|
||||
local function order_awards(name)
|
||||
local hash_is_unlocked = {}
|
||||
local retval = {}
|
||||
|
||||
local data = awards.player(name)
|
||||
if data and data.unlocked then
|
||||
for awardname, _ in pairs(data.unlocked) do
|
||||
local def = awards.registered_awards[awardname]
|
||||
if def then
|
||||
hash_is_unlocked[awardname] = true
|
||||
local score = -100000
|
||||
|
||||
local difficulty = def.difficulty or 1
|
||||
if def.trigger and def.trigger.target then
|
||||
difficulty = difficulty * def.trigger.target
|
||||
end
|
||||
score = score + difficulty
|
||||
|
||||
retval[#retval + 1] = {
|
||||
name = awardname,
|
||||
def = def,
|
||||
unlocked = true,
|
||||
started = true,
|
||||
score = score,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, def in pairs(awards.registered_awards) do
|
||||
if not hash_is_unlocked[def.name] and def:can_unlock(data) then
|
||||
local started = false
|
||||
local score = def.difficulty or 1
|
||||
if def.secret then
|
||||
score = 1000000
|
||||
elseif def.trigger and def.trigger.target and def.getProgress then
|
||||
local progress = def:getProgress(data).perc
|
||||
score = 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
|
||||
|
||||
table.sort(retval, function(a, b)
|
||||
return a.score < b.score
|
||||
end)
|
||||
return retval
|
||||
end
|
||||
|
||||
function awards.get_formspec(name, to, sid)
|
||||
local formspec = ""
|
||||
local awards_list = order_awards(name)
|
||||
local data = awards.player(name)
|
||||
local awards_list = awards.get_award_states(name)
|
||||
|
||||
if #awards_list == 0 then
|
||||
formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No achivements available.")).."]"
|
||||
@ -106,15 +42,11 @@ function awards.get_formspec(name, to, sid)
|
||||
if sdef and sdef.icon then
|
||||
formspec = formspec .. "image[0.45,0;3.5,3.5;" .. sdef.icon .. "]" -- adjusted values from 0.6,0;3,3
|
||||
end
|
||||
local barwidth = 3.95
|
||||
local perc = nil
|
||||
local label = nil
|
||||
if sdef.getProgress and data then
|
||||
local res = sdef:getProgress(data)
|
||||
perc = res.perc
|
||||
label = res.label
|
||||
end
|
||||
if perc then
|
||||
|
||||
if sitem.progress then
|
||||
local barwidth = 3.95
|
||||
local perc = sitem.progress.current / sitem.progress.target
|
||||
local label = sitem.progress.label
|
||||
if perc > 1 then
|
||||
perc = 1
|
||||
end
|
||||
@ -124,6 +56,7 @@ function awards.get_formspec(name, to, sid)
|
||||
formspec = formspec .. "label[1.6,8.15;" .. minetest.formspec_escape(label) .. "]"
|
||||
end
|
||||
end
|
||||
|
||||
if sdef and sdef.description then
|
||||
formspec = formspec .. "box[-0.05,3.75;3.9,4.2;#000]"
|
||||
formspec = formspec .. "textarea[0.25,3.75;3.9,4.2;;" ..
|
||||
@ -174,7 +107,7 @@ function awards.show_to(name, to, sid, text)
|
||||
return
|
||||
end
|
||||
if text then
|
||||
local awards_list = order_awards(name)
|
||||
local awards_list = awards.get_award_states(name)
|
||||
if #awards_list == 0 then
|
||||
minetest.chat_send_player(to, S("Error: No award available."))
|
||||
return
|
||||
|
Reference in New Issue
Block a user