Comment stuff, add (semi)-autosetup
This commit is contained in:
parent
7b4b2bca4f
commit
4db2dad592
@ -16,6 +16,7 @@ local function split(str, sep)
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Fetches an item with the given display name in the given quantity.
|
||||||
local function fetchItem(item, toGet)
|
local function fetchItem(item, toGet)
|
||||||
local result
|
local result
|
||||||
repeat
|
repeat
|
||||||
@ -33,6 +34,7 @@ local function fetchItem(item, toGet)
|
|||||||
until toGet <= 0 or result == "ERROR"
|
until toGet <= 0 or result == "ERROR"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Dumps an inventory slot into storage
|
||||||
function dump(slot)
|
function dump(slot)
|
||||||
if conf.introspection then
|
if conf.introspection then
|
||||||
conf.introspection.pushItems(conf.name, slot)
|
conf.introspection.pushItems(conf.name, slot)
|
||||||
@ -41,6 +43,7 @@ function dump(slot)
|
|||||||
query { cmd = "insert", fromInv = conf.name, fromSlot = slot }
|
query { cmd = "insert", fromInv = conf.name, fromSlot = slot }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Attempts to interpret the first of a list of tokens as a number.
|
||||||
function tryNumber(tokens)
|
function tryNumber(tokens)
|
||||||
local fst = table.remove(tokens, 1)
|
local fst = table.remove(tokens, 1)
|
||||||
local qty = tonumber(fst)
|
local qty = tonumber(fst)
|
||||||
@ -52,6 +55,7 @@ function tryNumber(tokens)
|
|||||||
return qty
|
return qty
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Help text
|
||||||
local help = [[
|
local help = [[
|
||||||
Welcome to the Dragon CLI.
|
Welcome to the Dragon CLI.
|
||||||
Commands:
|
Commands:
|
||||||
|
22
server.lua
22
server.lua
@ -3,6 +3,7 @@ local conf = util.conf
|
|||||||
|
|
||||||
rednet.open(conf.modem)
|
rednet.open(conf.modem)
|
||||||
|
|
||||||
|
-- Find all chests or shulker boxes
|
||||||
local inventories = {}
|
local inventories = {}
|
||||||
for _, n in pairs(peripheral.getNames()) do
|
for _, n in pairs(peripheral.getNames()) do
|
||||||
local p = peripheral.wrap(n)
|
local p = peripheral.wrap(n)
|
||||||
@ -15,6 +16,9 @@ end
|
|||||||
|
|
||||||
local nameCache = {}
|
local nameCache = {}
|
||||||
|
|
||||||
|
-- Gets the display name of the given item (in the given chest peripheral & slot)
|
||||||
|
-- If its name is not cached, cache it.
|
||||||
|
-- If it is, just return the cached name
|
||||||
function cache(item, chest, slot)
|
function cache(item, chest, slot)
|
||||||
local idx = item.name .. ":" .. item.damage
|
local idx = item.name .. ":" .. item.damage
|
||||||
|
|
||||||
@ -47,6 +51,7 @@ function updateIndex()
|
|||||||
print "Indexing complete."
|
print "Indexing complete."
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Finds all items matching a certain predicate
|
||||||
function find(predicate)
|
function find(predicate)
|
||||||
for name, items in pairs(index) do
|
for name, items in pairs(index) do
|
||||||
for slot, item in pairs(items) do
|
for slot, item in pairs(items) do
|
||||||
@ -57,6 +62,7 @@ function find(predicate)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Finds space in the chest system
|
||||||
function findSpace()
|
function findSpace()
|
||||||
for name, items in pairs(index) do
|
for name, items in pairs(index) do
|
||||||
if #items < inventories[name].size() then
|
if #items < inventories[name].size() then
|
||||||
@ -68,6 +74,9 @@ end
|
|||||||
function processRequest(msg)
|
function processRequest(msg)
|
||||||
print(textutils.serialise(msg))
|
print(textutils.serialise(msg))
|
||||||
|
|
||||||
|
-- Extract an item. If meta and name are supplied, each supplied value must match exactly.
|
||||||
|
-- Applies a fuzzy search to display names
|
||||||
|
-- Extracted items are either deposited in buffer or directly in target inventory.
|
||||||
if msg.cmd == "extract" then
|
if msg.cmd == "extract" then
|
||||||
local inv, slot, item = find(function(item)
|
local inv, slot, item = find(function(item)
|
||||||
return
|
return
|
||||||
@ -84,7 +93,8 @@ function processRequest(msg)
|
|||||||
moved = peripheral.call(conf.bufferOutExternal, "pushItems", msg.destInv, 1)
|
moved = peripheral.call(conf.bufferOutExternal, "pushItems", msg.destInv, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
return {moved, item}
|
return {moved, item}
|
||||||
|
-- Pulls items from an external inventory into storage.
|
||||||
elseif msg.cmd == "insert" then
|
elseif msg.cmd == "insert" then
|
||||||
if msg.fromInv and msg.fromSlot then
|
if msg.fromInv and msg.fromSlot then
|
||||||
peripheral.call(conf.bufferInExternal, "pullItems", msg.fromInv, msg.fromSlot, msg.qty or 64, 1)
|
peripheral.call(conf.bufferInExternal, "pullItems", msg.fromInv, msg.fromSlot, msg.qty or 64, 1)
|
||||||
@ -97,14 +107,18 @@ function processRequest(msg)
|
|||||||
|
|
||||||
updateIndexFor(toInv) -- I don't know a good way to figure out where exactly the items went
|
updateIndexFor(toInv) -- I don't know a good way to figure out where exactly the items went
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
|
-- Just return the external network names of the buffers
|
||||||
elseif msg.cmd == "buffers" then
|
elseif msg.cmd == "buffers" then
|
||||||
return { conf.bufferInExternal, conf.bufferOutExternal }
|
return { conf.bufferInExternal, conf.bufferOutExternal }
|
||||||
|
-- Reindexes system
|
||||||
elseif msg.cmd == "reindex" then
|
elseif msg.cmd == "reindex" then
|
||||||
updateIndex()
|
updateIndex()
|
||||||
return "OK"
|
return "OK"
|
||||||
|
-- Returns entire index
|
||||||
elseif msg.cmd == "list" then
|
elseif msg.cmd == "list" then
|
||||||
return index
|
return index
|
||||||
|
-- Looks up supplied name in the cache.
|
||||||
elseif msg.cmd == "name" then
|
elseif msg.cmd == "name" then
|
||||||
msg.meta = msg.meta or 0
|
msg.meta = msg.meta or 0
|
||||||
return msg.name and msg.meta and nameCache[msg.name .. ":" .. msg.meta]
|
return msg.name and msg.meta and nameCache[msg.name .. ":" .. msg.meta]
|
||||||
|
19
setup.lua
Normal file
19
setup.lua
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
local root = "https://osmarks.ml/git/osmarks/dragon/raw/branch/master/"
|
||||||
|
|
||||||
|
local function download(name, file)
|
||||||
|
local contents = http.get(root .. name).readAll()
|
||||||
|
local f = fs.open(file, "w")
|
||||||
|
f.write(contents)
|
||||||
|
f.close()
|
||||||
|
end
|
||||||
|
|
||||||
|
local files = { "client.lua", "server.lua", "util.lua" }
|
||||||
|
for _, f in pairs(files) do
|
||||||
|
download(f, f)
|
||||||
|
print("Downloaded", f)
|
||||||
|
end
|
||||||
|
|
||||||
|
print "Files downloaded. Either client.lua or server.lua should be run on startup."
|
||||||
|
print "Opening config editor..."
|
||||||
|
shell.run "edit conf"
|
||||||
|
fs.move("conf.lua", "conf") -- edit is really stupid, so un-.lua file
|
1
util.lua
1
util.lua
@ -2,6 +2,7 @@ local f = fs.open("conf", "r")
|
|||||||
local conf = textutils.unserialise(f.readAll())
|
local conf = textutils.unserialise(f.readAll())
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
-- Queries Dragon servers. In a loop.
|
||||||
local function query(m)
|
local function query(m)
|
||||||
local msg
|
local msg
|
||||||
repeat
|
repeat
|
||||||
|
Loading…
x
Reference in New Issue
Block a user