wyvern/client.lua

123 lines
3.6 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
reindex - force storage server to reindex its contents]]
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-13 07:54:32 +00:00
local items = w.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
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,
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
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")
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