Add awards.run_trigger_callbacks() and use it

This commit is contained in:
rubenwardy 2016-07-29 17:13:01 +01:00
parent e92be32ec1
commit dee1880fd3
3 changed files with 71 additions and 123 deletions

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

@ -28,6 +28,14 @@ old fork in Carbone, under same license.
* return true to cancel register_on_unlock callbacks and HUD * return true to cancel register_on_unlock callbacks and HUD
* awards.register_trigger(name, func(awardname, def)) * awards.register_trigger(name, func(awardname, def))
* Note: awards.on[name] is automatically created for triggers * 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.register_on_unlock(func(name, def)) * awards.register_on_unlock(func(name, def))
* name is the player name * name is the player name
* def is the award def. * def is the award def.

@ -73,6 +73,22 @@ awards.register_onChat = awards.register_on_chat
awards.register_onJoin = awards.register_on_join awards.register_onJoin = awards.register_on_join
awards.register_onCraft = awards.register_on_craft awards.register_onCraft = awards.register_on_craft
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
-- Trigger Handles -- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger) minetest.register_on_dignode(function(pos, oldnode, digger)
if not digger or not pos or not oldnode then if not digger or not pos or not oldnode then
@ -97,35 +113,19 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item] + 1 awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item] + 1
-- Run callbacks and triggers -- Run callbacks and triggers
local player = digger
local data = awards.players[playern] local data = awards.players[playern]
for i=1, #awards.on.dig do awards.run_trigger_callbacks(digger, data, "dig", function(entry)
local res = nil if entry.node and entry.target then
if type(awards.on.dig[i]) == "function" then local tnodedug = string.split(entry.node, ":")
-- Run trigger callback local tmod = tnodedug[1]
res = awards.on.dig[i](player,data) local titem = tnodedug[2]
elseif type(awards.on.dig[i]) == "table" then if not tmod or not titem or not data.count[tmod] or not data.count[tmod][titem] 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! -- table running failed!
print("[ERROR] awards - on.dig trigger "..i.." is invalid!") elseif data.count[tmod][titem] > entry.target-1 then
else return entry.award
-- run the table
local tnodedug = string.split(awards.on.dig[i].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
-- table running failed!
elseif data.count[tmod][titem] > awards.on.dig[i].target-1 then
res=awards.on.dig[i].award
end
end end
end end
end)
if res then
awards.unlock(playern,res)
end
end
end) end)
minetest.register_on_placenode(function(pos, node, digger) minetest.register_on_placenode(function(pos, node, digger)
@ -152,34 +152,19 @@ minetest.register_on_placenode(function(pos, node, digger)
awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item] + 1 awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item] + 1
-- Run callbacks and triggers -- Run callbacks and triggers
local player = digger
local data = awards.players[playern] local data = awards.players[playern]
for i=1,# awards.on.place do awards.run_trigger_callbacks(digger, data, "place", function(entry)
local res = nil if entry.node and entry.target then
if type(awards.on.place[i]) == "function" then local tnodedug = string.split(entry.node, ":")
-- Run trigger callback local tmod = tnodedug[1]
res = awards.on.place[i](player,data) local titem = tnodedug[2]
elseif type(awards.on.place[i]) == "table" then if not tmod or not titem or not data.place[tmod] or not data.place[tmod][titem] then
-- Handle table trigger -- table running failed!
if not awards.on.place[i].node or not awards.on.place[i].target or not awards.on.place[i].award then elseif data.place[tmod][titem] > entry.target-1 then
print("[ERROR] awards - on.place trigger "..i.." is invalid!") return entry.award
else
-- run the table
local tnodedug = string.split(awards.on.place[i].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
-- table running failed!
elseif data.place[tmod][titem] > awards.on.place[i].target-1 then
res = awards.on.place[i].award
end
end end
end end
end)
if res then
awards.unlock(playern,res)
end
end
end) end)
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
@ -199,6 +184,7 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
return return
end end
awards.assertPlayer(playern) awards.assertPlayer(playern)
awards.tbv(awards.players[playern], "craft")
awards.tbv(awards.players[playern].craft, mod) awards.tbv(awards.players[playern].craft, mod)
awards.tbv(awards.players[playern].craft[mod], item, 0) awards.tbv(awards.players[playern].craft[mod], item, 0)
@ -207,34 +193,18 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
-- Run callbacks and triggers -- Run callbacks and triggers
local data = awards.players[playern] local data = awards.players[playern]
for i=1, #awards.on.craft do awards.run_trigger_callbacks(player, data, "craft", function(entry)
local res = nil if entry.item and entry.target then
local entry = awards.on.craft[i] local titemcrafted = string.split(entry.item, ":")
if type(entry) == "function" then local tmod = titemcrafted[1]
-- Run trigger callback local titem = titemcrafted[2]
res = entry(player,data) if not tmod or not titem or not data.craft[tmod] or not data.craft[tmod][titem] then
elseif type(entry) == "table" then
-- Handle table trigger
if not entry.item or not entry.target or not entry.award then
-- table running failed! -- table running failed!
print("[ERROR] awards - onCraft trigger "..i.." is invalid!") elseif data.craft[tmod][titem] > entry.target-1 then
else return entry.award
-- run the table
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
-- table running failed!
elseif data.craft[tmod][titem] > entry.target-1 then
res=awards.on.craft[i].award
end
end end
end end
end)
if res then
awards.give_achievement(playern,res)
end
end
end) end)
minetest.register_on_dieplayer(function(player) minetest.register_on_dieplayer(function(player)
@ -251,22 +221,12 @@ minetest.register_on_dieplayer(function(player)
-- Increment counter -- Increment counter
data.deaths = data.deaths + 1 data.deaths = data.deaths + 1
-- Run callbacks and triggers awards.run_trigger_callbacks(player, data, "death", function(entry)
for _,trigger in pairs(awards.on.death) do if entry.target and entry.award and data.deaths and
local res = nil data.deaths >= entry.target then
if type(trigger) == "function" then return entry.award
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 end
if res ~= nil then end)
awards.unlock(name,res)
end
end
end) end)
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
@ -283,22 +243,12 @@ minetest.register_on_joinplayer(function(player)
-- Increment counter -- Increment counter
data.joins = data.joins + 1 data.joins = data.joins + 1
-- Run callbacks and triggers awards.run_trigger_callbacks(player, data, "join", function(entry)
for _, trigger in pairs(awards.on.join) do if entry.target and entry.award and data.joins and
local res = nil data.joins >= entry.target then
if type(trigger) == "function" then return entry.award
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 end
if res ~= nil then end)
awards.unlock(name,res)
end
end
end) end)
minetest.register_on_chat_message(function(name, message) minetest.register_on_chat_message(function(name, message)
@ -316,22 +266,12 @@ minetest.register_on_chat_message(function(name, message)
-- Increment counter -- Increment counter
data.chats = data.chats + 1 data.chats = data.chats + 1
-- Run callbacks and triggers awards.run_trigger_callbacks(player, data, "chat", function(entry)
for _,trigger in pairs(awards.on.chat) do if entry.target and entry.award and data.chats and
local res = nil data.chats >= entry.target then
if type(trigger) == "function" then return entry.award
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 end
if res ~= nil then end)
awards.unlock(name,res)
end
end
end) end)
minetest.register_on_newplayer(function(player) minetest.register_on_newplayer(function(player)