Advertisement
Alakazard12

AltProto_Controller

Mar 11th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.53 KB | None | 0 0
  1. local ALTPROTO_PORT = 16660
  2.  
  3. local function Log(text)
  4.     print(text)
  5. end
  6.  
  7. local modem = peripheral.find("modem", function(_, p) return p.isWireless() end)
  8. if not modem then
  9.     Log("No modem found")
  10.     return
  11. end
  12.  
  13. modem.open(ALTPROTO_PORT)
  14.  
  15.  
  16. local RECIPIENT_ALL = 0
  17. local function SendMessage(id, recipient, data)
  18.     modem.transmit(ALTPROTO_PORT, 0, {id = id, data = data, recipient = recipient, sender = 0})
  19. end
  20.  
  21.  
  22.  
  23. local lbase = math.log(10)
  24. local si_prefixes = {
  25.     [-4] = "p";
  26.     [-3] = "n";
  27.     [-2] = "u";
  28.     [-1] = "m";
  29.     [0] = "WTF";
  30.     [1] = "k";
  31.     [2] = "M";
  32.     [3] = "G";
  33.     [4] = "T";
  34.     [5] = "P";
  35.     [6] = "E";
  36.     [7] = "Z";
  37.     [8] = "Y";
  38. }
  39. local si_low = -4
  40. local si_high = 8
  41.  
  42. local function Round(number, decimals)
  43.     return math.floor((number * 10^decimals) + 0.5) / 10^decimals
  44. end
  45.  
  46. local function FormatSI(number, unit)
  47.     local mult = 1
  48.     if number < 0 then
  49.         mult = -1
  50.         number = -number
  51.     end
  52.  
  53.     local brack = math.floor((math.log(number) / lbase) / 3)
  54.     brack = math.min(math.max(brack, si_low), si_high)
  55.     if brack == 0 then
  56.         return tostring(mult * Round(number, 3)) .. " " .. unit
  57.     end
  58.  
  59.     number = number / 10^(brack * 3)
  60.     return tostring(Round(mult * number, 3)) .. " " .. si_prefixes[brack] .. unit
  61. end
  62.  
  63.  
  64.  
  65.  
  66.  
  67. local args = {...}
  68.  
  69.  
  70.  
  71.  
  72.  
  73. if args[1] == "ping" then
  74.     SendMessage("GPING", RECIPIENT_ALL, math.random())
  75.  
  76.     local timer = os.startTimer(1)
  77.     while true do
  78.         local event = {os.pullEvent()}
  79.         if event[1] == "timer" and event[2] == timer then
  80.             break
  81.         end
  82.  
  83.         if event[1] == "modem_message" then
  84.             local event, side, sender, reply, message, distance = unpack(event)
  85.  
  86.             if sender == ALTPROTO_PORT then
  87.                 if type(message) ~= "table" then
  88.                     Log("ERROR: Message was not a table")
  89.                     return
  90.                 end
  91.  
  92.                 local recipient = message.recipient
  93.                 if not recipient then
  94.                     Log("ERROR: Message has no recipient")
  95.                     return
  96.                 end
  97.                 local sender = message.sender
  98.                 if not sender then
  99.                     Log("ERROR: Message has no sender")
  100.                     return
  101.                 end
  102.  
  103.                 if recipient == proto_id or recipient == RECIPIENT_ALL then
  104.                     local id = message.id
  105.                     if not id then
  106.                         Log("ERROR: Message has no ID")
  107.                         return
  108.                     end
  109.  
  110.                     if id == "GPONG" then
  111.                         Log("Got pong from: " .. sender .. " (" .. (message.data or "no label") .. ")")
  112.                     end
  113.                 end
  114.             end
  115.         end
  116.     end
  117.  
  118. elseif args[1] == "reactor" then
  119.     if args[2] == "on" then
  120.         SendMessage("REACT_SET", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, true)
  121.         Log("Enabled reactor")
  122.     elseif args[2] == "off" then
  123.         SendMessage("REACT_SET", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, false)
  124.         Log("Disabled reactor")
  125.     else
  126.         Log("Usage: con reactor <on/off>")
  127.     end
  128.  
  129. elseif args[1] == "update" then
  130.     local filename = args[2]
  131.     if not filename then
  132.         Log("Usage: con update <file> [id]")
  133.         return
  134.     end
  135.  
  136.     local id = tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL
  137.  
  138.     local file = fs.open(filename, "r")
  139.     if not file then
  140.         Log("Invalid file")
  141.         return
  142.     end
  143.  
  144.     local data = file.readAll()
  145.     file.close()
  146.     local func, err = loadstring(data)
  147.     if not func then
  148.         Log("Syntax error: " .. err)
  149.         return
  150.     end
  151.  
  152.     SendMessage("UPDATE", id, data)
  153.     Log("Sent update file to " .. tostring(id))
  154.  
  155. elseif args[1] == "updatemod" then
  156.     local filename = args[2]
  157.     if not filename then
  158.         Log("Usage: con updatemod <file> [id]")
  159.         return
  160.     end
  161.  
  162.     local id = tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL
  163.  
  164.     local file = fs.open(filename, "r")
  165.     if not file then
  166.         Log("Invalid file")
  167.         return
  168.     end
  169.  
  170.     local data = file.readAll()
  171.     file.close()
  172.     local func, err = loadstring(data)
  173.     if not func then
  174.         Log("Syntax error: " .. err)
  175.         return
  176.     end
  177.  
  178.     SendMessage("UPDATEMOD", id, {name = filename, code = data})
  179.     Log("Sent module file to " .. tostring(id))
  180.  
  181. elseif args[1] == "lights" then
  182.     if args[2] == "on" then
  183.         SendMessage("LIGHTS", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, true)
  184.         Log("Enabled lights")
  185.     elseif args[2] == "off" then
  186.         SendMessage("LIGHTS", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, false)
  187.         Log("Disabled lights")
  188.     else
  189.         Log("Usage: con lights <on/off> [id]")
  190.     end
  191.  
  192. elseif args[1] == "spawner" then
  193.     if args[2] == "on" then
  194.         SendMessage("SETSPAWNER", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, true)
  195.         Log("Enabled spawner")
  196.     elseif args[2] == "off" then
  197.         SendMessage("SETSPAWNER", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, false)
  198.         Log("Disabled spawner")
  199.     else
  200.         Log("Usage: con spawner <on/off> [id]")
  201.     end
  202.  
  203. else
  204.     Log("Unknown command")
  205. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement