osmarks

MW CLI Terminal

Apr 11th, 2018
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local f = fs.open("conf", "r")
  2. local conf = textutils.unserialise(f.readAll())
  3. f.close()
  4.  
  5. rednet.open(conf.modem)
  6.  
  7. if conf.introspection then
  8.     conf.introspection = peripheral.call(conf.introspection, "getInventory")
  9.     print(conf.introspection)
  10. end
  11.  
  12. local function split(str, sep)
  13.     local t = {}
  14.     for sub in str:gmatch("[^" .. sep .. "]+") do
  15.         table.insert(t, sub)
  16.     end
  17.    
  18.     return t
  19. end
  20.  
  21. local function query(m)
  22.     local msg
  23.     repeat
  24.         rednet.broadcast(m, "mw")
  25.         _, msg = rednet.receive("mw", 1)
  26.     until msg
  27.     return msg
  28. end
  29.  
  30. local function fetchItem(item, toGet)
  31.     print(item, toGet)
  32.     local result
  33.     repeat
  34.         local toGetNow = 64
  35.         if toGet < 64 then toGetNow = toGet end
  36.  
  37.         result = query { cmd = "extract", dname = item, destInv = conf.name, qty = toGetNow }
  38.         if result and type(result) == "table" and result[1] then
  39.             toGet = toGet - result[1]
  40.         end
  41.  
  42.         if conf.introspection then
  43.             conf.introspection.pullItems(conf.name, 1)
  44.         end
  45.     until toGet <= 0 or result == "ERROR"
  46. end
  47.  
  48. function dump(slot)
  49.     if conf.introspection then
  50.         conf.introspection.pushItems(conf.name, slot)
  51.         slot = 1
  52.     end
  53.     query { cmd = "insert", fromInv = conf.name, fromSlot = slot }
  54. end
  55.  
  56. function tryNumber(tokens)
  57.     local fst = table.remove(tokens, 1)
  58.     local qty = tonumber(fst)
  59.  
  60.     if not qty then
  61.         table.insert(tokens, 1, fst)
  62.     end
  63.  
  64.     return qty
  65. end
  66.  
  67. local help = [[
  68. Welcome to the Multiplexed Warehousing CLI Terminal (Alpha).
  69. Commands:
  70. w [name] - withdraw all items whose names contain [name]
  71. w [qty] [name] - withdraw [qty] items whose names contain [name]
  72. c - Craft item, using the turtle's inventory as a grid (turtle.craft)
  73. d - Dump all items into storage
  74. d [slot] - Dump items in [slot] into storage
  75. r - Force connected storage server to reindex
  76. help - Display this
  77. This is an unstable alpha version and does not support a GUI or multiple storage servers.]]
  78.  
  79. print "Multiplexed Warehousing: CLI Terminal"
  80. while true do
  81.     write "|> "
  82.     local tokens = split(read(), " ")
  83.     local cmd = table.remove(tokens, 1)
  84.    
  85.     if cmd == "w" then
  86.         local qty = tryNumber(tokens)
  87.         if not qty then
  88.             qty = math.huge
  89.         end
  90.        
  91.         local item = table.concat(tokens, " ")
  92.         fetchItem(item, qty)
  93.    elseif cmd == "c" then
  94.         turtle.craft()
  95.     elseif cmd == "d" then
  96.         local slot = tryNumber(tokens)
  97.        
  98.         if slot then dump(slot) else
  99.             local size = 16
  100.             if conf.introspection then size = conf.introspection.size() end
  101.             for i = 1, size do
  102.                 dump(i)
  103.             end
  104.         end
  105.     elseif cmd == "r" then
  106.         query { cmd = "reindex" }
  107.     elseif cmd == "help" then
  108.         textutils.pagedPrint(help)
  109.     end
  110. end
Add Comment
Please, Sign In to add comment