Advertisement
jgheldG

Sort.Default.lua Eternal Weapon update for Imhothar's Bags

Apr 27th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. local Addon, private = ...
  2.  
  3. -- Locals
  4. local rarityOrder = {
  5.     sellable = 1,
  6.     common = 2,
  7.     uncommon = 3,
  8.     rare = 4,
  9.     epic = 5,
  10.     relic = 6,
  11.     transcendent = 7,
  12.     ascended = 8,
  13.     quest = 9,
  14.     eternal = 10,
  15.  
  16.     empty = 0,
  17. }
  18.  
  19.  
  20. setfenv(1, private)
  21. Sort = Sort or { }
  22. Sort.Default = { }
  23. local equipSlotCache = { }
  24.  
  25. -- Private methods
  26. -- ============================================================================
  27. local function getEquipSlot(item)
  28.     -- This function returns the slot that items are equipped on the player.  For example, a chest armor piece will return "chest".
  29.     -- It is important to note that, as of the writing of this function, only the feet, head, chest, shoulders, hands, legs, and
  30.     -- waist slots use different categories based upon the material of the item.  Therefore, those are the only ones that
  31.     -- require special treatment.
  32.     --
  33.     -- The equipSlotCache table is used for efficiency.
  34.  
  35.     local category = item and item.category or L.CategoryName.misc
  36.     local equipSlot = equipSlotCache[category]
  37.     if(equipSlot == nil) then
  38.         if (string.find(category,"feet")) then
  39.             equipSlot = "feet"
  40.         elseif (string.find(category,"head")) then
  41.             equipSlot = "head"         
  42.         elseif (string.find(category,"chest")) then
  43.             equipSlot = "chest"    
  44.         elseif (string.find(category,"shoulders")) then
  45.             equipSlot = "shoulders"    
  46.         elseif (string.find(category,"hands")) then
  47.             equipSlot = "hands"    
  48.         elseif (string.find(category,"waist")) then
  49.             equipSlot = "waist"                        
  50.         elseif (string.find(category,"legs")) then
  51.             equipSlot = "legs"                                                 
  52.         end
  53.         if(not equipSlot) then
  54.             equipSlot = category
  55.         end
  56.         --print("DEBUG: ", category, " == ", equipSlot)
  57.         equipSlotCache[category] = equipSlot
  58.     end
  59.    
  60.     return equipSlot
  61. end
  62.  
  63.  
  64.  
  65. -- Public methods
  66. -- ============================================================================
  67.  
  68. -- Sort two item types depending on their item name
  69. function Sort.Default.ByItemName(type1, type2)
  70.     return (type1 and type1.name or "") < (type2 and type2.name or "")
  71. end
  72.  
  73. -- Sort two item types depending on their icon path
  74. function Sort.Default.ByItemIcon(type1, type2)
  75.     return (type1 and type1.icon or "") < (type2 and type2.icon or "")
  76. end
  77.  
  78. -- Sort two item types depending on their item rarity
  79. function Sort.Default.ByItemRarity(type1, type2)
  80.     local r1 = type1 and type1.rarity or "common"
  81.     local b1 = type1 and type1.bind or "N/A"
  82.     local r2 = type2 and type2.rarity or "common"
  83.     local b2 = type2 and type2.bind or "N/A"
  84.     local s1 = 1
  85.     local s2 = 0
  86.    
  87.     ----
  88.     -- If item is armor or weapons, and the item is rare or better, sort by armor/weapon category (equip. slot position) first.
  89.     if  rarityOrder[r1] ~= nil and rarityOrder[r2] ~= nil  then
  90.         if (rarityOrder[r1] > 4 and rarityOrder[r2] > 4) then
  91.             local Category1 = Group.Default.GetLocalizedShortCategory(type1)
  92.             local Category2 = Group.Default.GetLocalizedShortCategory(type2)
  93.             if (Category1 == "Armor" and Category2 == "Armor" or Category1 == "Weapons" and Category2 == "Weapons") then
  94.                 local equipSlot1 = getEquipSlot(type1)
  95.                 local equipSlot2 = getEquipSlot(type2)
  96.                 if (equipSlot1 ~= equipSlot2) then
  97.                     return equipSlot1 > equipSlot2
  98.                 end
  99.             end
  100.         end
  101.     end
  102.    
  103.    
  104.     ----
  105.     -- Within each rarity type, place 'bind on pickup' items first, then items that are 'bind to account', then 'bind on equip' items, and then the remainder.
  106.     if (b1 == "pickup") then
  107.         s1 = rarityOrder[r1] + 0.1
  108.     elseif (b1 == "account") then
  109.         s1 = rarityOrder[r1] + 0.2
  110.     elseif (b1 == "equip") then
  111.         s1 = rarityOrder[r1] + 0.3
  112.     else
  113.         s1 = rarityOrder[r1]
  114.     end
  115.     if (b2 == "pickup") then
  116.         s2 = rarityOrder[r2] + 0.1
  117.     elseif (b2 == "account") then
  118.         s2 = rarityOrder[r2] + 0.2
  119.     elseif (b2 == "equip") then
  120.         s2 = rarityOrder[r2] + 0.3
  121.     else
  122.         s2 = rarityOrder[r2]
  123.     end
  124.  
  125.     if (s1 == s2) then
  126.         return (type1 and type1.name or "") < (type2 and type2.name or "")
  127.     end
  128.    
  129.     return s1 > s2
  130. end
  131.  
  132. -- Sort two item types depending on their inventory slot
  133. function Sort.Default.ByItemSlot(item1, item2)
  134.     return (item1 and item1.slot or "") < (item2 and item2.slot or "")
  135. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement