generated from your-land/yl_template
233 lines
7.1 KiB
Lua
233 lines
7.1 KiB
Lua
-- The functions and variables in this file are only for use in the mod itself.
|
|
-- Those that do real work should be local and wrapped in public functions
|
|
local debug = yl_announcements.settings.debug or true
|
|
|
|
local function say(text)
|
|
if debug then minetest.log("action", "[MOD] yl_announcements : " .. text) end
|
|
end
|
|
|
|
-- Storage
|
|
|
|
local function get_savepath()
|
|
local save_path = yl_announcements.settings.save_path
|
|
local path = yl_announcements.worldpath .. DIR_DELIM .. save_path
|
|
say("get_savepath : " .. dump(path))
|
|
return path
|
|
end
|
|
|
|
local function get_filepath(filename)
|
|
local path_to_file = get_savepath() .. DIR_DELIM ..
|
|
filename .. ".json"
|
|
say("get_filepath : " .. dump(filename) .. ":" .. dump(path_to_file))
|
|
return path_to_file
|
|
end
|
|
|
|
local function save_json(filename, content)
|
|
if type(filename) ~= "string" or type(content) ~= "table" then return false end
|
|
local save_path = get_filepath(filename)
|
|
local save_content = minetest.write_json(content)
|
|
say("save_json : " .. dump(save_path) .. ":" .. dump(save_content))
|
|
return minetest.safe_file_write(save_path, save_content)
|
|
end
|
|
|
|
local function load_json(path_to_file)
|
|
local file = io.open(path_to_file, "r")
|
|
if not file then return false, "Error opening file: " .. path_to_file end
|
|
|
|
local content = file:read("*all")
|
|
file:close()
|
|
|
|
if not content then return false, "Error reading file: " .. path_to_file end
|
|
say("load_json : " .. dump(path_to_file) .. ":" .. dump(content))
|
|
return true, minetest.parse_json(content)
|
|
end
|
|
|
|
-- Public functions wrap the private ones, so they can be exchanged easily
|
|
function yl_announcements.load_json(filename, ...) return load_json(filename, ...) end
|
|
|
|
function yl_announcements.save_json(filename, content, ...)
|
|
return save_json(filename, content, ...)
|
|
end
|
|
|
|
-- load_all_data
|
|
--
|
|
|
|
local function is_visible(filename) return (string.sub(filename, 1, 1) ~= ".") end
|
|
|
|
local function is_json(filename) return (filename:match("%.json$")) end
|
|
|
|
local function load_all_data()
|
|
-- Get all json files from savepath
|
|
-- Excluding invisible
|
|
-- Excluding non-json files
|
|
local save_path = get_savepath()
|
|
local files = minetest.get_dir_list(save_path, false) or {}
|
|
local data = {}
|
|
local total = 0
|
|
local good = 0
|
|
local bad = 0
|
|
for key, filename in ipairs(files) do
|
|
if is_visible(filename) and is_json(filename) then
|
|
total = total + 1
|
|
local filepath = get_filepath(filename)
|
|
local success, content = load_json(filepath)
|
|
|
|
if success and content.id then
|
|
good = good + 1
|
|
data[content.id] = content
|
|
else
|
|
bad = bad + 1
|
|
end
|
|
|
|
end
|
|
end
|
|
|
|
yl_announcements.data = data
|
|
|
|
if bad == 0 then
|
|
minetest.log("action",
|
|
"[MOD] yl_announcements : bad = " .. tostring(bad) ..
|
|
", good = " .. tostring(good) .. ", total = " ..
|
|
tostring(total))
|
|
return true, good, bad
|
|
else
|
|
minetest.log("warning",
|
|
"[MOD] yl_announcements : bad = " .. tostring(bad) ..
|
|
", good = " .. tostring(good) .. ", total = " ..
|
|
tostring(total))
|
|
return false, good, bad
|
|
end
|
|
end
|
|
|
|
function yl_announcements.load_all_data() return load_all_data() end
|
|
|
|
-- check privs
|
|
--
|
|
|
|
local function ends_with(str, suffix) return str:sub(-suffix:len()) == suffix end
|
|
|
|
local function split(str)
|
|
local parts = {}
|
|
for part in str:gmatch("[^,%s]+") do table.insert(parts, part) end
|
|
return parts
|
|
end
|
|
|
|
local function priv_exists(priv)
|
|
return (minetest.registered_privileges[priv] ~= nil) or false
|
|
end
|
|
|
|
function check_privs()
|
|
for key, value in pairs(yl_announcements.settings) do
|
|
if ends_with(key, "_privs") then
|
|
local parts = split(value)
|
|
for _, part in ipairs(parts) do
|
|
assert(priv_exists(part), "yl_announcements : configured priv " ..
|
|
dump(part) .. " does not exist.")
|
|
end
|
|
end
|
|
end
|
|
say("PASS priv check")
|
|
end
|
|
|
|
function yl_announcements.check_privs() return check_privs() end
|
|
|
|
-- Remove file
|
|
--
|
|
|
|
local function remove_file(filename)
|
|
local filepath = get_filepath(filename)
|
|
return os.remove(filepath)
|
|
end
|
|
|
|
function yl_announcements.remove_file(filename) return remove_file(filename) end
|
|
|
|
-- Chatcommands
|
|
--
|
|
|
|
function yl_announcements.chatcommand_announcement_add(name, param) -- param is a string containing a message and more
|
|
-- defense
|
|
local player = minetest.get_player_by_name(name)
|
|
if not player then
|
|
return false, "Player not online"
|
|
end
|
|
if (not param) or (type(param) ~= "string") or (param == "") then
|
|
return false, "Requirements not met"
|
|
end
|
|
-- Create announcement
|
|
local announcement = string.split(param, "$", true)
|
|
core.log("action","announcement="..dump(announcement))
|
|
local success, data, a_id = yl_announcements.add(name,announcement)
|
|
-- Save
|
|
if success == false then
|
|
return false, data
|
|
else
|
|
yl_announcements.save_announcements(data)
|
|
return true, "Add announcement of ID " .. a_id
|
|
end
|
|
end
|
|
|
|
function yl_announcements.chatcommand_announcement_copy(name, param) -- param is a numerical a_id
|
|
-- defense
|
|
local player = minetest.get_player_by_name(name)
|
|
if not player then
|
|
return false, "Player not online"
|
|
end
|
|
if param == "" then
|
|
return false, "Announcement ID missing"
|
|
end
|
|
local n_param = tonumber(param)
|
|
if type(n_param) ~= "number" then
|
|
return false, "Announcement ID not a number"
|
|
end
|
|
local announcement = yl_announcements.get_announcement(n_param)
|
|
if announcement == yl_announcements.error then
|
|
return false, "Cannot find announcement with given ID"
|
|
end
|
|
-- Send the formspec
|
|
local formspec = yl_announcements.copy(a_id)
|
|
minetest.show_formspec(name, "yl_announcements:copy", formspec)
|
|
-- Report
|
|
return true, "Copied announcement ID " .. n_param
|
|
end
|
|
|
|
function yl_announcements.chatcommand_announcement_delete(name, param) -- param is a numerical a_id
|
|
-- defense
|
|
local player = minetest.get_player_by_name(name)
|
|
if not player then
|
|
return false, "Player not online"
|
|
end
|
|
if param == "" then
|
|
return false, "Announcement ID missing"
|
|
end
|
|
local n_param = tonumber(param)
|
|
if type(n_param) ~= "number" then
|
|
return false, "Announcement ID not a number"
|
|
end
|
|
local announcement = yl_announcements.get_announcement(n_param)
|
|
if announcement == yl_announcements.error then
|
|
return false, "Cannot find announcement with given ID"
|
|
end
|
|
-- Delete the entry
|
|
local success, data = yl_announcements.delete(n_param)
|
|
-- Save
|
|
if success == false then
|
|
return false, data
|
|
else
|
|
yl_announcements.save_announcements(data)
|
|
return true, "Deleted announcement ID " .. n_param
|
|
end
|
|
end
|
|
|
|
function yl_announcements.chatcommand_announcement_list_all(name, param) -- param must be empty
|
|
end
|
|
|
|
function yl_announcements.chatcommand_announcement_list(name, param) -- param is a numerical a_id
|
|
|
|
end
|
|
|
|
function yl_announcements.chatcommand_announcement_say_all(name, param) -- param must be empty
|
|
end
|
|
|
|
function yl_announcements.chatcommand_announcement_say(name, param) -- param is a numerical a_id
|
|
end
|