awards/triggers.lua

229 lines
6.0 KiB
Lua
Raw Normal View History

2013-02-22 18:53:42 +01:00
-- AWARDS
2014-05-02 19:48:32 +02:00
-- by Rubenwardy, CC-BY-SA
2013-02-22 18:53:42 +01:00
-------------------------------------------------------
-- this is the trigger handler file for the awards mod
-------------------------------------------------------
2013-02-23 15:53:08 +01:00
-- Function and table holders for Triggers
2014-05-02 19:48:32 +02:00
awards.onDig = {}
awards.onPlace = {}
2014-05-02 20:01:41 +02:00
awards.onChat = {}
2014-05-02 19:48:32 +02:00
awards.onDeath = {}
awards.onJoin = {}
2013-02-22 18:53:42 +01:00
-- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger)
2013-11-06 19:33:01 +01:00
if not digger or not pos or not oldnode then
return
end
2013-02-22 18:53:42 +01:00
local nodedug = string.split(oldnode.name, ":")
2013-11-06 19:33:01 +01:00
if #nodedug ~= 2 then
2014-05-02 19:48:32 +02:00
minetest.log("error","Awards mod: "..oldnode.name.." is in wrong format!")
2013-11-06 19:33:01 +01:00
return
end
2014-05-02 19:48:32 +02:00
local mod = nodedug[1]
local item = nodedug[2]
2013-02-22 19:38:09 +01:00
local playern = digger:get_player_name()
2013-02-22 19:03:32 +01:00
2013-11-06 19:33:01 +01:00
if (not playern or not nodedug or not mod or not item) then
return
end
2013-11-12 20:50:25 +01:00
awards.assertPlayer(playern)
2014-05-02 19:48:32 +02:00
awards.tbv(awards.players[playern].count, mod)
awards.tbv(awards.players[playern].count[mod], item, 0)
2013-11-06 19:33:01 +01:00
2014-05-02 19:48:32 +02:00
-- Increment counter
2014-05-02 20:01:41 +02:00
awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item] + 1
2013-11-06 19:33:01 +01:00
-- Run callbacks and triggers
local player=digger
2013-11-06 19:38:55 +01:00
local data=awards.players[playern]
2013-11-06 19:33:01 +01:00
for i=1,# awards.onDig do
local res = nil
if type(awards.onDig[i]) == "function" then
-- Run trigger callback
res = awards.onDig[i](player,data)
elseif type(awards.onDig[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
-- table running failed!
2013-11-12 20:50:25 +01:00
print("[ERROR] awards - onDig trigger "..i.." is invalid!")
2013-11-06 19:33:01 +01:00
else
-- run the table
local tnodedug = string.split(awards.onDig[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
2013-02-23 13:02:02 +01:00
-- table running failed!
2013-11-06 19:33:01 +01:00
elseif data.count[tmod][titem] > awards.onDig[i].target-1 then
res=awards.onDig[i].award
2013-02-23 13:02:02 +01:00
end
end
2013-11-06 19:33:01 +01:00
end
2013-02-22 19:38:09 +01:00
2013-11-06 19:33:01 +01:00
if res then
awards.give_achievement(playern,res)
2013-02-22 19:38:09 +01:00
end
2013-02-22 18:53:42 +01:00
end
end)
2013-11-06 19:33:01 +01:00
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
2014-05-02 19:48:32 +02:00
minetest.log("error","Awards mod: "..node.name.." is in wrong format!")
2013-11-06 19:33:01 +01:00
return
end
2013-02-22 21:38:01 +01:00
local mod=nodedug[1]
local item=nodedug[2]
2013-11-06 19:33:01 +01:00
local playern = digger:get_player_name()
2013-02-22 21:38:01 +01:00
2013-11-06 19:33:01 +01:00
-- Run checks
if (not playern or not nodedug or not mod or not item) then
return
end
2013-11-12 20:50:25 +01:00
awards.assertPlayer(playern)
2014-05-02 19:48:32 +02:00
awards.tbv(awards.players[playern].place, mod)
awards.tbv(awards.players[playern].place[mod], item, 0)
2013-11-06 19:33:01 +01:00
2014-05-02 19:48:32 +02:00
-- Increment counter
2014-05-02 20:01:41 +02:00
awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item] + 1
2013-11-06 19:33:01 +01:00
-- Run callbacks and triggers
local player = digger
2013-11-06 19:38:55 +01:00
local data = awards.players[playern]
2013-11-06 19:33:01 +01:00
for i=1,# awards.onPlace do
local res = nil
if type(awards.onPlace[i]) == "function" then
-- Run trigger callback
res = awards.onPlace[i](player,data)
elseif type(awards.onPlace[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
2013-11-12 20:50:25 +01:00
print("[ERROR] awards - onPlace trigger "..i.." is invalid!")
2013-11-06 19:33:01 +01:00
else
-- run the table
local tnodedug = string.split(awards.onPlace[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
2013-02-23 13:02:02 +01:00
-- table running failed!
2013-11-06 19:33:01 +01:00
elseif data.place[tmod][titem] > awards.onPlace[i].target-1 then
res = awards.onPlace[i].award
2013-02-23 13:02:02 +01:00
end
end
2013-11-06 19:33:01 +01:00
end
2013-02-22 21:38:01 +01:00
2013-11-06 19:33:01 +01:00
if res then
awards.give_achievement(playern,res)
2013-02-22 21:38:01 +01:00
end
end
end)
2013-02-23 16:13:21 +01:00
minetest.register_on_dieplayer(function(player)
2013-11-06 19:33:01 +01:00
-- Run checks
2014-05-02 20:11:21 +02:00
local name = player:get_player_name()
if not player or not name or name=="" then
2013-11-06 19:33:01 +01:00
return
end
2014-05-02 20:01:41 +02:00
2014-05-02 20:11:21 +02:00
-- Get player
awards.assertPlayer(name)
2014-05-02 20:01:41 +02:00
local data = awards.players[name]
2013-11-06 19:33:01 +01:00
-- Increment counter
2014-05-02 20:01:41 +02:00
data.deaths = data.deaths + 1
2013-02-23 16:13:21 +01:00
2013-11-06 19:33:01 +01:00
-- Run callbacks and triggers
2014-05-02 20:01:41 +02:00
for _,trigger in pairs(awards.onDeath) 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
2013-02-23 16:13:21 +01:00
end
end
end
2014-05-02 20:01:41 +02:00
if res ~= nil then
awards.give_achievement(name,res)
end
end
end)
minetest.register_on_joinplayer(function(player)
-- Run checks
local name = player:get_player_name()
if not player or not name or name=="" then
return
end
-- Get player
awards.assertPlayer(name)
local data = awards.players[name]
-- Increment counter
data.joins = data.joins + 1
-- Run callbacks and triggers
for _,trigger in pairs(awards.onJoin) 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
2014-05-02 20:01:41 +02:00
awards.give_achievement(name,res)
end
end
end)
2013-02-23 16:13:21 +01:00
2014-05-02 20:01:41 +02:00
minetest.register_on_chat_message(function(name, message)
-- Run checks
2014-05-02 20:11:21 +02:00
local idx = string.find(message,"/")
if not name or (idx ~= nil and idx <= 1) then
2014-05-02 20:01:41 +02:00
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)
2013-02-23 16:13:21 +01:00
end
end
end)
2013-02-23 13:02:02 +01:00
2013-02-23 16:13:21 +01:00
minetest.register_on_newplayer(function(player)
2013-11-12 20:33:19 +01:00
local playern = player:get_player_name()
2013-11-12 20:50:25 +01:00
awards.assertPlayer(playern)
2013-02-22 20:57:24 +01:00
end)
minetest.register_on_shutdown(function()
2014-05-02 19:48:32 +02:00
awards.save()
2013-02-22 18:53:42 +01:00
end)