JereTheJuggler

Untitled

Mar 9th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. local root = shell.resolve("")
  2. local configPath = root.."/request/vars.config"
  3. local ordersPath = root.."/request/orders.config"
  4.  
  5. local outputInventory = nil
  6.  
  7. local connectionFreq = 23
  8. local replyFreq = 24
  9.  
  10. local modem = nil
  11.  
  12. term.clear()
  13.  
  14. for _,p in pairs(peripheral.getNames()) do
  15. if peripheral.getType(p) == "modem" then
  16. if peripheral.call(p,"isWireless") then
  17. modem = peripheral.wrap(p)
  18. break
  19. end
  20. end
  21. end
  22. if not modem.isOpen(connectionFreq) then
  23. modem.open(connectionFreq)
  24. end
  25. if not modem.isOpen(replyFreq) then
  26. modem.open(replyFreq)
  27. end
  28.  
  29. if fs.exists(configPath) then
  30. local file = fs.open(configPath,"r")
  31. local configData = textutils.unserialize(file.readAll())
  32. file.close()
  33. if configData.outputInventory ~= nil then
  34. if peripheral.isPresent(configData.outputInventory) then
  35. outputInventory = peripheral.wrap(configData.outputInventory)
  36. end
  37. end
  38. end
  39. if outputInventory == nil then
  40. term.setCursorPos(1,1)
  41. while outputInventory == nil do
  42. term.setTextColor(colors.yellow)
  43. print("Please connect an inventory to the network to serve as an output")
  44. local e,p1,p2,p3,p4,p5 = os.pullEvent()
  45. if e == "peripheral" then
  46. local perip = peripheral.wrap(p1)
  47. if perip.pullItems ~= nil then
  48. outputInventory = perip
  49. outputInventory.address = p1
  50. else
  51. term.setTextColor(colors.red)
  52. print("Invalid peripheral specified:")
  53. term.setTextColor(colors.white)
  54. print("\""..p1.."\"")
  55. print("")
  56. end
  57. end
  58. end
  59. local file = fs.open(configPath,"w")
  60. file.writeLine(textutils.serialize({
  61. output=outputInventory.address
  62. }))
  63. file.close()
  64. term.clear()
  65. end
  66.  
  67. term.clear()
  68. term.setCursorPos(1,1)
  69.  
  70. local inventories = {}
  71. for _,p in pairs(peripheral.getNames()) do
  72. if p ~= "front" and
  73. p ~= "back" and
  74. p ~= "left" and
  75. p ~= "right" and
  76. p ~= "top" and
  77. p ~= "bottom" and
  78. p ~= outputInventory.address then
  79. local perip = peripheral.wrap(p)
  80. if perip.pullItems ~= nil and type(perip.pullItems) == "function" then
  81. perip.address = p
  82. print("Found inventory \""..p.."\"")
  83. table.insert(inventories,perip)
  84. end
  85. end
  86. end
  87.  
  88. local running = true
  89. local orders = {}
  90.  
  91. if fs.exists(ordersPath) then
  92. local file = fs.open(ordersPath,"r")
  93. orders = textutils.unserialize(file.readAll())
  94. file.close()
  95. end
  96.  
  97. local function saveOrders()
  98. local file = fs.open(ordersPath,"w")
  99. file.writeLine(textutils.serialize(orders))
  100. file.close()
  101. end
  102.  
  103. local function listener()
  104. local e,p1,p2,p3,p4,p5 = os.pullEvent()
  105. if e == "peripheral" then
  106. if p1 ~= "front" and
  107. p1 ~= "back" and
  108. p1 ~= "left" and
  109. p1 ~= "right" and
  110. p1 ~= "top" and
  111. p1 ~= "bottom" then
  112. local perip = peripheral.wrap(p1)
  113. if perip.pullItems ~= nil and type(perip.pullItems) == "function" then
  114. perip.address = p1
  115. table.insert(inventories,perip)
  116. end
  117. end
  118. elseif e == "peripheral_detach" then
  119. for i,p in pairs(inventories) do
  120. if p.address == p1 then
  121. table.remove(inventories,i)
  122. break
  123. end
  124. end
  125. elseif e == "modem_message" then
  126. local side,freq,reply,message = p1,p2,p3,p4
  127. if freq == connectionFreq then
  128. if type(message) == "table" and message.operation ~= nil then
  129. if message.operation == "add_orders" then
  130. print("received order")
  131. if message.items ~= nil and type(message.items) == "table" then
  132. for _,order in ipairs(message.items) do
  133. if type(order) == "table" then
  134. if order.name ~= nil and order.qty ~= nil then
  135. local added = false
  136. for i,o in pairs(orders) do
  137. if o.name == order.name then
  138. orders[i].qty = orders[i].qty + order.qty
  139. added = true
  140. break
  141. end
  142. end
  143. if not added then
  144. table.insert(orders,{
  145. name=order.name,
  146. qty=order.qty
  147. })
  148. end
  149. end
  150. end
  151. end
  152. end
  153. elseif message.operation == "get_orders" then
  154. modem.transmit(reply,0,{
  155. operation="get_orders",
  156. orders=orders
  157. })
  158. elseif message.operation == "edit_order" then
  159. if message.oldName ~= nil and message.name ~= nil and message.qty ~= nil then
  160. for i,o in pairs(orders) do
  161. if o.name == message.oldName then
  162. orders[i] = {
  163. name=message.name,
  164. qty=message.qty
  165. }
  166. break
  167. end
  168. end
  169. end
  170. elseif message.operation == "remove_order" then
  171. if message.name ~= nil then
  172. for i,o in pairs(orders) do
  173. if o.name == message.name then
  174. table.remove(orders,i)
  175. break
  176. end
  177. end
  178. end
  179. end
  180. end
  181. end
  182. end
  183. end
  184.  
  185. local function main()
  186. while running do
  187. if #orders > 0 then
  188. local completedOrders = {}
  189. for i=1,#orders,1 do
  190. local o = orders[i]
  191. local skipOrder = false
  192. for _,inv in pairs(inventories) do
  193. local skipInv = false
  194. for slot=1,inv.size(),1 do
  195. local meta = inv.getItemMeta(slot)
  196. if meta ~= nil then
  197. if string.lower(meta.displayName) == string.lower(o.name) then
  198. local name = o.name
  199. local transferQty = o.qty
  200. if meta.count < o.qty then
  201. transferQty = meta.count
  202. end
  203. while transferQty > 0 do
  204. local numTransfered = inv.pushItems(outputInventory.address,slot,transferQty)
  205. transferQty = transferQty - numTransfered
  206. if transferQty > 0 then
  207. --output chest must be full. wait for some room to be made
  208. sleep(1)
  209. --check for anything that could break the script while it is waiting
  210.  
  211. --was the inventory removed?
  212. if not peripheral.isPresent(inv.address) then
  213. skipInv = true
  214. break
  215. end
  216. --was the order removed?
  217. if #orders < i then
  218. skipOrder = true
  219. break
  220. end
  221. if orders[i].name ~= name then
  222. skipOrder = true
  223. break
  224. end
  225. end
  226. end
  227. if meta.count < o.qty then
  228. orders[i].qty = orders[i].qty - meta.count
  229. else
  230. table.insert(completedOrders,orders[i].name)
  231. end
  232. end
  233. end
  234. if skipInv then
  235. break
  236. end
  237. if skipOrder then
  238. break
  239. end
  240. end
  241. if skipOrder then
  242. break
  243. end
  244. end
  245. end
  246. for _,n in pairs(completedOrders) do
  247. local i = 1
  248. while i <= #orders do
  249. if orders[i].name == n then
  250. table.remove(orders,i)
  251. else
  252. i = i + 1
  253. end
  254. end
  255. end
  256. else
  257. sleep(1)
  258. end
  259. end
  260. end
  261.  
  262. parallel.waitForAll(main,listener)
Add Comment
Please, Sign In to add comment