From 8b024accdd7b76229de87eb8b58ee688eb028beb Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 16:03:34 +0100 Subject: Add register_trigger_counted for homogenous events --- api.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'api.lua') diff --git a/api.lua b/api.lua index bae9b9e..6230814 100644 --- a/api.lua +++ b/api.lua @@ -54,6 +54,32 @@ function awards.register_trigger(name, tfunc) end end +-- Registers a trigger which replies on counting +function awards.register_trigger_counted(tname, tfunc) + awards.register_trigger(tname, tfunc) + + local key = tname .. "s" + + awards["notify_" .. tname] = function(player) + assert(player and player.is_player and player:is_player()) + local name = player:get_player_name() + + awards.assertPlayer(name) + local data = awards.players[name] + + -- Increment counter + data[key] = data[key] + 1 + local currentVal = data[key] + + awards.run_trigger_callbacks(player, data, tname, function(entry) + if entry.target and entry.award and currentVal and + currentVal >= entry.target then + return entry.award + end + end) + end +end + function awards.run_trigger_callbacks(player, data, trigger, table_func) for i = 1, #awards.on[trigger] do local res = nil -- cgit v1.2.3