ColdIV

sortingTurtle

Oct 4th, 2021 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. local programName = "sortingTurtle"
  2. local args = {...}
  3. local dtype = args[1]
  4.  
  5. function findDeviceIndex (t, target)
  6.     for i = 1, #t, 1 do
  7.         if t[i].id == target then
  8.             return i
  9.         end
  10.     end
  11.     return 0
  12. end
  13.  
  14. function client ()
  15.     local protocol = args[2] or "sortingTurtle"
  16.     local hostname = args[3] or "sortingTurtleServer"
  17.        
  18.     local host = rednet.lookup(protocol, hostname)
  19.    
  20.     if not rednet.send(host, "sortingTurtleConnect", protocol)  then
  21.         error("Could not send message.")
  22.     else
  23.         print("Connected!")
  24.     end
  25.    
  26.     local message
  27.     local sorting = false
  28.     while true do
  29.         if sorting then
  30.             -- sort stuff
  31.            
  32.             if false then -- if all items have been sorted
  33.             -- might want to add remaining items in chest as part of the message
  34.                 if not rednet.send(hosts[i], 'sortingTurtleDone', protocol)  then
  35.                     error("Could not send message.")
  36.                 end
  37.                 sorting = false
  38.             end
  39.         else
  40.             _, message, _ = rednet.receive(protocol)
  41.             -- Get Items + Amount from message and sort them
  42.             sorting = true
  43.         end
  44.     end
  45. end
  46.  
  47. function server ()
  48.     local protocol = args[2] or "sortingTurtle"
  49.     local name = args[3] or "sortingTurtleServer"
  50.     local devices = {}
  51.     rednet.host(protocol, name)
  52.     print ("Server running!")
  53.    
  54.     while true do
  55.         local deviceID
  56.         local message
  57.         -- waiting for sortingTurtle to connect
  58.         deviceID, message, _ = rednet.receive(protocol)
  59.         if message == "sortingTurtleConnect" then
  60.             print(deviceID .. " connected!")
  61.         end
  62.        
  63.         -- wait for input, then send them to be sorted
  64.        
  65.         -- Send commands to sortingTurtle
  66.         if not rednet.send(deviceID, message, protocol) then
  67.             error("Could not send message.")
  68.         end
  69.        
  70.         -- wait for response (sortingTurtleDone)
  71.         -- then wait for input again
  72.     end
  73.    
  74.     rednet.unhost(protocol)
  75. end
  76.  
  77. function main ()
  78.     if not rednet.isOpen("back") then
  79.         error("Could not open modem 'back'")
  80.         return
  81.     end
  82.    
  83.     if dtype == "client" then
  84.         client()
  85.     elseif dtype == "server" then
  86.         server()
  87.     elseif dtype == "update" then
  88.         shell.run("pastebin", "run", "FuQ3WvPs HmhXJtBm " .. programName)
  89.     elseif dtype == "help" then
  90.         prettyPrint ("", "Arguments:\n - " .. programName .. " server <protocol> <hostname>\n - " .. programName .. " client <protocol> <hostname> <username>\n - " .. programName .. " update\n - " .. programName .. " help\n", nil, "8")
  91.     else
  92.         error("Please specify the device type as 'server' or 'client' by passing it as the first argument.")
  93.     end
  94. end
  95.  
  96. rednet.open("back")
  97. main()
  98. rednet.close("back")
Add Comment
Please, Sign In to add comment