1
0
mirror of https://gitlab.com/rubenwardy/awards.git synced 2025-06-28 22:26:17 +02:00

10 Commits
v2.0 ... v2.2.0

8 changed files with 197 additions and 194 deletions

91
api.lua
View File

@ -61,6 +61,60 @@ function awards.register_trigger(name, func)
end
end
function awards.run_trigger_callbacks(player, data, trigger, table_func)
for i = 1, #awards.on[trigger] do
local res = nil
local entry = awards.on[trigger][i]
if type(entry) == "function" then
res = entry(player, data)
elseif type(entry) == "table" and entry.award then
res = table_func(entry)
end
if res then
awards.unlock(player:get_player_name(), res)
end
end
end
function awards.increment_item_counter(data, field, itemname, count)
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)
data[field][mod][item] = data[field][mod][item] + (count or 1)
return true
else
return false
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
@ -144,16 +198,14 @@ function awards.unlock(name, award)
if awdef and awdef.prizes then
for i = 1, #awdef.prizes do
local itemstack = ItemStack(awdef.prizes[i])
if itemstack:is_empty() or not itemstack:is_known() then
return
end
local receiverref = core.get_player_by_name(name)
if not receiverref then
return
end
if not itemstack:is_empty() then
local receiverref = minetest.get_player_by_name(name)
if receiverref then
receiverref:get_inventory():add_item("main", itemstack)
end
end
end
end
-- Run callbacks
if awdef.on_unlock and awdef.on_unlock(name, awdef) then
@ -249,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
@ -273,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
@ -367,3 +435,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end)
awards.init()
minetest.register_on_newplayer(function(player)
local playern = player:get_player_name()
awards.assertPlayer(playern)
end)
minetest.register_on_shutdown(function()
awards.save()
end)

View File

@ -26,6 +26,7 @@ function awards.assertPlayer(playern)
awards.tbv(awards.players[playern], "unlocked")
awards.tbv(awards.players[playern], "place")
awards.tbv(awards.players[playern], "count")
awards.tbv(awards.players[playern], "craft")
awards.tbv(awards.players[playern], "deaths", 0)
awards.tbv(awards.players[playern], "joins", 0)
awards.tbv(awards.players[playern], "chats", 0)

View File

@ -36,7 +36,7 @@ awards.register_achievement("award_lightitup",{
trigger = {
type = "place",
node = "default:torch",
target = 500
target = 100
}
})
@ -100,7 +100,7 @@ awards.register_achievement("award_obsessed_with_obsidian",{
trigger = {
type = "dig",
node = "default:obsidian",
target = 1
target = 50
}
})
@ -124,7 +124,7 @@ awards.register_achievement("award_lumberjack", {
trigger = {
type = "dig",
node = "default:tree",
target = 1
target = 100
}
})

View File

@ -4,4 +4,4 @@ author = rubenwardy
description = Adds achievements to Minetest, and an API to register new ones.
license = LGPL 2.1 or later
forum = https://forum.minetest.net/viewtopic.php?t=4870
version = 2.0
version = 2.2.0

View File

@ -28,6 +28,20 @@ old fork in Carbone, under same license.
* return true to cancel register_on_unlock callbacks and HUD
* awards.register_trigger(name, func(awardname, def))
* Note: awards.on[name] is automatically created for triggers
* awards.run_trigger_callbacks(player, data, trigger, table_func(entry))
* Goes through and checks all triggers registered to a trigger type,
unlocking the award if conditions are met.
* data is the player's award data, ie: awards.players[player_name]
* trigger is the name of the trigger type. Ie: awards.on[trigger]
* table_func is called if the trigger is a table - simply return an
award name to unlock it
* See triggers.lua for examples
* awards.increment_item_counter(data, field, itemname)
* add to an item's statistic count
* for example, (data, "place", "default:stone") will add 1 to the number of
times default:stone has been placed.
* data is the player's award data, ie: awards.players[player_name]
* returns true on success, false on failure (eg: cannot get modname and item from itemname)
* awards.register_on_unlock(func(name, def))
* name is the player name
* def is the award def.

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 B

View File

@ -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
@ -78,164 +121,71 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
if not digger or not pos or not oldnode then
return
end
local nodedug = string.split(oldnode.name, ":")
if #nodedug ~= 2 then
local data = awards.players[digger:get_player_name()]
if not awards.increment_item_counter(data, "count", oldnode.name) then
return
end
local mod = nodedug[1]
local item = nodedug[2]
local playern = digger:get_player_name()
if (not playern or not nodedug or not mod or not item) then
return
end
awards.assertPlayer(playern)
awards.tbv(awards.players[playern].count, mod)
awards.tbv(awards.players[playern].count[mod], item, 0)
-- Increment counter
awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item] + 1
-- Run callbacks and triggers
local player = digger
local data = awards.players[playern]
for i=1, #awards.on.dig do
local res = nil
if type(awards.on.dig[i]) == "function" then
-- Run trigger callback
res = awards.on.dig[i](player,data)
elseif type(awards.on.dig[i]) == "table" then
-- Handle table trigger
if not awards.on.dig[i].node or not awards.on.dig[i].target or not awards.on.dig[i].award then
-- table running failed!
print("[ERROR] awards - on.dig trigger "..i.." is invalid!")
else
-- run the table
local tnodedug = string.split(awards.on.dig[i].node, ":")
awards.run_trigger_callbacks(digger, data, "dig", function(entry)
if entry.node and entry.target then
local tnodedug = string.split(entry.node, ":")
local tmod = tnodedug[1]
local titem = tnodedug[2]
if tmod==nil or titem==nil or not data.count[tmod] or not data.count[tmod][titem] then
if not tmod or not titem or not data.count[tmod] or not data.count[tmod][titem] then
-- table running failed!
elseif data.count[tmod][titem] > awards.on.dig[i].target-1 then
res=awards.on.dig[i].award
end
end
end
if res then
awards.unlock(playern,res)
elseif data.count[tmod][titem] > entry.target-1 then
return entry.award
end
end
end)
end)
minetest.register_on_placenode(function(pos, node, digger)
if not digger or not pos or not node or not digger:get_player_name() or digger:get_player_name()=="" then
return
end
local nodedug = string.split(node.name, ":")
if #nodedug ~= 2 then
local data = awards.players[digger:get_player_name()]
if not awards.increment_item_counter(data, "place", node.name) then
return
end
local mod=nodedug[1]
local item=nodedug[2]
local playern = digger:get_player_name()
-- Run checks
if (not playern or not nodedug or not mod or not item) then
return
end
awards.assertPlayer(playern)
awards.tbv(awards.players[playern].place, mod)
awards.tbv(awards.players[playern].place[mod], item, 0)
-- Increment counter
awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item] + 1
-- Run callbacks and triggers
local player = digger
local data = awards.players[playern]
for i=1,# awards.on.place do
local res = nil
if type(awards.on.place[i]) == "function" then
-- Run trigger callback
res = awards.on.place[i](player,data)
elseif type(awards.on.place[i]) == "table" then
-- Handle table trigger
if not awards.on.place[i].node or not awards.on.place[i].target or not awards.on.place[i].award then
print("[ERROR] awards - on.place trigger "..i.." is invalid!")
else
-- run the table
local tnodedug = string.split(awards.on.place[i].node, ":")
awards.run_trigger_callbacks(digger, data, "place", function(entry)
if entry.node and entry.target then
local tnodedug = string.split(entry.node, ":")
local tmod = tnodedug[1]
local titem = tnodedug[2]
if tmod==nil or titem==nil or not data.place[tmod] or not data.place[tmod][titem] then
if not tmod or not titem or not data.place[tmod] or not data.place[tmod][titem] then
-- table running failed!
elseif data.place[tmod][titem] > awards.on.place[i].target-1 then
res = awards.on.place[i].award
end
end
end
if res then
awards.unlock(playern,res)
elseif data.place[tmod][titem] > entry.target-1 then
return entry.award
end
end
end)
end)
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
if not player or not itemstack then
return
end
local itemcrafted = string.split(itemstack:get_name(), ":")
if #itemcrafted ~= 2 then
--minetest.log("error","Awards mod: "..itemstack:get_name().." is in wrong format!")
local data = awards.players[player:get_player_name()]
if not awards.increment_item_counter(data, "craft", itemstack:get_name(), itemstack:get_count()) then
return
end
local mod = itemcrafted[1]
local item = itemcrafted[2]
local playern = player:get_player_name()
if (not playern or not itemcrafted or not mod or not item) then
return
end
awards.assertPlayer(playern)
awards.tbv(awards.players[playern].craft, mod)
awards.tbv(awards.players[playern].craft[mod], item, 0)
-- Increment counter
awards.players[playern].craft[mod][item] = awards.players[playern].craft[mod][item] + 1
-- Run callbacks and triggers
local data = awards.players[playern]
for i=1, #awards.on.craft do
local res = nil
local entry = awards.on.craft[i]
if type(entry) == "function" then
-- Run trigger callback
res = entry(player,data)
elseif type(entry) == "table" then
-- Handle table trigger
if not entry.item or not entry.target or not entry.award then
-- table running failed!
print("[ERROR] awards - onCraft trigger "..i.." is invalid!")
else
-- run the table
awards.run_trigger_callbacks(player, data, "craft", function(entry)
if entry.item and entry.target then
local titemcrafted = string.split(entry.item, ":")
local tmod = titemcrafted[1]
local titem = titemcrafted[2]
if not tmod==nil or not titem or not data.craft[tmod] or not data.craft[tmod][titem] then
if not tmod or not titem or not data.craft[tmod] or not data.craft[tmod][titem] then
-- table running failed!
elseif data.craft[tmod][titem] > entry.target-1 then
res=awards.on.craft[i].award
end
end
end
if res then
awards.give_achievement(playern,res)
return entry.award
end
end
end)
end)
minetest.register_on_dieplayer(function(player)
-- Run checks
@ -251,23 +201,13 @@ minetest.register_on_dieplayer(function(player)
-- Increment counter
data.deaths = data.deaths + 1
-- Run callbacks and triggers
for _,trigger in pairs(awards.on.death) do
local res = nil
if type(trigger) == "function" then
res = trigger(player,data)
elseif type(trigger) == "table" then
if trigger.target and trigger.award then
if data.deaths and data.deaths >= trigger.target then
res = trigger.award
end
end
end
if res ~= nil then
awards.unlock(name,res)
end
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
@ -283,23 +223,13 @@ minetest.register_on_joinplayer(function(player)
-- Increment counter
data.joins = data.joins + 1
-- Run callbacks and triggers
for _, trigger in pairs(awards.on.join) do
local res = nil
if type(trigger) == "function" then
res = trigger(player,data)
elseif type(trigger) == "table" then
if trigger.target and trigger.award then
if data.joins and data.joins >= trigger.target then
res = trigger.award
end
end
end
if res ~= nil then
awards.unlock(name,res)
end
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_chat_message(function(name, message)
-- Run checks
@ -316,29 +246,10 @@ minetest.register_on_chat_message(function(name, message)
-- Increment counter
data.chats = data.chats + 1
-- Run callbacks and triggers
for _,trigger in pairs(awards.on.chat) do
local res = nil
if type(trigger) == "function" then
res = trigger(player,data)
elseif type(trigger) == "table" then
if trigger.target and trigger.award then
if data.chats and data.chats >= trigger.target then
res = trigger.award
end
end
end
if res ~= nil then
awards.unlock(name,res)
end
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)
minetest.register_on_newplayer(function(player)
local playern = player:get_player_name()
awards.assertPlayer(playern)
end)
minetest.register_on_shutdown(function()
awards.save()
end)