Implements get_copy_of_valid_stage

This commit is contained in:
AliasAlreadyTaken 2024-07-03 23:42:55 +02:00
parent 1caa1157e7
commit d343938b09

View File

@ -1,7 +1,4 @@
local bucket = 0
local numz = 0
-- 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 function log(text)
@ -77,8 +74,6 @@ end
local function on_timer(pos, elapsed)
core.log("action","MEOW3")
local t1 = core.get_us_time()
numz = numz +1
@ -138,54 +133,79 @@ end
function yl_api_nodestages.on_timer(pos, elapsed) return on_timer(pos, elapsed) end
-- Validation
--
function get_copy_of_valid_stage(stage)
if (type(stage) ~= "table") then
return false, t("stage_not_table")
end
local stage = table.copy(stage)
if (yl_api_nodestages.is_valid_stage(stage) == false) then
return false, t("stage_not_valid")
end
local nodename = stage.stage_name
local overwrite = stage.overwrite
if (((overwrite == false) or (overwrite == nil)) and
(minetest.registered_nodes[nodename] ~= nil)) then
-- No overwrite, but item exists
return false, yl_api_nodestages.t("error_item_exists", dump(nodename))
end
return true, stage
end
function yl_api_nodestages.get_copy_of_valid_stage(stage)
return get_copy_of_valid_stage(stage)
end
local function is_valid_stage(stage)
end
function yl_api_nodestages.is_valid(stages)
-- TODO: Implement stages validation once we know how stages look like
return true
end
-- Preparation for random duration
function calc_duration(duration)
local good, bad, total = 0,0,0
local reasons = {}
assert(duration ~= nil, "TODO: ERROR")
local d_min, d_max
-- table
if type(duration) == "table" then
assert(((#duration >= 1) and (#duration <= 2)), "TODO: ERROR")
if #duration == 1 then
d_min = duration[1]
d_max = duration[1]
for _, stage in ipairs(stage) do
total = total + 1
local success, message = is_valid_stage(stage)
if (success == true) then
good = good + 1
else
d_min = math.min(duration[1], duration[2])
d_max = math.max(duration[1], duration[2])
bad = bad + 1
table.insert(reasons, message)
end
end
elseif type(duration) == "number" then
d_min = duration
d_max = duration
elseif type(duration) == "string" then
d_min = tonumber(duration)
d_max = tonumber(duration)
if (bad == 0) then
minetest.log("action",
"[MOD] yl_api_nodestages : bad = " .. tostring(bad) ..
", good = " .. tostring(good) .. ", total = " ..
tostring(total))
return true, good, bad, total, reasons
else
return yl_api_nodestages.error
minetest.log("warning",
"[MOD] yl_api_nodestages : bad = " .. tostring(bad) ..
", good = " .. tostring(good) .. ", total = " ..
tostring(total))
return false, good, bad, total, reasons
end
assert((d_min >= 0) and (d_max >= 0) and ((d_max >= d_min)), "TODO: ERROR")
return {min = d_min, max = d_max}
end
function yl_api_nodestages.calc_duration(duration) return
calc_duration(duration) end
local function gs(dtime)
if bucket > 0 then
core.chat_send_all("dtime =" .. dump(dtime) .. ", bucket= " .. dump(bucket) .. ", numz=".. dump(numz))
function yl_api_nodestages.validate_all_stages()
local stages = {}
for _, def in pairs(minetest.registered_nodes) do
local stage = def._stage
table.insert(stages, stage)
end
bucket = 0
numz = 0
return yl_api_nodestages.is_valid(stages)
end
core.register_globalstep(gs)