Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables
- local denominations = {1,10,32,64,-2,-5}
- local denominationsNames = {" 01 "," 10 "," 32 "," 64 ","2stacks","5stacks"}
- local catigoryColumnWidth = 11
- local itemColumnWidth = 12
- local selectedCatigory = 0
- local selectedItem = nil
- local sideBarOpen = false
- local catigories = {}
- local items = {}
- local sleepTimer = nil
- local selectedItemStacksize = nil
- -- Load Tools API
- if os.loadAPI("tools")==false then error("Failed to load Tools API!") end
- -- Get monitor
- local monitor = peripheral.find("monitor")
- if monitor==nil then error("Monitor not found!") end
- -- Prepare blank strings
- local catigoryColumnLineBG = string.createEmpty(catigoryColumnWidth-1)
- local itemColumnLineBG = string.createEmpty(itemColumnWidth)
- -- Set User Comp ID
- if http.queueFriendlyRequest("http://localhost/storage.php?func=setUserComputer", "cID="..os.getComputerID(), true)==nil then error("Failed to set user comp ID!") end
- -- Fail func
- local function fail(message)
- term.redirect(monitor)
- tools.gui.reset()
- term.restore()
- error(message)
- end
- -- Draw Functions
- local function listCatigories(reloadCatigories)
- if reloadCatigories then
- catigories = string.parse(http.queueFriendlyRequest("http://localhost/storage.php?func=getCatigories", nil, true), ",")
- if catigories==nil then fail("Failed to list catigories") end
- catigories[table.getn(catigories)+1] = "Auto-craft"
- end
- term.redirect(monitor)
- local _, height = term.getSize()
- tools.gui.drawRect(1, 1, catigoryColumnWidth-1, height, colors.black)
- for i=1, table.getn(catigories), 1 do
- if i == selectedCatigory then
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.white)
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lightGray)
- end
- term.setCursorPos(1, i) term.write(catigoryColumnLineBG)
- term.setCursorPos(1, i) term.write(catigories[i])
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.restore()
- end
- local function listItems(catigory, reloadItems)
- local itemsList = string.parse(http.queueFriendlyRequest("http://localhost/storage.php?func=listItemsByCatigory&catigory="..catigory, nil, true), ",")
- if itemsList==nil then fail("Failed to list items") end
- term.redirect(monitor)
- local width, height = term.getSize()
- tools.gui.drawRect(catigoryColumnWidth + 1, 1, width-catigoryColumnWidth, height, colors.black)
- local row = 1
- local column = 1
- items = { }
- items[column] = {}
- for i=1, table.getn(itemsList), 1 do
- term.setCursorPos(catigoryColumnWidth + 1 + (column-1)*itemColumnWidth, row)
- term.write(itemsList[i])
- items[column][row] = itemsList[i]
- row = row + 1
- if row > height then
- column = column + 1
- row = 1
- items[column] = {}
- end
- end
- term.restore()
- end
- local function getAmountString(amount, stacksize)
- local stacks = math.floor(amount/stacksize)
- local remainder = amount % stacksize
- if not math.isEvenLength(stacksize) then
- stacksize = "0"..stacksize
- end
- if stacks == 0 then
- if math.isEvenLength(remainder) then
- return "" .. remainder
- else
- return "0"..remainder
- end
- else
- if stacksize=="01" then
- if math.isEvenLength(stacks) then
- return ""..stacks
- else
- return "0"..stacks
- end
- else
- if remainder == 0 then
- if math.isEvenLength(stacks) then
- return "0"..stacks.."x"..stacksize
- else
- return stacks.."x"..stacksize
- end
- else
- if math.isEvenLength(stacks) and not math.isEvenLength(remainder) then
- return stacks.."x"..stacksize.."+0"..remainder
- elseif not math.isEvenLength(stacks) and math.isEvenLength(remainder) then
- return "0"..stacks.."x"..stacksize.."+"..remainder
- else
- return stacks.."x"..stacksize.."+"..remainder
- end
- end
- end
- end
- end
- local function getDenominationName(index, stacksize)
- if not math.isEvenLength(stacksize) then stacksize = "0"..stacksize end
- local text = denominationsNames[index]
- if text=="2stacks" then
- text = "2x"..stacksize
- elseif text=="5stacks" then
- text = "5x"..stacksize
- end
- return text
- end
- local function getDenominationAmount(index)
- local amount = denominations[index]
- local stacksize = selectedItemStacksize or 1
- if amount < 0 then
- return math.abs(amount)*stacksize
- end
- return amount
- end
- local function openTakeItemSidebar(name)
- sideBarOpen = true
- local amount = tonumber(http.queueFriendlyRequest("http://localhost/storage.php?func=getItemAmountByName&name="..name, nil, true))
- if amount==nil then fail("Failed to open sidebar! (Amount nil)") end
- local stacksize = tonumber(http.queueFriendlyRequest("http://localhost/storage.php?func=getStacksizeByName&name="..name, nil, true))
- if stacksize==nil then fail("Failed to open sidebar! (Stacksize nil)") end
- selectedItemStacksize = stacksize
- local amountStr = getAmountString(amount, stacksize)
- term.redirect(monitor)
- local w, h = term.getSize()
- paintutils.drawLine(w-20, 1, w-20, h, colors.white)
- tools.gui.drawRect(w-19, 1, 19, h, colors.gray)
- tools.gui.writeHighlighted(w-9-math.ceil(#name/2), 2, name, colors.gray)
- tools.gui.writeHighlighted(w-9-math.ceil(#amountStr/2), 3, amountStr, colors.gray)
- for i=1,table.getn(denominations),2 do
- tools.gui.writeHighlighted(w-14, i+5, getDenominationName(i, stacksize), colors.lime)
- tools.gui.writeHighlighted(w-8, i+5, getDenominationName(i+1, stacksize), colors.lime)
- end
- term.restore()
- end
- local function updateSidebarItemAmount()
- if sideBarOpen==false then return end
- local name = items[selectedItem[1]][selectedItem[2]]
- local amount = tonumber(http.queueFriendlyRequest("http://localhost/storage.php?func=getItemAmountByName&name="..name, nil, true))
- if amount==nil then fail("Failed to update sidebar! (Amount nil)") end
- local stacksize = tonumber(http.queueFriendlyRequest("http://localhost/storage.php?func=getStacksizeByName&name="..name, nil, true))
- if stacksize==nil then fail("Failed to update sidebar! (Stacksize nil)") end
- selectedItemStacksize = stacksize
- local amountStr = getAmountString(amount, stacksize)
- term.redirect(monitor)
- local w, h = term.getSize()
- paintutils.drawLine(w-19, 3, w, 3, colors.gray)
- tools.gui.writeHighlighted(w-9-math.ceil(#amountStr/2), 3, amountStr, colors.gray)
- term.restore()
- end
- local function refresh()
- term.redirect(monitor)
- tools.gui.reset()
- selectedCatigory = 0
- selectedItem = nil
- selectedItemStacksize = nil
- sideBarOpen = false
- listCatigories(true)
- term.restore()
- end
- local function showErrorNotice()
- term.redirect(monitor)
- local _, height = term.getSize()
- term.setTextColor(colors.red)
- term.setCursorPos(1, height)
- term.write("ERROR!")
- term.setTextColor(colors.white)
- term.restore()
- end
- local function clearErrorNotice()
- term.redirect(monitor)
- local _, height = term.getSize()
- term.setCursorPos(1, height)
- term.write(" ")
- term.restore()
- end
- -- local function showError(message)
- -- showingError = true
- -- tools.gui.reset()
- -- term.redirect(monitor)
- -- local width, height = term.getSize()
- -- term.setTextColor(colors.red)
- -- term.setCursorPos(math.floor((w/2)-(#message/2)), math.floor(h/2))
- -- term.write(message)
- -- term.setTextColor(colors.white)
- -- term.restore()
- -- end
- -- Button Functions
- local function catigoryButtonClick(catigoryIndex)
- selectedCatigory = catigoryIndex
- selectedItem = nil
- selectedItemStacksize = nil
- sideBarOpen = false
- listCatigories(false)
- listItems(catigories[catigoryIndex])
- end
- local function itemButtonClick(column, row)
- term.redirect(monitor)
- if selectedItem~=nil then
- term.setCursorPos(catigoryColumnWidth + 1 + (selectedItem[1]-1)*itemColumnWidth, selectedItem[2])
- term.write(itemColumnLineBG)
- term.setCursorPos(catigoryColumnWidth + 1 + (selectedItem[1]-1)*itemColumnWidth, selectedItem[2])
- term.write(items[selectedItem[1]][selectedItem[2]])
- end
- selectedItem = {column, row}
- term.setBackgroundColor(colors.green)
- term.setCursorPos(catigoryColumnWidth + 1 + (column-1)*itemColumnWidth, row)
- term.write(itemColumnLineBG)
- term.setCursorPos(catigoryColumnWidth + 1 + (column-1)*itemColumnWidth, row)
- term.write(items[column][row])
- term.setBackgroundColor(colors.black)
- term.restore()
- openTakeItemSidebar(items[column][row])
- end
- local function amountButtonClick(amount)
- local name = items[selectedItem[1]][selectedItem[2]]
- http.request("http://localhost/storage.php?func=pullItem", "name="..name.."&amt="..amount)
- end
- local function amountButtonClickHandler(response)
- local response = string.parse(response, ",")
- if response==nil then fail("Failed to pull items (parse A failed)") end
- for i=1,table.getn(response),1 do
- local command = string.parse(response[i], " ")
- if command==nil then fail("Failed to pull items (parse B failed)") end
- if command[1] == "PULL" then
- if tonumber(command[2]) ~= nil then
- rednet.send(tonumber(command[2]), response[i])
- end
- end
- end
- updateSidebarItemAmount()
- end
- refresh()
- sleepTimer = os.startTimer(60)
- print("Q to quit, C to clear, U to update, A to add item,")
- print("S to update all sorters")
- while true do
- local event, a, b, c = os.pullEvent()
- clearErrorNotice()
- local resetSleepTimer = true
- 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 display")
- shell.run("downloader screensaver")
- os.reboot()
- elseif a == "a" then
- print("<id>, <name>, <stacksize>, <catigory>, [meta]:")
- local input = read()
- if input ~= "" and input ~= "exit" then
- input = string.parse(input, ",")
- write("Adding item... ")
- if table.getn(input)>=4 and tonumber(input[1])~=nil and #input[2]>0 and tonumber(input[3])~=nil and #input[4]>0 then
- local post = "iID="..string.trim(input[1]).."&name="..string.trim(input[2]).."&stacksize="..string.trim(input[3]).."&catigory="..string.trim(input[4])
- if tonumber(input[5])~=nil then post = post.."&meta="..input[5] end
- tools.gui.load(nil, "return http.queueFriendlyRequest(\"http://localhost/storage.php?func=addItem\", \""..post.."\", true)")
- else
- print("Bad arguments.")
- end
- end
- elseif a == "s" then
- rednet.broadcast("all_sorters_update")
- print("All sorters are updating.")
- end
- elseif event=="http_success" then
- if string.startsWith(a, "http://localhost/storage.php?func=pullItem") then
- amountButtonClickHandler(b.readAll())
- end
- elseif event=="http_failure" then
- if string.startsWith(a, "http://localhost/storage.php?func=pullItem") then
- fail("Pull item failed (http_failure)")
- end
- elseif event=="monitor_touch" then
- local found=false
- if sideBarOpen then
- term.redirect(monitor)
- local w, h = term.getSize()
- term.restore()
- for i=1,table.getn(denominations),2 do
- if b>=w-14 and b<=w-10 and c==i+5 then
- amountButtonClick(getDenominationAmount(i))
- found = true
- elseif b>=w-8 and b<=w-4 and c==i+5 then
- amountButtonClick(getDenominationAmount(i+1))
- found = true
- end
- end
- end
- if not found then
- local column = math.ceil((b-catigoryColumnWidth)/itemColumnWidth)
- if items[column]~=nil and items[column][c]~=nil then
- itemButtonClick(column, c)
- elseif b<=catigoryColumnWidth and c<=table.getn(catigories) then
- catigoryButtonClick(c)
- end
- end
- elseif event=="rednet_message" then
- local command = string.parse(b, ":")
- if command~=nil and command[1] == "ERROR" then
- showErrorNotice()
- term.setTextColor(colors.red)
- print("Sorter Error:"..a..":"..command[2])
- term.setTextColor(colors.white)
- end
- elseif event=="timer" then
- if a==sleepTimer then
- shell.run("screensaver")
- refresh()
- else
- resetSleepTimer = false
- end
- end
- if resetSleepTimer then
- sleepTimer = os.startTimer(60)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement