dragon/server.lua

146 lines
3.9 KiB
Lua
Raw Normal View History

2018-04-29 14:17:41 +00:00
local util = require "util"
local conf = util.conf
rednet.open(conf.modem)
2018-04-29 14:32:49 +00:00
-- Find all chests or shulker boxes
2018-04-29 14:17:41 +00:00
local inventories = {}
for _, n in pairs(peripheral.getNames()) do
local p = peripheral.wrap(n)
if
string.find(n, "chest") or
string.find(n, "shulker") then
2018-04-29 14:17:41 +00:00
inventories[n] = p
end
end
local nameCache = {}
2018-04-29 14:32:49 +00:00
-- 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
2018-04-29 14:17:41 +00:00
function cache(item, chest, slot)
local idx = item.name .. ":" .. item.damage
if nameCache[idx] then
return nameCache[idx]
else
local n = chest.getItemMeta(slot).displayName
nameCache[idx] = n
return n
end
end
local index = {}
function updateIndexFor(name)
local inv = inventories[name]
local data = inv.list()
for slot, item in pairs(data) do
data[slot].displayName = cache(item, inv, slot)
end
index[name] = data
end
function updateIndex()
for n in pairs(inventories) do
updateIndexFor(n)
sleep()
end
print "Indexing complete."
end
2018-04-29 14:32:49 +00:00
-- Finds all items matching a certain predicate
2018-04-29 14:17:41 +00:00
function find(predicate)
for name, items in pairs(index) do
for slot, item in pairs(items) do
if predicate(item) then
return name, slot, item
end
end
end
end
2018-04-29 14:32:49 +00:00
-- Finds space in the chest system
2018-04-29 14:17:41 +00:00
function findSpace()
for name, items in pairs(index) do
if #items < inventories[name].size() then
return name
end
end
end
2018-04-29 16:11:59 +00:00
function search(msg)
return find(function(item)
return
(not msg.meta or item.damage == msg.meta) and
(not msg.name or item.name == msg.name) and
(not msg.dname or string.find(item.displayName:lower(), msg.dname:lower()))
end)
end
2018-04-29 14:17:41 +00:00
function processRequest(msg)
print(textutils.serialise(msg))
2018-04-29 14:32:49 +00:00
-- 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.
2018-04-29 14:17:41 +00:00
if msg.cmd == "extract" then
2018-04-29 16:11:59 +00:00
local inv, slot, item = search(msg)
2018-04-29 14:17:41 +00:00
local qty = msg.qty or 64
2018-04-29 14:17:41 +00:00
updateIndexFor(inv)
local moved = peripheral.call(conf.bufferOutInternal, "pullItems", inv, slot, qty, 1)
2018-04-29 14:17:41 +00:00
if msg.destInv then
2018-04-30 18:55:50 +00:00
moved = peripheral.call(conf.bufferOutExternal, "pushItems", msg.destInv, 1, 64, msg.destSlot)
2018-04-29 14:17:41 +00:00
end
2018-04-29 14:32:49 +00:00
return {moved, item}
-- Pulls items from an external inventory into storage.
2018-04-29 14:17:41 +00:00
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)
end
local toInv = findSpace()
if not toInv then return "ERROR" end
peripheral.call(conf.bufferInInternal, "pushItems", toInv, 1)
updateIndexFor(toInv) -- I don't know a good way to figure out where exactly the items went
2018-04-29 14:32:49 +00:00
return "OK"
-- Just return the external network names of the buffers
2018-04-29 14:17:41 +00:00
elseif msg.cmd == "buffers" then
2018-04-29 14:32:49 +00:00
return { conf.bufferInExternal, conf.bufferOutExternal }
-- Reindexes system
2018-04-29 14:17:41 +00:00
elseif msg.cmd == "reindex" then
updateIndex()
2018-04-29 14:32:49 +00:00
return "OK"
-- Returns entire index
2018-04-29 14:17:41 +00:00
elseif msg.cmd == "list" then
2018-04-29 19:55:16 +00:00
return util.collate(index)
2018-04-29 14:32:49 +00:00
-- Looks up supplied name in the cache.
2018-04-29 14:17:41 +00:00
elseif msg.cmd == "name" then
msg.meta = msg.meta or 0
return msg.name and msg.meta and nameCache[msg.name .. ":" .. msg.meta]
end
end
function processRequests()
while true do
util.processMessage(function(msg)
2018-04-29 14:17:41 +00:00
local ok, r = pcall(processRequest, msg)
if not ok then r = "ERROR" end
2018-05-06 07:32:01 +00:00
return true, r
2018-05-06 07:29:41 +00:00
end)
end
2018-04-29 14:17:41 +00:00
end
updateIndex()
processRequests()