Move helpers and chat commands to separate files

This commit is contained in:
rubenwardy 2015-06-10 17:45:18 +01:00
parent 85a40d2488
commit 85efc67049
4 changed files with 128 additions and 148 deletions

129
api.lua
View File

@ -18,6 +18,11 @@ function awards.save()
end end
end end
function awards.init()
awards.players = awards.load()
awards.def = {}
end
function awards.load() function awards.load()
local file = io.open(minetest.get_worldpath().."/awards.txt", "r") local file = io.open(minetest.get_worldpath().."/awards.txt", "r")
if file then if file then
@ -29,77 +34,15 @@ function awards.load()
return {} return {}
end end
awards.players = awards.load() awards.init()
function awards.player(name)
return awards.players[name]
end
-- A table of award definitions
awards.def = {}
function awards.tbv(tb,value,default)
if not default then
default = {}
end
if not tb or type(tb) ~= "table" then
if not value then
value = "[NULL]"
end
minetest.log("error", "awards.tbv - table "..dump(value).." is null, or not a table! Dump: "..dump(tb))
return
end
if not value then
error("[ERROR] awards.tbv was not used correctly!\n"..
"Value: '"..dump(value).."'\n"..
"Dump:"..dump(tb))
return
end
if not tb[value] then
tb[value] = default
end
end
function awards.assertPlayer(playern)
awards.tbv(awards.players, playern)
awards.tbv(awards.players[playern], "name", playern)
awards.tbv(awards.players[playern], "unlocked")
awards.tbv(awards.players[playern], "place")
awards.tbv(awards.players[playern], "count")
awards.tbv(awards.players[playern], "craft")
awards.tbv(awards.players[playern], "deaths", 0)
awards.tbv(awards.players[playern], "joins", 0)
awards.tbv(awards.players[playern], "chats", 0)
end
-- Load files -- Load files
dofile(minetest.get_modpath("awards").."/helpers.lua")
dofile(minetest.get_modpath("awards").."/triggers.lua") dofile(minetest.get_modpath("awards").."/triggers.lua")
-- API Functions -- API Functions
function awards._additional_triggers(name, data_table) function awards._additional_triggers(name, data_table)
-- To add triggers in another mod, you should override this function -- Depreciated!
-- If the code can't handle the trigger passed, then call the last value of _additional_triggers
--[[
local add_trig = awards._additional_triggers
awards._additional_triggers = function(name, data_table)
if data_table.trigger.type == "trigger" then
local tmp = {
award = name,
node = data_table.trigger.node,
target = data_table.trigger.target,
}
table.insert(awards.onTrigger,tmp)
elseif data_table.trigger.type == "trigger2" then
local tmp = {
award = name,
node = data_table.trigger.node,
target = data_table.trigger.target,
}
table.insert(awards.onTrigger2,tmp)
else
add_trig(name, data_table)
end
end
]]--
end end
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
@ -326,41 +269,6 @@ function awards.give_achievement(name, award)
end end
end end
-- List a player's achievements
minetest.register_chatcommand("list_awards", {
params = "obsolete",
description = "list_awards: obsolete. Use /awards",
func = function(name, param)
minetest.chat_send_player(name, "This command has been made obsolete. Use /awards instead.")
awards.showto(name, name, nil, false)
end
})
minetest.register_chatcommand("awards", {
params = "",
description = "awards: list awards",
func = function(name, param)
awards.showto(name, name, nil, false)
end
})
minetest.register_chatcommand("cawards", {
params = "",
description = "awards: list awards in chat",
func = function(name, param)
awards.showto(name, name, nil, true)
end
})
minetest.register_chatcommand("awd", {
params = "award name",
description = "awd: Details of awd gotten",
func = function(name, param)
local def = awards.def[param]
if def then
minetest.chat_send_player(name,def.title..": "..def.description)
else
minetest.chat_send_player(name,"Award not found.")
end
end
})
--[[minetest.register_chatcommand("gawd", { --[[minetest.register_chatcommand("gawd", {
params = "award name", params = "award name",
description = "gawd: give award to self", description = "gawd: give award to self",
@ -369,26 +277,6 @@ minetest.register_chatcommand("awd", {
end end
})]]-- })]]--
function awards._order_awards(name)
local done = {}
local retval = {}
local player = awards.player(name)
if player and player.unlocked then
for _,got in pairs(player.unlocked) do
if awards.def[got] then
done[got] = true
table.insert(retval,{name=got,got=true})
end
end
end
for _,def in pairs(awards.def) do
if not done[def.name] then
table.insert(retval,{name=def.name,got=false})
end
end
return retval
end
function awards.showto(name, to, sid, text) function awards.showto(name, to, sid, text)
if name == "" or name == nil then if name == "" or name == nil then
name = to name = to
@ -502,4 +390,3 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return true return true
end) end)

