awards.register_trigger

This commit is contained in:
rubenwardy 2015-06-10 18:39:22 +01:00
parent 85efc67049
commit 87c04be9ea
5 changed files with 175 additions and 163 deletions

114
api.lua

@ -21,6 +21,8 @@ end
function awards.init()
awards.players = awards.load()
awards.def = {}
awards.trigger_types = {}
awards.on = {}
end
function awards.load()
@ -36,100 +38,45 @@ end
awards.init()
-- Load files
dofile(minetest.get_modpath("awards").."/helpers.lua")
dofile(minetest.get_modpath("awards").."/triggers.lua")
function awards.register_trigger(name, func)
awards.trigger_types[name] = func
awards.on[name] = {}
awards['register_on_'..name] = function(func)
table.insert(awards.on[name], func)
end
end
-- API Functions
function awards._additional_triggers(name, data_table)
function awards._additional_triggers(name, def)
-- Depreciated!
end
function awards.register_achievement(name,data_table)
-- see if a trigger is defined in the achievement definition
if data_table.trigger and data_table.trigger.type then
if data_table.trigger.type == "dig" then
local tmp = {
award = name,
node = data_table.trigger.node,
target = data_table.trigger.target,
}
table.insert(awards.onDig,tmp)
elseif data_table.trigger.type == "place" then
local tmp = {
award = name,
node = data_table.trigger.node,
target = data_table.trigger.target,
}
table.insert(awards.onPlace,tmp)
elseif data_table.trigger.type == "craft" then
local tmp = {
award = name,
item = data_table.trigger.item,
target = data_table.trigger.target,
}
table.insert(awards.onCraft,tmp)
elseif data_table.trigger.type == "death" then
local tmp = {
award = name,
target = data_table.trigger.target,
}
table.insert(awards.onDeath,tmp)
elseif data_table.trigger.type == "chat" then
local tmp = {
award = name,
target = data_table.trigger.target,
}
table.insert(awards.onChat,tmp)
elseif data_table.trigger.type == "join" then
local tmp = {
award = name,
target = data_table.trigger.target,
}
table.insert(awards.onJoin,tmp)
function awards.register_achievement(name, def)
-- Add Triggers
if def.trigger and def.trigger.type then
local func = awards.trigger_types[def.trigger.type]
if func then
func(name, def)
else
awards._additional_triggers(name, data_table)
awards._additional_triggers(name, def)
end
end
-- check icon, background and custom_announce data
if data_table.icon == nil or data_table.icon == "" then
data_table.icon = "unknown.png"
if not def.icon or def.icon == "" then
def.icon = "unknown.png"
end
if data_table.background == nil or data_table.background == "" then
data_table.background = "bg_default.png"
if not def.background or def.background == "" then
def.background = "bg_default.png"
end
if data_table.custom_announce == nil or data_table.custom_announce == "" then
data_table.custom_announce = "Achievement Unlocked:"
if not def.custom_announce or def.custom_announce == "" then
def.custom_announce = "Achievement Unlocked:"
end
-- add the achievement to the definition table
data_table.name = name
awards.def[name] = data_table
end
-- run a function when a node is dug
function awards.register_onDig(func)
table.insert(awards.onDig,func)
end
-- run a function when a node is placed
function awards.register_onPlace(func)
table.insert(awards.onPlace,func)
end
-- run a function when a player dies
function awards.register_onDeath(func)
table.insert(awards.onDeath,func)
end
-- run a function when a player chats
function awards.register_onChat(func)
table.insert(awards.onChat,func)
end
-- run a function when a player joins
function awards.register_onJoin(func)
table.insert(awards.onJoin,func)
def.name = name
awards.def[name] = def
end
-- run a function when an item is crafted
@ -390,3 +337,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return true
end)
-- Load files
dofile(minetest.get_modpath("awards").."/helpers.lua")
dofile(minetest.get_modpath("awards").."/triggers.lua")

@ -35,3 +35,18 @@ minetest.register_chatcommand("awd", {
end
end
})
minetest.register_chatcommand("awpl", {
privs = {
server = true
},
description = "awpl: Get the statistics for the player given",
func = function(name, param)
if not param or param == "" then
param = name
end
minetest.chat_send_player(name, param)
local player = awards.player(param)
minetest.chat_send_player(name, dump(player))
end
})

