From b689dc8eaf799fe8f45724ace8bfa943436bc412 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Mon, 1 Aug 2016 20:27:12 +0100 Subject: [PATCH] Add progress bar --- api.lua | 33 ++++++++++++++++++++++ textures/awards_progress_gray.png | Bin 0 -> 146 bytes textures/awards_progress_green.png | Bin 0 -> 177 bytes triggers.lua | 43 +++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 textures/awards_progress_gray.png create mode 100644 textures/awards_progress_green.png diff --git a/api.lua b/api.lua index 7a4de0a..1fd8a29 100644 --- a/api.lua +++ b/api.lua @@ -98,6 +98,23 @@ function awards.increment_item_counter(data, field, itemname, count) end 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] + + 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 +end + function awards.register_on_unlock(func) table.insert(awards.on_unlock, func) end @@ -284,6 +301,7 @@ awards.give_achievement = awards.unlock function awards.getFormspec(name, to, sid) local formspec = "size[11,5]" local listofawards = awards._order_awards(name) + local playerdata = awards.players[name] -- Sidebar if sid then @@ -308,6 +326,21 @@ function awards.getFormspec(name, to, sid) 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 + formspec = formspec .. "background[0,4.80;" .. barwidth ..",0.25;awards_progress_gray.png]" + formspec = formspec .. "background[0,4.80;" .. (barwidth * perc) ..",0.25;awards_progress_green.png]" + if label then + formspec = formspec .. "label[1.75,4.63;" .. label .. "]" + end + end if def and def.description then formspec = formspec .. "label[0,3.25;"..def.description.."]" end diff --git a/textures/awards_progress_gray.png b/textures/awards_progress_gray.png new file mode 100644 index 0000000000000000000000000000000000000000..a5fc6cb35bdfcdc2ccc407f3ecfe6a9b8da20651 GIT binary patch literal 146 zcmeAS@N?(olHy`uVBq!ia0vp^j6f{F!2~2D{x&@TQk(@Ik;M!Q+`=Ht$S`Y;1W=H@ z#M9T6{Q(D~Fst_5@4c&lLXst}5hc#~xw)x%B@E6*sfi`2DGKG8B^e6tp1uL$jeO!j kMLeD^jv*Y^lM@#30C^4!jA`P@8-OeZPgg&ebxsLQ0Hi@7ssI20 literal 0 HcmV?d00001 diff --git a/textures/awards_progress_green.png b/textures/awards_progress_green.png new file mode 100644 index 0000000000000000000000000000000000000000..54b4e5d5a49d267f8dc5987bde274c351303bc81 GIT binary patch literal 177 zcmeAS@N?(olHy`uVBq!ia0vp^j6f{F!2~2D{x&@TQk(@Ik;M!Q+`=Ht$S`Y;1W=H@ z#M9T6{Q(D~khT0D7miS%kYtH#M2T~LZf zk+!FcV+hA}-;)~!85DR9JIrlQxx(Wv<2>8x===qJGt{eEl&hXb#@!b*KlF2*t=zHa S!fHTW3=E#GelF{r5}E++QZUZ| literal 0 HcmV?d00001 diff --git a/triggers.lua b/triggers.lua index 1a4e355..68a1856 100644 --- a/triggers.lua +++ b/triggers.lua @@ -21,6 +21,13 @@ awards.register_trigger("dig", function(def) target = def.trigger.target } table.insert(awards.on.dig, tmp) + def.getProgress = function(self, data) + local itemcount = awards.get_item_count(data, "count", tmp.node) or 0 + return { + perc = itemcount / tmp.target, + label = itemcount .. " / " .. tmp.target .. " dug" -- TODO: translation + } + end end) awards.register_trigger("place", function(def) @@ -30,6 +37,13 @@ awards.register_trigger("place", function(def) target = def.trigger.target } table.insert(awards.on.place, tmp) + def.getProgress = function(self, data) + local itemcount = awards.get_item_count(data, "place", tmp.node) or 0 + return { + perc = itemcount / tmp.target, + label = itemcount .. " / " .. tmp.target .. " placed" -- TODO: translation + } + end end) awards.register_trigger("death", function(def) @@ -38,6 +52,13 @@ awards.register_trigger("death", function(def) 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 = itemcount .. " deaths, need " .. tmp.target -- TODO: translation + } + end end) awards.register_trigger("chat", function(def) @@ -46,6 +67,13 @@ awards.register_trigger("chat", function(def) 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 = itemcount .. " / " .. tmp.target .. " line of chat" -- TODO: translation + } + end end) awards.register_trigger("join", function(def) @@ -54,6 +82,14 @@ awards.register_trigger("join", function(def) 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 = itemcount .. " game joins, need " .. tmp.target -- TODO: translation + } + end end) awards.register_trigger("craft", function(def) @@ -63,6 +99,13 @@ awards.register_trigger("craft", function(def) target = def.trigger.target } table.insert(awards.on.craft, tmp) + def.getProgress = function(self, data) + local itemcount = awards.get_item_count(data, "craft", tmp.item) or 0 + return { + perc = itemcount / tmp.target, + label = itemcount .. " / " .. tmp.target .. " crafted" -- TODO: translation + } + end end) -- Backwards compatibility