From b588060d3b7a1fe2353e9b7bc3ef457cfd4ff716 Mon Sep 17 00:00:00 2001 From: osmarks Date: Fri, 27 Jul 2018 16:18:33 +0100 Subject: [PATCH] Move collate_stacks --- backend-chests.lua | 11 +++-------- lib.lua | 13 ++++++++++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/backend-chests.lua b/backend-chests.lua index 9826fa9..b6962bc 100644 --- a/backend-chests.lua +++ b/backend-chests.lua @@ -165,14 +165,9 @@ local function server(command) return { moved = moved } elseif command.type == "search" then - local matching_items = d.map(search(command.query, command.threshold), function(x) return x.item end) - local out = {} - for _, stack in pairs(matching_items) do - local i = w.get_internal_identifier(stack) - if out[i] then out[i].count = out[i].count + stack.count - else out[i] = stack end - end - return out + return w.collate_stacks(d.map(search(command.query, command.threshold), function(x) return x.item end)) + elseif command.type == "list" then + return index end end diff --git a/lib.lua b/lib.lua index e60d9f1..b361967 100644 --- a/lib.lua +++ b/lib.lua @@ -182,6 +182,17 @@ local function collate(items) return ret end +-- Functions like "collate" but on itemstacks (adds their counts) +local function collate_stacks(s) + local out = {} + for _, stack in pairs(matching_items) do + local i = w.get_internal_identifier(stack) + if out[i] then out[i].count = out[i].count + stack.count + else out[i] = stack end + end + return out +end + -- Checks whether "needs"'s (a collate-formatted table) values are all greater than those of "has" local function satisfied(needs, has) local good = true @@ -229,4 +240,4 @@ local function init() d.map(find_peripherals(function(type, name, wrapped) return type == "modem" end), function(p) rednet.open(p.name) end) end -return { errors = errors, serve = serve, query_by_ID = query_by_ID, query_by_type = query_by_type, get_internal_identifier = get_internal_identifier, load_config = load_config, find_peripherals = find_peripherals, init = init, collate = collate, satisfied = satisfied } \ No newline at end of file +return { errors = errors, serve = serve, query_by_ID = query_by_ID, query_by_type = query_by_type, get_internal_identifier = get_internal_identifier, load_config = load_config, find_peripherals = find_peripherals, init = init, collate = collate, satisfied = satisfied, collate_stacks = collate_stacks } \ No newline at end of file