Advertisement
Guest User

customvalidate

a guest
Feb 15th, 2022
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.58 KB | None | 0 0
  1. --Copyright (c) 2013~2016, Byrthnoth
  2. --All rights reserved.
  3.  
  4. --Redistribution and use in source and binary forms, with or without
  5. --modification, are permitted provided that the following conditions are met:
  6.  
  7. --    * Redistributions of source code must retain the above copyright
  8. --      notice, this list of conditions and the following disclaimer.
  9. --    * Redistributions in binary form must reproduce the above copyright
  10. --      notice, this list of conditions and the following disclaimer in the
  11. --      documentation and/or other materials provided with the distribution.
  12. --    * Neither the name of <addon name> nor the
  13. --      names of its contributors may be used to endorse or promote products
  14. --      derived from this software without specific prior written permission.
  15.  
  16. --THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. --ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. --WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. --DISCLAIMED. IN NO EVENT SHALL <your name> BE LIABLE FOR ANY
  20. --DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. --(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. --LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. --ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. --(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. --SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  
  27. -------------------------------------------------------------------------------------------------------------------
  28. -- Primary entry point.
  29. -------------------------------------------------------------------------------------------------------------------
  30.  
  31. -- Validate either gear sets or inventory.
  32. -- gs validate [inv|set] [filterlist]
  33. -- Where inv == i or inv or inventory
  34. -- Where set == s or set or sets
  35. -- Where filterlist can use - to negate a value (eg: -charis for everything except charis, instead of only charis)
  36. function validate(options)
  37.     local validateType = 'sets'
  38.     if options and #options > 0 then
  39.         if S{'sets','set','s'}:contains(options[1]:lower()) then
  40.             validateType = 'sets'
  41.             table.remove(options,1)
  42.         elseif S{'inventory','inv','i'}:contains(options[1]:lower()) then
  43.             validateType = 'inventory'
  44.             table.remove(options,1)
  45.         elseif S{'mogsafe','safe','ms','bank'}:contains(options[1]:lower()) then
  46.             validateType = 'safe'
  47.             table.remove(options,1)
  48.         elseif S{'mogsafe2','safe2','ms2','bank2'}:contains(options[1]:lower()) then
  49.             validateType = 'safe2'
  50.             table.remove(options,1)
  51.         elseif S{'storage','st'}:contains(options[1]:lower()) then
  52.             validateType = 'storage'
  53.             table.remove(options,1)
  54.         elseif S{'moglocker','locker','ml'}:contains(options[1]:lower()) then
  55.             validateType = 'locker'
  56.             table.remove(options,1)
  57.         elseif S{'mogsatchel','satchel','sa'}:contains(options[1]:lower()) then
  58.             validateType = 'satchel'
  59.             table.remove(options,1)
  60.         elseif S{'mogsack','sack','sk'}:contains(options[1]:lower()) then
  61.             validateType = 'sack'
  62.             table.remove(options,1)
  63.         elseif S{'mogcase','case','c'}:contains(options[1]:lower()) then
  64.             validateType = 'case'
  65.             table.remove(options,1)
  66.         elseif S{'wardrobe','w'}:contains(options[1]:lower()) then
  67.             validateType = 'wardrobe'
  68.             table.remove(options,1)
  69.         elseif S{'wardrobe2','w2'}:contains(options[1]:lower()) then
  70.             validateType = 'wardrobe2'
  71.             table.remove(options,1)
  72.         elseif S{'wardrobe3','w3'}:contains(options[1]:lower()) then
  73.             validateType = 'wardrobe3'
  74.             table.remove(options,1)
  75.         elseif S{'wardrobe4','w4'}:contains(options[1]:lower()) then
  76.             validateType = 'wardrobe4'
  77.             table.remove(options,1)
  78.                 elseif S{'wardrobe5','w5'}:contains(options[1]:lower()) then
  79.             validateType = 'wardrobe5'
  80.             table.remove(options,1)
  81.         elseif S{'wardrobe6','w6'}:contains(options[1]:lower()) then
  82.             validateType = 'wardrobe6'
  83.             table.remove(options,1)
  84.         elseif S{'wardrobe7','w7'}:contains(options[1]:lower()) then
  85.             validateType = 'wardrobe7'
  86.             table.remove(options,1)
  87.         elseif S{'wardrobe8','w8'}:contains(options[1]:lower()) then
  88.             validateType = 'wardrobe8'
  89.             table.remove(options,1)
  90.         end
  91.     end
  92.    
  93.     if validateType == 'sets' then
  94.         validate_sets(options)
  95.     else
  96.         validate_inventory(options, validateType)
  97.     end
  98. end
  99.  
  100. -------------------------------------------------------------------------------------------------------------------
  101. -- Functions to handle the primary logic separation.
  102. -------------------------------------------------------------------------------------------------------------------
  103.  
  104. -- Function for determining and displaying which items from a player's selected repository are not in their gear sets.
  105. function validate_inventory(filter, repository)
  106.     msg.addon_msg(123,'Checking for items in '..repository..' that are not used in your gear sets.')
  107.    
  108.     --msg.addon_msg(123,items.repository)
  109.    
  110.     local extra_items = search_sets_for_items_in_bag(items[repository], filter)
  111.    
  112.     local display_list = get_item_names(extra_items):sort(insensitive_sort)
  113.     display_list:map(function(item) msg.add_to_chat(120, windower.to_shift_jis((string.gsub(item, "^%l", string.upper))) ) end)
  114.     msg.addon_msg(123,'Final count = '..tostring(display_list:length()))
  115. end
  116.  
  117. -- Function for determining and displaying which items of a player's gear sets are not in their inventory.
  118. function validate_sets(filter)
  119.     msg.addon_msg(123,'Checking for items in gear sets that are not in your inventory.')
  120.    
  121.     local missing_items = search_bags_for_items_in_set(sets, filter)
  122.  
  123.     local display_list = get_item_names(missing_items):sort(insensitive_sort)
  124.     display_list:map(function(item) msg.add_to_chat(120, windower.to_shift_jis((string.gsub(item, "^%l", string.upper))) ) end)
  125.     msg.addon_msg(123,'Final count = '..tostring(display_list:length()))
  126. end
  127.  
  128. -------------------------------------------------------------------------------------------------------------------
  129. -- Utility functions for output and id>name conversion.
  130. -------------------------------------------------------------------------------------------------------------------
  131.  
  132. -- Given a set of item IDs, create a set of item names.
  133. function get_item_names(item_set)
  134.     return item_set:map(get_item_name)
  135. end
  136.  
  137. -- Get the name of an item.  Handle the various types of items that can be passed to this function.
  138. function get_item_name(item)
  139.     local name = ''
  140.     local aug = ''
  141.  
  142.     if type(item) == 'string' then
  143.         name = item
  144.     elseif type(item) == 'table' then
  145.         if item.id then
  146.             name = get_formal_name_by_item_id(item.id)
  147.         elseif item.name then
  148.             name = item.name
  149.         end
  150.  
  151.         local aug = get_augment_string(item)
  152.         if aug then
  153.             name = name .. ' {' .. aug .. '}'
  154.         end
  155.     end
  156.  
  157.     return name
  158. end
  159.  
  160. -- Get the (preferably) capitalized version of an item's name, or the
  161. -- log version if the short version is abbreviated.
  162. function get_formal_name_by_item_id(id)
  163.     local shortname = get_short_name_by_item_id(id)
  164.     local logname = get_log_name_by_item_id(id)
  165.    
  166.     return (#logname > #shortname) and logname or shortname
  167. end
  168.  
  169. -- Given an item id, get the log item name.
  170. function get_log_name_by_item_id(id)
  171.     return res.items[id][language..'_log']
  172. end
  173.  
  174. -- Given an item id, get the short item name.
  175. function get_short_name_by_item_id(id)
  176.     return res.items[id][language]
  177. end
  178.  
  179. -- If the provided item has augments on it, return a string containing the list of augments.
  180. function get_augment_string(item)
  181.     local augments
  182.     if item.extdata then
  183.         augments = extdata.decode(item).augments or {}
  184.     else
  185.         augments = item.augment or item.augments
  186.     end
  187.  
  188.     local started = false
  189.     if augments and #augments > 0 then
  190.         local aug_str = ''
  191.         for aug_ind,augment in pairs(augments) do
  192.             if augment ~= 'none' then
  193.                 if started then
  194.                     aug_str = aug_str .. ','
  195.                 end
  196.                
  197.                 aug_str = aug_str.."'"..augment.."'"
  198.                 started = true
  199.             end
  200.         end
  201.        
  202.         return aug_str
  203.     end
  204. end
  205.  
  206. -------------------------------------------------------------------------------------------------------------------
  207. -- Utility functions for searching.
  208. -------------------------------------------------------------------------------------------------------------------
  209.  
  210. -- General search to find what 'extra' items are in inventory
  211. function search_sets_for_items_in_bag(bag, filter)
  212.     local extra_bag_items = S{}
  213.     for _,item in ipairs(bag) do
  214.         if item.id ~= 0 and tryfilter(lowercase_name(get_log_name_by_item_id(item.id)), filter) then
  215.             if not find_in_sets(item, sets) then
  216.                 extra_bag_items:add(item)
  217.             end
  218.         end
  219.     end
  220.    
  221.     return extra_bag_items
  222. end
  223.  
  224. -- General search to find what 'extra' items are in the job's gear sets
  225. function search_bags_for_items_in_set(gear_table, filter, missing_items, stack)
  226.     if stack and stack:contains(gear_table) then return end
  227.     if type(gear_table) ~= 'table' then return end
  228.     if missing_items == nil then missing_items = S{} end
  229.    
  230.     for i,v in pairs(gear_table) do
  231.         local name = (type(v) == 'table' and v.name) or v
  232.         local aug = (type (v) == 'table' and (v.augments or v.augment))
  233.        
  234.         if type(aug) == 'string' then aug = {aug} end
  235.         if type(name) == 'string' and name ~= 'empty' and name ~= '' and type(i) == 'string' then
  236.             if not slot_map[i] then
  237.                 msg.addon_msg(123,windower.to_shift_jis(tostring(i))..' contains a "name" element but is not a valid slot.')
  238.             elseif tryfilter(lowercase_name(name), filter) and not find_in_inv(items.inventory, name, aug) and not find_in_inv(items.wardrobe, name, aug) and not find_in_inv(items.wardrobe2, name, aug) then
  239.                 missing_items:add(lowercase_name(name))
  240.             end
  241.         elseif type(name) == 'table' and name ~= empty  then
  242.             if not stack then stack = S{} end
  243.  
  244.             stack:add(gear_table)
  245.             search_bags_for_items_in_set(v, filter, missing_items, stack)
  246.             stack:remove(gear_table)
  247.         end
  248.     end
  249.    
  250.     return missing_items
  251. end
  252.  
  253. -- Utility function to help search sets
  254. function find_in_sets(item, tab, stack)
  255.     if stack and stack:contains(tab) then
  256.         return false
  257.     end
  258.  
  259.     local item_short_name = lowercase_name(get_short_name_by_item_id(item.id))
  260.     local item_log_name = lowercase_name(get_log_name_by_item_id(item.id))
  261.  
  262.     for _,v in pairs(tab) do
  263.         local name = (type(v) == 'table' and v.name) or v
  264.         local aug = (type(v) == 'table' and (v.augments or v.augment))
  265.         if type(aug) == 'string' then aug = {aug} end
  266.         if type(name) == 'string' then
  267.             if compare_item(item, name, aug, item_short_name, item_log_name) then
  268.                 return true
  269.             end
  270.         elseif type(v) == 'table' then
  271.             if not stack then stack = S{} end
  272.  
  273.             stack:add(tab)
  274.             local try = find_in_sets(item, v, stack)
  275.             stack:remove(tab)
  276.  
  277.             if try then
  278.                 return true
  279.             end
  280.         end
  281.     end
  282.    
  283.     return false
  284. end
  285.  
  286. -- Utility function to help search inventory
  287. function find_in_inv(bag, name, aug)    
  288.     for _,item in ipairs(bag) do
  289.         if compare_item(item, name, aug) then
  290.             return true
  291.         end
  292.     end
  293.     return false
  294. end
  295.  
  296. -- Utility function to compare items that may possibly be augmented.
  297. function compare_item(item, name, aug, item_short_name, item_log_name)
  298.     if item.id == 0 or not res.items[item.id] then
  299.         return false
  300.     end
  301.    
  302.     name = lowercase_name(name)
  303.     item_short_name = lowercase_name(item_short_name or get_short_name_by_item_id(item.id))
  304.     item_log_name = lowercase_name(item_log_name or get_log_name_by_item_id(item.id))
  305.  
  306.     if item_short_name == name or item_log_name == name then
  307.         if not aug or extdata.compare_augments(aug, extdata.decode(item).augments) then
  308.             return true
  309.         end
  310.     end
  311.    
  312.     return false
  313. end
  314.  
  315.  
  316. -------------------------------------------------------------------------------------------------------------------
  317. -- Utility functions for filtering.
  318. -------------------------------------------------------------------------------------------------------------------
  319.  
  320. function tryfilter(itemname, filter)
  321.     if not filter or #filter == 0 then
  322.         return true
  323.     end
  324.    
  325.     local pass = true
  326.     for _,v in pairs(filter) do
  327.         if v[1] == '-' then
  328.             pass = false
  329.             v = v:sub(2)
  330.         end
  331.         if not v or type(v) ~= 'string' then
  332.             print_set(filter,'filter with bad v')
  333.         end
  334.         if itemname:contains(lowercase_name(v)) then
  335.             return pass
  336.         end
  337.     end
  338.     return not pass
  339. end
  340.  
  341.  
  342. function lowercase_name(name)
  343.     if type(name) == 'string' then
  344.         return name:lower()
  345.     else
  346.         return name
  347.     end
  348. end
  349.  
  350. function insensitive_sort(item1, item2)
  351.     if type(item1) == 'string' and type(item2) == 'string' then
  352.         return item1:lower() < item2:lower()
  353.     else
  354.         return item1 < item2
  355.     end
  356. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement