urusernamemolniya

regtest

Mar 21st, 2022 (edited)
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | None | 0 0
  1. local json = require("json")
  2. local tid = ""
  3.  
  4. print("Connecting to websocket server...")
  5. local wsc = http.websocket("ws://127.0.0.1:3000")
  6. if fs.exists("turtleid.lt") then
  7.     local f = fs.open("turtleid.lt", "r")
  8.     tid = f.readAll()
  9.     f.close()
  10.     print("Finding location...")
  11.     local tx, ty, tz = gps.locate(5)
  12.     if tx == nil then
  13.         print("Could not find location. Try 'gps locate'")
  14.         os.exit()
  15.     end
  16.     print("Updating turtle data...")
  17.     local sdata = json.encode({ type = "update", x = tx, y = ty, z = tz, id = tid })
  18.     wsc.send(sdata)
  19.     local rdata = wsc.receive(10)
  20.     if rdata == nil then
  21.         print("Updating failed. Exiting.")
  22.         os.exit()
  23.     end
  24. else
  25.     print("This turtle is not yet registered. Please enter this turtle's name: ")
  26.     local nname = io.read()
  27.     print("Finding location...")
  28.     local tx, ty, tz = gps.locate(5)
  29.     if tx == nil then
  30.         print("Could not find location. Try 'gps locate'")
  31.         os.exit()
  32.     end
  33.     print("Sending...")
  34.     local sdata = json.encode({ type = "register", x = tx, y = ty, z = tz, name = nname, auto = true })
  35.     wsc.send(sdata)
  36.     print("Waiting for response...")
  37.     local rdata = wsc.receive(10)
  38.     if rdata == nil then
  39.         print("Registering failed. Exiting.")
  40.         os.exit()
  41.     end
  42.     print("Got new ID: " .. rdata)
  43.     local f = fs.open("turtleid.lt", "w")
  44.     f.write(rdata)
  45.     f.close()
  46.     tid = rdata
  47. end
  48.  
  49. print("Refueling...")
  50. turtle.refuel()
  51. print("Let's go mining!")
  52.  
Add Comment
Please, Sign In to add comment