From 4db2dad5929a9847856b4af5def4c30358be7875 Mon Sep 17 00:00:00 2001 From: osmarks Date: Sun, 29 Apr 2018 15:32:49 +0100 Subject: [PATCH] Comment stuff, add (semi)-autosetup --- client.lua | 4 ++++ server.lua | 22 ++++++++++++++++++---- setup.lua | 19 +++++++++++++++++++ util.lua | 1 + 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 setup.lua diff --git a/client.lua b/client.lua index 2195c39..8d32824 100644 --- a/client.lua +++ b/client.lua @@ -16,6 +16,7 @@ local function split(str, sep) return t end +-- Fetches an item with the given display name in the given quantity. local function fetchItem(item, toGet) local result repeat @@ -33,6 +34,7 @@ local function fetchItem(item, toGet) until toGet <= 0 or result == "ERROR" end +-- Dumps an inventory slot into storage function dump(slot) if conf.introspection then conf.introspection.pushItems(conf.name, slot) @@ -41,6 +43,7 @@ function dump(slot) query { cmd = "insert", fromInv = conf.name, fromSlot = slot } end +-- Attempts to interpret the first of a list of tokens as a number. function tryNumber(tokens) local fst = table.remove(tokens, 1) local qty = tonumber(fst) @@ -52,6 +55,7 @@ function tryNumber(tokens) return qty end +-- Help text local help = [[ Welcome to the Dragon CLI. Commands: diff --git a/server.lua b/server.lua index b021b0f..a7dba38 100644 --- a/server.lua +++ b/server.lua @@ -3,6 +3,7 @@ local conf = util.conf rednet.open(conf.modem) +-- Find all chests or shulker boxes local inventories = {} for _, n in pairs(peripheral.getNames()) do local p = peripheral.wrap(n) @@ -15,6 +16,9 @@ end 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) local idx = item.name .. ":" .. item.damage @@ -47,6 +51,7 @@ function updateIndex() print "Indexing complete." end +-- Finds all items matching a certain predicate function find(predicate) for name, items in pairs(index) do for slot, item in pairs(items) do @@ -57,6 +62,7 @@ function find(predicate) end end +-- Finds space in the chest system function findSpace() for name, items in pairs(index) do if #items < inventories[name].size() then @@ -68,6 +74,9 @@ end function processRequest(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 local inv, slot, item = find(function(item) return @@ -84,7 +93,8 @@ function processRequest(msg) moved = peripheral.call(conf.bufferOutExternal, "pushItems", msg.destInv, 1) end - return {moved, item} + return {moved, item} + -- Pulls items from an external inventory into storage. elseif msg.cmd == "insert" then if msg.fromInv and msg.fromSlot then 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 - return "OK" + return "OK" + -- Just return the external network names of the buffers elseif msg.cmd == "buffers" then - return { conf.bufferInExternal, conf.bufferOutExternal } + return { conf.bufferInExternal, conf.bufferOutExternal } + -- Reindexes system elseif msg.cmd == "reindex" then updateIndex() - return "OK" + return "OK" + -- Returns entire index elseif msg.cmd == "list" then return index + -- Looks up supplied name in the cache. elseif msg.cmd == "name" then msg.meta = msg.meta or 0 return msg.name and msg.meta and nameCache[msg.name .. ":" .. msg.meta] diff --git a/setup.lua b/setup.lua new file mode 100644 index 0000000..2ff7214 --- /dev/null +++ b/setup.lua @@ -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 \ No newline at end of file diff --git a/util.lua b/util.lua index 20c0680..4e4d7a6 100644 --- a/util.lua +++ b/util.lua @@ -2,6 +2,7 @@ local f = fs.open("conf", "r") local conf = textutils.unserialise(f.readAll()) f.close() +-- Queries Dragon servers. In a loop. local function query(m) local msg repeat