From 4e8d17571413f1d712b8dc69591a485b97f6214e Mon Sep 17 00:00:00 2001 From: "Alexand(er|ra) Yst" Date: Tue, 16 Jun 2020 07:47:41 +0000 Subject: [PATCH 1/5] Fixed a couple typos. - The Mesecons-related award claimed to be awarded when placing mesecon wires, but instead was coded to be awarded when placing pipes from Pipeworks. Fixed code to match description. - The Basic-Materials-related award claimed to require crafting flint and steel one hundred times, but instead was coded to require crafting oil extract five hundred times. Fixed description to match code. --- src/awards.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/awards.lua b/src/awards.lua index a1500ca..2e88de3 100644 --- a/src/awards.lua +++ b/src/awards.lua @@ -1090,7 +1090,7 @@ if minetest.get_modpath("mesecons") then difficulty = 0.2, trigger = { type = "place", - node = "pipeworks:tube_1", + node = "mesecons:wire_00000000_off", target = 500, } }) @@ -1099,7 +1099,7 @@ end if minetest.get_modpath("basic_materials") then awards.register_award("awards_oil", { title = S("Oil Typhoon"), - description = S("Craft 100 times flint and steel."), + description = S("Craft 500 times oil extract."), trigger = { type = "craft", From b4a4a1fd745c0d1359f3a1789ddd71d4ab5a1c82 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 10 Jan 2021 16:24:45 +0000 Subject: [PATCH 2/5] Improve API: add awards.get_award_states() --- README.md | 58 +++++++++++++++++++++++-------- init.lua | 2 ++ src/api_awards.lua | 68 ++++++++++++++++++++++++++++++++++++ src/api_triggers.lua | 16 ++++----- src/gui.lua | 83 +++++--------------------------------------- 5 files changed, 129 insertions(+), 98 deletions(-) diff --git a/README.md b/README.md index e570f0e..4015354 100644 --- a/README.md +++ b/README.md @@ -37,10 +37,11 @@ awards.register_award("mymod:award", { The above trigger type is an example of a counted_key trigger: rather than a single counter there's a counter per key - in this -case the key is the value of the `node` field. If you leave out -the key in a `counted_key` trigger, then the total will be used -instead. For example, here is an award which unlocks after you've -placed 10 nodes of any type: +case the key is the value of the `node` field. + +If you leave out the key in a `counted_key` trigger, then the total will be used +instead. For example, here is an award which unlocks after you've placed 10 +nodes of any type: ```lua awards.register_award("mymod:award", { @@ -125,6 +126,10 @@ awards.register_trigger("foo", { type = "custom", progress = "@1/@2 foos", auto_description = { "Do a foo", "Foo @1 times" }, + + on_register = function(self, award) + print(award.name .. " was registered with foo trigger type") + end, }) minetest.register_on_foo(function() @@ -168,7 +173,9 @@ end # API -* awards.register_award(name, def), the def table has the following fields: +## Awards + +* `awards.register_award(name, def)`, the def table has the following fields: * `title` - title of the award (defaults to name) * `description` - longer description of the award, displayed in Awards tab * `difficulty` - see [Award Difficulty](#award-difficulty). @@ -182,23 +189,44 @@ end * `background` - the background image, use default otherwise. * `trigger` - trigger definition, see [Builtin Trigger Types](#builtin-trigger-types). * `on_unlock(name, def)` - callback on unlock. -* awards.register_trigger(name, def), the def table has the following fields: - * `type` - see [Trigger Types](#trigger-types). +* `awards.registered_awards` - table of award name to definition. +* `awards.register_on_unlock(func(name, def))` + * `name` is the player name + * `def` is the award def. + * return true to cancel HUD +* `awards.unlock(player_name, award)` + * gives an award to a player +* `awards.get_award_states(player_name)` + * Returns list of tables, sorted by `score`, each having the fields: + + { + name = "mymod:awardname", + def = {}, -- Award definition + unlocked = true, -- Whether award has been unlocked + started = true, -- Whether any progress has been made + score = 0, -- Score used in sorting + + -- Either a table or nil + progress = { + current = 5, + target = 10, + label = "label", -- Label to show over progress bar + } + } + +## Triggers + +* `awards.register_trigger(name, def)`, the def table has the following fields: + * `type` - see trigger type types in [Trigger Types](#trigger-types). * `progress` - used to format progress, defaults to "%1/%2". * `auto_description` - a table of two elements. Each element is a format string. Element 1 is singular, element 2 is plural. Used for the award description (not title) if none is given. - * `on_register(award_def)` - called when an award registers with this type. + * `on_register(self, award_def)` - called when an award registers with this type. * "counted_key" only: * `auto_description_total` - Used if the trigger is for the total. * `get_key(self, def)` - get key for particular award, return nil for a total. * `key_is_item` - true if the key is an item name. On notify(), any watched groups will also be notified as `group:groupname` keys. -* awards.register_on_unlock(func(name, def)) - * name is the player name - * def is the award def. - * return true to cancel HUD -* awards.unlock(name, award) - * gives an award to a player - * name is the player name +* `awards.registered_triggers` - table of trigger name to definition. ## Builtin Trigger Types diff --git a/init.lua b/init.lua index 25dc07f..b145476 100644 --- a/init.lua +++ b/init.lua @@ -3,7 +3,9 @@ -- The global award namespace awards = { show_mode = "hud", + registered_awards = {}, registered_triggers = {}, + on_unlock = {}, } -- Internationalization support. diff --git a/src/api_awards.lua b/src/api_awards.lua index c3cae5a..88f429a 100644 --- a/src/api_awards.lua +++ b/src/api_awards.lua @@ -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 diff --git a/src/api_triggers.lua b/src/api_triggers.lua index 11a1391..67826fd 100644 --- a/src/api_triggers.lua +++ b/src/api_triggers.lua @@ -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 diff --git a/src/gui.lua b/src/gui.lua index 8faca84..7bd8a11 100644 --- a/src/gui.lua +++ b/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 From 9c74a3f85a179e3f7bf15072af44b81789955e9a Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 10 Jan 2021 16:30:05 +0000 Subject: [PATCH 3/5] Update docs --- README.md | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4015354..79c6d13 100644 --- a/README.md +++ b/README.md @@ -194,25 +194,29 @@ end * `name` is the player name * `def` is the award def. * return true to cancel HUD -* `awards.unlock(player_name, award)` +* `awards.unlock(player_name, award_name)` * gives an award to a player * `awards.get_award_states(player_name)` * Returns list of tables, sorted by `score`, each having the fields: - { - name = "mymod:awardname", - def = {}, -- Award definition - unlocked = true, -- Whether award has been unlocked - started = true, -- Whether any progress has been made - score = 0, -- Score used in sorting + ```lua + { + name = "mymod:awardname", + def = {}, -- Award definition + unlocked = true, -- Whether award has been unlocked + started = true, -- Whether any progress has been made + score = 0, -- Score used in sorting - -- Either a table or nil - progress = { - current = 5, - target = 10, - label = "label", -- Label to show over progress bar - } + -- Either a table or nil + -- Will be nil if progress is indeterminable or + -- if the award is unlocked + progress = { + current = 5, + target = 10, + label = "label", -- Label to show over progress bar } + } + ``` ## Triggers From 6dc91cf89397af134e7ba4dabd49957dcdce6f56 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 10 Jan 2021 17:17:57 +0000 Subject: [PATCH 4/5] Fix error due to unchecked JSON parsing --- src/data.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/data.lua b/src/data.lua index 27ae5e3..0f2188d 100644 --- a/src/data.lua +++ b/src/data.lua @@ -80,7 +80,8 @@ function awards.load() os.rename(old_save_path, minetest.get_worldpath().."/awards.bk.txt") awards.save() else - __player_data = minetest.parse_json(storage:get_string("player_data")) or {} + local json = storage:get("player_data") + __player_data = json and minetest.parse_json(json) or {} end end From 37082d11862899602f04229aae3f3a634a57ae7e Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 10 Jan 2021 17:55:42 +0000 Subject: [PATCH 5/5] Rename background to hud_background --- README.md | 7 ++++--- src/api_awards.lua | 2 +- src/awards.lua | 26 +++++++++++++------------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 79c6d13..2903221 100644 --- a/README.md +++ b/README.md @@ -185,15 +185,16 @@ end * `secret` - boolean if this award is secret (i.e. showed on awards list) * `sound` - `SimpleSoundSpec` table to play on unlock. `false` to disable unlock sound. - * `icon` - the icon image, use default otherwise. - * `background` - the background image, use default otherwise. + * `icon` - the icon image. Defaults to `awards_unknown.png`. + * `hud_background` - the background image used in the HUD to contain the text and icon. + Defaults to `awards_bg_default.png`. * `trigger` - trigger definition, see [Builtin Trigger Types](#builtin-trigger-types). * `on_unlock(name, def)` - callback on unlock. * `awards.registered_awards` - table of award name to definition. * `awards.register_on_unlock(func(name, def))` * `name` is the player name * `def` is the award def. - * return true to cancel HUD + * return true to cancel HUD from appearing. * `awards.unlock(player_name, award_name)` * gives an award to a player * `awards.get_award_states(player_name)` diff --git a/src/api_awards.lua b/src/api_awards.lua index 88f429a..d68ebce 100644 --- a/src/api_awards.lua +++ b/src/api_awards.lua @@ -95,7 +95,7 @@ function awards.unlock(name, award) -- Get Notification Settings local title = awdef.title or award local desc = awdef.description or "" - local background = awdef.background or "awards_bg_default.png" + local background = awdef.hud_background or awdef.background or "awards_bg_default.png" local icon = (awdef.icon or "awards_unknown.png") .. "^[resize:32x32" local sound = awdef.sound if sound == nil then diff --git a/src/awards.lua b/src/awards.lua index 2e88de3..e9d487f 100644 --- a/src/awards.lua +++ b/src/awards.lua @@ -251,7 +251,7 @@ if minetest.get_modpath("default") then title = S("Lava and Water"), description = S("Mine your first obsidian."), icon = "awards_lava_and_water.png^awards_level1.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 1.5, trigger = { type = "dig", @@ -265,7 +265,7 @@ if minetest.get_modpath("default") then title = S("Obsessed with Obsidian"), description = S("Mine 50 obsidian."), icon = "awards_obsessed_with_obsidian.png^awards_level2.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 1.5, trigger = { type = "dig", @@ -279,7 +279,7 @@ if minetest.get_modpath("default") then title = S("Lava Miner"), description = S("Mine any block while being very close to lava."), icon = "awards_lava_miner.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 1, }) awards.register_on_dig(function(player,data) @@ -386,7 +386,7 @@ if minetest.get_modpath("default") then title = S("First Mese Find"), description = S("Mine your first mese ore."), icon = "awards_first_mese_find.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 1, trigger = { type = "dig", @@ -401,7 +401,7 @@ if minetest.get_modpath("default") then title = S("Mese Mastery"), description = S("Mine a mese block."), icon = "awards_mese_mastery.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 1.1, trigger = { type = "dig", @@ -415,7 +415,7 @@ if minetest.get_modpath("default") then title = S("You’re a copper"), description = S("Dig 1,000 copper ores."), icon = "awards_youre_a_copper.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 0.2, trigger = { type = "dig", @@ -429,7 +429,7 @@ if minetest.get_modpath("default") then title = S("Mini Miner"), description = S("Dig 100 stone blocks."), icon = "awards_mini_miner.png^awards_level1.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 0.02, trigger = { type = "dig", @@ -443,7 +443,7 @@ if minetest.get_modpath("default") then title = S("Hardened Miner"), description = S("Dig 1,000 stone blocks."), icon = "awards_hardened_miner.png^awards_level2.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 0.02, trigger = { type = "dig", @@ -457,7 +457,7 @@ if minetest.get_modpath("default") then title = S("Master Miner"), description = S("Dig 10,000 stone blocks."), icon = "awards_master_miner.png^awards_level3.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 0.02, trigger = { type = "dig", @@ -471,7 +471,7 @@ if minetest.get_modpath("default") then title = S("Marchand De Sable"), description = S("Dig 1,000 sand."), icon = "awards_marchand_de_sable.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 0.05, trigger = { type = "dig", @@ -581,7 +581,7 @@ if minetest.get_modpath("default") then title = S("First Gold Find"), description = S("Mine your first gold ore."), icon = "awards_first_gold_find.png^awards_level1.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 0.9, trigger = { type = "dig", @@ -594,7 +594,7 @@ if minetest.get_modpath("default") then title = S("Gold Rush"), description = S("Mine 45 gold ores."), icon = "awards_gold_rush.png^awards_level2.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 0.9, trigger = { type = "dig", @@ -619,7 +619,7 @@ if minetest.get_modpath("default") then title = S("Girl's Best Friend"), description = S("Mine 18 diamond ores."), icon = "awards_girls_best_friend.png^awards_level2.png", - background = "awards_bg_mining.png", + hud_background = "awards_bg_mining.png", difficulty = 1, trigger = { type = "dig",