37
chat_commands.lua Normal file
View File

@ -0,0 +1,37 @@
minetest.register_chatcommand("list_awards", {
params = "obsolete",
description = "list_awards: obsolete. Use /awards",
func = function(name, param)
minetest.chat_send_player(name, "This command has been made obsolete. Use /awards instead.")
awards.showto(name, name, nil, false)
end
})
minetest.register_chatcommand("awards", {
params = "",
description = "awards: list awards",
func = function(name, param)
awards.showto(name, name, nil, false)
end
})
minetest.register_chatcommand("cawards", {
params = "",
description = "awards: list awards in chat",
func = function(name, param)
awards.showto(name, name, nil, true)
end
})
minetest.register_chatcommand("awd", {
params = "award name",
description = "awd: Details of awd gotten",
func = function(name, param)
local def = awards.def[param]
if def then
minetest.chat_send_player(name,def.title..": "..def.description)
else
minetest.chat_send_player(name,"Award not found.")
end
end
})

56
helpers.lua Normal file
View File

@ -0,0 +1,56 @@
function awards.tbv(tb,value,default)
if not default then
default = {}
end
if not tb or type(tb) ~= "table" then
if not value then
value = "[NULL]"
end
minetest.log("error", "awards.tbv - table "..dump(value).." is null, or not a table! Dump: "..dump(tb))
return
end
if not value then
error("[ERROR] awards.tbv was not used correctly!\n"..
"Value: '"..dump(value).."'\n"..
"Dump:"..dump(tb))
return
end
if not tb[value] then
tb[value] = default
end
end
function awards.assertPlayer(playern)
awards.tbv(awards.players, playern)
awards.tbv(awards.players[playern], "name", playern)
awards.tbv(awards.players[playern], "unlocked")
awards.tbv(awards.players[playern], "place")
awards.tbv(awards.players[playern], "count")
awards.tbv(awards.players[playern], "deaths", 0)
awards.tbv(awards.players[playern], "joins", 0)
awards.tbv(awards.players[playern], "chats", 0)
end
function awards.player(name)
return awards.players[name]
end
function awards._order_awards(name)
local done = {}
local retval = {}
local player = awards.player(name)
if player and player.unlocked then
for _,got in pairs(player.unlocked) do
if awards.def[got] then
done[got] = true
table.insert(retval,{name=got,got=true})
end
end
end
for _,def in pairs(awards.def) do
if not done[def.name] then
table.insert(retval,{name=def.name,got=false})
end
end
return retval
end

View File

@ -13,6 +13,7 @@ else
end end
dofile(minetest.get_modpath("awards").."/api.lua") dofile(minetest.get_modpath("awards").."/api.lua")
dofile(minetest.get_modpath("awards").."/chat_commands.lua")
-- Light it up -- Light it up
awards.register_achievement("award_lightitup",{ awards.register_achievement("award_lightitup",{
@ -370,4 +371,3 @@ awards.register_onDeath(function(player,data)
end end
return nil return nil
end) end)