Make search nicer
This commit is contained in:
parent
55f992374c
commit
cea1d54240
@ -83,11 +83,12 @@ local function find_space()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function find_by_ID_meta(id, meta)
|
local function find_by_ID_meta_NBT(ID, meta, NBT)
|
||||||
return find(function(item)
|
return find(function(item)
|
||||||
return
|
return
|
||||||
(not meta or item.damage == meta) and -- if metadata provided, ensure match
|
(not meta or item.damage == meta) and -- if metadata provided, ensure match
|
||||||
(not id or item.name == id) -- if internal name provided, ensure match
|
(not id or item.name == ID) and -- if internal name provided, ensure match
|
||||||
|
(not NBT or item.nbtHash == NBT) -- if NBT hash provided, ensure match
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -125,7 +126,7 @@ local function server(command)
|
|||||||
elseif command.type == "reindex" then
|
elseif command.type == "reindex" then
|
||||||
os.queueEvent "reindex"
|
os.queueEvent "reindex"
|
||||||
elseif command.type == "extract" then
|
elseif command.type == "extract" then
|
||||||
local result = find_by_ID_meta(command.ID, command.meta)
|
local result = find_by_ID_meta(command.ID, command.meta, command.NBT)
|
||||||
local first_available = result[1]
|
local first_available = result[1]
|
||||||
|
|
||||||
-- Check if we have an item, and its stack is big enough; otherwise, send back an error.
|
-- Check if we have an item, and its stack is big enough; otherwise, send back an error.
|
||||||
@ -164,7 +165,14 @@ local function server(command)
|
|||||||
|
|
||||||
return { moved = moved }
|
return { moved = moved }
|
||||||
elseif command.type == "search" then
|
elseif command.type == "search" then
|
||||||
return d.map(search(command.query, command.threshold), function(x) return x.item end)
|
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] = out[i] + stack.count
|
||||||
|
else out[i] = stack.count end
|
||||||
|
end
|
||||||
|
return matching_items
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
20
lib.lua
20
lib.lua
@ -173,6 +173,24 @@ end
|
|||||||
|
|
||||||
-- GENERAL STUFF
|
-- GENERAL STUFF
|
||||||
|
|
||||||
|
-- Converts a table of the form {"x", "x", "y"} into {x = 2, y = 1}
|
||||||
|
local function collate(items)
|
||||||
|
local ret = {}
|
||||||
|
for _, i in pairs(items) do
|
||||||
|
ret[i] = (ret[i] or 0) + 1
|
||||||
|
end
|
||||||
|
return ret
|
||||||
|
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
|
||||||
|
for k, qty in pairs(needs) do
|
||||||
|
if qty > (has[k] or 0) then good = false end
|
||||||
|
end
|
||||||
|
return good
|
||||||
|
end
|
||||||
|
|
||||||
-- Loads a config file (in serialized-table format) from "filename" or wyvern_config.tbl
|
-- Loads a config file (in serialized-table format) from "filename" or wyvern_config.tbl
|
||||||
-- "required_data" is a list of keys which must be in the config file's data
|
-- "required_data" is a list of keys which must be in the config file's data
|
||||||
-- "defaults" is a map of keys and default values for them, which will be used if there is no matching key in the data
|
-- "defaults" is a map of keys and default values for them, which will be used if there is no matching key in the data
|
||||||
@ -211,4 +229,4 @@ local function init()
|
|||||||
d.map(find_peripherals(function(type, name, wrapped) return type == "modem" end), function(p) rednet.open(p.name) end)
|
d.map(find_peripherals(function(type, name, wrapped) return type == "modem" end), function(p) rednet.open(p.name) end)
|
||||||
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 }
|
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 }
|
Loading…
Reference in New Issue
Block a user