-
Notifications
You must be signed in to change notification settings - Fork 5
/
GemUtil.lua
249 lines (233 loc) · 9.03 KB
/
GemUtil.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
---@class RemixGemHelperPrivate
local Private = select(2, ...)
local const = Private.constants
local addon = Private.Addon
---@class SocketTypeInfo
---@field name string
---@field icon integer
---@field color colorRGB
local gemUtil = {
owned_gems = {}
}
Private.GemUtil = gemUtil
---@param key number|string|?
---@return SocketTypeInfo|?
function gemUtil:GetSocketTypeInfo(key)
if not key then return end
if type(key) == "number" then
key = const.SOCKET_TYPES_INDEX[key]
end
local category = const.SOCKET_TYPE_INFO[key]
return category
end
---@param key number|string|?
---@return string
function gemUtil:GetSocketTypeName(key)
local typeInfo = self:GetSocketTypeInfo(key)
return typeInfo and typeInfo.name or "All"
end
---@param socketTypeName string
---@return integer usedSlots
---@return integer maxSlots
---@return integer|? freeEquipmentSlot
---@return integer|? freeSocketSlot
function gemUtil:GetSocketsInfo(socketTypeName)
local usedSlots, maxSlots = 0, 0
local freeEquipmentSlot, freeSocketSlot, freeIlvl
for _, equipmentSlot in ipairs(const.SOCKET_EQUIPMENT_SLOTS) do
local itemLoc = ItemLocation:CreateFromEquipmentSlot(equipmentSlot)
if itemLoc:IsValid() then
local itemLink = C_Item.GetItemLink(itemLoc)
if itemLink then
local itemStats = C_Item.GetItemStats(itemLink)
if itemStats then
for stat, itemMaxSlots in pairs(itemStats) do
if stat:match("EMPTY_SOCKET_" .. socketTypeName:upper()) then
local itemGems = gemUtil:GetItemGems(itemLink)
local itemUsedSlots = #itemGems
maxSlots = maxSlots + itemMaxSlots
usedSlots = usedSlots + itemUsedSlots
if itemUsedSlots < itemMaxSlots then
local itemLevel = C_Item.GetCurrentItemLevel(itemLoc)
if not freeEquipmentSlot or freeIlvl < itemLevel then
freeEquipmentSlot = equipmentSlot
freeIlvl = itemLevel
for slotIndex = 1, 3 do
local fss = itemGems.freeSpots[slotIndex]
if fss then
freeSocketSlot = slotIndex
break
end
end
end
end
end
end
end
end
end
end
return usedSlots, maxSlots, freeEquipmentSlot, freeSocketSlot
end
---@class GemItemInfo
---@field hyperlink string
---@field stackCount number
---@field itemID number
---@param dataTable table
---@param itemInfo GemItemInfo|ContainerItemInfo
---@param locType "BAG_GEM"|"BAG_SOCKET"|"EQUIP_SOCKET"|"UNCOLLECTED"
---@param locIndex integer|?
---@param locSlot integer|?
---@param gemSlot integer|?
function gemUtil:AddGemData(dataTable, itemInfo, locType, locIndex, locSlot, gemSlot)
if type(itemInfo) ~= "table" then return end
local itemType = select(6, C_Item.GetItemInfoInstant(itemInfo.hyperlink))
if itemType == 3 and self:GetGemSocketType(itemInfo.itemID) then
for i = 1, itemInfo.stackCount do
tinsert(dataTable, {
itemID = itemInfo.itemID,
locType = locType,
locIndex = locIndex,
locSlot = locSlot,
gemType = self:GetSocketTypeName(self:GetGemSocketType(itemInfo.itemID)),
gemSlot = gemSlot
})
Private.Cache:CacheItemInfo(itemInfo.itemID)
end
else
local gems = self:GetItemGems(itemInfo.hyperlink)
if #gems < 1 then return end
for gemSlot, gemID in ipairs(gems) do
if type(gemID) == "number" then
local cacheInfo = Private.Cache:GetItemInfo(gemID)
if cacheInfo and cacheInfo.link then
local hyperlink = cacheInfo.link
self:AddGemData(dataTable, {
itemID = gemID,
stackCount = 1,
hyperlink = hyperlink,
}, "BAG_SOCKET", locIndex, locSlot, gemSlot)
end
end
end
end
end
function gemUtil:RefreshOwnedGems()
wipe(self.owned_gems)
for bag = BACKPACK_CONTAINER, NUM_TOTAL_EQUIPPED_BAG_SLOTS do
for slot = 1, C_Container.GetContainerNumSlots(bag) do
self:AddGemData(self.owned_gems, C_Container.GetContainerItemInfo(bag, slot), "BAG_GEM", bag, slot)
end
end
for _, itemSlot in ipairs(const.SOCKET_EQUIPMENT_SLOTS) do
local itemLoc = ItemLocation:CreateFromEquipmentSlot(itemSlot)
if itemLoc:IsValid() then
local itemLink = C_Item.GetItemLink(itemLoc)
if itemLink then
for gemIndex = 1, 3 do
local gemName, gemLink = C_Item.GetItemGem(itemLink, gemIndex)
if gemName and gemLink then
self:AddGemData(self.owned_gems, {
itemName = gemName,
itemID = C_Item.GetItemInfoInstant(gemLink),
stackCount = 1,
hyperlink = gemLink
}, "EQUIP_SOCKET", itemSlot, gemIndex)
end
end
end
end
end
end
---@param itemID integer
---@return string|?
function gemUtil:GetGemSocketType(itemID)
return const.GEM_SOCKET_TYPE[itemID]
end
function gemUtil:GetItemGems(itemLink)
local _, linkOptions = LinkUtil.ExtractLink(itemLink)
local item = { strsplit(":", linkOptions) }
local gemsList = {
freeSpots = {}
}
for i = 1, 4 do
local gem = tonumber(item[i + 2])
if gem then
tinsert(gemsList, gem)
else
gemsList.freeSpots[i] = true
end
end
return gemsList
end
local function isMatchingFilter(itemID, filter)
filter = filter:gsub("%%", "%%%%"):gsub("%+", "%%+")
local gemItemInfo = Private.Cache:GetItemInfo(itemID)
local gemNameAndDesc = (gemItemInfo and gemItemInfo.name or "") ..
(gemItemInfo and gemItemInfo.description or ""):lower()
if not (filter and not gemNameAndDesc:match(filter)) then
return true
end
return false
end
---@param socketTypeFilter string|number|?
---@param nameFilter string|?
---@return table
function gemUtil:GetFilteredGems(socketTypeFilter, nameFilter)
local validGems = {}
self:RefreshOwnedGems()
if nameFilter then nameFilter = nameFilter:lower() end
if type(socketTypeFilter) == "number" then
socketTypeFilter = self:GetSocketTypeName(socketTypeFilter)
end
socketTypeFilter = socketTypeFilter or "ALL"
socketTypeFilter = socketTypeFilter:upper()
for _, socketType in ipairs(const.SOCKET_TYPES_INDEX) do
validGems[socketType] = {}
end
for _, gemInfo in pairs(self.owned_gems) do
local gemType = self:GetGemSocketType(gemInfo.itemID)
if socketTypeFilter == "ALL" or gemType == socketTypeFilter then
if gemType ~= "PRIMORDIAL" or addon:GetDatabaseValue("show_primordial") then
if isMatchingFilter(gemInfo.itemID, nameFilter) then
tinsert(validGems[gemType], gemInfo)
end
end
end
end
if addon:GetDatabaseValue("show_unowned") then
for gemItemID, gemType in pairs(const.GEM_SOCKET_TYPE) do
if socketTypeFilter == "ALL" or gemType == socketTypeFilter then
if isMatchingFilter(gemItemID, nameFilter) then
local dupeID = false
for _, gemInfo in ipairs(self.owned_gems) do
if gemInfo.itemID == gemItemID then
dupeID = true
break
end
end
if (not dupeID) and (gemType ~= "PRIMORDIAL" or addon:GetDatabaseValue("show_primordial")) then
local cacheInfo = Private.Cache:GetItemInfo(gemItemID)
if cacheInfo and cacheInfo.link then
local hyperlink = cacheInfo.link
self:AddGemData(validGems[gemType],
{
itemID = gemItemID,
stackCount = 1,
hyperlink = hyperlink
}, "UNCOLLECTED")
end
end
end
end
end
end
return validGems
end
function gemUtil:GetGemStats(description)
local stat = ""
if description and type(description) == "string" then
stat = description:match("%++[^\n]+")
end
return stat
end