dragon/util.lua

87 lines
2.2 KiB
Lua
Raw Normal View History

2018-04-29 14:17:41 +00:00
local f = fs.open("conf", "r")
local conf = textutils.unserialise(f.readAll())
f.close()
2018-04-29 14:32:49 +00:00
-- Queries Dragon servers. In a loop.
2018-04-29 14:17:41 +00:00
local function query(m)
local msg
repeat
rednet.broadcast(m, "dragon")
_, msg = rednet.receive("dragon", 1)
until msg
return msg
end
2018-04-29 15:55:15 +00:00
-- Fetches an item with the given display name in the given quantity.
local function fetch(item, toGet)
local result
repeat
local toGetNow = 64
if toGet < 64 then toGetNow = toGet end
result = query { cmd = "extract", dname = item, destInv = conf.name, qty = toGetNow }
if result and type(result) == "table" and result[1] then
toGet = toGet - result[1]
end
if conf.introspection then
conf.introspection.pullItems(conf.name, 1)
end
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)
slot = 1
end
query { cmd = "insert", fromInv = conf.name, fromSlot = slot }
end
2018-04-30 18:55:50 +00:00
-- Converts a table of the form {"x", "x", "y"} into {x = 2, y = 1}
2018-04-29 19:55:16 +00:00
local function collate(items)
local ret = {}
for _, i in pairs(items) do
ret[i] = (ret[i] or 0) + 1
end
return ret
end
2018-04-30 18:55:50 +00:00
-- Checks whether "needs"'s (a collate-formatted table) values are all greater than those of "has"
2018-04-29 19:55:16 +00:00
local function satisfied(needs, has)
local good = true
for k, qty in pairs(needs) do
if qty > (has[k] or 0) then good = false end
end
return good
end
2018-04-30 18:55:50 +00:00
-- Python-style version from http://lua-users.org/wiki/SplitJoin
-- Why is this not in the standard Lua library?!
local function split(sSeparator, nMax, bRegexp)
assert(sSeparator ~= '')
assert(nMax == nil or nMax >= 1)
local aRecord = {}
if self:len() > 0 then
local bPlain = not bRegexp
nMax = nMax or -1
local nField, nStart = 1, 1
local nFirst,nLast = self:find(sSeparator, nStart, bPlain)
while nFirst and nMax ~= 0 do
aRecord[nField] = self:sub(nStart, nFirst-1)
nField = nField+1
nStart = nLast+1
nFirst,nLast = self:find(sSeparator, nStart, bPlain)
nMax = nMax-1
end
aRecord[nField] = self:sub(nStart)
end
return aRecord
end
return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied, split = split }