Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function split(s, delimiter)
- local result = {};
- for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- rednet.open("top")
- function receiveRednet()
- while true do
- local id, msg = rednet.receive()
- if type(msg) == "string" then
- if string.find(msg, ":") ~= nil then
- if string.sub(msg, 1, 1) == "[" then
- local ind = string.find(msg, "]")
- id = tonumber(string.sub(msg, 2, ind - 1))
- msg = string.sub(msg, ind + 1)
- end
- return id, msg
- end
- end
- end
- end
- function getMsgData(msg)
- local i, j = string.find(msg, ":")
- local tag = string.sub(msg, 1, i - 1)
- local pts = split(string.sub(msg, i + 1, -1), ",")
- return tag, pts
- end
- local others = {}
- local clients = {}
- function sendToClients(msg)
- for _, sid in ipairs(clients) do
- rednet.send(sid, msg)
- end
- end
- rednet.broadcast("@globalNet:register,startup")
- rednet.broadcast("@globalClients:register")
- while true do
- local id, msg = receiveRednet()
- local tag, pts = getMsgData(msg)
- if tag == "@globalNet" then
- if pts[1] == "access" then
- local alreadyRegistered = false
- for _, cid in ipairs(clients) do
- if cid == id then
- alreadyRegistered = true
- break
- end
- end
- if not alreadyRegistered then table.insert(clients, id) end
- rednet.send(id, "@gNetAnsw:hu")
- elseif pts[1] == "broadcast" then
- msg = string.sub(msg, #tag + 1 + #pts[1] + 2)
- if string.sub(msg, 1, 1) ~= "[" then
- msg = "[" .. id .. "]" .. msg
- end
- sendToClients("<global>" .. msg)
- for _, sid in ipairs(others) do
- rednet.send(sid, "@globalNet:broadcastSent," .. string.sub(msg, #tag + 1 + #pts[1] + 2))
- end
- elseif pts[1] == "broadcastSent" then
- print(id)
- sendToClients("<global>" .. string.sub(msg, #tag + 1 + #pts[1] + 2))
- elseif pts[1] == "register" then
- table.insert(others, id)
- if pts[2] == "startup" then
- rednet.send(id, "@globalNet:register")
- end
- else error("unknown message: ", msg)
- end
- end
- end
- --pastebin run 0YB9PsQV startup={files={global=<pb:Y7X9sjQb>},cmds={{'global'}}} reboot=true label=<input:'Label'>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement