Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local json = require("json")
- local tid = ""
- print("Connecting to websocket server...")
- local wsc = http.websocket("ws://127.0.0.1:3000")
- if fs.exists("turtleid.lt") then
- local f = fs.open("turtleid.lt", "r")
- tid = f.readAll()
- f.close()
- print("Finding location...")
- local tx, ty, tz = gps.locate(5)
- if tx == nil then
- print("Could not find location. Try 'gps locate'")
- os.exit()
- end
- print("Updating turtle data...")
- local sdata = json.encode({ type = "update", x = tx, y = ty, z = tz, id = tid })
- wsc.send(sdata)
- local rdata = wsc.receive(10)
- if rdata == nil then
- print("Updating failed. Exiting.")
- os.exit()
- end
- else
- print("This turtle is not yet registered. Please enter this turtle's name: ")
- local nname = io.read()
- print("Finding location...")
- local tx, ty, tz = gps.locate(5)
- if tx == nil then
- print("Could not find location. Try 'gps locate'")
- os.exit()
- end
- print("Sending...")
- local sdata = json.encode({ type = "register", x = tx, y = ty, z = tz, name = nname, auto = true })
- wsc.send(sdata)
- print("Waiting for response...")
- local rdata = wsc.receive(10)
- if rdata == nil then
- print("Registering failed. Exiting.")
- os.exit()
- end
- print("Got new ID: " .. rdata)
- local f = fs.open("turtleid.lt", "w")
- f.write(rdata)
- f.close()
- tid = rdata
- end
- print("Refueling...")
- turtle.refuel()
- print("Let's go mining!")
Add Comment
Please, Sign In to add comment