Heroicos_HM

SocketTurtleMain

May 16th, 2021 (edited)
466
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---------------------------------------------------------------------------
  2. -- Importing requirements
  3. require "movement"
  4. require "info"
  5. require "inventory"
  6. require "mining"
  7. require "building"
  8. require "utility"
  9.  
  10. -- Loading the json library
  11. os.loadAPI("json")
  12.  
  13. ---------------------------------------------------------------------------
  14. -- Attempt to connect to the websocket
  15. print('Attempting Connection...')
  16. local ws, err = http.websocket("ws://24.251.40.32:8030")
  17.  
  18. if not ws then
  19.   return printError(err)
  20. end
  21.  
  22. ---------------------------------------------------------------------------
  23. -- Handles websocket responses
  24. --
  25. -- Params:
  26. --   res (table):
  27. --     A table containing the data received from the websocket
  28. function handleResponse(res)
  29.   if res.type == "FORWARD" then
  30.     num = tonumber(res.args[1])
  31.     moveForward(ws, num)
  32.   elseif res.type == "BACK" then
  33.     num = tonumber(res.args[1])
  34.     moveBack(ws, num)
  35.   elseif res.type == "UP" then
  36.     num = tonumber(res.args[1])
  37.     moveUp(ws, num)
  38.   elseif res.type == "DOWN" then
  39.     num = tonumber(res.args[1])
  40.     moveDown(ws, num)
  41.   elseif res.type == "LEFT" then
  42.     num = tonumber(res.args[1])
  43.     turnLeft(ws, num)
  44.   elseif res.type == "RIGHT" then
  45.     num = tonumber(res.args[1])
  46.     turnRight(ws, num)
  47.   elseif res.type == "DIGDOWN" then
  48.     digDown(ws)
  49.   elseif res.type == "DIG" then
  50.     dig(ws)
  51.   elseif res.type == "DIGUP" then
  52.     digUp(ws)
  53.   elseif res.type == "PLACEDOWN" then
  54.     placeDown(ws)
  55.   elseif res.type == "PLACE" then
  56.     place(ws)
  57.   elseif res.type == "PLACEUP" then
  58.     placeUp(ws)
  59.   elseif res.type == "LOCATION" then
  60.     getLocation(ws)
  61.   elseif res.type == "FUEL" then
  62.     getFuel(ws)
  63.   elseif res.type == "REFUELONE" then
  64.     res.args[1] = res.args[1] or turtle.getSelectedSlot()
  65.     refuelSingleSlot(tonumber(res.args[1]))
  66.     getInfo(ws)
  67.   elseif res.type == "REFUELALL" then
  68.     res.args[1] = res.args[1] or 1
  69.     res.args[2] = res.args[2] or 16
  70.     refuelAll(tonumber(res.args[1]), tonumber(res.args[2]))
  71.     getInfo(ws)
  72.   elseif res.type == "CALIBRATE" then
  73.     calibrate(tonumber(res.args[1]))
  74.     getInfo(ws)
  75.   elseif res.type == "INFO" then
  76.     getInfo(ws)
  77.   elseif res.type == "HOME" then
  78.     home(ws)
  79.   elseif res.type == "ECHO" then
  80.     if (res.args ~= "PING!") then
  81.       print(res.args)
  82.     end
  83.   elseif res.type == "SLOT" then
  84.     selectSlot(tonumber(res.args[1]))
  85.     getInfo(ws)
  86.   elseif res.type == "STARTQUARRY" then
  87.     quarry(ws, res.args['x'], res.args['y'], res.args['z'])
  88.   end
  89. end
  90.  
  91. ---------------------------------------------------------------------------
  92. -- Send message to the websocket to confirm connection
  93. ws.send(json.encode({type = "ECHO", args = "Connection Confirmed!"}))
  94.  
  95. ---------------------------------------------------------------------------
  96. -- Continually receive new instructions from the websocket until the
  97. -- quit message is received, then close the websocket connection.
  98. repeat
  99.   res = ws.receive()
  100.   res = json.decode(res)
  101.   handleResponse(res)
  102.  
  103. until res.type == "QUIT"
  104.  
  105. ws.close()
  106.  
RAW Paste Data