Abstract out request processing functionality
This commit is contained in:
parent
01bbdc3a92
commit
84ce642973
@ -36,7 +36,7 @@ Commands:
|
||||
w [name] - withdraw all items whose names contain [name]
|
||||
w [qty] [name] - withdraw [qty] items whose names contain [name]
|
||||
c - Craft item, using the turtle's inventory as a grid (turtle.craft)
|
||||
ac - Atempt to craft item, using Dragon autocrafting. Takes an internal name.
|
||||
ac [item] - Atempt to craft item, using Dragon autocrafting. Takes an internal name.
|
||||
d - Dump all items into storage
|
||||
d [slot] - Dump items in [slot] into storage
|
||||
r - Force connected storage server to reindex
|
||||
|
11
crafter.lua
11
crafter.lua
@ -68,15 +68,16 @@ local function craft(i)
|
||||
for _, tsk in pairs(tsks) do
|
||||
craftOne(tsk)
|
||||
end
|
||||
return "OK"
|
||||
else
|
||||
return "ERROR"
|
||||
end
|
||||
end
|
||||
|
||||
while true do
|
||||
local id, msg = rednet.receive "dragon"
|
||||
if msg and msg.cmd and msg.cmd == "craft" and msg.item then
|
||||
craft(msg.item)
|
||||
rednet.send(id, "OK", "dragon")
|
||||
end
|
||||
util.processMessage(function(m)
|
||||
if m.cmd and m.item and m.cmd == "craft" then
|
||||
return craft(m.item)
|
||||
end
|
||||
end)
|
||||
end
|
@ -133,15 +133,12 @@ end
|
||||
|
||||
function processRequests()
|
||||
while true do
|
||||
local id, msg = rednet.receive "dragon"
|
||||
if msg and msg.cmd and msg.uid then
|
||||
util.processMessage(function(msg)
|
||||
local ok, r = pcall(processRequest, msg)
|
||||
|
||||
if not ok then r = "ERROR" end
|
||||
|
||||
rednet.send(id, { msg = r, uid = msg.uid }, "dragon")
|
||||
end
|
||||
end
|
||||
return r
|
||||
end)
|
||||
end
|
||||
|
||||
updateIndex()
|
||||
|
10
util.lua
10
util.lua
@ -103,4 +103,12 @@ local function split(str, sSeparator, nMax, bRegexp)
|
||||
return aRecord
|
||||
end
|
||||
|
||||
return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied, split = split }
|
||||
local function processMessage(f)
|
||||
local id, msg = rednet.receive "dragon"
|
||||
if msg and msg.uid then
|
||||
local r = f(msg)
|
||||
rednet.send(id, { uid = msg.uid, msg = r }, "dragon")
|
||||
end
|
||||
end
|
||||
|
||||
return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied, split = split, process = process }
|
Loading…
Reference in New Issue
Block a user