druidbruce

AEst startup

Apr 25th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. os.loadAPI("stockpile/settingsPersistence")
  2. os.loadAPI("stockpile/craftingRequester")
  3. os.loadAPI("stockpile/itemSearcher")
  4. os.loadAPI("userIo")
  5.  
  6. function handleSetThreshold()
  7. itemSearcher.refreshItems()
  8. local item = itemSearcher.chooseCraftableItem()
  9.  
  10. if item ~= nil then
  11. local currentThreshold = settingsPersistence.getItemThreshold(item)
  12.  
  13. print("Enter the number of items to keep stocked")
  14. if currentThreshold ~= nil then
  15. print("(Currently set to: " .. currentThreshold ..")")
  16. end
  17. local threshold = userIo.promptForNumberInRange(1, math.huge)
  18.  
  19. settingsPersistence.setItemThreshold(item, threshold)
  20.  
  21. print("Successfully set threshold.")
  22. else
  23. print("Couldn't find a matching item.")
  24. end
  25. end
  26.  
  27. function handleRemoveItem()
  28. itemSearcher.refreshItems()
  29. local stockedItems = getStockedItems()
  30. viewStockedItems("Which item do you want to remove?")
  31.  
  32. print("Item number to remove? (0 to cancel)")
  33. local itemNum = userIo.promptForNumberInRange(0, table.getn(stockedItems))
  34.  
  35. if itemNum == 0 then
  36. print("Skipping removal.")
  37. elseif itemNum ~= nil then
  38. settingsPersistence.removeStockedItem(stockedItems[itemNum].stockedItem.item)
  39. print("Successfully removed stocked item.")
  40. else
  41. print("Couldn't find a matching item.")
  42. end
  43. end
  44.  
  45. function viewStockedItems(banner)
  46. local printableItems = {}
  47. for i, item in ipairs(getStockedItems()) do
  48. table.insert(printableItems, item.itemDetailStr)
  49. end
  50.  
  51. userIo.printOptions(printableItems, banner)
  52. end
  53.  
  54. function getStockedItems()
  55. local items = {}
  56. for key, stockedItem in pairs(settingsPersistence.getStockedItems()) do
  57. local itemName = stockedItem.id
  58. local itemDetail = itemSearcher.getItemDetail(stockedItem.item)
  59.  
  60. if itemDetail ~= nil then
  61. itemName = itemDetail.display_name
  62. end
  63.  
  64. table.insert(items, {
  65. itemDetailStr = string.format("%s (%d)", itemName, stockedItem.threshold),
  66. stockedItem = stockedItem
  67. })
  68. end
  69.  
  70. table.sort(items, function(a, b) return a.itemDetailStr < b.itemDetailStr end)
  71. return items
  72. end
  73.  
  74. function main()
  75. options = {
  76. {
  77. name = "Set threshold for an item",
  78. handler = handleSetThreshold
  79. },
  80. {
  81. name = "Remove item from stock monitoring",
  82. handler = handleRemoveItem
  83. },
  84. {
  85. name = "View stocked items",
  86. handler = function() viewStockedItems("") end
  87. }
  88. }
  89.  
  90. while true do
  91. settingsPersistence.load()
  92.  
  93. print()
  94. print()
  95.  
  96. print("What do you want to do?")
  97. local selection = userIo.chooseOption(options)
  98. selection.handler()
  99. end
  100. end
  101.  
  102. parallel.waitForAll(main, craftingRequester.monitorStockedItems, craftingRequester.monitorFinishedCraftingJobs)
Add Comment
Please, Sign In to add comment