Advertisement
Riewest14

suicidesquidcc.lua

Sep 5th, 2021
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | None | 0 0
  1. local base_url = "http://73.181.45.168:5005/"
  2.  
  3. os.loadAPI("squidlib/common.lua")
  4.  
  5. function determine_tools()
  6.     slot = common.findEmptySlot(true)
  7.     if slot then
  8.         turtle.equipRight()
  9.         right_tool = nil
  10.         if turtle.getItemCount() > 0 then
  11.             right_tool = turtle.getItemDetail().name
  12.             turtle.equipRight()    
  13.         end
  14.         turtle.equipLeft()
  15.         left_tool = nil
  16.         if turtle.getItemCount() > 0 then
  17.             left_tool = turtle.getItemDetail().name
  18.             turtle.equipLeft()
  19.         end
  20.         return left_tool, right_tool
  21.     end
  22.     return false
  23. end
  24.  
  25. function basic_post(postdata, endpoint)
  26.     if not postdata then return end
  27.     local postdata_pairs = {}
  28.     for k,d in pairs(postdata) do
  29.         local pairstring = k .. "=" .. d
  30.         table.insert(postdata_pairs, pairstring)
  31.     end
  32.     local poststring = table.concat(postdata_pairs, "&")
  33.     endpoint = endpoint or ""
  34.     local result = http.post(base_url .. endpoint, poststring)
  35.     local response = false
  36.     if result then
  37.         response = result.readAll()
  38.         result.close()
  39.     end
  40.     return response
  41. end
  42.  
  43. function basePostData()
  44.     local post_data = {}
  45.     post_data["computer_id"] = os.getComputerID()
  46.     if turtle then
  47.         post_data["fuel"] = turtle.getFuelLevel()
  48.     end
  49.     local exists, side = common.findModem()
  50.     if exists then
  51.         local x, y, z = gps.locate(.2)
  52.         if z then
  53.             post_data["x_position"] = x
  54.             post_data["y_position"] = y
  55.             post_data["z_position"] = z
  56.         end
  57.     end
  58.     return post_data
  59. end
  60.  
  61. function register()
  62.     print("Registering With Python Server")
  63.     local post_data = basePostData()
  64.     post_data["label"] = os.getComputerLabel()
  65.     post_data["computer_type"] = "computer"
  66.     if turtle then
  67.         post_data["computer_type"] = "turtle"
  68.         local left_tool, right_tool = determine_tools()
  69.         if right_tool then
  70.             post_data["tool_right"] = right_tool
  71.         end
  72.         if left_tool then
  73.             post_data["tool_left"] = left_tool
  74.         end
  75.     end
  76.     local exists, side = common.findModem()
  77.     if exists then
  78.         post_data["modem_side"] = side
  79.     end
  80.     basic_post(post_data, "computers/register/")
  81. end
  82.  
  83. function update()
  84.     local post_data = basePostData()
  85.     basic_post(post_data, "computers/update/")
  86. end
  87.  
  88. function download_file(url, file, dir)
  89.     dir = dir or ""
  90.     local filepath = fs.combine(dir, file)
  91.     local content = http.get(base_url .. url).readAll()
  92.     if not content then
  93.         error("Could not connect to website")
  94.     end
  95.     local f = fs.open(filepath, "w")
  96.     f.write(content)
  97.     f.close()
  98. end
  99.  
  100. local function compile_items_data()
  101.     if not turtle then
  102.        return false
  103.     end
  104.     local inventory = {}
  105.     for i=1, 16 do
  106.         if turtle.getItemCount(i) > 0 then
  107.             local detail = turtle.getItemDetail(i)
  108.             table.insert(inventory, {
  109.                 name = detail.name,
  110.                 count = detail.count,
  111.                 slot = i
  112.             })
  113.         end
  114.     end
  115.     return inventory
  116. end
  117.  
  118. function getWorkOrder()
  119.     print("Asking For Work...")
  120.     local post_data = basePostData()
  121.     post_data['inventory'] = textutils.serializeJSON(compile_items_data())
  122.     local work_order_json = basic_post(post_data, "computers/get_work/")
  123.     if not work_order_json then
  124.         return false
  125.     end
  126.     local work_order = textutils.unserializeJSON(work_order_json)
  127.     if work_order.has_work then
  128.         print("Got Work!")
  129.         return work_order
  130.     end
  131.     print("No Work!")
  132.     return false
  133. end
  134.  
  135. function workCompleted(work_order)
  136.     print('Notifying Server of Work Completion')
  137.     local post_data = basePostData()
  138.     post_data.work_id = work_order.id
  139.     basic_post(post_data, "computers/work_completed/")
  140. end
  141.  
  142.  
  143. -- register()
  144. --update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement