Restructure for bulletproof-ness

This commit is contained in:
Rubenwardy
2013-11-06 18:33:01 +00:00
parent 076f91853c
commit 773f799320
2 changed files with 181 additions and 171 deletions

104
api.lua
View File

@ -31,6 +31,19 @@ player_data=load_playerD()
-- A table of award definitions
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
dofile(minetest.get_modpath("awards").."/triggers.lua")
dofile(minetest.get_modpath("awards").."/config.txt")
@ -38,43 +51,43 @@ dofile(minetest.get_modpath("awards").."/config.txt")
-- API Functions
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
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'],
node=data_table.trigger.node,
target=data_table.trigger.target,
}
table.insert(awards.onDig,tmp)
elseif data_table['trigger']['type']=="place" then
elseif data_table.trigger.type=="place" then
local tmp={
award=name,
node=data_table['trigger']['node'],
target=data_table['trigger']['target'],
node=data_table.trigger.node,
target=data_table.trigger.target,
}
table.insert(awards.onPlace,tmp)
elseif data_table['trigger']['type']=="death" then
elseif data_table.trigger.type=="death" then
local tmp={
award=name,
target=data_table['trigger']['target'],
target=data_table.trigger.target,
}
table.insert(awards.onDeath,tmp)
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 data_table.icon == nil or data_table.icon == "" then
data_table.icon = "unknown.png"
end
if data_table['background'] == nil or data_table['background'] == "" then
data_table['background'] = "bg_default.png"
if data_table.background == nil or data_table.background == "" then
data_table.background = "bg_default.png"
end
if data_table['custom_announce'] == nil or data_table['custom_announce'] == "" then
data_table['custom_announce'] = "Achievement Unlocked:"
if data_table.custom_announce == nil or data_table.custom_announce == "" then
data_table.custom_announce = "Achievement Unlocked:"
end
-- add the achievement to the definition table
awards['def'][name] = data_table
awards.def[name] = data_table
end
-- this function adds a trigger function or table to the ondig table
@ -100,34 +113,43 @@ end
-- name - the name of the player
-- award - the name of the award to give
function awards.give_achievement(name,award)
-- load the player's data table
-- Access Player Data
local data=player_data[name]
-- check if the table that holds a player's achievements exists
if not data['unlocked'] then
data['unlocked']={}
end
-- check to see if the player does not already have that achievement
if not data['unlocked'][award] or data['unlocked'][award]~=award then
-- save the achievement to the player_data table
data['unlocked'][award]=award
-- Perform checks
if not data then
return
end
if not awards.def[award] then
return
end
awards.tbv(data,"unlocked")
-- define local variables, so award data can be saved
-- check to see if the player does not already have that achievement
if not data.unlocked[award] or data.unlocked[award]~=award then
-- Set award flag
data.unlocked[award]=award
-- Get data from definition tables
local title = award
local desc = ""
-- check definition table to get values
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
title=awards['def'][award]['title']
background=awards['def'][award]['background']
icon=awards['def'][award]['icon']
custom_announce=awards['def'][award]['custom_announce']
local background = ""
local icon = ""
local custom_announce = ""
if awards.def[award].title then
title = awards.def[award].title
end
-- check definition table to get description
if awards['def'][award] and awards['def'][award]['description'] then
desc=awards['def'][award]['description']
if awards.def[award].custom_announce then
custom_announce = awards.def[award].custom_announce
end
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
-- send the won award message to the player
@ -159,7 +181,11 @@ minetest.register_chatcommand("list_awards", {
params = "",
description = "list_awards: list your awards",
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
minetest.chat_send_player(name, str);