Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not os.getComputerLabel() then
- print(" ")
- print("Give this turtle a name: ")
- local label = read()
- shell.execute("label", "set", label)
- print("Thank you for giving me a wonderfull name UwU: ")
- end
- local serverUrl = "ws://84.86.255.191:8765"
- -- login info
- loginInfo = {
- ["name"] = os.getComputerLabel(),
- ["device"] = "turtle",
- ["loggedIn"] = false,
- }
- -- general info
- info = {
- ["response"] = "none",
- ["fuel"] = turtle.getFuelLevel(),
- ["inspect"] = inspect,
- ["device"] = loginInfo.device,
- ["loggedIn"] = loginInfo.loggedIn
- }
- os.loadAPI("json")
- if fs.exists("latestPos") then
- local file = fs.open("latestPos","r")
- pos = json.decode(file.readLine())
- else
- print(" ")
- print("This turtle does not know where it is!")
- print("x =")
- local newX = tonumber(read())
- print("y =")
- local newY = tonumber(read())
- print("z =")
- local newZ = tonumber(read())
- print("this turtle is facing: <north> <south> <east> <west>")
- local newFacing = read()
- pos = {
- ["x"] = newX,
- ["y"] = newY,
- ["z"] = newZ,
- ["facing"] = newFacing,
- ["world"] = "overworld"
- }
- print("This turtle now knows where it is. becouse it knows where it isnt. thanks to you <3")
- local file = fs.open("latestPos","a")
- file.write(json.encode(pos))
- file.close()
- end
- -- Connect to the WebSocket server
- function connect()
- local attemptCount = 0
- local connected = false
- while not connected do
- attemptCount = attemptCount + 1
- print("Attempt #" .. attemptCount .. " to connect to: ", serverUrl)
- ws, err = http.websocket(serverUrl)
- if ws then
- connected = true
- print("Connection successful!")
- else
- print("Connection failed. Retrying in 5 seconds...")
- os.sleep(5) -- Adjust the sleep duration as needed
- end
- end
- end
- function close()
- ws.close()
- for i = 30, 0, -1 do
- term.clear()
- print("server has closed.....")
- print("rebooting: " .. i .. " seconds")
- sleep(1)
- end
- os.reboot()
- return true
- end
- -- Communication functions
- local function sendData(data)
- if ws then
- local success, error_message = pcall(function()
- ws.send(json.encode(data))
- end)
- if not success then
- print("Error sending data:", error_message)
- shell.execute("turtle_main")
- end
- else
- print("WebSocket connection does not exist. Unable to send data.")
- -- Optionally, you can attempt to reconnect or handle the situation accordingly.
- end
- end
- function forward()
- local result = turtle.forward()
- if result then
- if pos["facing"] == "north" then
- pos["z"] = pos["z"] - 1
- elseif pos["facing"] == "east" then
- pos["x"] = pos["x"] + 1
- elseif pos["facing"] == "south" then
- pos["z"] = pos["z"] + 1
- elseif pos["facing"] == "west" then
- pos["x"] = pos["x"] - 1
- end
- print(textutils.serialise(pos))
- end
- return result
- end
- function back()
- local result = turtle.back()
- if result then
- if pos["facing"] == "north" then
- pos["z"] = pos["z"] + 1
- elseif pos["facing"] == "east" then
- pos["x"] = pos["x"] - 1
- elseif pos["facing"] == "south" then
- pos["z"] = pos["z"] - 1
- elseif pos["facing"] == "west" then
- pos["x"] = pos["x"] + 1
- end
- print(textutils.serialise(pos))
- end
- return result
- end
- function left()
- local result = turtle.turnLeft()
- if result then
- if pos["facing"] == "north" then
- pos["facing"] = "west"
- elseif pos["facing"] == "east" then
- pos["facing"] = "north"
- elseif pos["facing"] == "south" then
- pos["facing"] = "east"
- elseif pos["facing"] == "west" then
- pos["facing"] = "south"
- end
- print(textutils.serialise(pos))
- end
- return result
- end
- function right()
- local result = turtle.turnRight()
- if result then
- if pos["facing"] == "north" then
- pos["facing"] = "east"
- elseif pos["facing"] == "east" then
- pos["facing"] = "south"
- elseif pos["facing"] == "south" then
- pos["facing"] = "west"
- elseif pos["facing"] == "west" then
- pos["facing"] = "north"
- end
- print(textutils.serialise(pos))
- end
- return result
- end
- function up()
- local result = turtle.up()
- if result then
- pos["y"] = pos["y"] + 1
- print(textutils.serialise(pos))
- end
- return result
- end
- function down()
- local result = turtle.down()
- if result then
- pos["y"] = pos["y"] - 1
- print(textutils.serialise(pos))
- end
- return result
- end
- connect()
- -- Check WebSocket server
- if not ws then
- print("Failed to connect to WebSocket server:", err)
- return
- end
- print("Connected to Server. trying to login...")
- sendData(loginInfo)
- -- Receive and handle messages from the WebSocket server
- while true do
- --print("Waiting on server...")
- local inMessageStr, err = ws.receive()
- local inMessage = json.decode(inMessageStr)
- if not inMessageStr then
- print("Failed to receive message:", err)
- break
- else
- if not loginInfo["loggedIn"] then
- loginInfo["loggedIn"] = inMessage["loggedIn"]
- print("Loginstatus: " .. tostring(loginInfo["loggedIn"]))
- end
- if loginInfo["loggedIn"] then
- local serverCode, err = load(inMessage["code"], "whatever", "t", _ENV)
- if serverCode then
- response = serverCode() -- Execute the loaded code
- else
- print("Error:", err)
- end
- --inspecting ground
- local frontBlock, frontInfo = turtle.inspect()
- local upBlock, upInfo = turtle.inspectUp()
- local downBlock, downInfo = turtle.inspectDown()
- inspect = {
- ["frontBlock"] = frontBlock,
- ["upBlock"] = upBlock,
- ["downBlock"] = downBlock,
- ["frontInfo"] = frontInfo,
- ["upInfo"] = upInfo,
- ["downInfo"] = downInfo
- }
- --gathering information to send
- info = {
- ["response"] = response,
- ["fuel"] = turtle.getFuelLevel(),
- ["inspect"] = inspect,
- ["pos"] = pos,
- ["device"] = loginInfo.device,
- ["loggedIn"] = loginInfo.loggedIn,
- ["name"] = os.getComputerLabel()
- }
- fs.delete("latestPos")
- local file = fs.open("latestPos","a")
- file.write(json.encode(pos))
- file.close()
- sendData(info)
- end
- end
- end
- -- Close the WebSocket connection when done
- ws.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement