Advertisement
NanoBob

Storage server

Mar 5th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.47 KB | None | 0 0
  1. local localChannel=666
  2.  
  3. if fs.exists("event")==false then shell.run("pastebin get L7KYC10V event") end
  4. os.loadAPI("event")
  5.  
  6. if fs.exists("utils")==false then shell.run("pastebin get c4CBkgKV utils") end
  7. os.loadAPI("utils")
  8.  
  9. local taskQue={
  10.    
  11. }
  12.  
  13. local turtles={
  14.    
  15. }
  16.  
  17. local chests={
  18.    
  19. }
  20.  
  21. for _,name in pairs(peripheral.getNames()) do
  22.     local per=peripheral.wrap(name)
  23.     if per.pushItem~=nil then
  24.         chests[#chests+1]={chest=per}
  25.     end
  26. end
  27.  
  28. local items={}
  29. local modems={}
  30. local drop={x=468,y=48,z=1072}
  31.  
  32. function init()
  33.     modem=peripheral.wrap("back")
  34.     modem.open(localChannel)
  35.     modems["back"]=modem
  36.     modems["wired"]=modem
  37.     wireless=peripheral.wrap("top")
  38.     wireless.open(localChannel)
  39.     modems["top"]=wireless
  40.     modems["wireless"]=wireless
  41. end
  42. init()
  43.  
  44. function indexItems()
  45.     items={}
  46.     for chestID,chest in pairs(chests) do
  47.         for i,stack in pairs(chest.chest.getAllStacks()) do
  48.             local data=stack.all()
  49.             if items[string.lower(data.display_name)]==nil then
  50.                 items[string.lower(data.display_name)]={[chestID]=data.qty}
  51.             else
  52.                 if items[string.lower(data.display_name)][chestID]==nil then
  53.                     items[string.lower(data.display_name)][chestID]=data.qty
  54.                 else
  55.                     items[string.lower(data.display_name)][chestID]=items[string.lower(data.display_name)][chestID]+data.qty
  56.                 end            
  57.             end
  58.         end
  59.     end
  60. end
  61. indexItems()
  62.  
  63. function getItemCount(name)
  64.     local item=items[string.lower(name)]
  65.     if item==nil then return 0 end
  66.     local count=0
  67.     for chestID,itemCount in pairs(item) do
  68.         count=count+itemCount
  69.     end
  70.     return count
  71. end
  72.  
  73. function getItemPosition(name,count)
  74.     local item=items[string.lower(name)]
  75.     if item==nil then return end
  76.     local positions={}
  77.     if count==0 then
  78.         count=1
  79.     end
  80.     local remainder=count
  81.     for chestID,itemCount in pairs(item) do
  82.         remainder=remainder-itemCount
  83.         positions[#positions+1]={pos=chests[chestID].pos,count=itemCount}
  84.         if remainder<=0 then
  85.             return positions
  86.         end
  87.     end
  88.     return positions
  89. end
  90.  
  91. function getIdleTurtle()
  92.     for id,turtle in pairs(turtles) do
  93.         if turtle.status=="idle" then
  94.             return turtle
  95.         end
  96.     end
  97.     return nil
  98. end
  99.  
  100. function statusUpdate(channel,turtle,previous,new)
  101.     print("Que:\n"..textutils.serialise(taskQue))
  102.     if new=="idle" then
  103.         turtle.task=nil
  104.         if #taskQue==0 then return end
  105.         modems["wireless"].transmit(channel,localChannel,taskQue[1])
  106.         for id,data in ipairs(taskQue) do
  107.             taskQue[id-1]=data
  108.         end
  109.         taskQue[0]=nil
  110.         taskQue[#taskQue]=nil
  111.     end
  112. end
  113. event.addHandler("onTurtleStatusChange",statusUpdate)
  114.  
  115. local messageHandles={
  116.     ["ping"]=function(replyChannel,message)
  117.         if turtles[replyChannel]==nil then return end
  118.         return "pong"
  119.     end,
  120.    
  121.     ["getItemCount"]=function(replyChannel,message)
  122.         indexItems()
  123.         local itemName=utils.split(message,"|",2)
  124.         return "returnItemCount|"..getItemCount(itemName)
  125.     end,
  126.    
  127.     ["requestItemList"]=function(replyChannel,message)
  128.         indexItems()
  129.         local itemString=""
  130.         for itemname,chests in pairs(items) do
  131.             itemString=itemString..itemname..","..getItemCount(itemname)..";"
  132.         end
  133.         return "returnItemList|"..itemString
  134.     end,
  135.    
  136.     ["requestTurtles"]=function(replyChannel,message)
  137.         local turtleString=""
  138.         for channel,turtle in pairs(turtles) do
  139.             turtleString=turtleString..channel..","..turtle.status..","..tostring(turtle.task)..";"
  140.         end
  141.         return "returnTurtles|"..turtleString
  142.     end,
  143.    
  144.     ["requestQue"]=function(replyChannel,message)
  145.         local queString=""
  146.         for _,queCommand in pairs(taskQue) do
  147.             queString=queString..string.gsub(queCommand,"|",";").."|"
  148.         end
  149.         return "returnQue|"..queString
  150.     end,
  151.    
  152.     ["requestItem"]=function(replyChannel,message)
  153.         indexItems()
  154.         local itemName=utils.split(message,"|",2)
  155.         local count=tonumber(utils.split(message,"|",3))
  156.         print("Item request: "..itemName.."x"..count)
  157.         local remainder=count
  158.         for chestID,chest in pairs(chests) do
  159.             for slot,stack in pairs(chest.chest.getAllStacks()) do
  160.                 local data=stack.all()
  161.                 if string.lower(data.display_name)==itemName then
  162.                     print(slot..":"..count)
  163.                     local count=data.qty
  164.                     if count>remainder then
  165.                         chest.chest.pushItem("up",slot,remainder)
  166.                         remainder=0
  167.                     else
  168.                         chest.chest.pushItem("up",slot,count)
  169.                         remainder=remainder-count
  170.                     end
  171.                     if remainder<=0 then
  172.                         return
  173.                     end
  174.                 end
  175.             end
  176.         end
  177.        
  178.     end,
  179.    
  180.     ["getItemPosition"]=function(replyChannel,message)
  181.         indexItems()
  182.         local itemName=utils.split(message,"|",2)
  183.         local posString=""
  184.         local positions=getItemPosition(itemName)
  185.         if positions==nil then return end
  186.         for _,pos in pairs(positions) do
  187.             posString=posString..x..","..y..","..z.."|"
  188.         end
  189.         return posString
  190.     end,
  191.    
  192.     ["sendToTurtles"]=function(replyChannel,message)
  193.         local turtleChannel=utils.split(message,"|",2)
  194.         local message=utils.split(message,"|",3)
  195.         local turtle=turtles[replyChannel]
  196.         turtle.task=message
  197.         turtle.status="working"
  198.        
  199.     end,
  200.    
  201.     ["indexItems"]=function(replyChannel,message)
  202.         indexItems()
  203.         return "confirmed"
  204.     end,
  205.    
  206.     ["assign"]=function(replyChannel)
  207.         if turtles[replyChannel]==nil then
  208.             turtles[replyChannel]={
  209.                 channel=replyChannel,
  210.                 status="idle",
  211.             }
  212.             event.trigger("onTurtleAssign",replyChannel,turtles[replyChannel],"confirmed")
  213.             event.trigger("onTurtleStatusChange",replyChannel,turtles[replyChannel],false,"idle")
  214.             return "confirmed"
  215.         else
  216.             event.trigger("onTurtleAssign",replyChannel,turtles[replyChannel],"denied")
  217.             return "denied"
  218.         end
  219.     end,
  220.    
  221.     ["status"]=function(replyChannel,message)
  222.         if turtles[replyChannel]==nil then
  223.             return "denied"
  224.         end
  225.         local previous=turtles[replyChannel].status
  226.         local status=utils.split(message,"|",2)
  227.         turtles[replyChannel].status=status
  228.         event.trigger("onTurtleStatusChange",replyChannel,turtles[replyChannel],previous,status)
  229.     end,
  230.    
  231.     ["requestDropoff"]=function(replyChannel,message)
  232.         return "returnDropoff|"..drop.x..","..drop.y..","..drop.z
  233.     end,
  234.    
  235.     ["requestRestPos"]=function(replyChannel,message)
  236.         return "returnHomePos|"..rest.x..","..rest.y..","..rest.z
  237.     end,
  238. }
  239.  
  240.  
  241. function handleMessages(side,channel,replyChannel,message)
  242.     print(replyChannel.." : "..message)
  243.     local messageType=utils.split(message,"|",1)
  244.     if messageHandles[messageType]==nil then return end
  245.     local returnMessage=messageHandles[messageType](replyChannel,message)
  246.     if returnMessage==nil then return end
  247.     modems[side].transmit(replyChannel,localChannel,returnMessage)
  248. end
  249. event.addHandler("modem_message",handleMessages)
  250.  
  251. while true do
  252.     event.handleCCEvents()
  253. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement