From feb572005106e5301f9c084acd33f2c6fad4f3e1 Mon Sep 17 00:00:00 2001 From: AliasAlreadyTaken Date: Thu, 12 Sep 2024 05:20:09 +0200 Subject: [PATCH] Implements get_data and get_record --- README.md | 4 ++-- api.lua | 51 +++++++++++++++++++----------------------------- internal.lua | 26 ++++++++++++++++++------ settingtypes.txt | 6 +++--- 4 files changed, 45 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 81576f2..879b683 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ A `s_id` (survey_id) is a UUID, a `q_id` is a positive integer. An `owner` is th Use the following public functions to get, set, list, remove and evaluate surveys ``` -yl_survey.get_survey(s_id) +yl_survey.get_record(s_id) ``` Returns a table `{survey}` with the values of this UUID or `nil`, if it does not exist @@ -87,7 +87,7 @@ yl_survey.remove_survey(s_id) Returns `true, {survey}` if the survey was successfully removed, `false, "errormessage"` otherwise. ``` -yl_survey.list_surveys() +yl_survey.get_data() ``` Returns `true, {"UUID1", "UUID2", ...}` if one or more surveys were found, `false, {}` if none were found. diff --git a/api.lua b/api.lua index 18d9525..30a09de 100644 --- a/api.lua +++ b/api.lua @@ -1,34 +1,23 @@ --- Use this file for functions that can be called from other mods --- Make sure the functions are well defended against wrong input and --- document them on the readme, what they do, what types and values --- they expect as parameters and what types and values they return. --- If you ever change those, consider adding backwards compatibility, --- since other mods may rely on them. -function yl_survey.some_api_call(target, message, color) +-- yl_survey.get_data() +-- - if (type(target) ~= "string") then - return false, yl_survey.t("error_not_a_string", "target") +local function get_data() + if next(yl_survey.data) then + return true, yl_survey.data + else + return false, {} end - if (minetest.get_player_by_name(target) == nil) then - return false, yl_survey.t("error_player_not_online", target) - end - - if (type(message) ~= "string") then - return false, yl_survey.t("error_not_a_string", "message") - end - - -- is_color(color) does not exist, you need to implement it if you want to use it - if (is_color(color) == false) then - return false, yl_survey.t("error_not_a_colorspec", "color") - end - - if (minetest.colorize == nil) then - return false, yl_survey.t("error_function_not_available", - "minetest.colorize") - end - - local message_with_color = minetest.colorize(color, message) - minetest.chat_send_player(target, message_with_color) - - return true, yl_survey.t("api_sent_x_to_y", message_with_color, target) end + +function yl_survey.get_data() return get_data() end + +-- yl_survey.get_survey(UUID) +-- + +local function get_record(id) + local success, data = yl_survey.get_data() + if (success == false) then return nil end + return data[id] -- {data} or nil +end + +function yl_survey.get_record(id) return get_record(id) end diff --git a/internal.lua b/internal.lua index 764655a..6ffd353 100644 --- a/internal.lua +++ b/internal.lua @@ -16,11 +16,25 @@ local function is_visible(filename) return (string.sub(filename, 1, 1) ~= ".") e local function is_json(filename) return (filename:match("%.json$")) end +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 yl_survey.priv_exists(priv) return priv_exists(priv) 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))) + log(savepath or "") return savepath end @@ -63,7 +77,7 @@ function yl_survey.save(filename, content, ...) return save_json(filename, content, ...) end --- Load all surveys +-- Load all data -- local function load_all_data() @@ -85,7 +99,7 @@ local function load_all_data() if success and (content.id == UUID) then good = good + 1 - table.insert(data, content) + data[UUID] = content else bad = bad + 1 end @@ -108,7 +122,7 @@ end function yl_survey.load_all_data() return load_all_data() end --- Chekc privs +-- Check privs -- local function check_privs(settings) @@ -122,7 +136,7 @@ local function check_privs(settings) end end end - say("PASS priv check") + log("PASS priv check") end -function yl_survey.check_privs() return check_privs() end +function yl_survey.check_privs(settings) return check_privs(settings) end diff --git a/settingtypes.txt b/settingtypes.txt index 8202508..d7722c6 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -13,14 +13,14 @@ yl_survey.save_path (Save path) string yl_survey # Survey edit privs # Enable holders of these privs to edit surveys # Optional, default: server, no setting disables the feature -yl_scheduler.surveyedit_privs (Survey edit privs) string server +yl_survey.surveyedit_privs (Survey edit privs) string server # Survey answer privs # Enable holders of these privs to answer surveys # Optional, default: server, no setting disables the feature -yl_scheduler.surveyanswer_privs (Survey answer privs) string interact +yl_survey.surveyanswer_privs (Survey answer privs) string interact # Survey evaluate privs # Enable holders of these privs to evaluate surveys # Optional, default: server, no setting disables the feature -yl_scheduler.surveyevaluate_privs (Survey evaluate privs) string server +yl_survey.surveyevaluate_privs (Survey evaluate privs) string server