From dd2a5d2d1f1884f638c9be05c43b1e7966f14dbe Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 15:46:11 +0100 Subject: Remove formspec notification --- api.lua | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index b3253ef..bae9b9e 100644 --- a/api.lua +++ b/api.lua @@ -241,14 +241,6 @@ function awards.unlock(name, award) -- Explicit check for nil because sound could be `false` to disable it sound = {name="awards_got_generic", gain=0.25} end - local custom_announce = awdef.custom_announce - if not custom_announce then - if awdef.secret then - custom_announce = S("Secret Achievement Unlocked:") - else - custom_announce = S("Achievement Unlocked:") - end - end -- Do Notification if sound then @@ -260,14 +252,7 @@ function awards.unlock(name, award) end end - if awards.show_mode == "formspec" then - -- use a formspec to send it - minetest.show_formspec(name, "achievements:unlocked", "size[6,2]".. - "image_button_exit[0,0;6,2;"..background..";close1; ]".. - "image_button_exit[0.2,0.8;1,1;"..icon..";close2; ]".. - "label[1.1,1;"..title.."]".. - "label[0.3,0.1;"..custom_announce.."]") - elseif awards.show_mode == "chat" then + if awards.show_mode == "chat" then local chat_announce if awdef.secret == true then chat_announce = S("Secret Achievement Unlocked: %s") -- cgit v1.2.3 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 From ef19940edc20791d2342b7b68b7bb098a0abc2db Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 17:01:55 +0100 Subject: Add trigger definitions rather than register function --- api.lua | 218 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 127 insertions(+), 91 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 6230814..a932e95 100644 --- a/api.lua +++ b/api.lua @@ -14,9 +14,11 @@ -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -- -local S = awards.gettext +local S, NS = awards.gettext, awards.ngettext -dofile(minetest.get_modpath("awards").."/api_helpers.lua") +awards.def = {} +awards.on = {} +awards.on_unlock = {} -- Table Save Load Functions function awards.save() @@ -27,63 +29,59 @@ function awards.save() end end -function awards.init() - awards.players = awards.load() - awards.def = {} - awards.trigger_types = {} - awards.on = {} - awards.on_unlock = {} -end - function awards.load() local file = io.open(minetest.get_worldpath().."/awards.txt", "r") if file then local table = minetest.deserialize(file:read("*all")) if type(table) == "table" then - return table + awards.players = table end end - return {} + awards.players = {} end - -function awards.register_trigger(name, tfunc) - awards.trigger_types[name] = tfunc - awards.on[name] = {} - awards['register_on_'..name] = function(func) - table.insert(awards.on[name], func) - 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 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) + for i = 1, #self.on do local res = nil - local entry = awards.on[trigger][i] + local entry = self.on[i] if type(entry) == "function" then res = entry(player, data) elseif type(entry) == "table" and entry.award then @@ -96,42 +94,89 @@ function awards.run_trigger_callbacks(player, data, trigger, table_func) end end -function awards.increment_item_counter(data, field, itemname, count) - local name_split = string.split(itemname, ":") - if #name_split ~= 2 then - return false +function awards.register_trigger(tname, tdef) + if type(tdef) == "function" then + tdef = { + on_register = tdef + } + end + tdef.name = tname + tdef.run_callbacks = run_trigger_callbacks + + if tdef.type == "counted" then + local datakey = tname .. "s" + local old_reg = tdef.on_register + + function tdef:on_register(def) + local tmp = { + award = def.name, + target = def.trigger.target, + } + tdef.register(tmp) + + function def.getProgress(_, data) + local done = data[datakey] or 0 + return { + perc = done / tmp.target, + label = S(tdef.progress, done, tmp.target), + } + end + + function def.getDefaultDescription(_) + local n = self.trigger.target + return NS(tdef.auto_description[1], tdef.auto_description[2], n, n) + end + + if old_reg then + return old_reg(tdef, def) + end + end + + function tdef.notify(player) + assert(player and player.is_player and player:is_player()) + local name = player:get_player_name() + local data = awards.player(name) + print(dump(data)) + + -- Increment counter + local currentVal = data[datakey] + 1 + data[datakey] = currentVal + + tdef:run_callbacks(player, data, function(entry) + if entry.target and entry.award and currentVal and + currentVal >= entry.target then + return entry.award + end + end) + end + + awards["notify_" .. tname] = tdef.notify end - local mod = name_split[1] - local item = name_split[2] - if data and field and mod and item then - awards.assertPlayer(data) - awards.tbv(data, field) - awards.tbv(data[field], mod) - awards.tbv(data[field][mod], item, 0) + awards.registered_triggers[tname] = tdef - data[field][mod][item] = data[field][mod][item] + (count or 1) - return true - else - return false + tdef.on = {} + tdef.register = function(func) + table.insert(tdef.on, func) end + + -- Backwards compat + awards.on[tname] = tdef.on + awards['register_on_' .. tname] = tdef.register +end + +function awards.increment_item_counter(data, field, itemname, count) + itemname = minetest.registered_aliases[itemname] or itemname + data[field][itemname] = (data[field][itemname] or 0) + 1 end function awards.get_item_count(data, field, itemname) - local name_split = string.split(itemname, ":") - if #name_split ~= 2 then - return false - end - local mod = name_split[1] - local item = name_split[2] + itemname = minetest.registered_aliases[itemname] or itemname + return data[field][itemname] or 0 +end - if data and field and mod and item then - awards.assertPlayer(data) - awards.tbv(data, field) - awards.tbv(data[field], mod) - awards.tbv(data[field][mod], item, 0) - return data[field][mod][item] - end +function awards.get_total_keyed_count(data, field) + return data[field].__total or 0 end function awards.get_total_item_count(data, field) @@ -154,23 +199,14 @@ function awards.register_on_unlock(func) table.insert(awards.on_unlock, func) end --- API Functions -function awards._additional_triggers(name, def) - -- Depreciated! -end - function awards.register_achievement(name, def) def.name = name -- Add Triggers if def.trigger and def.trigger.type then - local func = awards.trigger_types[def.trigger.type] - - if func then - func(def) - else - awards._additional_triggers(name, def) - end + local tdef = awards.registered_triggers[def.trigger.type] + assert(tdef, "Trigger not found: " .. def.trigger.type) + tdef:on_register(def) end -- Add Award @@ -515,7 +551,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return true end) -awards.init() +awards.load() minetest.register_on_newplayer(function(player) local playern = player:get_player_name() -- cgit v1.2.3 From eeee4a8398a487c0d2f51b14c36ddb7b0cbbe4ac Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 17:23:46 +0100 Subject: Clean up player data validation --- api.lua | 60 +++++++++++++----------------------------------------------- 1 file changed, 13 insertions(+), 47 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index a932e95..bd449f7 100644 --- a/api.lua +++ b/api.lua @@ -95,11 +95,9 @@ local function run_trigger_callbacks(self, player, data, table_func) end function awards.register_trigger(tname, tdef) - if type(tdef) == "function" then - tdef = { - on_register = tdef - } - end + assert(type(tdef) == "table", + "Passing a callback to register_trigger is not supported in 3.0") + tdef.name = tname tdef.run_callbacks = run_trigger_callbacks @@ -139,7 +137,7 @@ function awards.register_trigger(tname, tdef) print(dump(data)) -- Increment counter - local currentVal = data[datakey] + 1 + local currentVal = (data[datakey] or 0) + 1 data[datakey] = currentVal tdef:run_callbacks(player, data, function(entry) @@ -179,22 +177,6 @@ function awards.get_total_keyed_count(data, field) return data[field].__total or 0 end -function awards.get_total_item_count(data, field) - local i = 0 - if data and field then - awards.assertPlayer(data) - awards.tbv(data, field) - for mod,_ in pairs(data[field]) do - awards.tbv(data[field], mod) - for item,_ in pairs(data[field][mod]) do - awards.tbv(data[field][mod], item, 0) - i = i + data[field][mod][item] - end - end - end - return i -end - function awards.register_on_unlock(func) table.insert(awards.on_unlock, func) end @@ -245,23 +227,12 @@ end -- award - the name of the award to give function awards.unlock(name, award) -- Access Player Data - local data = awards.players[name] + local data = awards.player(name) local awdef = awards.def[award] + assert(awdef, "Unable to unlock an award which doesn't exist!") - -- Perform checks - if not data then - return - end - if not awdef then - return - end - if data.disabled then - return - end - awards.tbv(data,"unlocked") - - -- Don't give the achievement if it has already been given - if data.unlocked[award] and data.unlocked[award] == award then + if data.disabled or + (data.unlocked[award] and data.unlocked[award] == award) then return end @@ -307,10 +278,10 @@ function awards.unlock(name, award) -- Do Notification if sound then -- Enforce sound delay to prevent sound spamming - local lastsound = awards.players[name].lastsound + local lastsound = awards.player(name).lastsound if lastsound == nil or os.difftime(os.time(), lastsound) >= 1 then minetest.sound_play(sound, {to_player=name}) - awards.players[name].lastsound = os.time() + awards.player(name).lastsound = os.time() end end @@ -395,7 +366,7 @@ awards.give_achievement = awards.unlock function awards.getFormspec(name, to, sid) local formspec = "" local listofawards = awards._order_awards(name) - local playerdata = awards.players[name] + local playerdata = awards.player(name) if #listofawards == 0 then formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No awards available.")).."]" @@ -497,13 +468,13 @@ function awards.show_to(name, to, sid, text) if #listofawards == 0 then minetest.chat_send_player(to, S("Error: No awards available.")) return - elseif not awards.players[name] or not awards.players[name].unlocked then + elseif not awards.player(name) or not awards.player(name).unlocked then minetest.chat_send_player(to, S("You have not unlocked any awards.")) return end minetest.chat_send_player(to, string.format(S("%s’s awards:"), name)) - for _, str in pairs(awards.players[name].unlocked) do + for _, str in pairs(awards.player(name).unlocked) do local def = awards.def[str] if def then if def.title then @@ -553,11 +524,6 @@ end) awards.load() -minetest.register_on_newplayer(function(player) - local playern = player:get_player_name() - awards.assertPlayer(playern) -end) - minetest.register_on_shutdown(function() awards.save() end) -- cgit v1.2.3 From c579f862b6d4952bc56f2a0a402ad5c77e5b11f2 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 17:32:37 +0100 Subject: Reduce awards.player() calls --- api.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index bd449f7..6cb282b 100644 --- a/api.lua +++ b/api.lua @@ -278,10 +278,10 @@ function awards.unlock(name, award) -- Do Notification if sound then -- Enforce sound delay to prevent sound spamming - local lastsound = awards.player(name).lastsound + local lastsound = data.lastsound if lastsound == nil or os.difftime(os.time(), lastsound) >= 1 then minetest.sound_play(sound, {to_player=name}) - awards.player(name).lastsound = os.time() + data.lastsound = os.time() end end @@ -459,7 +459,8 @@ function awards.show_to(name, to, sid, text) if name == "" or name == nil then name = to end - if name == to and awards.player(to).disabled then + local data = awards.player(to) + if name == to and data.disabled then minetest.chat_send_player(S("You've disabled awards. Type /awards enable to reenable.")) return end @@ -468,13 +469,13 @@ function awards.show_to(name, to, sid, text) if #listofawards == 0 then minetest.chat_send_player(to, S("Error: No awards available.")) return - elseif not awards.player(name) or not awards.player(name).unlocked then + elseif not data or not data.unlocked then minetest.chat_send_player(to, S("You have not unlocked any awards.")) return end minetest.chat_send_player(to, string.format(S("%s’s awards:"), name)) - for _, str in pairs(awards.player(name).unlocked) do + for _, str in pairs(data.unlocked) do local def = awards.def[str] if def then if def.title then -- cgit v1.2.3 From 7c5fd79b57fad2b9485d801eea07c1c9fedaabe9 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 17:37:43 +0100 Subject: Clean up formspec related code --- api.lua | 153 ---------------------------------------------------------------- 1 file changed, 153 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 6cb282b..4cbf6cb 100644 --- a/api.lua +++ b/api.lua @@ -352,159 +352,6 @@ function awards.unlock(name, award) end end --- Backwards compatibility -awards.give_achievement = awards.unlock - ---[[minetest.register_chatcommand("gawd", { - params = "award name", - description = "gawd: give award to self", - func = function(name, param) - awards.unlock(name,param) - end -})]]-- - -function awards.getFormspec(name, to, sid) - local formspec = "" - local listofawards = awards._order_awards(name) - local playerdata = awards.player(name) - - if #listofawards == 0 then - 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")).."]" - return formspec - end - - -- Sidebar - if sid then - local item = listofawards[sid+0] - local def = awards.def[item.name] - - if def and def.secret and not item.got then - formspec = formspec .. "label[1,2.75;"..minetest.formspec_escape(S("(Secret Award)")).."]".. - "image[1,0;3,3;awards_unknown.png]" - if def and def.description then - formspec = formspec .. "textarea[0.25,3.25;4.8,1.7;;"..minetest.formspec_escape(S("Unlock this award to find out what it is."))..";]" - end - else - local title = item.name - if def and def.title then - title = def.title - end - local status = "%s" - if item.got then - status = S("%s (got)") - end - - formspec = formspec .. "textarea[0.5,2.7;4.8,1.45;;" .. - string.format(status, minetest.formspec_escape(title)) .. - ";]" - - if def and def.icon then - formspec = formspec .. "image[1,0;3,3;" .. def.icon .. "]" - end - local barwidth = 4.6 - local perc = nil - local label = nil - if def.getProgress and playerdata then - local res = def:getProgress(playerdata) - perc = res.perc - label = res.label - end - if perc then - if perc > 1 then - perc = 1 - end - formspec = formspec .. "background[0,4.80;" .. barwidth ..",0.25;awards_progress_gray.png;false]" - formspec = formspec .. "background[0,4.80;" .. (barwidth * perc) ..",0.25;awards_progress_green.png;false]" - if label then - formspec = formspec .. "label[1.75,4.63;" .. minetest.formspec_escape(label) .. "]" - end - end - if def and def.description then - formspec = formspec .. "textarea[0.25,3.75;4.8,1.7;;"..minetest.formspec_escape(def.description)..";]" - end - end - end - - -- Create list box - formspec = formspec .. "textlist[4.75,0;6,5;awards;" - local first = true - for _,award in pairs(listofawards) do - local def = awards.def[award.name] - if def then - if not first then - formspec = formspec .. "," - end - first = false - - if def.secret and not award.got then - formspec = formspec .. "#707070"..minetest.formspec_escape(S("(Secret Award)")) - else - local title = award.name - if def and def.title then - title = def.title - end - if award.got then - formspec = formspec .. minetest.formspec_escape(title) - else - formspec = formspec .. "#ACACAC".. minetest.formspec_escape(title) - end - end - end - end - return formspec .. ";"..sid.."]" -end - -function awards.show_to(name, to, sid, text) - if name == "" or name == nil then - name = to - end - local data = awards.player(to) - if name == to and data.disabled then - minetest.chat_send_player(S("You've disabled awards. Type /awards enable to reenable.")) - return - end - if text then - local listofawards = awards._order_awards(name) - if #listofawards == 0 then - minetest.chat_send_player(to, S("Error: No awards available.")) - return - elseif not data or not data.unlocked then - minetest.chat_send_player(to, S("You have not unlocked any awards.")) - return - end - minetest.chat_send_player(to, string.format(S("%s’s awards:"), name)) - - for _, str in pairs(data.unlocked) do - local def = awards.def[str] - if def then - if def.title then - if def.description then - minetest.chat_send_player(to, string.format(S("%s: %s"), def.title, def.description)) - else - minetest.chat_send_player(to, def.title) - end - else - minetest.chat_send_player(to, str) - end - end - end - else - if sid == nil or sid < 1 then - sid = 1 - end - local deco = "" - if minetest.global_exists("default") then - deco = default.gui_bg .. default.gui_bg_img - end - -- Show formspec to user - minetest.show_formspec(to,"awards:awards", - "size[11,5]" .. deco .. - awards.getFormspec(name, to, sid)) - end -end -awards.showto = awards.show_to - minetest.register_on_player_receive_fields(function(player, formname, fields) if formname ~= "awards:awards" then return false -- cgit v1.2.3 From 5de9dcb7976f1c581601ab876d6bf3087991c4f6 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 17:42:12 +0100 Subject: Move api_helpers.lua in api.lua --- api.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 4cbf6cb..fd4b2dc 100644 --- a/api.lua +++ b/api.lua @@ -39,6 +39,19 @@ function awards.load() end awards.players = {} end + +function awards.player(name) + local data = awards.players[name] or {} + awards.players[name] = data + data.name = data.name or name + data.unlocked = data.unlocked or {} + return data +end + +function awards.player_or_nil(name) + return awards.players[name] +end + -- -- local function make_on_reg_wrapper() -- return function(def) -- cgit v1.2.3 From 72cc346c35d3610c2367d7dbb02e130ac8a96411 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 17:43:44 +0100 Subject: Rename awards.def to awards.registered_awards --- api.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index fd4b2dc..73182db 100644 --- a/api.lua +++ b/api.lua @@ -16,7 +16,7 @@ local S, NS = awards.gettext, awards.ngettext -awards.def = {} +awards.registered_awards = {} awards.on = {} awards.on_unlock = {} @@ -205,9 +205,9 @@ function awards.register_achievement(name, def) end -- Add Award - awards.def[name] = def + awards.registered_awards[name] = def - local tdef = awards.def[name] + local tdef = awards.registered_awards[name] if def.description == nil and tdef.getDefaultDescription then def.description = tdef:getDefaultDescription() end @@ -241,7 +241,7 @@ end function awards.unlock(name, award) -- Access Player Data local data = awards.player(name) - local awdef = awards.def[award] + local awdef = awards.registered_awards[award] assert(awdef, "Unable to unlock an award which doesn't exist!") if data.disabled or -- cgit v1.2.3 From d82972b44890611e6eb9dca506f4c2ead0b0ffa7 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 18:02:43 +0100 Subject: Update README.md --- api.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 73182db..86ff9ea 100644 --- a/api.lua +++ b/api.lua @@ -174,6 +174,7 @@ function awards.register_trigger(tname, tdef) -- Backwards compat awards.on[tname] = tdef.on awards['register_on_' .. tname] = tdef.register + return tdef end function awards.increment_item_counter(data, field, itemname, count) -- cgit v1.2.3 From 28c59340eae2fa64a42e0c063343b9966b13a737 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 18:30:47 +0100 Subject: Implement conted_key trigger type --- api.lua | 118 ++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 40 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 86ff9ea..e235fd8 100644 --- a/api.lua +++ b/api.lua @@ -52,45 +52,6 @@ function awards.player_or_nil(name) return awards.players[name] 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) for i = 1, #self.on do local res = nil @@ -113,9 +74,10 @@ function awards.register_trigger(tname, tdef) tdef.name = tname tdef.run_callbacks = run_trigger_callbacks + local datakey = tname .. "s" + tdef.data_key = datakey if tdef.type == "counted" then - local datakey = tname .. "s" local old_reg = tdef.on_register function tdef:on_register(def) @@ -162,6 +124,82 @@ function awards.register_trigger(tname, tdef) end 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 awards.registered_triggers[tname] = tdef -- cgit v1.2.3 From cca278024d2f665d60111192b744ef61c030088d Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 18:36:47 +0100 Subject: Remove datakey and use trigger names instead --- api.lua | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index e235fd8..74a784e 100644 --- a/api.lua +++ b/api.lua @@ -74,8 +74,6 @@ function awards.register_trigger(tname, tdef) tdef.name = tname tdef.run_callbacks = run_trigger_callbacks - local datakey = tname .. "s" - tdef.data_key = datakey if tdef.type == "counted" then local old_reg = tdef.on_register @@ -88,7 +86,7 @@ function awards.register_trigger(tname, tdef) tdef.register(tmp) function def.getProgress(_, data) - local done = data[datakey] or 0 + local done = data[tname] or 0 return { perc = done / tmp.target, label = S(tdef.progress, done, tmp.target), @@ -112,8 +110,8 @@ function awards.register_trigger(tname, tdef) print(dump(data)) -- Increment counter - local currentVal = (data[datakey] or 0) + 1 - data[datakey] = currentVal + local currentVal = (data[tname] or 0) + 1 + data[tname] = currentVal tdef:run_callbacks(player, data, function(entry) if entry.target and entry.award and currentVal and @@ -137,11 +135,11 @@ function awards.register_trigger(tname, tdef) function def.getProgress(_, data) local done - data[datakey] = data[datakey] or {} + data[tname] = data[tname] or {} if tmp.key then - done = data[datakey][tmp.key] or 0 + done = data[tname][tmp.key] or 0 else - done = data[datakey].__total or 0 + done = data[tname].__total or 0 end return { perc = done / tmp.target, @@ -175,17 +173,17 @@ function awards.register_trigger(tname, tdef) 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 + data[tname] = data[tname] or {} + local currentVal = (data[tname][key] or 0) + n + data[tname][key] = currentVal + data[tname].__total = (data[tname].__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 + current = data[tname].__total else return end -- cgit v1.2.3 From f7956d97e2e3071718cff9562b86d332b48e6570 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 18:39:06 +0100 Subject: Add default_def for trigger prototype --- api.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 74a784e..d7d11e3 100644 --- a/api.lua +++ b/api.lua @@ -52,7 +52,9 @@ function awards.player_or_nil(name) return awards.players[name] end -local function run_trigger_callbacks(self, player, data, table_func) +local default_def = {} + +function default_def:run_callbacks(player, data, table_func) for i = 1, #self.on do local res = nil local entry = self.on[i] @@ -73,7 +75,9 @@ function awards.register_trigger(tname, tdef) "Passing a callback to register_trigger is not supported in 3.0") tdef.name = tname - tdef.run_callbacks = run_trigger_callbacks + for key, value in pairs(default_def) do + tdef[key] = value + end if tdef.type == "counted" then local old_reg = tdef.on_register -- cgit v1.2.3 From 6c79a2f73fdf5f2a7e79da0f0291e61f7239f2ee Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 18:49:59 +0100 Subject: Use mod_storage and add converter for awards.txt --- api.lua | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 6 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index d7d11e3..21f196b 100644 --- a/api.lua +++ b/api.lua @@ -20,24 +20,76 @@ awards.registered_awards = {} awards.on = {} awards.on_unlock = {} +local storage = minetest.get_mod_storage() + -- Table Save Load Functions function awards.save() - local file = io.open(minetest.get_worldpath().."/awards.txt", "w") - if file then - file:write(minetest.serialize(awards.players)) - file:close() + storage:set_string("player_data", minetest.write_json(awards.players)) +end + +local function convert_data() + minetest.log("warning", "Importing awards data from previous version") + + for name, data in pairs(awards.players) do + while name.name do + name = name.name + end + data.name = name + print("Converting data for " .. name) + + -- Just rename counted + local counted = { + chats = "chat", + deaths = "death", + joins = "join", + } + for from, to in pairs(counted) do + data[to] = data[from] + data[from] = nil + end + + -- Convert item db to new format + local counted_items = { + count = "dig", + place = "place", + craft = "craft", + } + for from, to in pairs(counted_items) do + local ret = {} + + local count = 0 + for modname, items in pairs(data[from]) do + for itemname, value in pairs(items) do + itemname = modname .. ":" .. itemname + local key = minetest.registered_aliases[itemname] or itemname + ret[key] = value + count = count + value + end + end + + ret.__total = count + data[from] = nil + data[to] = ret + end end end function awards.load() - local file = io.open(minetest.get_worldpath().."/awards.txt", "r") + local old_save_path = minetest.get_worldpath().."/awards.txt" + local file = io.open(old_save_path, "r") if file then local table = minetest.deserialize(file:read("*all")) if type(table) == "table" then awards.players = table + convert_data() + else + awards.players = {} end + file:close() + os.rename(old_save_path, minetest.get_worldpath().."/awards.bk.txt") + else + awards.players = minetest.parse_json(storage:get_string("player_data")) or {} end - awards.players = {} end function awards.player(name) -- cgit v1.2.3 From f571f9d6eb7b11dc003aaf6c0a19b9db40ad356e Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 20:55:33 +0100 Subject: Add support for death reasons --- api.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 21f196b..b19daf9 100644 --- a/api.lua +++ b/api.lua @@ -48,6 +48,11 @@ local function convert_data() data[from] = nil end + data.death = { + unknown = data.death, + __total = data.death, + } + -- Convert item db to new format local counted_items = { count = "dig", -- cgit v1.2.3 From f84e6d8db5fa9f5c068432a9c3100f55d3188f51 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 21:04:54 +0100 Subject: Fix conversion error --- api.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index b19daf9..a4c7a3e 100644 --- a/api.lua +++ b/api.lua @@ -30,7 +30,9 @@ end local function convert_data() minetest.log("warning", "Importing awards data from previous version") - for name, data in pairs(awards.players) do + local old_players = awards.players + awards.players = {} + for name, data in pairs(old_players) do while name.name do name = name.name end @@ -76,7 +78,11 @@ local function convert_data() data[from] = nil data[to] = ret end + + awards.players[name] = data end + + print(dump(awards.players)) end function awards.load() @@ -92,6 +98,7 @@ function awards.load() end file:close() os.rename(old_save_path, minetest.get_worldpath().."/awards.bk.txt") + awards.save() else awards.players = minetest.parse_json(storage:get_string("player_data")) or {} end -- cgit v1.2.3 From 07997a54f4c082b6c2499f904825fb51394c9bf5 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 21:11:40 +0100 Subject: Readd awards --- api.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index a4c7a3e..d85ef6c 100644 --- a/api.lua +++ b/api.lua @@ -162,7 +162,7 @@ function awards.register_trigger(tname, tdef) end function def.getDefaultDescription(_) - local n = self.trigger.target + local n = def.trigger.target return NS(tdef.auto_description[1], tdef.auto_description[2], n, n) end @@ -216,7 +216,7 @@ function awards.register_trigger(tname, tdef) end function def.getDefaultDescription(_) - local n = self.trigger.target + local n = def.trigger.target if tmp.key then local nname = tmp.key return NS(tdef.auto_description[1], -- cgit v1.2.3