yl_ticker/api.lua
2024-05-13 23:20:17 +02:00

168 lines
5.2 KiB
Lua

function yl_announcements.list()
if next(yl_announcements.data) ~= nil then
return true, yl_announcements.data
end
return false, {}
end
function yl_announcements.get(announcement_id)
-- Check announcement_id
if (type(announcement_id) ~= "number") then
return false, "Announcement ID is not a number"
end
if (announcement_id <= 0) then
return false, "Announcement ID cannot be zero or negative"
end
local success, data = yl_announcements.list()
if ((success == true) and (data[announcement_id] ~= nil)) then
return true, table.copy(data[announcement_id])
end
return nil
end
function yl_announcements.set(message, frequency, runtime, owner)
-- Check message
if (type(message) ~= "string") then return false, "message not a string" end
-- Check frequency
if (type(frequency) ~= "number") then
return false, "frequency not a number"
end
if (frequency <= 0) then
return false, "Frequency cannot be zero or negative"
end
-- Check runtime
if (type(runtime) ~= "number") then return false, "Runtime not a number" end
if (runtime <= 0) then return false, "Runtime cannot be zero or negative" end
-- Check owner
if (type(owner) ~= "string") then return false, "Owner not a string" end
if (owner == "") then return false, "Owner cannot be empty" end
local index = 1
while yl_announcements.data[index] ~= nil do index = index + 1 end
local content = {
id = index,
creation_date = os.time(),
message = message,
frequency = frequency,
runtime = runtime,
owner = owner
}
yl_announcements.data[index] = content
local success = yl_announcements.save_json(tostring(index) .. ".json",
content)
if (success == true) then return true, index end
return false, "Could not store announcement"
end
-- /announcement_add message$frequency in minutes$runtime in minutes
function yl_announcements.formspec(announcement_id)
-- Check announcement_id
if (type(announcement_id) ~= "number") then
return false, "Announcement ID is not a number"
end
if (announcement_id <= 0) then
return false, "Announcement ID cannot be zero or negative"
end
local success, announcement = yl_announcements.get(announcement_id)
core.log("action", "announcement = " .. dump(announcement))
if ((success == false) or (announcement == nil)) then
return false, "No announcement"
end
local m = announcement.message or "message"
local f = announcement.frequency or "frequency"
local r = announcement.runtime or "runtime"
local c = "/announcement_add " .. m .. "$" .. f .. "$" .. r
local content = minetest.formspec_escape(c .. "\n\n" .. dump(announcement))
local formspec = "formspec_version[6]" .. "size[16,4]" ..
"button_exit[15.4,0.1;0.5,0.5;X;X]" ..
"textarea[0.05,0.05;15.3,3.9;;;" .. content .. "]"
return true, formspec
end
function yl_announcements.delete(announcement_id)
-- Check announcement_id
if (type(announcement_id) ~= "number") then
return false, "Announcement ID is not a number"
end
if (announcement_id <= 0) then
return false, "Announcement ID cannot be zero or negative"
end
local success, data = yl_announcements.list()
if ((success == false) or (data == nil)) then
return false, "Could not retrieve list"
end
if ((success == true) and (data[announcement_id] == nil)) then
return false, "Could not retrieve announcement"
end
local announcement = table.copy(data[announcement_id]) or
"No data available"
if (announcement ~= nil) then
data[announcement_id] = nil
local success_f, err = yl_announcements.remove_file(announcement_id ..
".json")
if (success_f == nil) then return false, err end
success = true
end
if (success ~= true) then
announcement = "Could not find announcement with id " ..
dump(announcement_id)
end
return success, announcement
end
function yl_announcements.say(announcement_id, target)
-- Check announcement_id
if (type(announcement_id) ~= "number") then
return false, "Announcement ID is not a number"
end
if (announcement_id <= 0) then
return false, "Announcement ID cannot be zero or negative"
end
-- No check for target, it is optional
local success = false
local announcement = yl_announcements.data[announcement_id]
if (announcement ~= nil) then
local color = yl_announcements.settings.colour
local message = announcement.message
local content = minetest.colorize(color, message)
if ((target == "*") or (target == nil)) then
minetest.chat_send_all(content)
else
minetest.chat_send_player(target, content)
end
success = true
end
if (success ~= true) then
announcement = "Could not find announcement with id " ..
dump(announcement_id)
end
return success, announcement
end