Browse Source

Add first version of the /findall duplicates command.

Search query language remains the same except for the optional
duplicates key word.
pull/4/head
Alex Vallee 5 years ago
parent
commit
2eedd32788
  1. 65
      findall.lua

65
findall.lua

@ -46,6 +46,8 @@ for name, id in pairs(Containers) do sorted_container_names[id + 1] = {id = id,
-- global_storages[server str][character_name str][inventory_name str][item_id num] = count num -- global_storages[server str][character_name str][inventory_name str][item_id num] = count num
global_storages = {} global_storages = {}
item_names = {} item_names = {}
-- items_by_id[id num][character_name str][inventory_name str] = count num
items_by_id = {}
function get_item_names_by_id(storage_name, id) function get_item_names_by_id(storage_name, id)
if storage_name == KEY_ITEM_STORAGE_NAME then if storage_name == KEY_ITEM_STORAGE_NAME then
@ -170,6 +172,16 @@ function update_keyitem_storage(storage)
storage[KEY_ITEM_STORAGE_NAME] = ki_storage storage[KEY_ITEM_STORAGE_NAME] = ki_storage
end end
function update_item_index(itemId, count, player_name, container_name)
if items_by_id[itemId] == nil then
items_by_id[itemId] = {}
end
if items_by_id[itemId][player_name] == nil then
items_by_id[itemId][player_name] = {}
end
items_by_id[itemId][player_name][container_name] = count
end
function update_player_storages() function update_player_storages()
local player_name = get_player_name() local player_name = get_player_name()
@ -200,6 +212,7 @@ function update_player_storages()
quantity = inv_entry.Count; quantity = inv_entry.Count;
end end
current_storage[inv_entry.Id] = (current_storage[inv_entry.Id] or 0) + quantity current_storage[inv_entry.Id] = (current_storage[inv_entry.Id] or 0) + quantity
update_item_index(inv_entry.Id, current_storage[inv_entry.Id], player_name, container_name)
end end
update_slip_storage(storages, inv_entry, item) update_slip_storage(storages, inv_entry, item)
@ -312,11 +325,14 @@ function determine_query_elements(searchparams)
local char_exclude = {} local char_exclude = {}
local search_terms = {} local search_terms = {}
local export = nil local export = nil
local operation = search
for _, query_element in pairs(searchparams) do for i, query_element in pairs(searchparams) do
-- character specifiers must start with a '!' or a ':' -- character specifiers must start with a '!' or a ':'
local char_search = string.match(query_element, '^([:!]%a+)$') local char_search = string.match(query_element, '^([:!]%a+)$')
if char_search then if i == 1 and query_element == 'duplicates' then
operation = duplicates
elseif char_search then
local char_name = string.gsub(string.lower(string.sub(char_search, 2)), "^%l", string.upper) local char_name = string.gsub(string.lower(string.sub(char_search, 2)), "^%l", string.upper)
if string.sub(char_search, 1, 1) == '!' then if string.sub(char_search, 1, 1) == '!' then
table.insert(char_exclude, char_name) table.insert(char_exclude, char_name)
@ -341,7 +357,7 @@ function determine_query_elements(searchparams)
end end
end end
return char_include, char_exclude, table.concat(search_terms,' '), export return operation, char_include, char_exclude, table.concat(search_terms,' '), export
end end
@ -356,6 +372,45 @@ function build_search_pattern(terms)
return terms_pattern return terms_pattern
end end
function duplicates(char_include, char_exclude, terms)
log('duplicates', 'Searching duplicates: ' .. terms)
local terms_pattern = build_search_pattern(terms)
local entries = {}
-- entry = {char_name, storage_name, quantity}
for item_id, instances in pairs(items_by_id) do
local names = get_item_names_by_id(Containers.Inventory, item_id)
if terms_pattern == ''
or string.find(names.name, terms_pattern)
or string.find(names.long_name, terms_pattern)
then
entries[item_id] = {}
for char_name, storages in pairs(instances) do
if (#char_include == 0 or table.hasvalue(char_include, char_name)) and
not table.hasvalue(char_exclude, char_name) then
for storage_name, quantity in pairs(storages) do
table.insert(
entries[item_id],
{char_name = char_name, storage_name = storage_name, quantity = quantity})
end
end
end
end
end
local results = {}
-- results[char_name][storage_name][item_id] = quantity
for item_id, entry_list in pairs(entries) do
if #entry_list > 1 then
for _, entry in pairs(entry_list) do
results[entry.char_name] = results[entry.char_name] or {}
results[entry.char_name][entry.storage_name] = results[entry.char_name][entry.storage_name] or {}
results[entry.char_name][entry.storage_name][item_id] = entry.quantity
end
end
end
return results
end
function search(char_include, char_exclude, terms) function search(char_include, char_exclude, terms)
local results = {} local results = {}
@ -472,8 +527,8 @@ end
function handle_command(args) function handle_command(args)
local char_include, char_exclude, terms, export = determine_query_elements(args) local command, char_include, char_exclude, terms, export = determine_query_elements(args)
local result = search(char_include, char_exclude, terms) local result = command(char_include, char_exclude, terms)
local all_chars = #char_include == 0 and #char_exclude == 0 local all_chars = #char_include == 0 and #char_exclude == 0
display_search_results(result, all_chars, terms) display_search_results(result, all_chars, terms)
if export ~=nil then if export ~=nil then

Loading…
Cancel
Save