mirror of
https://gitlab.com/rubenwardy/awards.git
synced 2024-12-29 04:00:38 +01:00
Restructure for bulletproof-ness
This commit is contained in:
parent
076f91853c
commit
773f799320
98
api.lua
98
api.lua
@ -31,6 +31,19 @@ player_data=load_playerD()
|
|||||||
-- A table of award definitions
|
-- A table of award definitions
|
||||||
awards.def={}
|
awards.def={}
|
||||||
|
|
||||||
|
function awards.tbv(tb,value,default)
|
||||||
|
if not default then
|
||||||
|
default = {}
|
||||||
|
end
|
||||||
|
if not tb then
|
||||||
|
print("Table not defined!")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if not tb[value] then
|
||||||
|
tb[value] = default
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Load files
|
-- Load files
|
||||||
dofile(minetest.get_modpath("awards").."/triggers.lua")
|
dofile(minetest.get_modpath("awards").."/triggers.lua")
|
||||||
dofile(minetest.get_modpath("awards").."/config.txt")
|
dofile(minetest.get_modpath("awards").."/config.txt")
|
||||||
@ -38,43 +51,43 @@ dofile(minetest.get_modpath("awards").."/config.txt")
|
|||||||
-- API Functions
|
-- API Functions
|
||||||
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)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- check icon, background and custom_announce data
|
-- check icon, background and custom_announce data
|
||||||
if data_table['icon'] == nil or data_table['icon'] == "" then
|
if data_table.icon == nil or data_table.icon == "" then
|
||||||
data_table['icon'] = "unknown.png"
|
data_table.icon = "unknown.png"
|
||||||
end
|
end
|
||||||
if data_table['background'] == nil or data_table['background'] == "" then
|
if data_table.background == nil or data_table.background == "" then
|
||||||
data_table['background'] = "bg_default.png"
|
data_table.background = "bg_default.png"
|
||||||
end
|
end
|
||||||
if data_table['custom_announce'] == nil or data_table['custom_announce'] == "" then
|
if data_table.custom_announce == nil or data_table.custom_announce == "" then
|
||||||
data_table['custom_announce'] = "Achievement Unlocked:"
|
data_table.custom_announce = "Achievement Unlocked:"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- add the achievement to the definition table
|
-- add the achievement to the definition 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
|
-- this function adds a trigger function or table to the ondig table
|
||||||
@ -100,34 +113,43 @@ end
|
|||||||
-- name - the name of the player
|
-- name - the name of the player
|
||||||
-- award - the name of the award to give
|
-- award - the name of the award to give
|
||||||
function awards.give_achievement(name,award)
|
function awards.give_achievement(name,award)
|
||||||
-- load the player's data table
|
-- Access Player Data
|
||||||
local data=player_data[name]
|
local data=player_data[name]
|
||||||
|
|
||||||
-- check if the table that holds a player's achievements exists
|
-- Perform checks
|
||||||
if not data['unlocked'] then
|
if not data then
|
||||||
data['unlocked']={}
|
return
|
||||||
end
|
end
|
||||||
|
if not awards.def[award] then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
awards.tbv(data,"unlocked")
|
||||||
|
|
||||||
-- check to see if the player does not already have that achievement
|
-- check to see if the player does not already have that achievement
|
||||||
if not data['unlocked'][award] or data['unlocked'][award]~=award then
|
if not data.unlocked[award] or data.unlocked[award]~=award then
|
||||||
-- save the achievement to the player_data table
|
-- Set award flag
|
||||||
data['unlocked'][award]=award
|
data.unlocked[award]=award
|
||||||
|
|
||||||
-- define local variables, so award data can be saved
|
-- Get data from definition tables
|
||||||
local title = award
|
local title = award
|
||||||
local desc = ""
|
local desc = ""
|
||||||
|
local background = ""
|
||||||
-- check definition table to get values
|
local icon = ""
|
||||||
if awards['def'][award] and awards['def'][award]['title'] and awards['def'][award]['custom_announce'] and awards['def'][award]['background'] and awards['def'][award]['icon'] then
|
local custom_announce = ""
|
||||||
title=awards['def'][award]['title']
|
if awards.def[award].title then
|
||||||
background=awards['def'][award]['background']
|
title = awards.def[award].title
|
||||||
icon=awards['def'][award]['icon']
|
|
||||||
custom_announce=awards['def'][award]['custom_announce']
|
|
||||||
end
|
end
|
||||||
|
if awards.def[award].custom_announce then
|
||||||
-- check definition table to get description
|
custom_announce = awards.def[award].custom_announce
|
||||||
if awards['def'][award] and awards['def'][award]['description'] then
|
end
|
||||||
desc=awards['def'][award]['description']
|
if awards.def[award].background then
|
||||||
|
background = awards.def[award].background
|
||||||
|
end
|
||||||
|
if awards.def[award].icon then
|
||||||
|
icon = awards.def[award].icon
|
||||||
|
end
|
||||||
|
if awards.def[award] and awards.def[award].description then
|
||||||
|
desc = awards.def[award].description
|
||||||
end
|
end
|
||||||
|
|
||||||
-- send the won award message to the player
|
-- send the won award message to the player
|
||||||
@ -159,7 +181,11 @@ minetest.register_chatcommand("list_awards", {
|
|||||||
params = "",
|
params = "",
|
||||||
description = "list_awards: list your awards",
|
description = "list_awards: list your awards",
|
||||||
func = function(name, param)
|
func = function(name, param)
|
||||||
minetest.chat_send_player(name, name.."'s awards:");
|
if not player_data[name] then
|
||||||
|
minetest.chat_send_player(name, "Unable to find your award listings!")
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.chat_send_player(name, name.."'s awards:")
|
||||||
|
|
||||||
for _, str in pairs(player_data[name].unlocked) do
|
for _, str in pairs(player_data[name].unlocked) do
|
||||||
minetest.chat_send_player(name, str);
|
minetest.chat_send_player(name, str);
|
||||||
|
194
triggers.lua
194
triggers.lua
@ -10,175 +10,164 @@ awards.onPlace={}
|
|||||||
awards.onTick={}
|
awards.onTick={}
|
||||||
awards.onDeath={}
|
awards.onDeath={}
|
||||||
|
|
||||||
-- Player functions
|
|
||||||
|
|
||||||
-- Trigger Handles
|
-- Trigger Handles
|
||||||
minetest.register_on_dignode(function(pos, oldnode, digger)
|
minetest.register_on_dignode(function(pos, oldnode, digger)
|
||||||
|
if not digger or not pos or not oldnode then
|
||||||
|
return
|
||||||
|
end
|
||||||
local nodedug = string.split(oldnode.name, ":")
|
local nodedug = string.split(oldnode.name, ":")
|
||||||
|
if #nodedug ~= 2 then
|
||||||
|
print(oldnode.name.." is in wrong format!")
|
||||||
|
return
|
||||||
|
end
|
||||||
local mod=nodedug[1]
|
local mod=nodedug[1]
|
||||||
local item=nodedug[2]
|
local item=nodedug[2]
|
||||||
|
|
||||||
local playern = digger:get_player_name()
|
local playern = digger:get_player_name()
|
||||||
|
|
||||||
if (playern~=nil and nodedug~=nil and mod~=nil and item~=nil) then
|
if (not playern or not nodedug or not mod or not item) then
|
||||||
--check the player's directory
|
return
|
||||||
if not player_data[playern] then
|
|
||||||
player_data[playern]={}
|
|
||||||
player_data[playern]['name']=playern
|
|
||||||
end
|
|
||||||
if not player_data[playern]['count'] then
|
|
||||||
player_data[playern]['count']={}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--check player.count.mod
|
-- Run checks
|
||||||
if not player_data[playern].count[mod] then
|
awards.tbv(player_data, playern )
|
||||||
player_data[playern]['count'][mod]={}
|
awards.tbv(player_data[playern], "count" )
|
||||||
end
|
awards.tbv(player_data[playern], "name", playern )
|
||||||
|
awards.tbv(player_data[playern].count, mod)
|
||||||
|
awards.tbv(player_data[playern].count[mod], item, 0 )
|
||||||
|
|
||||||
--check player.count.mod.item
|
-- Increment counder
|
||||||
if not player_data[playern]['count'][mod][item] then
|
player_data[playern].count[mod][item]=player_data[playern].count[mod][item]+1
|
||||||
player_data[playern]['count'][mod][item]=0
|
print(" - "..mod..":"..item.." 's count is now "..(player_data[playern].count[mod][item]))
|
||||||
end
|
|
||||||
|
|
||||||
player_data[playern]['count'][mod][item]=player_data[playern]['count'][mod][item]+1
|
-- Run callbacks and triggers
|
||||||
|
|
||||||
print(" - "..mod..":"..item.." 's count is now "..(player_data[playern]['count'][mod][item]))
|
|
||||||
|
|
||||||
-- Roll through the onDig functions
|
|
||||||
local player=digger
|
local player=digger
|
||||||
local data=player_data[playern]
|
local data=player_data[playern]
|
||||||
|
|
||||||
for i=1,# awards.onDig do
|
for i=1,# awards.onDig do
|
||||||
local res=nil
|
local res = nil
|
||||||
|
|
||||||
if type(awards.onDig[i]) == "function" then
|
if type(awards.onDig[i]) == "function" then
|
||||||
-- run the function
|
-- Run trigger callback
|
||||||
print(i.." is a function")
|
print(i.." is a function")
|
||||||
res=awards.onDig[i](player,data)
|
res = awards.onDig[i](player,data)
|
||||||
elseif type(awards.onDig[i]) == "table" then
|
elseif type(awards.onDig[i]) == "table" then
|
||||||
-- handle table here
|
-- Handle table trigger
|
||||||
print(i.." is a table")
|
print(i.." is a table")
|
||||||
if not awards.onDig[i]['node'] or not awards.onDig[i]['target'] or not awards.onDig[i]['award'] then
|
if not awards.onDig[i].node or not awards.onDig[i].target or not awards.onDig[i].award then
|
||||||
-- table running failed!
|
-- table running failed!
|
||||||
|
print("onDig trigger "..i.." is invalid!")
|
||||||
else
|
else
|
||||||
-- run the table
|
-- run the table
|
||||||
local tnodedug = string.split(awards.onDig[i]['node'], ":")
|
local tnodedug = string.split(awards.onDig[i].node, ":")
|
||||||
|
|
||||||
local tmod=tnodedug[1]
|
local tmod=tnodedug[1]
|
||||||
local titem=tnodedug[2]
|
local titem=tnodedug[2]
|
||||||
|
if tmod==nil or titem==nil or not data.count[tmod] or not data.count[tmod][titem] then
|
||||||
if tmod==nil or titem==nil or not data['count'][tmod] or not data['count'][tmod][titem] then
|
|
||||||
-- table running failed!
|
-- table running failed!
|
||||||
elseif data['count'][tmod][titem] > awards.onDig[i]['target']-1 then
|
elseif data.count[tmod][titem] > awards.onDig[i].target-1 then
|
||||||
res=awards.onDig[i]['award']
|
res=awards.onDig[i].award
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if res~=nil then
|
if res then
|
||||||
awards.give_achievement(playern,res)
|
awards.give_achievement(playern,res)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_on_placenode(function(pos, newnode, placer)
|
minetest.register_on_placenode(function(pos,node,digger)
|
||||||
local nodedug = string.split(newnode.name, ":")
|
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
|
||||||
|
print(oldnode.name.." is in wrong format!")
|
||||||
|
return
|
||||||
|
end
|
||||||
local mod=nodedug[1]
|
local mod=nodedug[1]
|
||||||
local item=nodedug[2]
|
local item=nodedug[2]
|
||||||
|
local playern = digger:get_player_name()
|
||||||
|
|
||||||
local playern = placer:get_player_name()
|
-- Run checks
|
||||||
|
if (not playern or not nodedug or not mod or not item) then
|
||||||
if (playern~=nil and nodedug~=nil and mod~=nil and item~=nil) then
|
return
|
||||||
--check the player's directory
|
|
||||||
if not player_data[playern] then
|
|
||||||
player_data[playern]={}
|
|
||||||
player_data[playern].place=playern
|
|
||||||
end
|
|
||||||
if not player_data[playern].place then
|
|
||||||
player_data[playern]['place']={}
|
|
||||||
end
|
end
|
||||||
|
awards.tbv(player_data, playern )
|
||||||
|
awards.tbv(player_data[playern], "place" )
|
||||||
|
awards.tbv(player_data[playern], "name", playern )
|
||||||
|
awards.tbv(player_data[playern].place, mod)
|
||||||
|
awards.tbv(player_data[playern].place[mod], item, 0 )
|
||||||
|
|
||||||
--check player.count.mod
|
-- Increment counder
|
||||||
if not player_data[playern].place[mod] then
|
player_data[playern].place[mod][item] = player_data[playern].place[mod][item]+1
|
||||||
player_data[playern].place[mod]={}
|
print(" - "..mod..":"..item.." 's count is now "..(player_data[playern].place[mod][item]))
|
||||||
end
|
|
||||||
|
|
||||||
--check player.count.mod.item
|
|
||||||
if not player_data[playern].place[mod][item] then
|
|
||||||
player_data[playern].place[mod][item]=0
|
|
||||||
end
|
|
||||||
|
|
||||||
player_data[playern].place[mod][item]=player_data[playern].place[mod][item]+1
|
|
||||||
|
|
||||||
print(" - "..mod..":"..item.." 's place is now "..(player_data[playern].place[mod][item]))
|
|
||||||
|
|
||||||
-- Roll through the onDig functions
|
|
||||||
local player=placer
|
|
||||||
local data=player_data[playern]
|
|
||||||
|
|
||||||
|
-- Run callbacks and triggers
|
||||||
|
local player = digger
|
||||||
|
local data = player_data[playern]
|
||||||
for i=1,# awards.onPlace do
|
for i=1,# awards.onPlace do
|
||||||
local res=nil
|
local res = nil
|
||||||
|
|
||||||
if type(awards.onPlace[i]) == "function" then
|
if type(awards.onPlace[i]) == "function" then
|
||||||
-- run the function
|
-- Run trigger callback
|
||||||
print(i.." is a function")
|
print(i.." is a function")
|
||||||
res=awards.onPlace[i](player,data)
|
res = awards.onPlace[i](player,data)
|
||||||
elseif type(awards.onPlace[i]) == "table" then
|
elseif type(awards.onPlace[i]) == "table" then
|
||||||
-- handle table here
|
-- Handle table trigger
|
||||||
print(i.." is a table")
|
print(i.." is a table")
|
||||||
if not awards.onPlace[i]['node'] or not awards.onPlace[i].target or not awards.onPlace[i].award then
|
if not awards.onPlace[i].node or not awards.onPlace[i].target or not awards.onPlace[i].award then
|
||||||
-- table running failed!
|
-- table running failed!
|
||||||
|
print("onPlace trigger "..i.." is invalid!")
|
||||||
else
|
else
|
||||||
-- run the table
|
-- run the table
|
||||||
local tnodedug = string.split(awards.onPlace[i]['node'], ":")
|
local tnodedug = string.split(awards.onPlace[i].node, ":")
|
||||||
|
local tmod = tnodedug[1]
|
||||||
local tmod=tnodedug[1]
|
local titem = tnodedug[2]
|
||||||
local titem=tnodedug[2]
|
|
||||||
|
|
||||||
if tmod==nil or titem==nil or not data.place[tmod] or not data.place[tmod][titem] then
|
if tmod==nil or titem==nil or not data.place[tmod] or not data.place[tmod][titem] then
|
||||||
-- table running failed!
|
-- table running failed!
|
||||||
elseif data['place'][tmod][titem] > awards.onPlace[i]['target']-1 then
|
elseif data.place[tmod][titem] > awards.onPlace[i].target-1 then
|
||||||
res=awards.onPlace[i]['award']
|
res = awards.onPlace[i].award
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if res~=nil then
|
if res then
|
||||||
awards.give_achievement(playern,res)
|
awards.give_achievement(playern,res)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_on_dieplayer(function(player)
|
minetest.register_on_dieplayer(function(player)
|
||||||
player_data[player:get_player_name()]['deaths']=player_data[player:get_player_name()]['deaths']+1
|
-- Run checks
|
||||||
|
if not player or not player:get_player_name() or player:get_player_name()=="" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
awards.tbv(player_data, playern )
|
||||||
|
awards.tbv(player_data[playern], "name", playern )
|
||||||
|
awards.tbv(player_data[playern], "deaths", 0 )
|
||||||
|
|
||||||
-- Set up the variables
|
-- Increment counter
|
||||||
|
player_data[player:get_player_name()].deaths = player_data[player:get_player_name()].deaths + 1
|
||||||
|
|
||||||
|
-- Run callbacks and triggers
|
||||||
local playern=player:get_player_name()
|
local playern=player:get_player_name()
|
||||||
local data=player_data[playern]
|
local data=player_data[playern]
|
||||||
|
|
||||||
-- Roll through the onDeath functions
|
|
||||||
for i=1,# awards.onDeath do
|
for i=1,# awards.onDeath do
|
||||||
local res=nil
|
local res=nil
|
||||||
if type(awards.onDeath[i]) == "function" then
|
if type(awards.onDeath[i]) == "function" then
|
||||||
-- run the function
|
-- Run trigger callback
|
||||||
print(i.." is a function")
|
print(i.." is a function")
|
||||||
res=awards.onDeath[i](player,data)
|
res=awards.onDeath[i](player,data)
|
||||||
elseif type(awards.onDeath[i]) == "table" then
|
elseif type(awards.onDeath[i]) == "table" then
|
||||||
-- handle table here
|
-- handle table here
|
||||||
print(i.." is a table")
|
print(i.." is a table")
|
||||||
if not awards.onDeath[i]['target'] or not awards.onDeath[i]['award'] then
|
if not awards.onDeath[i].target or not awards.onDeath[i].award then
|
||||||
-- table running failed!
|
-- table running failed!
|
||||||
|
print("onDeath trigger "..i.." is invalid!")
|
||||||
else
|
else
|
||||||
-- run the table
|
-- run the table
|
||||||
|
if not data.deaths then
|
||||||
if not data['deaths'] then
|
|
||||||
-- table running failed!
|
-- table running failed!
|
||||||
elseif data['deaths'] > awards.onDeath[i]['target']-1 then
|
elseif data.deaths > awards.onDeath[i].target-1 then
|
||||||
res=awards.onDeath[i]['award']
|
res=awards.onDeath[i].award
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -190,17 +179,12 @@ minetest.register_on_dieplayer(function(player)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_on_newplayer(function(player)
|
minetest.register_on_newplayer(function(player)
|
||||||
--Player data root
|
awards.tbv(player_data, player:get_player_name())
|
||||||
player_data[player:get_player_name()]={}
|
awards.tbv(player_data[playern], "name", player:get_player_name())
|
||||||
player_data[player:get_player_name()]['name']=player:get_player_name()
|
awards.tbv(player_data[playern], "unlocked")
|
||||||
player_data[player:get_player_name()]['deaths']=0
|
awards.tbv(player_data[playern], "place")
|
||||||
|
awards.tbv(player_data[playern], "count")
|
||||||
--The player counter
|
awards.tbv(player_data[playern], "deaths", 0)
|
||||||
player_data[player:get_player_name()]['count']={}
|
|
||||||
player_data[player:get_player_name()]['place']={}
|
|
||||||
|
|
||||||
--Table to contain achievement records
|
|
||||||
player_data[player:get_player_name()]['unlocked']={}
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_on_shutdown(function()
|
minetest.register_on_shutdown(function()
|
||||||
|
Loading…
Reference in New Issue
Block a user