Advertisement
1amw31rd

ComputerCraft - Sorter Display

Jun 6th, 2013
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.83 KB | None | 0 0
  1. -- Variables
  2. local denominations = {1,10,32,64,-2,-5}
  3. local denominationsNames = {" 01 "," 10 "," 32 "," 64 ","2stacks","5stacks"}
  4. local catigoryColumnWidth = 11
  5. local itemColumnWidth = 12
  6. local selectedCatigory = 0
  7. local selectedItem = nil
  8. local sideBarOpen = false
  9. local catigories = {}
  10. local items = {}
  11. local sleepTimer = nil
  12. local selectedItemStacksize = nil
  13.  
  14. -- Load Tools API
  15. if os.loadAPI("tools")==false then error("Failed to load Tools API!") end
  16.  
  17. -- Get monitor
  18. local monitor = peripheral.find("monitor")
  19. if monitor==nil then error("Monitor not found!") end
  20.  
  21. -- Prepare blank strings
  22. local catigoryColumnLineBG = string.createEmpty(catigoryColumnWidth-1)
  23. local itemColumnLineBG = string.createEmpty(itemColumnWidth)
  24.  
  25. -- Set User Comp ID
  26. if http.queueFriendlyRequest("http://localhost/storage.php?func=setUserComputer", "cID="..os.getComputerID(), true)==nil then error("Failed to set user comp ID!") end
  27.  
  28. -- Fail func
  29. local function fail(message)
  30.     term.redirect(monitor)
  31.     tools.gui.reset()
  32.     term.restore()
  33.     error(message)
  34. end
  35.  
  36. -- Draw Functions
  37. local function listCatigories(reloadCatigories)
  38.     if reloadCatigories then
  39.         catigories = string.parse(http.queueFriendlyRequest("http://localhost/storage.php?func=getCatigories", nil, true), ",")
  40.         if catigories==nil then fail("Failed to list catigories") end
  41.         catigories[table.getn(catigories)+1] = "Auto-craft"
  42.     end
  43.    
  44.     term.redirect(monitor)
  45.     local _, height = term.getSize()
  46.     tools.gui.drawRect(1, 1, catigoryColumnWidth-1, height, colors.black)
  47.    
  48.     for i=1, table.getn(catigories), 1 do
  49.         if i == selectedCatigory then
  50.             term.setBackgroundColor(colors.green)
  51.             term.setTextColor(colors.white)
  52.         else
  53.             term.setBackgroundColor(colors.black)
  54.             term.setTextColor(colors.lightGray)
  55.         end
  56.        
  57.         term.setCursorPos(1, i) term.write(catigoryColumnLineBG)
  58.         term.setCursorPos(1, i) term.write(catigories[i])
  59.     end
  60.    
  61.     term.setBackgroundColor(colors.black)
  62.     term.setTextColor(colors.white)
  63.     term.restore()
  64. end
  65. local function listItems(catigory, reloadItems)
  66.     local itemsList = string.parse(http.queueFriendlyRequest("http://localhost/storage.php?func=listItemsByCatigory&catigory="..catigory, nil, true), ",")
  67.     if itemsList==nil then fail("Failed to list items") end
  68.    
  69.     term.redirect(monitor)
  70.     local width, height = term.getSize()
  71.     tools.gui.drawRect(catigoryColumnWidth + 1, 1, width-catigoryColumnWidth, height, colors.black)
  72.    
  73.     local row = 1
  74.     local column = 1
  75.     items = { }
  76.     items[column] = {}
  77.    
  78.     for i=1, table.getn(itemsList), 1 do
  79.         term.setCursorPos(catigoryColumnWidth + 1 + (column-1)*itemColumnWidth, row)
  80.         term.write(itemsList[i])
  81.         items[column][row] = itemsList[i]
  82.         row = row + 1
  83.         if row > height then
  84.             column = column + 1
  85.             row = 1
  86.             items[column] = {}
  87.         end
  88.     end
  89.    
  90.     term.restore()
  91. end
  92. local function getAmountString(amount, stacksize)
  93.     local stacks = math.floor(amount/stacksize)
  94.     local remainder = amount % stacksize
  95.  
  96.     if not math.isEvenLength(stacksize) then
  97.         stacksize = "0"..stacksize
  98.     end
  99.    
  100.     if stacks == 0 then
  101.         if math.isEvenLength(remainder) then
  102.             return "" .. remainder
  103.         else
  104.             return "0"..remainder
  105.         end
  106.     else
  107.         if stacksize=="01" then
  108.             if math.isEvenLength(stacks) then
  109.                 return ""..stacks
  110.             else
  111.                 return "0"..stacks
  112.             end
  113.         else
  114.             if remainder == 0 then
  115.                 if math.isEvenLength(stacks) then
  116.                     return "0"..stacks.."x"..stacksize
  117.                 else
  118.                     return stacks.."x"..stacksize
  119.                 end
  120.             else
  121.                 if math.isEvenLength(stacks) and not math.isEvenLength(remainder) then
  122.                     return stacks.."x"..stacksize.."+0"..remainder
  123.                 elseif not math.isEvenLength(stacks) and math.isEvenLength(remainder) then
  124.                     return "0"..stacks.."x"..stacksize.."+"..remainder
  125.                 else
  126.                     return stacks.."x"..stacksize.."+"..remainder
  127.                 end
  128.             end
  129.         end
  130.     end
  131. end
  132. local function getDenominationName(index, stacksize)
  133.     if not math.isEvenLength(stacksize) then stacksize = "0"..stacksize end
  134.    
  135.     local text = denominationsNames[index]
  136.     if text=="2stacks" then
  137.         text = "2x"..stacksize
  138.     elseif text=="5stacks" then
  139.         text = "5x"..stacksize
  140.     end
  141.    
  142.     return text
  143. end
  144. local function getDenominationAmount(index)
  145.     local amount = denominations[index]
  146.     local stacksize = selectedItemStacksize or 1
  147.    
  148.     if amount < 0 then
  149.         return math.abs(amount)*stacksize
  150.     end
  151.    
  152.     return amount
  153. end
  154. local function openTakeItemSidebar(name)
  155.     sideBarOpen = true
  156.    
  157.     local amount = tonumber(http.queueFriendlyRequest("http://localhost/storage.php?func=getItemAmountByName&name="..name, nil, true))
  158.     if amount==nil then fail("Failed to open sidebar! (Amount nil)") end
  159.     local stacksize = tonumber(http.queueFriendlyRequest("http://localhost/storage.php?func=getStacksizeByName&name="..name, nil, true))
  160.     if stacksize==nil then fail("Failed to open sidebar! (Stacksize nil)") end
  161.    
  162.     selectedItemStacksize = stacksize
  163.     local amountStr = getAmountString(amount, stacksize)
  164.    
  165.     term.redirect(monitor)
  166.     local w, h = term.getSize()
  167.    
  168.     paintutils.drawLine(w-20, 1, w-20, h, colors.white)
  169.     tools.gui.drawRect(w-19, 1, 19, h, colors.gray)
  170.    
  171.     tools.gui.writeHighlighted(w-9-math.ceil(#name/2), 2, name, colors.gray)
  172.     tools.gui.writeHighlighted(w-9-math.ceil(#amountStr/2), 3, amountStr, colors.gray)
  173.    
  174.     for i=1,table.getn(denominations),2 do
  175.         tools.gui.writeHighlighted(w-14, i+5, getDenominationName(i, stacksize), colors.lime)
  176.         tools.gui.writeHighlighted(w-8, i+5, getDenominationName(i+1, stacksize), colors.lime)
  177.     end
  178.    
  179.     term.restore()
  180. end
  181. local function updateSidebarItemAmount()
  182.     if sideBarOpen==false then return end
  183.    
  184.     local name = items[selectedItem[1]][selectedItem[2]]
  185.    
  186.     local amount = tonumber(http.queueFriendlyRequest("http://localhost/storage.php?func=getItemAmountByName&name="..name, nil, true))
  187.     if amount==nil then fail("Failed to update sidebar! (Amount nil)") end
  188.     local stacksize = tonumber(http.queueFriendlyRequest("http://localhost/storage.php?func=getStacksizeByName&name="..name, nil, true))
  189.     if stacksize==nil then fail("Failed to update sidebar! (Stacksize nil)") end
  190.    
  191.     selectedItemStacksize = stacksize
  192.     local amountStr = getAmountString(amount, stacksize)
  193.    
  194.     term.redirect(monitor)
  195.     local w, h = term.getSize()
  196.     paintutils.drawLine(w-19, 3, w, 3, colors.gray)
  197.     tools.gui.writeHighlighted(w-9-math.ceil(#amountStr/2), 3, amountStr, colors.gray)
  198.     term.restore()
  199. end
  200. local function refresh()
  201.     term.redirect(monitor)
  202.     tools.gui.reset()
  203.     selectedCatigory = 0
  204.     selectedItem = nil
  205.     selectedItemStacksize = nil
  206.     sideBarOpen = false
  207.     listCatigories(true)
  208.     term.restore()
  209. end
  210. local function showErrorNotice()
  211.     term.redirect(monitor)
  212.     local _, height = term.getSize()
  213.     term.setTextColor(colors.red)
  214.     term.setCursorPos(1, height)
  215.     term.write("ERROR!")
  216.     term.setTextColor(colors.white)
  217.     term.restore()
  218. end
  219. local function clearErrorNotice()
  220.     term.redirect(monitor)
  221.     local _, height = term.getSize()
  222.     term.setCursorPos(1, height)
  223.     term.write("      ")
  224.     term.restore()
  225. end
  226. -- local function showError(message)
  227.     -- showingError = true
  228.     -- tools.gui.reset()
  229.     -- term.redirect(monitor)
  230.     -- local width, height = term.getSize()
  231.     -- term.setTextColor(colors.red)
  232.     -- term.setCursorPos(math.floor((w/2)-(#message/2)), math.floor(h/2))
  233.     -- term.write(message)
  234.     -- term.setTextColor(colors.white)
  235.     -- term.restore()
  236. -- end
  237.  
  238. -- Button Functions
  239. local function catigoryButtonClick(catigoryIndex)
  240.     selectedCatigory = catigoryIndex
  241.     selectedItem = nil
  242.     selectedItemStacksize = nil
  243.     sideBarOpen = false
  244.     listCatigories(false)
  245.     listItems(catigories[catigoryIndex])
  246. end
  247. local function itemButtonClick(column, row)
  248.     term.redirect(monitor)
  249.    
  250.     if selectedItem~=nil then
  251.         term.setCursorPos(catigoryColumnWidth + 1 + (selectedItem[1]-1)*itemColumnWidth, selectedItem[2])
  252.         term.write(itemColumnLineBG)
  253.         term.setCursorPos(catigoryColumnWidth + 1 + (selectedItem[1]-1)*itemColumnWidth, selectedItem[2])
  254.         term.write(items[selectedItem[1]][selectedItem[2]])
  255.     end
  256.    
  257.     selectedItem = {column, row}
  258.     term.setBackgroundColor(colors.green)
  259.     term.setCursorPos(catigoryColumnWidth + 1 + (column-1)*itemColumnWidth, row)
  260.     term.write(itemColumnLineBG)
  261.     term.setCursorPos(catigoryColumnWidth + 1 + (column-1)*itemColumnWidth, row)
  262.     term.write(items[column][row])
  263.     term.setBackgroundColor(colors.black)
  264.     term.restore()
  265.    
  266.     openTakeItemSidebar(items[column][row])
  267. end
  268. local function amountButtonClick(amount)
  269.     local name = items[selectedItem[1]][selectedItem[2]]
  270.     http.request("http://localhost/storage.php?func=pullItem", "name="..name.."&amt="..amount)
  271. end
  272. local function amountButtonClickHandler(response)
  273.     local response = string.parse(response, ",")
  274.     if response==nil then fail("Failed to pull items (parse A failed)") end
  275.    
  276.     for i=1,table.getn(response),1 do
  277.         local command = string.parse(response[i], " ")
  278.         if command==nil then fail("Failed to pull items (parse B failed)") end
  279.         if command[1] == "PULL" then
  280.             if tonumber(command[2]) ~= nil then
  281.                 rednet.send(tonumber(command[2]), response[i])
  282.             end
  283.         end
  284.     end
  285.    
  286.     updateSidebarItemAmount()
  287. end
  288.  
  289. refresh()
  290. sleepTimer = os.startTimer(60)
  291.  
  292. print("Q to quit, C to clear, U to update, A to add item,")
  293. print("S to update all sorters")
  294. while true do
  295.     local event, a, b, c = os.pullEvent()
  296.     clearErrorNotice()
  297.     local resetSleepTimer = true
  298.    
  299.     if event=="char" then
  300.         if a=="q" then
  301.             return
  302.         elseif a=="c" then
  303.             tools.gui.reset()
  304.         elseif a=="u" then
  305.             shell.run("downloader tools")
  306.             shell.run("downloader display")
  307.             shell.run("downloader screensaver")
  308.             os.reboot()
  309.         elseif a == "a" then
  310.             print("<id>, <name>, <stacksize>, <catigory>, [meta]:")
  311.             local input = read()
  312.             if input ~= "" and input ~= "exit" then
  313.                 input = string.parse(input, ",")
  314.                 write("Adding item... ")
  315.                 if table.getn(input)>=4 and tonumber(input[1])~=nil and #input[2]>0 and tonumber(input[3])~=nil and #input[4]>0 then
  316.                     local post = "iID="..string.trim(input[1]).."&name="..string.trim(input[2]).."&stacksize="..string.trim(input[3]).."&catigory="..string.trim(input[4])
  317.                     if tonumber(input[5])~=nil then post = post.."&meta="..input[5] end
  318.                     tools.gui.load(nil, "return http.queueFriendlyRequest(\"http://localhost/storage.php?func=addItem\", \""..post.."\", true)")
  319.                 else
  320.                     print("Bad arguments.")
  321.                 end
  322.             end
  323.         elseif a == "s" then
  324.             rednet.broadcast("all_sorters_update")
  325.             print("All sorters are updating.")
  326.         end
  327.     elseif event=="http_success" then
  328.         if string.startsWith(a, "http://localhost/storage.php?func=pullItem") then
  329.             amountButtonClickHandler(b.readAll())
  330.         end
  331.     elseif event=="http_failure" then
  332.         if string.startsWith(a, "http://localhost/storage.php?func=pullItem") then
  333.             fail("Pull item failed (http_failure)")
  334.         end
  335.     elseif event=="monitor_touch" then
  336.         local found=false
  337.         if sideBarOpen then
  338.             term.redirect(monitor)
  339.             local w, h = term.getSize()
  340.             term.restore()
  341.            
  342.             for i=1,table.getn(denominations),2 do
  343.                 if b>=w-14 and b<=w-10 and c==i+5 then
  344.                     amountButtonClick(getDenominationAmount(i))
  345.                     found = true
  346.                 elseif b>=w-8 and b<=w-4 and c==i+5 then
  347.                     amountButtonClick(getDenominationAmount(i+1))
  348.                     found = true
  349.                 end
  350.             end
  351.         end
  352.        
  353.         if not found then
  354.             local column = math.ceil((b-catigoryColumnWidth)/itemColumnWidth)
  355.             if items[column]~=nil and items[column][c]~=nil then
  356.                 itemButtonClick(column, c)
  357.             elseif b<=catigoryColumnWidth and c<=table.getn(catigories) then
  358.                 catigoryButtonClick(c)
  359.             end
  360.         end
  361.     elseif event=="rednet_message" then
  362.         local command = string.parse(b, ":")
  363.         if command~=nil and command[1] == "ERROR" then
  364.             showErrorNotice()
  365.             term.setTextColor(colors.red)
  366.             print("Sorter Error:"..a..":"..command[2])
  367.             term.setTextColor(colors.white)
  368.         end
  369.     elseif event=="timer" then
  370.         if a==sleepTimer then
  371.             shell.run("screensaver")
  372.             refresh()
  373.         else
  374.             resetSleepTimer = false
  375.         end
  376.     end
  377.    
  378.     if resetSleepTimer then
  379.         sleepTimer = os.startTimer(60)
  380.     end
  381. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement