Adds settings and data import

This commit is contained in:
AliasAlreadyTaken 2024-09-09 06:34:34 +02:00
parent 146a42052d
commit 9802fa293a
8 changed files with 88 additions and 100 deletions

View File

@ -1,2 +1,2 @@
dofile(yl_survey.modpath .. "chatcommand_admin.lua")
dofile(yl_survey.modpath .. "chatcommand_player.lua")
-- dofile(yl_survey.modpath .. "chatcommand_admin.lua")
-- dofile(yl_survey.modpath .. "chatcommand_player.lua")

View File

@ -1,13 +0,0 @@
local timer = 0
local gs = function(dtime)
timer = timer + dtime
if timer <= yl_survey.config.interval then
return
end
timer = 0
-- do stuff
end
minetest.register_globalstep(gs)

View File

@ -18,14 +18,12 @@ dofile(yl_survey.modpath .. "texts.lua")
dofile(yl_survey.modpath .. "information.lua")
dofile(yl_survey.modpath .. "config.lua")
dofile(yl_survey.modpath .. "setup.lua")
dofile(yl_survey.modpath .. "privs.lua")
-- dofile(yl_survey.modpath .. "privs.lua")
dofile(yl_survey.modpath .. "internal.lua")
dofile(yl_survey.modpath .. "api.lua")
dofile(yl_survey.modpath .. "initialize.lua")
dofile(yl_survey.modpath .. "distinct_feature.lua")
dofile(yl_survey.modpath .. "overwrite_feature.lua")
dofile(yl_survey.modpath .. "globalsteps.lua")
dofile(yl_survey.modpath .. "chatcommands.lua")
-- dofile(yl_survey.modpath .. "globalsteps.lua")
-- dofile(yl_survey.modpath .. "chatcommands.lua")
local mod_end_time = (minetest.get_us_time() - mod_start_time) / 1000000
minetest.log("action", "[MOD] yl_survey loaded in [" .. mod_end_time .. "s]")

View File

@ -2,8 +2,8 @@
local function run_each_serverstart()
yl_survey.data = {}
--minetest.on_mods_loaded(0.0, yl_survey.on_mods_loaded)
--minetest.after(0.0, yl_survey.after)
minetest.after(0.0, yl_survey.load_all_data)
minetest.after(0.0, yl_survey.check_privs, yl_survey.settings)
end
run_each_serverstart()

View File

@ -8,6 +8,16 @@ end
function yl_survey.log(text) return log(text) end
-- Helpers
local function filename2uuid(filename) return filename:sub(1, -6) end
local function is_visible(filename) return (string.sub(filename, 1, 1) ~= ".") end
local function is_json(filename) return (filename:match("%.json$")) end
-- Load and save
local function get_savepath()
local savepath = yl_survey.worldpath .. yl_survey.settings.save_path
log(yl_survey.t("log_prefix", dump(savepath)))
@ -52,3 +62,67 @@ function yl_survey.load(filename, ...) return load_json(filename, ...) end
function yl_survey.save(filename, content, ...)
return save_json(filename, content, ...)
end
-- Load all surveys
--
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 UUID = filename2uuid(filename)
local filepath = get_filepath(UUID)
local success, content = load_json(filepath)
if success and (content.id == UUID) then
good = good + 1
table.insert(data, content)
else
bad = bad + 1
end
end
end
yl_survey.data = data
local message = "[MOD] %s : bad = %s, good = %s, total = %s"
local formatted = string.format(message, yl_survey.information.name, tostring(bad), tostring(good), tostring(total))
if bad == 0 then
minetest.log("action", formatted)
return true, good, bad
else
minetest.log("warning", formatted)
return false, good, bad
end
end
function yl_survey.load_all_data() return load_all_data() end
-- Chekc privs
--
local function check_privs(settings)
for key, value in pairs(settings) do
if ends_with(key, "_privs") then
local parts = split(value)
for _, part in ipairs(parts) do
local message = "[MOD] %s : configured priv %s does not exist."
local formatted = string.format(message, yl_survey.information.name, dump(part))
assert(priv_exists(part), formatted)
end
end
end
say("PASS priv check")
end
function yl_survey.check_privs() return check_privs() end

View File

@ -1,76 +0,0 @@
local priv_name = "example"
local priv_definition = {
description = yl_survey.t("privs_example_description"),
-- Privilege description
give_to_singleplayer = false,
-- Whether to grant the privilege to singleplayer.
give_to_admin = true,
-- Whether to grant the privilege to the server admin.
-- Uses value of 'give_to_singleplayer' by default.
on_grant = function(name, granter_name)
-- logging
if (type(granter_name) ~= "string") then
local errormessage = yl_survey.t("error_name_not_found",
dump(granter_name))
yl_survey.log(errormessage)
return false, errormessage
end
if not name then
local errormessage = yl_survey.t("error_name_not_found",
dump(name))
yl_survey.log(errormessage)
return false, errormessage
end
if not priv_name then
local errormessage = yl_survey.t("error_priv_not_found",
dump(priv_name))
yl_survey.log(errormessage)
return false, errormessage
end
local text = yl_survey.t("privs_example_grant_logmessage",
dump(granter_name), dump(priv_name),
dump(name))
yl_survey.log(text)
end,
-- Called when given to player 'name' by 'granter_name'.
-- 'granter_name' will be nil if the priv was granted by a mod.
on_revoke = function(name, revoker_name)
-- logging
if (type(revoker_name) ~= "string") then
local errormessage = yl_survey.t("error_name_not_found",
dump(revoker_name))
yl_survey.log(errormessage)
return false, errormessage
end
if not name then
local errormessage = yl_survey.t("error_name_not_found",
dump(name))
yl_survey.log(errormessage)
return false, errormessage
end
if not priv_name then
local errormessage = yl_survey.t("error_priv_not_found",
dump(priv_name))
yl_survey.log(errormessage)
return false, errormessage
end
local text = yl_survey.t("privs_example_revoke_logmessage",
dump(revoker_name), dump(priv_name),
dump(name))
yl_survey.log(text)
end
-- Called when taken from player 'name' by 'revoker_name'.
-- 'revoker_name' will be nil if the priv was revoked by a mod.
-- Note that the above two callbacks will be called twice if a player is
-- responsible, once with the player name, and then with a nil player
-- name.
-- Return true in the above callbacks to stop register_on_priv_grant or
-- revoke being called.
}
minetest.register_privilege(priv_name, priv_definition)

View File

@ -1 +0,0 @@
dofile(yl_survey.modpath .. "priv_example.lua")

View File

@ -2,7 +2,13 @@ local S = minetest.get_translator(yl_survey.modname)
local texts = {}
function yl_survey.t(key, ...) return S(texts[key], ...) or "" end
function yl_survey.t(key, ...)
if (texts[key] == nil) then
minetest.log("warning","key " .. (key or "") .. " does not exist")
return key
end
return S(texts[key], ...) or ""
end
-- Fixed texts