wyvern/client.lua

96 lines
2.7 KiB
Lua
Raw Normal View History

2018-07-31 12:02:45 +00:00
local w = require "lib"
local d = require "luadash"
2018-08-13 07:06:20 +00:00
local readline = require "readline"
2018-07-31 12:02:45 +00:00
local conf = w.load_config({
"network_name"
})
2018-07-31 12:02:45 +00:00
local function split_at_spaces(s)
local t = {}
for i in string.gmatch(s, "%S+") do
table.insert(t, i)
end
return t
end
local function first_letter(s)
return string.sub(s, 1, 1)
end
2018-08-13 07:06:20 +00:00
local usage =
[[Welcome to the Wyvern CLI Client, "Because Gollark Was Lazy".
All commands listed below can also be accessed using single-letter shortcuts for convenience.
withdraw [quantity] [name] - withdraw [quantity] items with display names close to [name] from storage
2018-08-13 07:06:20 +00:00
withdraw [items] - as above but withdraws all available matching items]]
2018-07-31 12:02:45 +00:00
local commands = {
help = function() return usage end,
withdraw = function(number, ...)
local query_tokens = {...}
local quantity = math.huge
if tonumber(number) ~= nil then
quantity = tonumber(number)
else
2018-08-13 06:52:44 +00:00
table.insert(query_tokens, 1, number)
end
local query = table.concat(query_tokens, " ") -- unsplit query
2018-08-13 07:48:03 +00:00
local items = unwrap(w.query_by_type("storage", {
type = "search",
query = query
2018-08-13 07:48:03 +00:00
}), "searching for items")
for _, item_type in pairs(items) do
2018-08-13 07:49:24 +00:00
repeat
2018-08-13 07:48:03 +00:00
local max_quantity
if quantity < 64 then max_quantity = quantity end
local moved = unwrap(w.query_by_type("storage", {
type = "extract",
2018-08-13 07:48:52 +00:00
ID = item_type.ID,
meta = item_type.meta,
2018-08-13 07:48:03 +00:00
NBT_hash = item_type.NBT_hash,
quantity = max_quantity,
destination_inventory = conf.network_name
}), "extracting a stack").moved
quantity = quantity - moved
item_type.count = item_type.count - moved
until quantity == 0 or item_type.count == 0
end
if quantity == 0 then break end
end
2018-07-31 12:02:45 +00:00
}
w.init()
if not turtle then error "Wyvern CLI must be run on a turtle." end
2018-07-31 12:02:45 +00:00
print "Wyvern CLI Client"
2018-08-13 07:06:20 +00:00
local history = {}
2018-07-31 12:02:45 +00:00
while true do
write "|> "
2018-08-13 07:06:20 +00:00
local text = readline(nil, history)
2018-08-13 07:11:13 +00:00
if text ~= "" then table.insert(history, text) end
2018-08-13 07:06:20 +00:00
2018-07-31 12:02:45 +00:00
local tokens = split_at_spaces(text)
2018-08-13 07:11:13 +00:00
2018-07-31 12:02:45 +00:00
local command = tokens[1]
local args = d.tail(tokens)
local fn = commands[command]
if not fn then
for command_name, func in pairs(commands) do
2018-08-13 07:11:13 +00:00
if command and first_letter(command_name) == first_letter(command) then fn = func end
2018-07-31 12:02:45 +00:00
end
end
if not fn then
print("Command", command, "not found.")
2018-08-13 06:50:23 +00:00
else
local ok, result = pcall(fn, table.unpack(args))
if result then textutils.pagedPrint(result) end
2018-07-31 12:02:45 +00:00
end
end