Extend findall's search capabilities #5

Merged
atom0s merged 3 commits from Yunamahoutsukai/findall:master into master 5 years ago
  1. 2
      README.md
  2. 81
      findall.lua

2
README.md

@ -26,7 +26,9 @@ findall [:<character1> [:...]] <query> [-e<filename>|--export=<filename>]
* `character1`: the name of the characters to use for the search. * `character1`: the name of the characters to use for the search.
* `...`: variable list of character names. * `...`: variable list of character names.
* `query` the word you are looking for. * `query` the word you are looking for.
* `-d` or `--duplicates` to list only items which are found in more than one container
* `-e<filename>` or `--export=<filename>` exports the results to a csv file. The file will be created in the data folder. * `-e<filename>` or `--export=<filename>` exports the results to a csv file. The file will be created in the data folder.
* `-s` or `--stackables` to list only items which can stack
Looks for any item whose name (long or short) contains the specified value on the specified characters. Looks for any item whose name (long or short) contains the specified value on the specified characters.

81
findall.lua

@ -27,8 +27,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]] ]]
_addon.name = 'findAll' _addon.name = 'findAll'
_addon.author = 'Zohno (ported by farmboy0)' _addon.author = 'Zohno (ported and extended by farmboy0)'
_addon.version = '1.20170501' _addon.version = '2.0'
require 'ffxi.enums' require 'ffxi.enums'
require 'tableex' require 'tableex'
@ -79,6 +79,7 @@ function error(method, message)
print('ERR(' .. method .. '): ' .. message) print('ERR(' .. method .. '): ' .. message)
end end
function encase_key(key) function encase_key(key)
if type(key) == 'number' then if type(key) == 'number' then
return '[' .. tostring(key) .. ']' return '[' .. tostring(key) .. ']'
@ -258,7 +259,9 @@ function print_help()
{ ' [:<character1> [:...]]', '- the names of the characters to use for the search.' }, { ' [:<character1> [:...]]', '- the names of the characters to use for the search.' },
{ ' [!<character1> [!...]]', '- the names of the characters to exclude from the search.' }, { ' [!<character1> [!...]]', '- the names of the characters to exclude from the search.' },
{ ' [<query>]', '- the word you are looking for.' }, { ' [<query>]', '- the word you are looking for.' },
{ ' [-d|--duplicates]', '- list only items which are found in more than one container.' },
{ ' [-e<filename>|--export=<filename>]', '- exports the results to a csv file in the data folder.' }, { ' [-e<filename>|--export=<filename>]', '- exports the results to a csv file in the data folder.' },
{ ' [-s|--stackables]', '- list only items which can stack.' },
} }
local examples = { local examples = {
{ '/findall thaumas', '- Search for "thaumas" on all your characters.' }, { '/findall thaumas', '- Search for "thaumas" on all your characters.' },
@ -312,6 +315,8 @@ function determine_query_elements(searchparams)
local char_exclude = {} local char_exclude = {}
local search_terms = {} local search_terms = {}
local export = nil local export = nil
local duplicates = false
local stackables = false
for _, query_element in pairs(searchparams) do for _, query_element in pairs(searchparams) do
-- character specifiers must start with a '!' or a ':' -- character specifiers must start with a '!' or a ':'
@ -336,12 +341,16 @@ function determine_query_elements(searchparams)
error('determine_query_elements', 'The filename cannot contain any of the following characters: \\ / : * ? " < > |') error('determine_query_elements', 'The filename cannot contain any of the following characters: \\ / : * ? " < > |')
end end
end end
elseif string.match(query_element, '^--duplicates$') or string.match(query_element, '^-d$') then
duplicates = true
elseif string.match(query_element, '^--stackables') or string.match(query_element, '^-s$') then
stackables = true
else else
table.insert(search_terms, query_element) table.insert(search_terms, query_element)
end end
end end
return char_include, char_exclude, table.concat(search_terms,' '), export return char_include, char_exclude, table.concat(search_terms,' '), export, duplicates, stackables
end end
@ -389,7 +398,53 @@ function search(char_include, char_exclude, terms)
end end
function display_search_results(result, from_all_chars, terms) function filter_duplicates(search_result)
for char_name, storage_list in pairs(search_result) do
storage_list[KEY_ITEM_STORAGE_NAME] = nil
end
local counts_by_itemid = {}
for char_name, storage_list in pairs(search_result) do
for storage_name, item_list in pairs(storage_list) do
for item_id, quantity in pairs(item_list) do
counts_by_itemid[item_id] = (counts_by_itemid[item_id] or 0) + 1
end
end
end
for char_name, storage_list in pairs(search_result) do
for storage_name, item_list in pairs(storage_list) do
for item_id, quantity in pairs(item_list) do
if counts_by_itemid[item_id] == 1 then
item_list[item_id] = nil
end
end
end
end
return search_result
end
function filter_stackables(search_result)
for char_name, storage_list in pairs(search_result) do
storage_list[KEY_ITEM_STORAGE_NAME] = nil
end
for char_name, storage_list in pairs(search_result) do
for storage_name, item_list in pairs(storage_list) do
for item_id, quantity in pairs(item_list) do
local item = AshitaCore:GetResourceManager():GetItemById(item_id);
if not item or item.StackSize <= 1 then
item_list[item_id] = nil
end
end
end
end
return search_result
end
function display_search_results(result, from_all_chars, terms, duplicates)
local terms_pattern = build_search_pattern(terms) local terms_pattern = build_search_pattern(terms)
local total_quantity = 0 local total_quantity = 0
@ -403,7 +458,7 @@ function display_search_results(result, from_all_chars, terms)
total_quantity = total_quantity + quantity total_quantity = total_quantity + quantity
end end
local prefix = '\30\03' .. char_name .. '/' .. storage_name .. ':\30\01 ' local prefix = '\30\03' .. char_name .. '/' .. storage_name .. '\30\01'
local suffix = (quantity > 1 and ' \30\03' .. '(' .. quantity .. ')\30\01' or '') local suffix = (quantity > 1 and ' \30\03' .. '(' .. quantity .. ')\30\01' or '')
local item_name local item_name
@ -417,7 +472,11 @@ function display_search_results(result, from_all_chars, terms)
else else
item_name = names.name item_name = names.name
end end
table.insert(results, prefix .. item_name .. suffix) if duplicates then
table.insert(results, item_name .. ': ' .. prefix .. suffix)
else
table.insert(results, prefix .. '\30\03: \30\01' .. item_name .. suffix)
end
end end
end end
end end
@ -472,10 +531,16 @@ end
function handle_command(args) function handle_command(args)
local char_include, char_exclude, terms, export = determine_query_elements(args) local char_include, char_exclude, terms, export, duplicates, stackables = determine_query_elements(args)
local result = search(char_include, char_exclude, terms) local result = search(char_include, char_exclude, terms)
if duplicates then
result = filter_duplicates(result)
end
if stackables then
result = filter_stackables(result)
end
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, duplicates)
if export ~=nil then if export ~=nil then
export_to_file(result, export) export_to_file(result, export)
end end

Loading…
Cancel
Save