From 8b024accdd7b76229de87eb8b58ee688eb028beb Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 16:03:34 +0100 Subject: [PATCH] Add register_trigger_counted for homogenous events --- api.lua | 26 ++++++++ description.txt | 1 - mod.conf | 3 +- triggers.lua | 171 ++++++++++++++++-------------------------------- 4 files changed, 85 insertions(+), 116 deletions(-) delete mode 100644 description.txt 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 diff --git a/description.txt b/description.txt deleted file mode 100644 index f2b9944..0000000 --- a/description.txt +++ /dev/null @@ -1 +0,0 @@ -Adds achievements to Minetest, and an API to register new ones. diff --git a/mod.conf b/mod.conf index 24042f2..9de4e0a 100644 --- a/mod.conf +++ b/mod.conf @@ -2,6 +2,7 @@ name = awards title = Achievements author = rubenwardy description = Adds achievements to Minetest, and an API to register new ones. -license = LGPL 2.1 or later +optional_depends = intllib,sfinv,unified_inventory,default,stairs,farming,dye,beds,wool,vessels,moreblocks,fire,flowers +license = LGPLv2.1+ forum = https://forum.minetest.net/viewtopic.php?t=4870 version = 2.3.0 diff --git a/triggers.lua b/triggers.lua index ac3afc8..7d028ee 100644 --- a/triggers.lua +++ b/triggers.lua @@ -118,63 +118,6 @@ awards.register_trigger("eat", function(def) end end) -awards.register_trigger("death", function(def) - local tmp = { - award = def.name, - target = def.trigger.target, - } - table.insert(awards.on.death, tmp) - def.getProgress = function(self, data) - local itemcount = data.deaths or 0 - return { - perc = itemcount / tmp.target, - label = S("@1/@2 deaths", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - return NS("Die.", "Die @1 times.", n, n) - end -end) - -awards.register_trigger("chat", function(def) - local tmp = { - award = def.name, - target = def.trigger.target, - } - table.insert(awards.on.chat, tmp) - def.getProgress = function(self, data) - local itemcount = data.chats or 0 - return { - perc = itemcount / tmp.target, - label = S("@1/@2 chat messages", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - return NS("Write something in chat.", "Write @1 chat messages.", n, n) - end -end) - -awards.register_trigger("join", function(def) - local tmp = { - award = def.name, - target = def.trigger.target, - } - table.insert(awards.on.join, tmp) - def.getProgress = function(self, data) - local itemcount = data.joins or 0 - return { - perc = itemcount / tmp.target, - label = S("@1/@2 game joins", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - return NS("Join the game.", "Join the game @1 times.", n, n) - end -end) - awards.register_trigger("craft", function(def) local tmp = { award = def.name, @@ -326,69 +269,69 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv end) end) -minetest.register_on_dieplayer(function(player) - -- Run checks - local name = player:get_player_name() - if not player or not name or name=="" then - return +awards.register_trigger_counted("death", function(def) + local tmp = { + award = def.name, + target = def.trigger.target, + } + table.insert(awards.on.death, tmp) + def.getProgress = function(self, data) + local itemcount = data.deaths or 0 + return { + perc = itemcount / tmp.target, + label = S("@1/@2 deaths", itemcount, tmp.target), + } end - - -- Get player - awards.assertPlayer(name) - local data = awards.players[name] - - -- Increment counter - data.deaths = data.deaths + 1 - - awards.run_trigger_callbacks(player, data, "death", function(entry) - if entry.target and entry.award and data.deaths and - data.deaths >= entry.target then - return entry.award - end - end) -end) - -minetest.register_on_joinplayer(function(player) - -- Run checks - local name = player:get_player_name() - if not player or not name or name=="" then - return + def.getDefaultDescription = function(self) + local n = self.trigger.target + return NS("Die.", "Die @1 times.", n, n) end - - -- Get player - awards.assertPlayer(name) - local data = awards.players[name] - - -- Increment counter - data.joins = data.joins + 1 - - awards.run_trigger_callbacks(player, data, "join", function(entry) - if entry.target and entry.award and data.joins and - data.joins >= entry.target then - return entry.award - end - end) end) +minetest.register_on_dieplayer(awards.notify_death) +awards.register_trigger_counted("chat", function(def) + local tmp = { + award = def.name, + target = def.trigger.target, + } + table.insert(awards.on.chat, tmp) + def.getProgress = function(self, data) + local itemcount = data.chats or 0 + return { + perc = itemcount / tmp.target, + label = S("@1/@2 chat messages", itemcount, tmp.target), + } + end + def.getDefaultDescription = function(self) + local n = self.trigger.target + return NS("Write something in chat.", "Write @1 chat messages.", n, n) + end +end) minetest.register_on_chat_message(function(name, message) - -- Run checks - local idx = string.find(message,"/") - if not name or (idx ~= nil and idx <= 1) then + local player = minetest.get_player_by_name(name) + if not player or string.find(message, "/") then return end - -- Get player - awards.assertPlayer(name) - local data = awards.players[name] - local player = minetest.get_player_by_name(name) - - -- Increment counter - data.chats = data.chats + 1 - - awards.run_trigger_callbacks(player, data, "chat", function(entry) - if entry.target and entry.award and data.chats and - data.chats >= entry.target then - return entry.award - end - end) + awards.notify_chat(player) end) + +awards.register_trigger_counted("join", function(def) + local tmp = { + award = def.name, + target = def.trigger.target, + } + table.insert(awards.on.join, tmp) + def.getProgress = function(self, data) + local itemcount = data.joins or 0 + return { + perc = itemcount / tmp.target, + label = S("@1/@2 game joins", itemcount, tmp.target), + } + end + def.getDefaultDescription = function(self) + local n = self.trigger.target + return NS("Join the game.", "Join the game @1 times.", n, n) + end +end) +minetest.register_on_joinplayer(awards.notify_join)