gravityio

staryt

Jul 15th, 2021 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.02 KB | None | 0 0
  1. local userID = nil;
  2. local computerName = nil;
  3. local isAdvanced = nil;
  4. local isTurtle = nil;
  5. local leftItem = nil
  6. local rightItem = nil;
  7.  
  8. function InputHandler()
  9.   write("Enter User ID: ");
  10.   userID = read();
  11.   write("Enter Computer Name: ");
  12.   computerName = read();
  13. end
  14.  
  15. function TurtleHandler()
  16.   isTurtle = turtle ~= nil;
  17.   if (isTurtle) then
  18.     local emptySlot = nil;
  19.     for i = 1, 16 do
  20.         local item = turtle.getItemDetail(i);
  21.         if (not item) then emptySlot = i break end;
  22.     end
  23.     if (emptySlot) then
  24.       turtle.equipLeft();
  25.       local left = turtle.getItemDetail(emptySlot);
  26.       if (left) then
  27.           turtle.equipLeft();
  28.           turtle.leftItem = left.name;
  29.       end
  30.       turtle.equipRight();
  31.       local right = turtle.getItemDetail(emptySlot);
  32.       if (right) then
  33.           turtle.equipRight();
  34.           turtle.rightItem = right.name;
  35.       end
  36.     end
  37.   end
  38. end
  39.  
  40. function Main()
  41.   local url ="http://192.168.0.69:5050/api/computers";
  42.   local headers = { ["content-type"]="application/json" };
  43.   local body = {};
  44.   body.userID = userID;
  45.   body.computerName = computerName;
  46.   body.isTurtle = isTurtle;
  47.   body.leftItem = turtle and turtle.leftItem;
  48.   body.rightItem = turtle and turtle.rightItem;
  49.   body.isAdvanced = term.isColor();
  50.   local res, error, errorRes = http.post(url, textutils.serialiseJSON(body), headers);
  51.   local resBody;
  52.   if (res) then
  53.     resBody = textutils.unserialiseJSON(res.readAll());
  54.     local id = resBody["computerID"];
  55.     if (id) then
  56.       settings.set("computerID", id);
  57.       settings.save(".settings");
  58.     end
  59.   elseif (errorRes) then
  60.     resBody = textutils.unserialiseJSON(errorRes.readAll());
  61.   end
  62.   if (resBody) then
  63.     if (resBody.message) then
  64.       print(resBody.message);
  65.     end
  66.   end
  67. end
  68.  
  69. function WriteStartup()
  70.   local startup = fs.open("/startup.lua", "w");
  71.   startup.write([[local ws;
  72.   local function connectWS()
  73.     ws = http.websocket("ws://localhost:5050", {["computerID"]=settings.get("computerID")});
  74.     if (ws) then print("Connnected to controller.")
  75.     else print("WebSocket not available.") end
  76.   end
  77.   connectWS();
  78.   while true do
  79.     if (ws) then
  80.       print("Listening for message.")
  81.       local status, msg = pcall(ws.receive);
  82.       if (status) then
  83.         if (msg ~= nil) then
  84.           print("Received message, executing. " .. msg);
  85.           local success, error = pcall(loadstring(msg))
  86.           if (success) then
  87.             ws.send('{"success": true}')
  88.             print("Execution succesful.")
  89.           else
  90.             print("Execution failed, error: \n" .. error)
  91.             error = error:gsub('"', "'")
  92.             ws.send('{"success": false, "error": "'.. error ..'"}')
  93.           end
  94.         end
  95.       else
  96.         connectWS();
  97.         os.sleep(5)
  98.       end
  99.     else
  100.       connectWS();
  101.       os.sleep(5)
  102.     end
  103.   end
  104.  
  105.   ws.close();]]);
  106.   startup.close();
  107.   os.reboot();
  108. end
  109.  
  110.  
  111. parallel.waitForAll(InputHandler, TurtleHandler);
  112. Main();
  113. WriteStartup();
Add Comment
Please, Sign In to add comment