awards/triggers.lua

345 lines
9.2 KiB
Lua
Raw Permalink Normal View History

2014-09-26 21:36:40 +02:00
-- AWARDS
2015-06-10 19:48:20 +02:00
--
-- Copyright (C) 2013-2015 rubenwardy
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
-- You should have received a copy of the GNU Lesser General Public License along
-- with this program; if not, write to the Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--
2013-02-22 18:53:42 +01:00
2015-06-10 20:15:16 +02:00
awards.register_trigger("dig", function(def)
2015-06-10 19:39:22 +02:00
local tmp = {
2015-06-10 20:15:16 +02:00
award = def.name,
2015-06-10 19:39:22 +02:00
node = def.trigger.node,
target = def.trigger.target
}
table.insert(awards.on.dig, tmp)
end)
2015-06-10 20:15:16 +02:00
awards.register_trigger("place", function(def)
2015-06-10 19:39:22 +02:00
local tmp = {
2015-06-10 20:15:16 +02:00
award = def.name,
2015-06-10 19:39:22 +02:00
node = def.trigger.node,
target = def.trigger.target
}
table.insert(awards.on.place, tmp)
end)
2015-06-10 20:15:16 +02:00
awards.register_trigger("death", function(def)
2015-06-10 19:39:22 +02:00
local tmp = {
2015-06-10 20:15:16 +02:00
award = def.name,
2015-06-10 19:39:22 +02:00
target = def.trigger.target
}
table.insert(awards.on.death, tmp)
end)
2015-06-10 20:15:16 +02:00
awards.register_trigger("chat", function(def)
2015-06-10 19:39:22 +02:00
local tmp = {
2015-06-10 20:15:16 +02:00
award = def.name,
2015-06-10 19:39:22 +02:00
target = def.trigger.target
}
table.insert(awards.on.chat, tmp)
end)
2015-06-10 20:15:16 +02:00
awards.register_trigger("join", function(def)
2015-06-10 19:39:22 +02:00
local tmp = {
2015-06-10 20:15:16 +02:00
award = def.name,
2015-06-10 19:39:22 +02:00
target = def.trigger.target
}
table.insert(awards.on.join, tmp)
end)
awards.register_trigger("craft", function(def)
local tmp = {
award = def.name,
item = def.trigger.item,
target = def.trigger.target
}
table.insert(awards.on.craft, tmp)
end)
2015-06-10 19:39:22 +02:00
-- 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
awards.register_onCraft = awards.register_on_craft
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
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
2015-06-10 19:39:22 +02:00
local player = digger
local data = awards.players[playern]
for i=1, #awards.on.dig do
2013-11-06 19:33:01 +01:00
local res = nil
2015-06-10 19:39:22 +02:00
if type(awards.on.dig[i]) == "function" then
2013-11-06 19:33:01 +01:00
-- Run trigger callback
2015-06-10 19:39:22 +02:00
res = awards.on.dig[i](player,data)
elseif type(awards.on.dig[i]) == "table" then
2013-11-06 19:33:01 +01:00
-- Handle table trigger
2015-06-10 19:39:22 +02:00
if not awards.on.dig[i].node or not awards.on.dig[i].target or not awards.on.dig[i].award then
2013-11-06 19:33:01 +01:00
-- table running failed!
2015-06-10 19:39:22 +02:00
print("[ERROR] awards - on.dig trigger "..i.." is invalid!")
2013-11-06 19:33:01 +01:00
else
-- run the table
2015-06-10 19:39:22 +02:00
local tnodedug = string.split(awards.on.dig[i].node, ":")
2013-11-06 19:33:01 +01:00
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!
2015-06-10 19:39:22 +02:00
elseif data.count[tmod][titem] > awards.on.dig[i].target-1 then
res=awards.on.dig[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
2015-06-11 18:35:32 +02:00
awards.unlock(playern,res)
2013-02-22 19:38:09 +01:00
end
2013-02-22 18:53:42 +01:00
end
end)
2015-06-10 19:39:22 +02:00
minetest.register_on_placenode(function(pos, node, digger)
2013-11-06 19:33:01 +01:00
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
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]
2015-06-10 19:39:22 +02:00
for i=1,# awards.on.place do
2013-11-06 19:33:01 +01:00
local res = nil
2015-06-10 19:39:22 +02:00
if type(awards.on.place[i]) == "function" then
2013-11-06 19:33:01 +01:00
-- Run trigger callback
2015-06-10 19:39:22 +02:00
res = awards.on.place[i](player,data)
elseif type(awards.on.place[i]) == "table" then
2013-11-06 19:33:01 +01:00
-- Handle table trigger
2015-06-10 19:39:22 +02:00
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!")
2013-11-06 19:33:01 +01:00
else
-- run the table
2015-06-10 19:39:22 +02:00
local tnodedug = string.split(awards.on.place[i].node, ":")
2013-11-06 19:33:01 +01:00
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!
2015-06-10 19:39:22 +02:00
elseif data.place[tmod][titem] > awards.on.place[i].target-1 then
res = awards.on.place[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
2015-06-11 18:35:32 +02:00
awards.unlock(playern,res)
2013-02-22 21:38:01 +01:00
end
end
end)
2016-06-01 19:41:24 +02:00
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
if not player or not itemstack then
return
end
local itemcrafted = string.split(itemstack:get_name(), ":")
if #itemcrafted ~= 2 then
--minetest.log("error","Awards mod: "..itemstack:get_name().." is in wrong format!")
return
end
local mod = itemcrafted[1]
local item = itemcrafted[2]
local playern = player:get_player_name()
if (not playern or not itemcrafted or not mod or not item) then
return
end
awards.assertPlayer(playern)
awards.tbv(awards.players[playern].craft, mod)
awards.tbv(awards.players[playern].craft[mod], item, 0)
-- Increment counter
awards.players[playern].craft[mod][item] = awards.players[playern].craft[mod][item] + 1
2016-06-01 19:41:24 +02:00
-- Run callbacks and triggers
local data = awards.players[playern]
for i=1, #awards.on.craft do
2016-06-01 19:41:24 +02:00
local res = nil
local entry = awards.on.craft[i]
if type(entry) == "function" then
2016-06-01 19:41:24 +02:00
-- Run trigger callback
res = entry(player,data)
elseif type(entry) == "table" then
2016-06-01 19:41:24 +02:00
-- Handle table trigger
if not entry.item or not entry.target or not entry.award then
2016-06-01 19:41:24 +02:00
-- table running failed!
print("[ERROR] awards - onCraft trigger "..i.." is invalid!")
else
-- run the table
local titemcrafted = string.split(entry.item, ":")
2016-06-01 19:41:24 +02:00
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
2016-06-01 19:41:24 +02:00
-- table running failed!
elseif data.craft[tmod][titem] > entry.target-1 then
res=awards.on.craft[i].award
2016-06-01 19:41:24 +02:00
end
end
end
if res then
awards.give_achievement(playern,res)
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
2015-06-10 19:39:22 +02:00
-- Get player
2014-05-02 20:11:21 +02:00
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
2015-06-10 19:39:22 +02:00
2013-11-06 19:33:01 +01:00
-- Run callbacks and triggers
2015-06-10 19:39:22 +02:00
for _,trigger in pairs(awards.on.death) do
2014-05-02 20:01:41 +02:00
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
2015-06-11 18:35:32 +02:00
awards.unlock(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
2015-06-10 19:39:22 +02:00
-- Get player
awards.assertPlayer(name)
local data = awards.players[name]
-- Increment counter
data.joins = data.joins + 1
2015-06-10 19:39:22 +02:00
-- Run callbacks and triggers
2015-06-10 19:39:22 +02:00
for _, trigger in pairs(awards.on.join) 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
2015-06-11 18:35:32 +02:00
awards.unlock(name,res)
2014-05-02 20:01:41 +02:00
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)
2015-06-10 19:39:22 +02:00
2014-05-02 20:01:41 +02:00
-- Increment counter
data.chats = data.chats + 1
2015-06-10 19:39:22 +02:00
-- Run callbacks and triggers
for _,trigger in pairs(awards.on.chat) do
2014-05-02 20:01:41 +02:00
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
2015-06-11 18:35:32 +02:00
awards.unlock(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()
2014-09-26 21:21:43 +02:00
end)