melzneni

sys_singleUnit

Feb 9th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.74 KB | None | 0 0
  1. function string:split(s, delimiter)
  2.     local result = {};
  3.     for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
  4.         table.insert(result, match);
  5.     end
  6.     return result;
  7. end
  8.  
  9. function getMsgData(msg)
  10.     local i, j = string.find(msg, ":")
  11.     local tag = string.sub(msg, 1, i - 1)
  12.     local pts = string:split(string.sub(msg, i + 1, -1), ",")
  13.     return tag, pts
  14. end
  15.  
  16. --syslib end
  17.  
  18. rednet.open("back")
  19. function identify()
  20.     local label = os.getComputerLabel()
  21.     if label == nil then
  22.         label="<unnamed>"
  23.     end
  24.     rednet.broadcast("@root:identify," .. label)
  25. end
  26.  
  27. identify()
  28. local rootId
  29. while true do
  30.     local id, msg = rednet.receive()
  31.     local tag, pts = getMsgData(msg)
  32.     if tag == "root" then
  33.         if pts[1] == "initialise" then
  34.             rootId = id
  35.             identify()
  36.         elseif pts[1] == "reboot" then os.reboot()
  37.         else print("unknown msg>root-msg: ", msg)
  38.         end
  39.     elseif tag == "action" then
  40.         if pts[1] == "executePastebin" then
  41.             shell.run("delete", "temp")
  42.             shell.run("pastebin", "get", pts[2], "temp")
  43.             shell.run("temp", "" .. id)
  44.             rednet.send(id, "answer:executePastebinFinished," .. pts[2])
  45.         elseif pts[1] == "setStartup" then
  46.             shell.run("delete", "startup")
  47.             local f = fs.open("startup", "w")
  48.             f.write("shell.run(\"delete\",\"unitHandler\")\n")
  49.             f.write("shell.run(\"pastebin\",\"get\",\"" .. pts[2] .. "\",\"unitHandler\")\n")
  50.             f.write("shell.run(\"unitHandler\")\n")
  51.             f.close()
  52.         else print("unknown msg>action-msg: ", msg)
  53.         end
  54.     elseif tag ~= "@root" then
  55.         print("unknown msg-tag: ", msg)
  56.     end
  57. end
Add Comment
Please, Sign In to add comment