yl_scheduler/api.lua
2024-04-09 17:21:44 +02:00

234 lines
6.2 KiB
Lua

-- yl_scheduler.get_task(UUID)
local function get_task(UUID)
local success, tasks = yl_scheduler.list_all_tasks()
if (success == false) then return nil end
for key, task in ipairs(tasks) do
if task.id == UUID then return true, task end
end
return nil
end
function yl_scheduler.get_task(UUID) return get_task(UUID) end
-- yl_scheduler.set_task(at, func, params, owner, notes)
local function set_task(at, func, params, owner, notes)
-- Defense
-- Those values come from files and chatcommands
local validate_success, validate_message = yl_scheduler.validate(at, func, params, owner, notes)
if (validate_success == false) then
return false, validate_message
end
local task = {}
-- UUID
local get_uuid_success, UUID = yl_scheduler.create_uuid()
if get_uuid_success == false then
return false, UUID
end
task.id = UUID
-- at
-- Try to find what timeformat we are
-- Caclulate utc time from this given time format
task.at = at
-- func
task.func = func
-- params, optional
task.params = params or {}
-- owner
task.owner = owner
-- notes, optional
task.notes= notes or ""
-- save to file
local write_file_success = yl_scheduler.save_json(UUID, task)
if write_file_success == false then
return false, "Cannot write file"
end
-- store in table
table.insert(yl_scheduler.tasks, task)
return true, UUID
end
function yl_scheduler.set_task(at, func, params, owner, notes)
return set_task(at, func, params, owner, notes)
end
-- yl_scheduler.list_all_tasks()
local function list_all_tasks()
if next(yl_scheduler.tasks) then
return true, yl_scheduler.tasks
else
return false, {}
end
end
function yl_scheduler.list_all_tasks() return list_all_tasks() end
-- yl_scheduler.find_id(part of UUID)
local function find_id(UUID)
local tasks = yl_scheduler.list_all_tasks()
local result = {}
for i, task in ipairs(tasks) do
if (string.find(task.id, UUID) ~= nil) then
table.insert(result, task.id)
end
end
if next(result) then
return true, result
else
return false, result
end
end
function yl_scheduler.find_id(UUID) return find_id(UUID) end
-- yl_scheduler.find_task(UUID)
local function find_task(UUID)
local tasks = yl_scheduler.list_all_tasks()
local result = {}
for i, task in ipairs(tasks) do
if (string.find(task.id, UUID) ~= nil) then
table.insert(result, task)
end
end
if next(result) then
return true, result
else
return false, result
end
end
function yl_scheduler.find_task(UUID) return find_task(UUID) end
-- yl_scheduler.remove_task(UUID)
local function remove_task(UUID)
local tasks = yl_scheduler.list_all_tasks()
for i, task in ipairs(tasks) do
if task.id == UUID then
table.remove(tasks, i)
local success, errormsg = yl_scheduler.remove_file(UUID)
if (success == false) then
minetest.log("warning",
"[MOD] yl_scheduler : Cannot remove UUID " .. UUID ..
": File not found. Errormsg: " .. errormsg)
end
return true, task
end
end
return false, "UUID not found"
end
function yl_scheduler.remove_task(UUID) return remove_task(UUID) end
-- yl_scheduler.clean_past_tasks()
local function clean_past_tasks()
local tasks = yl_scheduler.list_all_tasks()
local amount_deleted = 0
local current_utc_time = os.time(os.date("!*t")) -- utc
for i = #tasks, 1, -1 do
if tasks[i].at > current_utc_time then
local UUID = tasks[i].id
table.remove(tasks, i)
local success, errormsg = yl_scheduler.remove_file(UUID)
if (success == false) then
minetest.log("warning",
"[MOD] yl_scheduler : Cannot remove UUID " .. UUID ..
": File not found. Errormsg: " .. errormsg)
end
amount_deleted = amount_deleted + 1
end
end
yl_scheduler.tasks = tasks
local amount_remaining = #tasks
if amount_deleted >= 1 then
return true, amount_deleted, amount_remaining
else
return false, "no tasks removed"
end
end
function yl_scheduler.clean_past_tasks() return clean_past_tasks() end
-- yl_scheduler.clean_executed_tasks()
local function clean_executed_tasks()
local tasks = yl_scheduler.list_all_tasks()
local amount_deleted = 0
for i = #tasks, 1, -1 do
if tasks[i].done > 0 then
local UUID = tasks[i].id
table.remove(tasks, i)
local success, errormsg = yl_scheduler.remove_file(UUID)
if (success == false) then
minetest.log("warning",
"[MOD] yl_scheduler : Cannot remove UUID " .. UUID ..
": File not found. Errormsg: " .. errormsg)
end
amount_deleted = amount_deleted + 1
end
end
yl_scheduler.tasks = tasks
local amount_remaining = #tasks
if amount_deleted >= 1 then
return true, amount_deleted, amount_remaining
else
return false, "no tasks removed"
end
end
function yl_scheduler.clean_executed_tasks() return clean_executed_tasks() end
-- yl_scheduler.execute_task(UUID)
local function execute_task(UUID)
local task = yl_scheduler.get_task(UUID)
-- Execute the function
if task.func and _G[task.func] and (type(_G[task.func]) == "function") then
local success, message = pcall(_G[task.func](unpack(task.params)))
if not success then return false, "Pcall " .. message end
else
return false, "Function not found"
end
-- Change the done value in the file
local current_utc_time = os.time(os.date("!*t")) -- utc
task.done = current_utc_time
yl_scheduler.save_json(UUID, task)
-- Change the done value in the table
for _, t_task in ipairs(yl_scheduler.tasks) do
if t_task.id == UUID then t_task.done = current_utc_time end
end
end
function yl_scheduler.execute_task(UUID) return execute_task(UUID) end