Advertisement
popatop15

Untitled

Oct 23rd, 2020 (edited)
1,937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. local me = peripheral.wrap("left")
  2.  
  3. function resetTerm()
  4.     term.clear()
  5.     term.setCursorPos(1,1)
  6. end
  7.  
  8. function printHelp()
  9.     resetTerm()
  10.     print("[1] Add or update item")
  11.     print("[2] Remove item")
  12.     print("[3] List all items")
  13. end
  14.  
  15. function loadConfig()
  16.     local file = fs.open("./stock.config", "r")
  17.     local data = file.readAll()
  18.     file.close()
  19.     return textutils.unserialize(data) or {}
  20. end
  21.  
  22. function saveConfig(table)
  23.     local file = fs.open("./stock.config", "w")
  24.     file.write(textutils.serialize(table))
  25.     file.close()
  26.     resetTerm()
  27.     print("Config saved successfully.")
  28.     sleep(2)
  29. end
  30.  
  31. function q(text)
  32.     resetTerm()
  33.     print(text)
  34.     local t = read()
  35.     return t
  36. end
  37.  
  38. function tableLength(T)
  39.     local count = 0
  40.     for _ in pairs(T) do count = count + 1 end
  41.     return count
  42. end
  43.  
  44. function listTable(table)
  45.     local toShow = 8
  46.     local count = 1
  47.     for key, val in pairs(table) do
  48.         print("Key: ", key, " - Value: ", val)
  49.         if count % toShow then
  50.             print("Press any key to continue...")
  51.             os.pullEvent("key")
  52.         end
  53.     end
  54. end
  55.  
  56. while true do
  57.     printHelp()
  58.     local action = read()
  59.    
  60.     if action == "1" then
  61.  
  62.         resetTerm()
  63.         local config = loadConfig()
  64.         local item = q("ID of item to add or update:")
  65.         local ok, meItem = pcall(me.findItem, item)
  66.         if not ok then
  67.             resetTerm()
  68.             print("Item not found. Canceling...")
  69.             sleep(2)
  70.         else
  71.             local name = meItem.getMetadata().name
  72.             if config[name] then
  73.                 local yn = q("That item already exists. Would you like to update quantity? (y/n)\nCurrent quantity: " .. config[name].qty)
  74.                 if yn == "y" then
  75.                     local qty = tonumber(q("Quantity to keep in stock:"))
  76.                     config[name].qty = qty
  77.                     saveConfig(config)
  78.                 else
  79.                     resetTerm()
  80.                     print("Canceling...")
  81.                     sleep(2)
  82.                 end
  83.             else
  84.                 local qty = tonumber(q("Quantity to keep in stock:"))
  85.                 local t = {}
  86.                 t["qty"] = qty
  87.                 t["name"] = name
  88.                 config[name] = t
  89.                 saveConfig(config)
  90.             end
  91.         end
  92.  
  93.     elseif action == "2" then
  94.  
  95.         local config = loadConfig()
  96.         if tableLength(config) > 0 then
  97.             local item = q("Enter the item you would like to remove.")
  98.             local ok, meItem = pcall(me.findItem, item)
  99.             if not ok then
  100.                 resetTerm()
  101.                 print("Item not found. Canceling...")
  102.                 sleep(2)
  103.             else
  104.                 config[meItem.getMetadata().name] = nil
  105.                 saveConfig(config)
  106.             end
  107.         else
  108.             resetTerm()
  109.             print("No items to delete.")
  110.             sleep(2)
  111.         end
  112.  
  113.     elseif action == "3" then
  114.  
  115.         local config = loadConfig()
  116.         resetTerm()
  117.         print("Listing all saved items:")
  118.         if tableLength(config) < 1 then
  119.             print("No saved items... Continuing in 2 seconds.")
  120.             sleep(2)
  121.         else
  122.             listTable(config)
  123.         end
  124.  
  125.     end
  126. end
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement