On chat trigger and code style

This commit is contained in:
rubenwardy 2014-05-02 19:01:41 +01:00
parent 8180ab6755
commit 8adf859127
2 changed files with 78 additions and 42 deletions

47
api.lua
View File

@ -43,7 +43,7 @@ function awards.tbv(tb,value,default)
if not value then if not value then
value = "[NULL]" value = "[NULL]"
end end
minetest.log("error", "awards.tbv - table '"..value.."' is null, or not a table! Dump: "..dump(tb)) minetest.log("error", "awards.tbv - table "..dump(value).." is null, or not a table! Dump: "..dump(tb))
return return
end end
if not value then if not value then
@ -74,26 +74,32 @@ dofile(minetest.get_modpath("awards").."/config.txt")
function awards.register_achievement(name,data_table) function awards.register_achievement(name,data_table)
-- see if a trigger is defined in the achievement definition -- see if a trigger is defined in the achievement definition
if data_table.trigger and data_table.trigger.type then if data_table.trigger and data_table.trigger.type then
if data_table.trigger.type=="dig" then if data_table.trigger.type == "dig" then
local tmp={ local tmp = {
award=name, award = name,
node=data_table.trigger.node, node = data_table.trigger.node,
target=data_table.trigger.target, target = data_table.trigger.target,
} }
table.insert(awards.onDig,tmp) table.insert(awards.onDig,tmp)
elseif data_table.trigger.type=="place" then elseif data_table.trigger.type == "place" then
local tmp={ local tmp = {
award=name, award = name,
node=data_table.trigger.node, node = data_table.trigger.node,
target=data_table.trigger.target, target = data_table.trigger.target,
} }
table.insert(awards.onPlace,tmp) table.insert(awards.onPlace,tmp)
elseif data_table.trigger.type=="death" then elseif data_table.trigger.type == "death" then
local tmp={ local tmp = {
award=name, award = name,
target=data_table.trigger.target, target = data_table.trigger.target,
} }
table.insert(awards.onDeath,tmp) 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)
end end
end end
@ -113,21 +119,26 @@ function awards.register_achievement(name,data_table)
awards.def[name] = data_table awards.def[name] = data_table
end end
-- this function adds a trigger function or table to the ondig table -- run a function when a node is dug
function awards.register_onDig(func) function awards.register_onDig(func)
table.insert(awards.onDig,func) table.insert(awards.onDig,func)
end end
-- this function adds a trigger function or table to the ondig table -- run a function when a node is placed
function awards.register_onPlace(func) function awards.register_onPlace(func)
table.insert(awards.onPlace,func) table.insert(awards.onPlace,func)
end end
-- this function adds a trigger function or table to the ondeath table -- run a function when a player dies
function awards.register_onDeath(func) function awards.register_onDeath(func)
table.insert(awards.onDeath,func) table.insert(awards.onDeath,func)
end end
-- run a function when a player chats
function awards.register_onChat(func)
table.insert(awards.onChat,func)
end
-- This function is called whenever a target condition is met. -- This function is called whenever a target condition is met.
-- It checks if a player already has that achievement, and if they do not, -- It checks if a player already has that achievement, and if they do not,
-- it gives it to them -- it gives it to them

View File

@ -8,6 +8,7 @@
awards.onDig = {} awards.onDig = {}
awards.onPlace = {} awards.onPlace = {}
awards.onTick = {} awards.onTick = {}
awards.onChat = {}
awards.onDeath = {} awards.onDeath = {}
-- Trigger Handles -- Trigger Handles
@ -32,7 +33,7 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
awards.tbv(awards.players[playern].count[mod], item, 0) awards.tbv(awards.players[playern].count[mod], item, 0)
-- Increment counter -- Increment counter
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 player=digger
@ -88,7 +89,7 @@ minetest.register_on_placenode(function(pos,node,digger)
awards.tbv(awards.players[playern].place[mod], item, 0) awards.tbv(awards.players[playern].place[mod], item, 0)
-- Increment counter -- Increment counter
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 player = digger
@ -126,37 +127,61 @@ minetest.register_on_dieplayer(function(player)
if not player or not player:get_player_name() or player:get_player_name()=="" then if not player or not player:get_player_name() or player:get_player_name()=="" then
return return
end end
local playern = player:get_player_name()
-- Get player
local name = player:get_player_name()
awards.assertPlayer(playern) awards.assertPlayer(playern)
local data = awards.players[name]
-- Increment counter -- Increment counter
awards.players[player:get_player_name()].deaths = awards.players[player:get_player_name()].deaths + 1 data.deaths = data.deaths + 1
-- Run callbacks and triggers -- Run callbacks and triggers
local data=awards.players[playern] for _,trigger in pairs(awards.onDeath) do
for i=1,# awards.onDeath do local res = nil
local res=nil if type(trigger) == "function" then
if type(awards.onDeath[i]) == "function" then res = trigger(player,data)
-- Run trigger callback elseif type(trigger) == "table" then
res=awards.onDeath[i](player,data) if trigger.target and trigger.award then
elseif type(awards.onDeath[i]) == "table" then if data.deaths and data.deaths >= trigger.target then
-- handle table here res = trigger.award
if not awards.onDeath[i].target or not awards.onDeath[i].award then
-- table running failed!
print("[ERROR] awards - onDeath trigger "..i.." is invalid!")
else
-- run the table
if not data.deaths then
-- table running failed!
elseif data.deaths > awards.onDeath[i].target-1 then
res=awards.onDeath[i].award
end end
end end
end end
if res ~= nil then
awards.give_achievement(name,res)
end
end
end)
if res~=nil then minetest.register_on_chat_message(function(name, message)
awards.give_achievement(playern,res) -- Run checks
if not name then
return
end
-- Get player
awards.assertPlayer(name)
local data = awards.players[name]
local player = minetest.get_player_by_name(name)
-- Increment counter
data.chats = data.chats + 1
-- Run callbacks and triggers
for _,trigger in pairs(awards.onChat) 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.give_achievement(name,res)
end end
end end
end) end)