Guest User

Untitled

a guest
Nov 5th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.96 KB | None | 0 0
  1. -------------------------------------------------------------------------------------------------------------------
  2. -- Primary entry point.
  3. -------------------------------------------------------------------------------------------------------------------
  4.  
  5. -- Validate either gear sets or inventory.
  6. -- gs validate [inv|set] [filterlist]
  7. -- Where inv == i or inv or inventory
  8. -- Where set == s or set or sets
  9. -- Where filterlist can use - to negate a value (eg: -charis for everything except charis, instead of only charis)
  10. function validate(options)
  11.     local validateType = 'sets'
  12.     if options and #options > 0 then
  13.         if S{'sets','set','s'}:contains(options[1]:lower()) then
  14.             validateType = 'sets'
  15.             table.remove(options,1)
  16.         elseif S{'inventory','inv','i'}:contains(options[1]:lower()) then
  17.             validateType = 'inventory'
  18.             table.remove(options,1)
  19.         elseif S{'mogsafe','safe','ms'}:contains(options[1]:lower()) then
  20.             validateType = 'safe'
  21.             table.remove(options,1)
  22.         elseif S{'mogsafe2','safe2','ms2'}:contains(options[1]:lower()) then
  23.             validateType = 'safe 2'
  24.             table.remove(options,1)
  25.         elseif S{'storage','st'}:contains(options[1]:lower()) then
  26.             validateType = 'storage'
  27.             table.remove(options,1)
  28.         elseif S{'moglocker','locker','ml'}:contains(options[1]:lower()) then
  29.             validateType = 'locker'
  30.             table.remove(options,1)
  31.         elseif S{'mogsatchel','satchel','sa'}:contains(options[1]:lower()) then
  32.             validateType = 'satchel'
  33.             table.remove(options,1)
  34.         elseif S{'mogsack','sack','sk'}:contains(options[1]:lower()) then
  35.             validateType = 'sack'
  36.             table.remove(options,1)
  37.         elseif S{'mogcase','case','c'}:contains(options[1]:lower()) then
  38.             validateType = 'case'
  39.             table.remove(options,1)
  40.         elseif S{'wardrobe','w'}:contains(options[1]:lower()) then
  41.             validateType = 'wardrobe'
  42.             table.remove(options,1)
  43.         end
  44.     end
  45.    
  46.     if validateType == 'sets' then
  47.         validate_sets(options)
  48.     else
  49.         validate_inventory(options, validateType)
  50.     end
  51. end
  52.  
  53. -------------------------------------------------------------------------------------------------------------------
  54. -- Functions to handle the primary logic separation.
  55. -------------------------------------------------------------------------------------------------------------------
  56.  
  57. -- Function for determining and displaying which items from a player's selected repository are not in their gear sets.
  58. function validate_inventory(filter, repository)
  59.     msg.addon_msg(123,'Checking for items in '..repository..' that are not used in your gear sets.')
  60.    
  61.     local extra_items = search_sets_for_items_in_bag(items.repository, filter)
  62.    
  63.     local display_list = get_item_names(extra_items):sort(insensitive_sort)
  64.     display_list:map(function(item) msg.add_to_chat(120, (string.gsub(item, "^%l", string.upper)) ) end)
  65.     msg.addon_msg(123,'Final count = '..tostring(display_list:length()))
  66. end
  67.  
  68. -- Function for determining and displaying which items of a player's gear sets are not in their inventory.
  69. function validate_sets(filter)
  70.     msg.addon_msg(123,'Checking for items in gear sets that are not in your inventory.')
  71.    
  72.     local missing_items = search_bags_for_items_in_set(sets, filter)
  73.  
  74.     local display_list = get_item_names(missing_items):sort(insensitive_sort)
  75.     display_list:map(function(item) msg.add_to_chat(120, (string.gsub(item, "^%l", string.upper)) ) end)
  76.     msg.addon_msg(123,'Final count = '..tostring(display_list:length()))
  77. end
  78.  
  79. -------------------------------------------------------------------------------------------------------------------
  80. -- Utility functions for output and id>name conversion.
  81. -------------------------------------------------------------------------------------------------------------------
  82.  
  83. -- Given a set of item IDs, create a set of item names.
  84. function get_item_names(item_set)
  85.     return item_set:map(get_item_name)
  86. end
  87.  
  88. -- Get the name of an item.  Handle the various types of items that can be passed to this function.
  89. function get_item_name(item)
  90.     local name = ''
  91.     local aug = ''
  92.  
  93.     if type(item) == 'string' then
  94.         name = item
  95.     elseif type(item) == 'table' then
  96.         if item.id then
  97.             name = get_formal_name_by_item_id(item.id)
  98.         elseif item.name then
  99.             name = item.name
  100.         end
  101.  
  102.         local aug = get_augment_string(item)
  103.         if aug then
  104.             name = name .. ' {' .. aug .. '}'
  105.         end
  106.     end
  107.  
  108.     return name
  109. end
  110.  
  111. -- Get the (preferably) capitalized version of an item's name, or the
  112. -- log version if the short version is abbreviated.
  113. function get_formal_name_by_item_id(id)
  114.     local shortname = get_short_name_by_item_id(id)
  115.     local logname = get_log_name_by_item_id(id)
  116.    
  117.     return (#logname > #shortname) and logname or shortname
  118. end
  119.  
  120. -- Given an item id, get the log item name.
  121. function get_log_name_by_item_id(id)
  122.     return res.items[id][language..'_log']
  123. end
  124.  
  125. -- Given an item id, get the short item name.
  126. function get_short_name_by_item_id(id)
  127.     return res.items[id][language]
  128. end
  129.  
  130. -- If the provided item has augments on it, return a string containing the list of augments.
  131. function get_augment_string(item)
  132.     local augments
  133.     if item.extdata then
  134.         augments = extdata.decode(item).augments or {}
  135.     else
  136.         augments = item.augment or item.augments
  137.     end
  138.  
  139.     local started = false
  140.     if augments and #augments > 0 then
  141.         local aug_str = ''
  142.         for aug_ind,augment in pairs(augments) do
  143.             if augment ~= 'none' then
  144.                 if started then
  145.                     aug_str = aug_str .. ','
  146.                 end
  147.                
  148.                 aug_str = aug_str.."'"..augment.."'"
  149.                 started = true
  150.             end
  151.         end
  152.        
  153.         return aug_str
  154.     end
  155. end
  156.  
  157. -------------------------------------------------------------------------------------------------------------------
  158. -- Utility functions for searching.
  159. -------------------------------------------------------------------------------------------------------------------
  160.  
  161. -- General search to find what 'extra' items are in specified repository
  162. function search_sets_for_items_in_bag(bag, filter)
  163.     local extra_bag_items = S{}
  164.     for _,item in ipairs(bag) do
  165.         if item.id ~= 0 and tryfilter(lowercase_name(get_log_name_by_item_id(item.id)), filter) then
  166.             if not find_in_sets(item, sets) then
  167.                 extra_bag_items:add(item)
  168.             end
  169.         end
  170.     end
  171.    
  172.     return extra_bag_items
  173. end
  174.  
  175. -- General search to find what 'extra' items are in the job's gear sets
  176. function search_bags_for_items_in_set(gear_table, filter, missing_items, stack)
  177.     if stack and stack:contains(gear_table) then return end
  178.     if type(gear_table) ~= 'table' then return end
  179.     if missing_items == nil then missing_items = S{} end
  180.    
  181.     for i,v in pairs(gear_table) do
  182.         local name = (type(v) == 'table' and v.name) or v
  183.         local aug = (type (v) == 'table' and (v.augments or v.augment))
  184.        
  185.         if type(aug) == 'string' then aug = {aug} end
  186.         if type(name) == 'string' and name ~= 'empty' and name ~= '' and type(i) == 'string' then
  187.             if not slot_map[i] then
  188.                 msg.addon_msg(123,windower.to_shift_jis(tostring(i))..' contains a "name" element but is not a valid slot.')
  189.             elseif tryfilter(lowercase_name(name), filter) and not find_in_inv(items.inventory, name, aug) and not find_in_inv(items.wardrobe, name, aug) then
  190.                 missing_items:add(lowercase_name(name))
  191.             end
  192.         elseif type(name) == 'table' and name ~= empty  then
  193.             if not stack then stack = S{} end
  194.  
  195.             stack:add(gear_table)
  196.             search_bags_for_items_in_set(v, filter, missing_items, stack)
  197.             stack:remove(gear_table)
  198.         end
  199.     end
  200.    
  201.     return missing_items
  202. end
  203.  
  204. -- Utility function to help search sets
  205. function find_in_sets(item, tab, stack)
  206.     if stack and stack:contains(tab) then
  207.         return false
  208.     end
  209.  
  210.     local item_short_name = lowercase_name(get_short_name_by_item_id(item.id))
  211.     local item_log_name = lowercase_name(get_log_name_by_item_id(item.id))
  212.  
  213.     for _,v in pairs(tab) do
  214.         local name = (type(v) == 'table' and v.name) or v
  215.         local aug = (type(v) == 'table' and (v.augments or v.augment))
  216.         if type(aug) == 'string' then aug = {aug} end
  217.         if type(name) == 'string' then
  218.             if compare_item(item, name, aug, item_short_name, item_log_name) then
  219.                 return true
  220.             end
  221.         elseif type(v) == 'table' then
  222.             if not stack then stack = S{} end
  223.  
  224.             stack:add(tab)
  225.             local try = find_in_sets(item, v, stack)
  226.             stack:remove(tab)
  227.  
  228.             if try then
  229.                 return true
  230.             end
  231.         end
  232.     end
  233.    
  234.     return false
  235. end
  236.  
  237. -- Utility function to help search inventory
  238. function find_in_inv(bag, name, aug)    
  239.     for _,item in ipairs(bag) do
  240.         if compare_item(item, name, aug) then
  241.             return true
  242.         end
  243.     end
  244.     return false
  245. end
  246.  
  247. -- Utility function to compare items that may possibly be augmented.
  248. function compare_item(item, name, aug, item_short_name, item_log_name)
  249.     if item.id == 0 or not res.items[item.id] then
  250.         return false
  251.     end
  252.    
  253.     name = lowercase_name(name)
  254.     item_short_name = lowercase_name(item_short_name or get_short_name_by_item_id(item.id))
  255.     item_log_name = lowercase_name(item_log_name or get_log_name_by_item_id(item.id))
  256.  
  257.     if item_short_name == name or item_log_name == name then
  258.         if not aug or extdata.compare_augments(aug, extdata.decode(item).augments) then
  259.             return true
  260.         end
  261.     end
  262.    
  263.     return false
  264. end
  265.  
  266.  
  267. -------------------------------------------------------------------------------------------------------------------
  268. -- Utility functions for filtering.
  269. -------------------------------------------------------------------------------------------------------------------
  270.  
  271. function tryfilter(itemname, filter)
  272.     if not filter or #filter == 0 then
  273.         return true
  274.     end
  275.    
  276.     local pass = true
  277.     for _,v in pairs(filter) do
  278.         if v[1] == '-' then
  279.             pass = false
  280.             v = v:sub(2)
  281.         end
  282.         if not v or type(v) ~= 'string' then
  283.             print_set(filter,'filter with bad v')
  284.         end
  285.         if itemname:contains(lowercase_name(v)) then
  286.             return pass
  287.         end
  288.     end
  289.     return not pass
  290. end
  291.  
  292.  
  293. function lowercase_name(name)
  294.     if type(name) == 'string' then
  295.         return name:lower()
  296.     else
  297.         return name
  298.     end
  299. end
  300.  
  301. function insensitive_sort(item1, item2)
  302.     if type(item1) == 'string' and type(item2) == 'string' then
  303.         return item1:lower() < item2:lower()
  304.     else
  305.         return item1 < item2
  306.     end
  307. end
Advertisement
Add Comment
Please, Sign In to add comment