@ -92,7 +92,7 @@ awards.register_achievement("award_obsessed_with_obsidian",{
})
-- On the way
awards.register_achievement("award_on_the_way",{
awards.register_achievement("award_on_the_way", {
title = S("On The Way"),
description = S("Place 100 rails."),
icon = "novicebuilder.png",
@ -104,19 +104,19 @@ awards.register_achievement("award_on_the_way",{
})
-- Lumberjack
awards.register_achievement("award_lumberjack",{
awards.register_achievement("award_lumberjack", {
title = S("Lumberjack"),
description = S("Dig 100 tree blocks."),
icon = "default_tree.png",
trigger = {
type = "dig",
node = "default:tree",
target = 100
target = 1
}
})
-- Semi-pro Lumberjack
awards.register_achievement("award_lumberjack_semipro",{
awards.register_achievement("award_lumberjack_semipro", {
title = S("Semi-pro Lumberjack"),
description = S("Dig 1,000 tree blocks."),
icon = "default_tree.png",
@ -128,7 +128,7 @@ awards.register_achievement("award_lumberjack_semipro",{
})
-- Professional Lumberjack
awards.register_achievement("award_lumberjack_professional",{
awards.register_achievement("award_lumberjack_professional", {
title = S("Professional Lumberjack"),
description = S("Dig 10,000 tree blocks."),
icon = "default_tree.png",
@ -140,7 +140,7 @@ awards.register_achievement("award_lumberjack_professional",{
})
-- L33T Lumberjack
awards.register_achievement("award_lumberjack_leet",{
awards.register_achievement("award_lumberjack_leet", {
title = S("L33T Lumberjack"),
description = S("Dig 100,000 tree blocks."),
icon = "default_tree.png",
@ -152,7 +152,7 @@ awards.register_achievement("award_lumberjack_leet",{
})
-- Junglebaby
awards.register_achievement("award_junglebaby",{
awards.register_achievement("award_junglebaby", {
title = S("Junglebaby"),
description = S("Dig 100 jungle tree blocks."),
icon = "default_jungletree.png",
@ -164,7 +164,7 @@ awards.register_achievement("award_junglebaby",{
})
-- Jungleman
awards.register_achievement("award_jungleman",{
awards.register_achievement("award_jungleman", {
title = S("Jungleman"),
description = S("Dig 1,000 jungle tree blocks."),
icon = "default_jungletree.png",
@ -176,7 +176,7 @@ awards.register_achievement("award_jungleman",{
})
-- Found some Mese!
awards.register_achievement("award_mesefind",{
awards.register_achievement("award_mesefind", {
title = S("First Mese Find"),
description = S("Find some Mese."),
icon = "default_mese_block.png",
@ -189,7 +189,7 @@ awards.register_achievement("award_mesefind",{
})
-- You're a copper
awards.register_achievement("award_youre_a_copper",{
awards.register_achievement("award_youre_a_copper", {
title = S("You're a copper"),
description = S("Dig 1,000 copper ores."),
icon = "miniminer.png",
@ -202,7 +202,7 @@ awards.register_achievement("award_youre_a_copper",{
})
-- You're winner
awards.register_achievement("award_youre_winner",{
awards.register_achievement("award_youre_winner", {
title = S("YOU'RE A WINNER!"),
description = S("Dig 1 mossy cobblestone."),
icon = "miniminer.png",
@ -216,7 +216,7 @@ awards.register_achievement("award_youre_winner",{
})
-- Found a Nyan cat!
awards.register_achievement("award_nyanfind",{
awards.register_achievement("award_nyanfind", {
title = S("OMG, Nyan Cat!"),
description = S("Find a nyan cat."),
icon = "default_nc_rb.png",
@ -228,7 +228,7 @@ awards.register_achievement("award_nyanfind",{
})
-- Mini Miner
awards.register_achievement("award_mine2",{
awards.register_achievement("award_mine2", {
title = S("Mini Miner"),
description = S("Dig 100 stone blocks."),
icon = "miniminer.png",
@ -241,7 +241,7 @@ awards.register_achievement("award_mine2",{
})
-- Hardened Miner
awards.register_achievement("award_mine3",{
awards.register_achievement("award_mine3", {
title = S("Hardened Miner"),
description = S("Dig 1,000 stone blocks"),
icon = "miniminer.png",
@ -254,7 +254,7 @@ awards.register_achievement("award_mine3",{
})
-- Master Miner
awards.register_achievement("award_mine4",{
awards.register_achievement("award_mine4", {
title = S("Master Miner"),
description = S("Dig 10,000 stone blocks."),
icon = "miniminer.png",
@ -267,7 +267,7 @@ awards.register_achievement("award_mine4",{
})
-- Marchand de sable
awards.register_achievement("award_marchand_de_sable",{
awards.register_achievement("award_marchand_de_sable", {
title = S("Marchand De Sable"),
description = S("Dig 1,000 sand."),
background = "bg_mining.png",
@ -279,7 +279,7 @@ awards.register_achievement("award_marchand_de_sable",{
})
-- Join
awards.register_achievement("award_join2",{
awards.register_achievement("award_join2", {
title = S("Frequent Visitor"),
description = S("Connect to the server 50 times."),
trigger = {
@ -290,7 +290,7 @@ awards.register_achievement("award_join2",{
})
-- Dying Spree
awards.register_achievement("award_dying_spree",{
awards.register_achievement("award_dying_spree", {
title = S("Dying Spree"),
description = S("Die 5 times."),
trigger = {
@ -300,7 +300,7 @@ awards.register_achievement("award_dying_spree",{
})
-- Bot-like
awards.register_achievement("award_bot_like",{
awards.register_achievement("award_bot_like", {
title = S("Bot-like"),
description = S("Die 10 times."),
trigger = {
@ -310,7 +310,7 @@ awards.register_achievement("award_bot_like",{
})
-- You Suck!
awards.register_achievement("award_you_suck",{
awards.register_achievement("award_you_suck", {
title = S("You Suck!"),
description = S("Die 100 times."),
trigger = {
@ -321,7 +321,7 @@ awards.register_achievement("award_you_suck",{
})
-- Burned to death
awards.register_achievement("award_burn",{
awards.register_achievement("award_burn", {
title = S("You're a witch!"),
description = S("Burn to death in a fire.")
})
@ -334,7 +334,7 @@ awards.register_onDeath(function(player,data)
end)
-- Died in flowing lava
awards.register_achievement("award_in_the_flow",{
awards.register_achievement("award_in_the_flow", {
title = S("In the Flow"),
description = S("Die in flowing lava.")
})
@ -347,11 +347,11 @@ awards.register_onDeath(function(player,data)
end)
-- Die near diamond ore
awards.register_achievement("award_this_is_sad",{
awards.register_achievement("award_this_is_sad", {
title = S("This is Sad"),
description = S("Die near diamond ore.")
})
awards.register_onDeath(function(player,data)
awards.register_on_death(function(player,data)
local pos = player:getpos()
if pos and minetest.find_node_near(pos, 5, "default:stone_with_diamond") ~= nil then
return "award_this_is_sad"
@ -360,11 +360,11 @@ awards.register_onDeath(function(player,data)
end)
-- Die near diamond ore
awards.register_achievement("award_the_stack",{
awards.register_achievement("award_the_stack", {
title = S("The Stack"),
description = S("Die near bones.")
})
awards.register_onDeath(function(player,data)
awards.register_on_death(function(player,data)
local pos = player:getpos()
if pos and minetest.find_node_near(pos, 5, "bones:bones") ~= nil then
return "award_the_stack"

@ -1,5 +1,4 @@
Awards
------
# Awards
by Andrew "Rubenwardy" Ward, LGPL 2.1 or later.
@ -9,40 +8,45 @@ Majority of awards are back ported from Calinou's
old fork in Carbone, under same license.
Code Reference
--------------
# Basic API
The API
=======
* awards.register_achievement(name,data_table)
* awards.register_achievement(name, def)
* name
* desciption
* sound [optional]
* image [optional]
* image [optional] - texture name, eg: award_one.png
* background [optional] - texture name, eg: award_one.png
* trigger [optional] [table]
* type - "dig", "place", "craft", "death", "chat" or "join"
* (for dig/place type) node - the nodes name
* (for craft type) item - the items name
* (for all types) target - how many to dig / place
* See Triggers
* secret [optional] - if true, then player needs to unlock to find out what it is.
* awards.register_trigger(name, func(awardname, def))
* Note: awards.on[name] is automatically created for triggers
* awards.give_achievement(name,award)
* -- gives an award to a player
* awards.register_onDig(func(player,data))
# Included in the Mod
## Triggers
* awards.register_on_dig(func(player, data))
* -- return award name or null
* awards.register_onPlace(func(player,data))
* awards.register_on_place(func(player, data))
* -- return award name or null
* awards.register_onDeath(func(player,data))
* awards.register_on_death(func(player, data))
* -- return award name or null
* awards.register_onChat(func(player,data))
* awards.register_on_chat(func(player, data))
* -- return award name or null
* awards.register_onJoin(func(player,data))
* awards.register_on_join(func(player, data))
* -- return award name or null
* awards.register_onCraft(func(player,data))
* -- return award name or null
Player Data
===========
# Player Data
A list of data referenced/hashed by the player's name.
* player name

@ -4,13 +4,54 @@
-- this is the trigger handler file for the awards mod
-------------------------------------------------------
-- Function and table holders for Triggers
awards.onDig = {}
awards.onPlace = {}
awards.onCraft = {}
awards.onChat = {}
awards.onDeath = {}
awards.onJoin = {}
awards.register_trigger("dig", function(name, def)
local tmp = {
award = name,
node = def.trigger.node,
target = def.trigger.target
}
table.insert(awards.on.dig, tmp)
end)
awards.register_trigger("place", function(name, def)
local tmp = {
award = name,
node = def.trigger.node,
target = def.trigger.target
}
table.insert(awards.on.place, tmp)
end)
awards.register_trigger("death", function(name, def)
local tmp = {
award = name,
target = def.trigger.target
}
table.insert(awards.on.death, tmp)
end)
awards.register_trigger("chat", function(name, def)
local tmp = {
award = name,
target = def.trigger.target
}
table.insert(awards.on.chat, tmp)
end)
awards.register_trigger("join", function(name, def)
local tmp = {
award = name,
target = def.trigger.target
}
table.insert(awards.on.join, tmp)
end)
-- Backwards compatibility
awards.register_onDig = awards.register_on_dig
awards.register_onPlace = awards.register_on_place
awards.register_onDeath = awards.register_on_death
awards.register_onChat = awards.register_on_chat
awards.register_onJoin = awards.register_on_join
-- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger)
@ -19,7 +60,6 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
end
local nodedug = string.split(oldnode.name, ":")
if #nodedug ~= 2 then
--minetest.log("error","Awards mod: "..oldnode.name.." is in wrong format!")
return
end
local mod = nodedug[1]
@ -37,27 +77,27 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
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.onDig do
local player = digger
local data = awards.players[playern]
for i=1, #awards.on.dig do
local res = nil
if type(awards.onDig[i]) == "function" then
if type(awards.on.dig[i]) == "function" then
-- Run trigger callback
res = awards.onDig[i](player,data)
elseif type(awards.onDig[i]) == "table" then
res = awards.on.dig[i](player,data)
elseif type(awards.on.dig[i]) == "table" then
-- Handle table trigger
if not awards.onDig[i].node or not awards.onDig[i].target or not awards.onDig[i].award then
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 - onDig trigger "..i.." is invalid!")
print("[ERROR] awards - on.dig trigger "..i.." is invalid!")
else
-- run the table
local tnodedug = string.split(awards.onDig[i].node, ":")
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.onDig[i].target-1 then
res=awards.onDig[i].award
elseif data.count[tmod][titem] > awards.on.dig[i].target-1 then
res=awards.on.dig[i].award
end
end
end
@ -68,13 +108,12 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
end
end)
minetest.register_on_placenode(function(pos,node,digger)
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
--minetest.log("error","Awards mod: "..node.name.." is in wrong format!")
return
end
local mod=nodedug[1]
@ -95,24 +134,24 @@ minetest.register_on_placenode(function(pos,node,digger)
-- Run callbacks and triggers
local player = digger
local data = awards.players[playern]
for i=1,# awards.onPlace do
for i=1,# awards.on.place do
local res = nil
if type(awards.onPlace[i]) == "function" then
if type(awards.on.place[i]) == "function" then
-- Run trigger callback
res = awards.onPlace[i](player,data)
elseif type(awards.onPlace[i]) == "table" then
res = awards.on.place[i](player,data)
elseif type(awards.on.place[i]) == "table" then
-- Handle table trigger
if not awards.onPlace[i].node or not awards.onPlace[i].target or not awards.onPlace[i].award then
print("[ERROR] awards - onPlace trigger "..i.." is invalid!")
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.onPlace[i].node, ":")
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.onPlace[i].target-1 then
res = awards.onPlace[i].award
elseif data.place[tmod][titem] > awards.on.place[i].target-1 then
res = awards.on.place[i].award
end
end
end
@ -192,7 +231,7 @@ minetest.register_on_dieplayer(function(player)
data.deaths = data.deaths + 1
-- Run callbacks and triggers
for _,trigger in pairs(awards.onDeath) do
for _,trigger in pairs(awards.on.death) do
local res = nil
if type(trigger) == "function" then
res = trigger(player,data)
@ -224,7 +263,7 @@ minetest.register_on_joinplayer(function(player)
data.joins = data.joins + 1
-- Run callbacks and triggers
for _,trigger in pairs(awards.onJoin) do
for _, trigger in pairs(awards.on.join) do
local res = nil
if type(trigger) == "function" then
res = trigger(player,data)
@ -257,7 +296,7 @@ minetest.register_on_chat_message(function(name, message)
data.chats = data.chats + 1
-- Run callbacks and triggers
for _,trigger in pairs(awards.onChat) do
for _,trigger in pairs(awards.on.chat) do
local res = nil
if type(trigger) == "function" then
res = trigger(player,data)