Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Description: This will receive all the messages sent over from the cunkloader detectors
- local modem = peripheral.find("modem", rednet.open)
- local monitor = peripheral.find("monitor")
- local nodes = {}
- function Draw()
- while (true) do
- term.clear()
- for i, node in ipairs(nodes) do
- term.setCursorPos(1, i)
- print(node.name .. "(" .. node.x .. "/" .. node.y .. "/" .. node.z .. ") " .. node.lastPing .. "s")
- end
- sleep(1)
- end
- end
- function checkNodes()
- while (true) do
- for i, node in ipairs(nodes) do
- if (node.lastPing > 10) then
- table.remove(nodes, i)
- break
- else
- node.lastPing = node.lastPing + 1
- end
- end
- sleep(1)
- end
- end
- function receivePing()
- while (true) do
- local event, sender, message, protocol = os.pullEvent("rednet_message")
- local Data = textutils.unserialiseJSON(tostring(message))
- -- Check if the node already exists and update last ping time
- local exists = false
- for i, node in ipairs(nodes) do
- if (node.name == Data.name) then
- node.lastPing = 0
- exists = true
- break
- end
- end
- -- Create node if non existent
- if (exists == false) then
- Data.lastPing = 0
- table.insert(nodes, Data)
- end
- end
- end
- -- It there is a monitor use it
- if (monitor ~= nil) then
- term.redirect(monitor)
- end
- -- Main loops
- parallel.waitForAny(
- function()
- Draw()
- end,
- function()
- checkNodes()
- end,
- function()
- receivePing()
- end
- )
Advertisement
Add Comment
Please, Sign In to add comment