Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Load Tools API
- write("Loading Tools API... ")
- if os.loadAPI("tools") then print("Success.") else print("Failed.") return end
- -- Get sorter ID
- local sID = os.getComputerID()
- local sLabel = os.getComputerLabel()
- -- Get the user computer ID
- local userID = tools.gui.load("Getting user computer", "return tonumber(http.queueFriendlyRequest(\"http://localhost/storage.php?func=getUserComputer\", nil, true))")
- if not userID then return end
- -- Get the sorter peripheral
- local sorter = tools.gui.load("Getting sorter", "return peripheral.find(\"interactiveSorter\")")--"for n,side in ipairs(rs.getSides()) do if peripheral.getType(side) == \"interactiveSorter\" then return peripheral.wrap(side) end end")
- if not sorter then return end
- -- Register this sorter in the database
- local success = tools.gui.load("Initializing", "return http.queueFriendlyRequest(\"http://localhost/storage.php?func=initSorter\",\"sID="..sID.."\", true)", true)
- if not success then return end
- -- Get this sorter's facing direction
- local function getSorterDirection()
- local cursorX, cursorY = term.getCursorPos()
- write("Getting direction... ")
- local d = tonumber(http.queueFriendlyRequest("http://localhost/storage.php?func=getSorterDirection&sID="..sID, nil, true))
- if d==nil then
- print("Failed.")
- return nil
- elseif d>=0 and d<4 then
- print("Success.")
- return d
- else
- term.clearLine()
- term.setCursorPos(cursorX, cursorY)
- write("Please enter sorter direction: ")
- local input = read()
- cursorX, cursorY = term.getCursorPos()
- cursorY = cursorY-1
- term.setCursorPos(cursorX, cursorY)
- term.clearLine()
- write("Setting direction... ")
- if tonumber(input) and tonumber(input)>=0 and tonumber(input)<4 then
- input = tonumber(input)
- elseif tonumber(tools.directions[input]) then
- input = tonumber(tools.directions[input])
- else
- print("Bad direction.")
- return nil
- end
- local response = http.queueFriendlyRequest("http://localhost/storage.php?func=setSorterDirection", "&sID="..sID.."&dir="..input, true)
- if response==nil then
- print("Failed.")
- return nil
- else
- print("Success.")
- return input
- end
- end
- end
- local direction = getSorterDirection()
- if not direction then return end
- local chestInfo = {
- ["output"] = tools.directions.toSorter("up"),
- ["next"] = tools.directions.toSorter(direction),
- ["back"] = tools.directions.toSorter(tools.directions.rotate(direction, -2)),
- ["chest1"] = tools.directions.toSorter(tools.directions.rotate(direction, -1)),
- ["chest2"] = tools.directions.toSorter(tools.directions.rotate(direction, 1)),
- }
- -- Set this sorter's chest slot counts
- local success = tools.gui.load("Setting slots", "return http.queueFriendlyRequest(\"http://localhost/storage.php?func=setChestSizes\",\"sID="..sID.."&chest1="..(sorter.numSlots(chestInfo["chest1"]) or -1).."&chest2="..(sorter.numSlots(chestInfo["chest2"]) or -1).."\", true)", true)
- if not success then return end
- -- Reset screen
- os.queueFriendlySleep(1)
- local _, cursorY = term.getCursorPos()
- for i=1,6,1 do
- term.setCursorPos(1, cursorY-i)
- term.clearLine()
- end
- -- Check if items are already in sorter
- if sorter.getItem()~=nil then
- local id, amount = sorter.getItem()
- os.queueEvent("isort_item", id, amount)
- end
- -- Functions
- local function sendErrorToUser(message)
- if userID == nil then
- print("ERROR: "..message.."; cannot send")
- return
- end
- print("ERROR: "..message.."; sending to " .. userID)
- rednet.send(userID, "ERROR:" .. message)
- end
- local function extract(direction, id, outputDirection, amount)
- local loops = 0
- while amount > 0 do
- amount = amount - sorter.extract(direction, id, outputDirection, amount)
- loops = loops + 1
- if loops > 20 then
- return false
- end
- end
- return true
- end
- local function waitForNextSorterEmpty(extractingFromChest)
- local itemsInNextSorter = sorter.list(chestInfo["next"])
- while itemsInNextSorter ~= nil and next(itemsInNextSorter) ~= nil do
- if sorter.getItem()==nil and not extractingFromChest then
- break
- end
- os.queueFriendlySleep(0.05)
- itemsInNextSorter = sorter.list(chestInfo["next"])
- end
- end
- -- Message loop
- print("Commands: Q to quit, C to clear, U to update")
- while true do
- local event, a, b = os.pullEvent()
- if event=="char" then
- if a=="q" then
- return
- elseif a=="c" then
- tools.gui.reset()
- elseif a=="u" then
- shell.run("downloader tools")
- shell.run("downloader sorter")
- os.reboot()
- end
- elseif event=="isort_item" then
- local command = string.parse(http.queueFriendlyRequest("http://localhost/storage.php?func=recieveItem", "sID="..sID.."&iID="..a.."&amt="..b, true), ",")
- if command==nil then
- sendErrorToUser("recieveItem("..sID..", "..id..", "..amount..") A returned nil")
- else
- for i=1,table.getn(command),1 do
- if command[i]=="SENDNEXT" then
- waitForNextSorterEmpty(false)
- sorter.sort(chestInfo["next"])
- break
- else
- local subCommand = string.parse(command[i], ">")
- if tonumber(subCommand[1])~=nil and tonumber(subCommand[2])~=nil then
- sorter.sort(chestInfo["chest"..subCommand[2]], tonumber(subCommand[1]))
- else
- sendErrorToUser("recieveItem("..sID..", "..id..", "..amount..") returned bad command")
- end
- end
- end
- end
- elseif event=="redstone" and not string.startsWith(sLabel, "notfirst") then
- while rs.getInput("front") do
- local items=sorter.list(chestInfo["back"])
- local id, amount = next(items)
- id = tonumber(id)
- amount = tonumber(amount)
- if id~=nil and amount~=nil then
- local itemInfo = string.parse(http.queueFriendlyRequest("http://localhost/storage.php?func=getStackSizeAndCatigoryByID&iID="..id, nil, true), ",") -- [1]=stacksize, [2]=catigory
- if itemInfo==nil or tonumber(itemInfo[1])==nil or itemInfo[2]==nil then
- sendErrorToUser("Item info request returned nil")
- sorter.extract(chestInfo["back"], id, chestInfo["output"], amount)
- elseif itemInfo[2]==sLabel then
- itemInfo[1] = tonumber(itemInfo[1])
- if amount>itemInfo[1] then amount = itemInfo[1] end
- local command = string.parse(http.queueFriendlyRequest("http://localhost/storage.php?func=recieveItem", "sID="..sID.."&iID="..id.."&amt="..amount, true), ",")
- if command==nil then
- sendErrorToUser("recieveItem("..sID..", "..id..", "..amount..") B returned nil")
- else
- for i=1,table.getn(command),1 do
- if command[i]=="SENDNEXT" then
- waitForNextSorterEmpty(true)
- amount = amount - sorter.extract(chestInfo["back"], id, chestInfo["next"], amount)
- if amount==0 then break end
- else
- local subCommand = string.parse(command[i], ">")
- if tonumber(subCommand[1])~=nil and tonumber(subCommand[2])~=nil then
- amount = amount - sorter.extract(chestInfo["back"], id, chestInfo["chest"..subCommand[2]], tonumber(subCommand[1]))
- else
- sendErrorToUser("recieveItem("..sID..", "..id..", "..amount..") returned bad command")
- end
- end
- end
- end
- end
- end
- end
- elseif event=="rednet_message" then --This should be sent by the user's computer, with a command set from the database
- if b=="all_sorters_update" then
- shell.run("downloader tools")
- shell.run("downloader sorter")
- os.reboot()
- end
- local command = string.parse(b, " ")
- if command==nil then
- sendErrorToUser("Pull request is nil")
- elseif command[1]=="PULL" then
- if tonumber(command[3]) ~= nil and tonumber(command[4]) ~= nil and tonumber(command[5]) ~= nil then
- if not extract(chestInfo["chest"..tonumber(command[3])], tonumber(command[4]), chestInfo["output"], tonumber(command[5])) then
- sendErrorToUser("Chest extraction failed")
- end
- else
- sendErrorToUser("Bad pull request")
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment