Advertisement
Guest User

Custom Gearswap Validate

a guest
Mar 12th, 2020
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.03 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.         end
  79.     end
  80.    
  81.     if validateType == 'sets' then
  82.         validate_sets(options)
  83.     else
  84.         validate_inventory(options, validateType)
  85.     end
  86. end
  87.  
  88. -------------------------------------------------------------------------------------------------------------------
  89. -- Functions to handle the primary logic separation.
  90. -------------------------------------------------------------------------------------------------------------------
  91.  
  92. -- Function for determining and displaying which items from a player's selected repository are not in their gear sets.
  93. function validate_inventory(filter, repository)
  94.     msg.addon_msg(123,'Checking for items in '..repository..' that are not used in your gear sets.')
  95.    
  96.     --msg.addon_msg(123,items.repository)
  97.    
  98.     local extra_items = search_sets_for_items_in_bag(items[repository], filter)
  99.    
  100.     local display_list = get_item_names(extra_items):sort(insensitive_sort)
  101.     display_list:map(function(item) msg.add_to_chat(120, windower.to_shift_jis((string.gsub(item, "^%l", string.upper))) ) end)
  102.     msg.addon_msg(123,'Final count = '..tostring(display_list:length()))
  103. end
  104.  
  105. -- Function for determining and displaying which items of a player's gear sets are not in their inventory.
  106. function validate_sets(filter)
  107.     msg.addon_msg(123,'Checking for items in gear sets that are not in your inventory.')
  108.    
  109.     local missing_items = search_bags_for_items_in_set(sets, filter)
  110.  
  111.     local display_list = get_item_names(missing_items):sort(insensitive_sort)
  112.     display_list:map(function(item) msg.add_to_chat(120, windower.to_shift_jis((string.gsub(item, "^%l", string.upper))) ) end)
  113.     msg.addon_msg(123,'Final count = '..tostring(display_list:length()))
  114. end
  115.  
  116. -------------------------------------------------------------------------------------------------------------------
  117. -- Utility functions for output and id>name conversion.
  118. -------------------------------------------------------------------------------------------------------------------
  119.  
  120. -- Given a set of item IDs, create a set of item names.
  121. function get_item_names(item_set)
  122.     return item_set:map(get_item_name)
  123. end
  124.  
  125. -- Get the name of an item.  Handle the various types of items that can be passed to this function.
  126. function get_item_name(item)
  127.     local name = ''
  128.     local aug = ''
  129.  
  130.     if type(item) == 'string' then
  131.         name = item
  132.     elseif type(item) == 'table' then
  133.         if item.id then
  134.             name = get_formal_name_by_item_id(item.id)
  135.         elseif item.name then
  136.             name = item.name
  137.         end
  138.  
  139.         local aug = get_augment_string(item)
  140.         if aug then
  141.             name = name .. ' {' .. aug .. '}'
  142.         end
  143.     end
  144.  
  145.     return name
  146. end
  147.  
  148. -- Get the (preferably) capitalized version of an item's name, or the
  149. -- log version if the short version is abbreviated.
  150. function get_formal_name_by_item_id(id)
  151.     local shortname = get_short_name_by_item_id(id)
  152.     local logname = get_log_name_by_item_id(id)
  153.    
  154.     return (#logname > #shortname) and logname or shortname
  155. end
  156.  
  157. -- Given an item id, get the log item name.
  158. function get_log_name_by_item_id(id)
  159.     return res.items[id][language..'_log']
  160. end
  161.  
  162. -- Given an item id, get the short item name.
  163. function get_short_name_by_item_id(id)
  164.     return res.items[id][language]
  165. end
  166.  
  167. -- If the provided item has augments on it, return a string containing the list of augments.
  168. function get_augment_string(item)
  169.     local augments
  170.     if item.extdata then
  171.         augments = extdata.decode(item).augments or {}
  172.     else
  173.         augments = item.augment or item.augments
  174.     end
  175.  
  176.     local started = false
  177.     if augments and #augments > 0 then
  178.         local aug_str = ''
  179.         for aug_ind,augment in pairs(augments) do
  180.             if augment ~= 'none' then
  181.                 if started then
  182.                     aug_str = aug_str .. ','
  183.                 end
  184.                
  185.                 aug_str = aug_str.."'"..augment.."'"
  186.                 started = true
  187.             end
  188.         end
  189.        
  190.         return aug_str
  191.     end
  192. end
  193.  
  194. -------------------------------------------------------------------------------------------------------------------
  195. -- Utility functions for searching.
  196. -------------------------------------------------------------------------------------------------------------------
  197.  
  198. -- General search to find what 'extra' items are in inventory
  199. function search_sets_for_items_in_bag(bag, filter)
  200.     local extra_bag_items = S{}
  201.     for _,item in ipairs(bag) do
  202.         if item.id ~= 0 and tryfilter(lowercase_name(get_log_name_by_item_id(item.id)), filter) then
  203.             if not find_in_sets(item, sets) then
  204.                 extra_bag_items:add(item)
  205.             end
  206.         end
  207.     end
  208.    
  209.     return extra_bag_items
  210. end
  211.  
  212. -- General search to find what 'extra' items are in the job's gear sets
  213. function search_bags_for_items_in_set(gear_table, filter, missing_items, stack)
  214.     if stack and stack:contains(gear_table) then return end
  215.     if type(gear_table) ~= 'table' then return end
  216.     if missing_items == nil then missing_items = S{} end
  217.    
  218.     for i,v in pairs(gear_table) do
  219.         local name = (type(v) == 'table' and v.name) or v
  220.         local aug = (type (v) == 'table' and (v.augments or v.augment))
  221.        
  222.         if type(aug) == 'string' then aug = {aug} end
  223.         if type(name) == 'string' and name ~= 'empty' and name ~= '' and type(i) == 'string' then
  224.             if not slot_map[i] then
  225.                 msg.addon_msg(123,windower.to_shift_jis(tostring(i))..' contains a "name" element but is not a valid slot.')
  226.             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
  227.                 missing_items:add(lowercase_name(name))
  228.             end
  229.         elseif type(name) == 'table' and name ~= empty  then
  230.             if not stack then stack = S{} end
  231.  
  232.             stack:add(gear_table)
  233.             search_bags_for_items_in_set(v, filter, missing_items, stack)
  234.             stack:remove(gear_table)
  235.         end
  236.     end
  237.    
  238.     return missing_items
  239. end
  240.  
  241. -- Utility function to help search sets
  242. function find_in_sets(item, tab, stack)
  243.     if stack and stack:contains(tab) then
  244.         return false
  245.     end
  246.  
  247.     local item_short_name = lowercase_name(get_short_name_by_item_id(item.id))
  248.     local item_log_name = lowercase_name(get_log_name_by_item_id(item.id))
  249.  
  250.     for _,v in pairs(tab) do
  251.         local name = (type(v) == 'table' and v.name) or v
  252.         local aug = (type(v) == 'table' and (v.augments or v.augment))
  253.         if type(aug) == 'string' then aug = {aug} end
  254.         if type(name) == 'string' then
  255.             if compare_item(item, name, aug, item_short_name, item_log_name) then
  256.                 return true
  257.             end
  258.         elseif type(v) == 'table' then
  259.             if not stack then stack = S{} end
  260.  
  261.             stack:add(tab)
  262.             local try = find_in_sets(item, v, stack)
  263.             stack:remove(tab)
  264.  
  265.             if try then
  266.                 return true
  267.             end
  268.         end
  269.     end
  270.    
  271.     return false
  272. end
  273.  
  274. -- Utility function to help search inventory
  275. function find_in_inv(bag, name, aug)    
  276.     for _,item in ipairs(bag) do
  277.         if compare_item(item, name, aug) then
  278.             return true
  279.         end
  280.     end
  281.     return false
  282. end
  283.  
  284. -- Utility function to compare items that may possibly be augmented.
  285. function compare_item(item, name, aug, item_short_name, item_log_name)
  286.     if item.id == 0 or not res.items[item.id] then
  287.         return false
  288.     end
  289.    
  290.     name = lowercase_name(name)
  291.     item_short_name = lowercase_name(item_short_name or get_short_name_by_item_id(item.id))
  292.     item_log_name = lowercase_name(item_log_name or get_log_name_by_item_id(item.id))
  293.  
  294.     if item_short_name == name or item_log_name == name then
  295.         if not aug or extdata.compare_augments(aug, extdata.decode(item).augments) then
  296.             return true
  297.         end
  298.     end
  299.    
  300.     return false
  301. end
  302.  
  303.  
  304. -------------------------------------------------------------------------------------------------------------------
  305. -- Utility functions for filtering.
  306. -------------------------------------------------------------------------------------------------------------------
  307.  
  308. function tryfilter(itemname, filter)
  309.     if not filter or #filter == 0 then
  310.         return true
  311.     end
  312.    
  313.     local pass = true
  314.     for _,v in pairs(filter) do
  315.         if v[1] == '-' then
  316.             pass = false
  317.             v = v:sub(2)
  318.         end
  319.         if not v or type(v) ~= 'string' then
  320.             print_set(filter,'filter with bad v')
  321.         end
  322.         if itemname:contains(lowercase_name(v)) then
  323.             return pass
  324.         end
  325.     end
  326.     return not pass
  327. end
  328.  
  329.  
  330. function lowercase_name(name)
  331.     if type(name) == 'string' then
  332.         return name:lower()
  333.     else
  334.         return name
  335.     end
  336. end
  337.  
  338. function insensitive_sort(item1, item2)
  339.     if type(item1) == 'string' and type(item2) == 'string' then
  340.         return item1:lower() < item2:lower()
  341.     else
  342.         return item1 < item2
  343.     end
  344. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement