Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local root = shell.resolve("")
- local configPath = root.."/request/vars.config"
- local ordersPath = root.."/request/orders.config"
- local outputInventory = nil
- local connectionFreq = 23
- local replyFreq = 24
- local modem = nil
- term.clear()
- for _,p in pairs(peripheral.getNames()) do
- if peripheral.getType(p) == "modem" then
- if peripheral.call(p,"isWireless") then
- modem = peripheral.wrap(p)
- break
- end
- end
- end
- if not modem.isOpen(connectionFreq) then
- modem.open(connectionFreq)
- end
- if not modem.isOpen(replyFreq) then
- modem.open(replyFreq)
- end
- if fs.exists(configPath) then
- local file = fs.open(configPath,"r")
- local configData = textutils.unserialize(file.readAll())
- file.close()
- if configData.outputInventory ~= nil then
- if peripheral.isPresent(configData.outputInventory) then
- outputInventory = peripheral.wrap(configData.outputInventory)
- end
- end
- end
- if outputInventory == nil then
- term.setCursorPos(1,1)
- while outputInventory == nil do
- term.setTextColor(colors.yellow)
- print("Please connect an inventory to the network to serve as an output")
- local e,p1,p2,p3,p4,p5 = os.pullEvent()
- if e == "peripheral" then
- local perip = peripheral.wrap(p1)
- if perip.pullItems ~= nil then
- outputInventory = perip
- outputInventory.address = p1
- else
- term.setTextColor(colors.red)
- print("Invalid peripheral specified:")
- term.setTextColor(colors.white)
- print("\""..p1.."\"")
- print("")
- end
- end
- end
- local file = fs.open(configPath,"w")
- file.writeLine(textutils.serialize({
- output=outputInventory.address
- }))
- file.close()
- term.clear()
- end
- term.clear()
- term.setCursorPos(1,1)
- local inventories = {}
- for _,p in pairs(peripheral.getNames()) do
- if p ~= "front" and
- p ~= "back" and
- p ~= "left" and
- p ~= "right" and
- p ~= "top" and
- p ~= "bottom" and
- p ~= outputInventory.address then
- local perip = peripheral.wrap(p)
- if perip.pullItems ~= nil and type(perip.pullItems) == "function" then
- perip.address = p
- print("Found inventory \""..p.."\"")
- table.insert(inventories,perip)
- end
- end
- end
- local running = true
- local orders = {}
- if fs.exists(ordersPath) then
- local file = fs.open(ordersPath,"r")
- orders = textutils.unserialize(file.readAll())
- file.close()
- end
- local function saveOrders()
- local file = fs.open(ordersPath,"w")
- file.writeLine(textutils.serialize(orders))
- file.close()
- end
- local function listener()
- local e,p1,p2,p3,p4,p5 = os.pullEvent()
- if e == "peripheral" then
- if p1 ~= "front" and
- p1 ~= "back" and
- p1 ~= "left" and
- p1 ~= "right" and
- p1 ~= "top" and
- p1 ~= "bottom" then
- local perip = peripheral.wrap(p1)
- if perip.pullItems ~= nil and type(perip.pullItems) == "function" then
- perip.address = p1
- table.insert(inventories,perip)
- end
- end
- elseif e == "peripheral_detach" then
- for i,p in pairs(inventories) do
- if p.address == p1 then
- table.remove(inventories,i)
- break
- end
- end
- elseif e == "modem_message" then
- local side,freq,reply,message = p1,p2,p3,p4
- if freq == connectionFreq then
- if type(message) == "table" and message.operation ~= nil then
- if message.operation == "add_orders" then
- print("received order")
- if message.items ~= nil and type(message.items) == "table" then
- for _,order in ipairs(message.items) do
- if type(order) == "table" then
- if order.name ~= nil and order.qty ~= nil then
- local added = false
- for i,o in pairs(orders) do
- if o.name == order.name then
- orders[i].qty = orders[i].qty + order.qty
- added = true
- break
- end
- end
- if not added then
- table.insert(orders,{
- name=order.name,
- qty=order.qty
- })
- end
- end
- end
- end
- end
- elseif message.operation == "get_orders" then
- modem.transmit(reply,0,{
- operation="get_orders",
- orders=orders
- })
- elseif message.operation == "edit_order" then
- if message.oldName ~= nil and message.name ~= nil and message.qty ~= nil then
- for i,o in pairs(orders) do
- if o.name == message.oldName then
- orders[i] = {
- name=message.name,
- qty=message.qty
- }
- break
- end
- end
- end
- elseif message.operation == "remove_order" then
- if message.name ~= nil then
- for i,o in pairs(orders) do
- if o.name == message.name then
- table.remove(orders,i)
- break
- end
- end
- end
- end
- end
- end
- end
- end
- local function main()
- while running do
- if #orders > 0 then
- local completedOrders = {}
- for i=1,#orders,1 do
- local o = orders[i]
- local skipOrder = false
- for _,inv in pairs(inventories) do
- local skipInv = false
- for slot=1,inv.size(),1 do
- local meta = inv.getItemMeta(slot)
- if meta ~= nil then
- if string.lower(meta.displayName) == string.lower(o.name) then
- local name = o.name
- local transferQty = o.qty
- if meta.count < o.qty then
- transferQty = meta.count
- end
- while transferQty > 0 do
- local numTransfered = inv.pushItems(outputInventory.address,slot,transferQty)
- transferQty = transferQty - numTransfered
- if transferQty > 0 then
- --output chest must be full. wait for some room to be made
- sleep(1)
- --check for anything that could break the script while it is waiting
- --was the inventory removed?
- if not peripheral.isPresent(inv.address) then
- skipInv = true
- break
- end
- --was the order removed?
- if #orders < i then
- skipOrder = true
- break
- end
- if orders[i].name ~= name then
- skipOrder = true
- break
- end
- end
- end
- if meta.count < o.qty then
- orders[i].qty = orders[i].qty - meta.count
- else
- table.insert(completedOrders,orders[i].name)
- end
- end
- end
- if skipInv then
- break
- end
- if skipOrder then
- break
- end
- end
- if skipOrder then
- break
- end
- end
- end
- for _,n in pairs(completedOrders) do
- local i = 1
- while i <= #orders do
- if orders[i].name == n then
- table.remove(orders,i)
- else
- i = i + 1
- end
- end
- end
- else
- sleep(1)
- end
- end
- end
- parallel.waitForAll(main,listener)
Add Comment
Please, Sign In to add comment