WIP: Add ability to find duplicates #4

Closed
Flamey wants to merge 2 commits from Flamey/findall:master into master
  1. 67
      findall.lua

67
findall.lua

@ -46,6 +46,8 @@ for name, id in pairs(Containers) do sorted_container_names[id + 1] = {id = id, @@ -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 = {}
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)
if storage_name == KEY_ITEM_STORAGE_NAME then
@ -170,6 +172,16 @@ function update_keyitem_storage(storage) @@ -170,6 +172,16 @@ function update_keyitem_storage(storage)
storage[KEY_ITEM_STORAGE_NAME] = ki_storage
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()
local player_name = get_player_name()
@ -200,6 +212,7 @@ function update_player_storages() @@ -200,6 +212,7 @@ function update_player_storages()
quantity = inv_entry.Count;
end
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
update_slip_storage(storages, inv_entry, item)
@ -259,12 +272,14 @@ function print_help() @@ -259,12 +272,14 @@ function print_help()
{ ' [!<character1> [!...]]', '- the names of the characters to exclude from the search.' },
{ ' [<query>]', '- the word you are looking for.' },
{ ' [-e<filename>|--export=<filename>]', '- exports the results to a csv file in the data folder.' },
{ '/findall duplicates', '- searches for items that exist in more than 1 container.' },
}
local examples = {
{ '/findall thaumas', '- Search for "thaumas" on all your characters.' },
{ '/findall :alpha :beta thaumas', '- Search for "thaumas" on "alpha" and "beta" characters.' },
{ '/findall :omega', '- Show all the items stored on "omega".' },
{ '/findall !alpha thaumas', '- Search for "thaumas" on all your characters except "alpha".' },
{ '/findall duplicates :alpha bolt', '- Search for "bolts" that exist in more than 1 storage area on character "alpha".' },
}
print('\31\200[\31\05' .. _addon.name .. '\31\200]\30\01' .. ' Version ' .. _addon.version)
@ -312,11 +327,14 @@ function determine_query_elements(searchparams) @@ -312,11 +327,14 @@ function determine_query_elements(searchparams)
local char_exclude = {}
local search_terms = {}
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 ':'
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)
if string.sub(char_search, 1, 1) == '!' then
table.insert(char_exclude, char_name)
@ -341,7 +359,7 @@ function determine_query_elements(searchparams) @@ -341,7 +359,7 @@ function determine_query_elements(searchparams)
end
end
return char_include, char_exclude, table.concat(search_terms,' '), export
return operation, char_include, char_exclude, table.concat(search_terms,' '), export
end
@ -356,6 +374,45 @@ function build_search_pattern(terms) @@ -356,6 +374,45 @@ function build_search_pattern(terms)
return terms_pattern
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)
local results = {}
@ -472,8 +529,8 @@ end @@ -472,8 +529,8 @@ end
function handle_command(args)
local char_include, char_exclude, terms, export = determine_query_elements(args)
local result = search(char_include, char_exclude, terms)
local command, char_include, char_exclude, terms, export = determine_query_elements(args)
local result = command(char_include, char_exclude, terms)
local all_chars = #char_include == 0 and #char_exclude == 0
display_search_results(result, all_chars, terms)
if export ~=nil then

Loading…
Cancel
Save