138 lines
3.0 KiB
Lua
138 lines
3.0 KiB
Lua
local PROTOCOL = "wireless-todo"
|
|
local TODOS_FILE = "todos.data.lua"
|
|
|
|
local function pprint(...)
|
|
local pretty = require("cc.pretty")
|
|
pretty.pretty_print(...)
|
|
end
|
|
|
|
local function isWirelessModem(peripheral_name)
|
|
return peripheral.getType(peripheral_name) == "modem"
|
|
and peripheral.call(peripheral_name, "isWireless")
|
|
end
|
|
|
|
local function getWirelessModem()
|
|
for _, name in ipairs(peripheral.getNames()) do
|
|
if isWirelessModem(name) then
|
|
return name
|
|
end
|
|
end
|
|
end
|
|
|
|
local function readTodos(filename)
|
|
if fs.exists(filename) then
|
|
return loadfile(filename)()
|
|
else
|
|
return {}
|
|
end
|
|
end
|
|
|
|
local function writeTodos(filename, todos)
|
|
local f = fs.open(filename, "w")
|
|
f.write("return ")
|
|
f.write(textutils.serialise(todos))
|
|
f.close()
|
|
end
|
|
|
|
local function includeValue(set, value)
|
|
for _, v in ipairs(set) do
|
|
if v == value then
|
|
return
|
|
end
|
|
end
|
|
|
|
table.insert(set, value)
|
|
end
|
|
|
|
local function decodeMessage(message)
|
|
local parts = {}
|
|
for part in message:gmatch("([^\n]+)") do
|
|
table.insert(parts, part)
|
|
end
|
|
return parts
|
|
end
|
|
|
|
local function sendToMultiple(recipients, payload, protocol, except_recepient)
|
|
for _, recipient in ipairs(recipients) do
|
|
if recipient ~= except_recepient then
|
|
rednet.send(recipient, payload, protocol)
|
|
end
|
|
end
|
|
end
|
|
|
|
local function main(hostname)
|
|
if not hostname then
|
|
print("ERROR: please provide a hostname")
|
|
return
|
|
end
|
|
|
|
local modem_name = getWirelessModem()
|
|
if not modem_name then
|
|
print("ERROR: please attach a wireless modem")
|
|
return
|
|
end
|
|
|
|
rednet.open(modem_name)
|
|
rednet.host(PROTOCOL, hostname)
|
|
|
|
local seen_users = {}
|
|
local todos = readTodos(TODOS_FILE)
|
|
|
|
local function updateUsersTodos(except_user)
|
|
writeTodos(TODOS_FILE, todos)
|
|
local payload = textutils.serialise(todos, { compact = true })
|
|
sendToMultiple(seen_users, payload, PROTOCOL, except_user)
|
|
end
|
|
|
|
while true do
|
|
local sender, message = rednet.receive(PROTOCOL)
|
|
print(sender, message)
|
|
local parts = decodeMessage(message)
|
|
pprint(parts)
|
|
if parts[1] == "get" then
|
|
includeValue(seen_users, sender)
|
|
local payload = textutils.serialise(todos, { compact = true })
|
|
rednet.send(sender, payload, PROTOCOL)
|
|
elseif parts[1] == "edit" then
|
|
local index = tonumber(parts[2])
|
|
todos[index].text = parts[3]
|
|
|
|
updateUsersTodos(sender)
|
|
elseif parts[1] == "mark" then
|
|
local index = tonumber(parts[2])
|
|
todos[index].marked = true
|
|
|
|
updateUsersTodos(sender)
|
|
elseif parts[1] == "unmark" then
|
|
local index = tonumber(parts[2])
|
|
todos[index].marked = false
|
|
|
|
updateUsersTodos(sender)
|
|
elseif parts[1] == "add" then
|
|
table.insert(todos, {
|
|
text = parts[2] or "",
|
|
marked = false
|
|
})
|
|
|
|
updateUsersTodos(sender)
|
|
elseif parts[1] == "remove" then
|
|
local index = tonumber(parts[2])
|
|
table.remove(todos, index)
|
|
|
|
updateUsersTodos(sender)
|
|
elseif parts[1] == "move-up" then
|
|
local index = tonumber(parts[2])
|
|
-- TOOD: finish this
|
|
|
|
updateUsersTodos(sender)
|
|
elseif parts[1] == "move-down" then
|
|
local index = tonumber(parts[2])
|
|
-- TOOD: finish this
|
|
|
|
updateUsersTodos(sender)
|
|
end
|
|
end
|
|
end
|
|
|
|
main(...)
|