1
0
mirror of https://gitlab.com/rubenwardy/awards.git synced 2024-12-28 03:30:40 +01:00

Implement conted_key trigger type

This commit is contained in:
rubenwardy 2018-04-04 18:30:47 +01:00
parent d82972b448
commit 28c59340ea
3 changed files with 100 additions and 61 deletions

118
api.lua
View File

@ -52,45 +52,6 @@ function awards.player_or_nil(name)
return awards.players[name] return awards.players[name]
end end
--
-- local function make_on_reg_wrapper()
-- return function(def)
-- local tmp = {
-- award = def.name,
-- key = def.trigger.node,
-- target = def.trigger.target,
-- }
-- table.insert(awards.on.dig, tmp)
--
-- function def:getProgress(data)
-- local itemcount
-- if tmp.key then
-- itemcount = data["dig"][tmp.key] or 0
-- else
-- itemcount = awards.get_total_keyed_count(data, "dig")
-- end
-- return {
-- perc = itemcount / tmp.target,
-- label = S("@1/@2 dug", itemcount, tmp.target),
-- }
-- end
--
-- function def:getDefaultDescription()
-- local n = self.trigger.target
-- if self.trigger.node then
-- local nname = minetest.registered_nodes[self.trigger.node].description
-- if nname == nil then
-- nname = self.trigger.node
-- end
-- -- Translators: @1 is count, @2 is description.
-- return NS("Mine: @2", "Mine: @1×@2", n, n, nname)
-- else
-- return NS("Mine @1 block.", "Mine @1 blocks.", n, n)
-- end
-- end
-- end
-- end
local function run_trigger_callbacks(self, player, data, table_func) local function run_trigger_callbacks(self, player, data, table_func)
for i = 1, #self.on do for i = 1, #self.on do
local res = nil local res = nil
@ -113,9 +74,10 @@ function awards.register_trigger(tname, tdef)
tdef.name = tname tdef.name = tname
tdef.run_callbacks = run_trigger_callbacks tdef.run_callbacks = run_trigger_callbacks
local datakey = tname .. "s"
tdef.data_key = datakey
if tdef.type == "counted" then if tdef.type == "counted" then
local datakey = tname .. "s"
local old_reg = tdef.on_register local old_reg = tdef.on_register
function tdef:on_register(def) function tdef:on_register(def)
@ -162,6 +124,82 @@ function awards.register_trigger(tname, tdef)
end end
awards["notify_" .. tname] = tdef.notify awards["notify_" .. tname] = tdef.notify
elseif tdef.type == "counted_key" then
local old_reg = tdef.on_register
function tdef:on_register(def)
local tmp = {
award = def.name,
key = tdef:get_key(def),
target = def.trigger.target,
}
tdef.register(tmp)
function def.getProgress(_, data)
local done
data[datakey] = data[datakey] or {}
if tmp.key then
done = data[datakey][tmp.key] or 0
else
done = data[datakey].__total or 0
end
return {
perc = done / tmp.target,
label = S(tdef.progress, done, tmp.target),
}
end
function def.getDefaultDescription(_)
local n = self.trigger.target
if tmp.key then
local nname = tmp.key
return NS(tdef.auto_description[1],
tdef.auto_description[2], n, n, nname)
else
return NS(tdef.auto_description_total[1],
tdef.auto_description_total[2], n, n)
end
end
if old_reg then
return old_reg(tdef, def)
end
end
function tdef.notify(player, key, n)
n = n or 1
assert(player and player.is_player and player:is_player() and key)
local name = player:get_player_name()
local data = awards.player(name)
print(dump(data))
-- Increment counter
data[datakey] = data[datakey] or {}
local currentVal = (data[datakey][key] or 0) + n
data[datakey][key] = currentVal
data[datakey].__total = (data[datakey].__total or 0) + n
tdef:run_callbacks(player, data, function(entry)
local current
if entry.key == key then
current = currentVal
elseif entry.key == nil then
current = data[datakey].__total
else
return
end
if current > entry.target then
return entry.award
end
end)
end
awards["notify_" .. tname] = tdef.notify
elseif tdef.type and tdef.type ~= "custom" then
error("Unrecognised trigger type " .. tdef.type)
end end
awards.registered_triggers[tname] = tdef awards.registered_triggers[tname] = tdef

View File

@ -47,7 +47,8 @@ awards.register_achievement("award_saint_maclou",{
description = "Place 20 coal checkers.", description = "Place 20 coal checkers.",
icon = "awards_novicebuilder.png", icon = "awards_novicebuilder.png",
trigger = { trigger = {
type = "chat", type = "dig",
node = "default:stone",
target = 3, target = 3,
}, },
}) })

View File

@ -42,26 +42,26 @@ awards.register_trigger("join", {
}) })
minetest.register_on_joinplayer(awards.notify_join) minetest.register_on_joinplayer(awards.notify_join)
--
-- awards.register_trigger("dig", { awards.register_trigger("dig", {
-- type = "counted_key", type = "counted_key",
-- progress = "@1/@2 dug", progress = "@1/@2 dug",
-- auto_description = { "Mine: @2", "Mine: @1×@2" }, auto_description = { "Mine: @2", "Mine: @1×@2" },
-- auto_description_total = { "Mine @1 block.", "Mine @1 blocks." }, auto_description_total = { "Mine @1 block.", "Mine @1 blocks." },
-- get_key = function(self, def) get_key = function(self, def)
-- return minetest.registered_aliases[def.trigger.node] or def.trigger.node return minetest.registered_aliases[def.trigger.node] or def.trigger.node
-- end end
-- }) })
--
-- minetest.register_on_dignode(function(pos, oldnode, player) minetest.register_on_dignode(function(pos, oldnode, player)
-- if not player or not pos or not oldnode then if not player or not pos or not oldnode then
-- return return
-- end end
--
-- local node_name = oldnode.name local node_name = oldnode.name
-- node_name = minetest.registered_aliases[node_name] or node_name node_name = minetest.registered_aliases[node_name] or node_name
-- awards.notify_dig(player, node_name) awards.notify_dig(player, node_name)
-- end) end)
-- --
-- awards.register_trigger("place", { -- awards.register_trigger("place", {
-- type = "counted_key", -- type = "counted_key",