wyvern/client.lua

143 lines
4.3 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 08:03:44 +00:00
withdraw [items] - as above but withdraws all available matching items
dump [slot] - dump stack in slot back to storage
2018-08-13 08:08:34 +00:00
dump - dump whole inventory to storage
2018-08-13 08:11:58 +00:00
craft - runs turtle.craft
2018-08-16 09:46:04 +00:00
reindex - force storage server to reindex its contents
quantity [name] - see how many of an item you have (uses exact match)]]
2018-08-13 08:03:44 +00:00
local function dump(slot)
2018-08-13 08:11:58 +00:00
return w.query_by_type("storage", {
2018-08-13 08:03:44 +00:00
type = "insert",
from_slot = slot,
from_inventory = conf.network_name
})
end
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-15 13:56:59 +00:00
local exact = false
local query_match = string.match(query, "!(.*)")
if query_match ~= nil then query = query_match exact = true end
2018-08-13 07:54:32 +00:00
local items = w.unwrap(w.query_by_type("storage", {
type = "search",
2018-08-15 15:21:28 +00:00
query = query,
exact = exact
2018-08-13 07:48:03 +00:00
}), "searching for items")
for _, item_type in pairs(items) do
while quantity > 0 and item_type.count > 0 do
2018-08-13 07:48:03 +00:00
local max_quantity
if quantity < 64 then max_quantity = quantity end
2018-08-13 07:54:32 +00:00
local moved = w.unwrap(w.query_by_type("storage", {
2018-08-13 07:48:03 +00:00
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,
2018-08-15 13:56:59 +00:00
destination_inventory = conf.network_name,
2018-08-13 07:48:03 +00:00
}), "extracting a stack").moved
if moved == 0 then -- inventory full
quantity = 0
end
2018-08-13 07:48:03 +00:00
quantity = quantity - moved
item_type.count = item_type.count - moved
end
2018-08-13 07:48:03 +00:00
end
2018-08-13 08:03:44 +00:00
end,
dump = function(slot)
local slot = tonumber(slot)
if not slot then
for i = 1, 16 do
2018-08-13 08:16:11 +00:00
w.unwrap(dump(i), "dumping inventory")
2018-08-13 08:03:44 +00:00
end
else
2018-08-13 08:16:11 +00:00
w.unwrap(dump(slot), "dumping slot " .. tostring(slot))
2018-08-13 08:03:44 +00:00
end
2018-08-13 08:08:34 +00:00
end,
craft = function()
local result = turtle.craft()
if not result then return "Invalid or no recipe." end
2018-08-13 08:11:58 +00:00
end,
reindex = function()
2018-08-13 08:16:11 +00:00
w.unwrap(w.query_by_type("storage", { type = "reindex" }), "requesting reindexing")
2018-08-16 09:46:04 +00:00
end,
quantity = function(query_tokens)
local query = table.concat(query_tokens, " ")
local items = w.unwrap(w.query_by_type("storage", {
type = "search",
query = query,
exact = exact
}), "searching for items", {w.errors.NOITEMS}) or {{ count = 0 }}
print(items[1].count, query, "available.")
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