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
View File

@ -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")

View File

@ -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
})

View File

@ -111,7 +111,7 @@ awards.register_achievement("award_lumberjack",{
trigger = {
type = "dig",
node = "default:tree",
target = 100
target = 1
}
})
@ -351,7 +351,7 @@ 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"
@ -364,7 +364,7 @@ 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"

View File

@ -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

View File

@ -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]
@ -39,25 +79,25 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
-- Run callbacks and triggers
local player = digger
local data = awards.players[playern]
for i=1,# awards.onDig do
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
@ -74,7 +114,6 @@ minetest.register_on_placenode(function(pos,node,digger)
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)