From 84ce6429734b12b0219cee47a26cc35c656bc5fb Mon Sep 17 00:00:00 2001 From: osmarks Date: Sun, 6 May 2018 08:28:22 +0100 Subject: [PATCH] Abstract out request processing functionality --- client.lua | 2 +- crafter.lua | 11 ++++++----- server.lua | 11 ++++------- util.lua | 10 +++++++++- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/client.lua b/client.lua index abf6cbc..bf9dea7 100644 --- a/client.lua +++ b/client.lua @@ -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 diff --git a/crafter.lua b/crafter.lua index c4b363d..d2e042b 100644 --- a/crafter.lua +++ b/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 \ No newline at end of file diff --git a/server.lua b/server.lua index 6dbac1f..8811b7c 100644 --- a/server.lua +++ b/server.lua @@ -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() diff --git a/util.lua b/util.lua index 172d6f9..6872254 100644 --- a/util.lua +++ b/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 } \ No newline at end of file +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 } \ No newline at end of file