Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not fs.exists("msglist") then
- nFile = fs.open("msglist", "w")
- nFile.writeLine("messages = {}")
- nFile.close()
- end
- function reverseTable(table)
- num = 1
- local result = {}
- for i = #table, 1, -1 do
- result[num] = {}
- result[num][1] = table[i][1]
- result[num][2] = table[i][2]
- num = num + 1
- end
- return result
- end
- function serialize(table)
- local result = "messages = {}\n"
- local revTab = reverseTable(table)
- for i, v in pairs(revTab) do
- result = result .. "messages[" .. tostring(i) .. "] = {}\n"
- for k, e in pairs(v) do
- result = result .. "messages[" .. tostring(i) .. "][" .. tostring(k) .. "] = \"" .. tostring(e) .. "\"\n"
- end
- end
- return result
- end
- while true do
- os.loadAPI("msglist")
- id, message, protocol = nil, nil, nil
- id, message, protocol = rednet.receive()
- print(id, message, "prot", protocol)
- if protocol == "getmsglist" then
- print("getmsglist")
- rednet.send(id, serialize(msglist.messages), "sendmsglist")
- else
- print(message)
- rFile = fs.open("msglist", "r")
- local previous = rFile.readAll()
- rFile.close()
- wFile = fs.open("msglist", "w")
- wFile.write(previous .. "\n")
- wFile.writeLine("messages[" .. #msglist.messages + 1 .. "] = {}")
- wFile.writeLine("messages[" .. #msglist.messages + 1 .. "][1] = \"" .. tostring(id) .. ": " .. tostring(message) .. "\"")
- wFile.writeLine("messages[" .. #msglist.messages + 1 .. "][2] = \"" .. tostring(os.day()) .. ":" .. tostring(os.time()) .. "\"")
- wFile.close()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement