-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBonusRollPreview.lua
More file actions
399 lines (338 loc) · 12.1 KB
/
BonusRollPreview.lua
File metadata and controls
399 lines (338 loc) · 12.1 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
local _, addon = ...
-- sourced from _G
local LE_SPELL_CONFIRMATION_PROMPT_TYPE_BONUS_ROLL = LE_SPELL_CONFIRMATION_PROMPT_TYPE_BONUS_ROLL or 1
local GetSpecialization = GetSpecialization or C_SpecializationInfo.GetSpecialization -- deprecated in 12.x
local GetSpecializationInfo = GetSpecializationInfo or C_SpecializationInfo.GetSpecializationInfo -- deprecated in 12.x
local ignoredSpells = {
-- 7.0
[232109] = true, -- Return to Karazhan: Nighbane (no EJ entry)
[240042] = true, -- Arena 2v2 Weekly Quest
[240048] = true, -- Arena 3v3 Weekly Quest
[240052] = true, -- Battlegrounds 10v10 Weekly Quest
}
local specialItems = { -- non-equippable items confirmed available from bonus rolls
-- https://twitter.com/olandgren/status/428229005221691392
[89783] = true, -- Mount: Son of Galleon's Saddle
[94228] = true, -- Mount: Reins of the Cobalt Primordial Direhorn
[87771] = true, -- Mount: Reins of the Heavenly Onyx Cloud Serpent
[95057] = true, -- Mount: Reins of the Thundering Cobalt Cloud Serpent
}
local nop = function() end
local function resetButton(_, button)
button:Hide()
button:ClearAllPoints()
button.Icon:SetTexture(QUESTION_MARK_ICON)
button.Name:SetText(RETRIEVING_ITEM_INFO)
button.Slot:SetText('')
button.Class:SetText('')
button.Fav:SetText('')
button.itemID = nil
button.itemLink = nil
button.favoriteTag = nil
end
BonusRollPreviewMixin = {}
function BonusRollPreviewMixin:OnLoad()
if(BackdropTemplateMixin) then
Mixin(self, BackdropTemplateMixin)
end
self:SetBackdrop({
bgFile = [[Interface\ChatFrame\ChatFrameBackground]],
edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]],
tile = true,
edgeSize = 16,
tileSize = 16,
insets = {left=4, right=4, top=4, bottom=4},
})
self:RegisterEvent('SPELL_CONFIRMATION_PROMPT')
self:RegisterEvent('SPELL_CONFIRMATION_TIMEOUT')
self:RegisterEvent('PLAYER_ENTERING_WORLD')
self:RegisterEvent('PLAYER_LOGIN')
self:SetBackdropColor(0, 0, 0, 0.8)
self:SetFrameLevel(self:GetParent():GetFrameLevel() - 2)
-- TODO: consider parenting this frame to GroupLootContainer, but I'm afraid of taints
BonusRollFrame:ClearAllPoints()
BonusRollFrame:SetPoint('CENTER', BonusRollPreviewAnchor)
BonusRollFrame.ClearAllPoints = nop
BonusRollFrame.SetPoint = nop
self.buttons = CreateUnsecuredFramePool('Button', self.ScrollFrame.ScrollChild, 'BonusRollPreviewButtonTemplate', resetButton)
self.itemButtons = addon:T()
end
function BonusRollPreviewMixin:OnEvent(event, ...)
if(event == 'EJ_DIFFICULTY_UPDATE') then
-- difficulty to track has updated, update filter and item list
self:UnregisterSafeEvent(event)
self:UpdateItemFilter()
self:UpdateItems()
elseif(event == 'PLAYER_LOOT_SPEC_UPDATED') then
-- we need to restart the entire encounter logic just in case the user
-- has used the EncounterJournal before changing loot specializations.
self:StartEncounter()
elseif(event == 'SPELL_CONFIRMATION_PROMPT') then
local spellID, confirmType, _, _, currencyID, currencyCost, difficultyID = ...
if(confirmType ~= LE_SPELL_CONFIRMATION_PROMPT_TYPE_BONUS_ROLL) then
return
end
if(not ignoredSpells[spellID]) then -- ignore blacklisted encounters
local instanceID, encounterID = addon:GetJournalInfoForSpellConfirmation(spellID)
if(encounterID) then
local currency = C_CurrencyInfo.GetCurrencyInfo(currencyID)
if(currency.quantity >= currencyCost) then
self.difficultyID = difficultyID
self.encounterID = encounterID
self.instanceID = instanceID
self:RegisterEvent('PLAYER_LOOT_SPEC_UPDATED')
self:StartEncounter()
-- show/hide list and handle
self:SetShown(BonusRollPreviewDB.alwaysShow)
self:UpdatePosition()
BonusRollPreviewHandle:Show()
end
end
end
elseif(event == 'SPELL_CONFIRMATION_TIMEOUT') then
self:UnregisterEvent('PLAYER_LOOT_SPEC_UPDATED')
self:Hide()
BonusRollPreviewHandle:Hide()
self.buttons:ReleaseAll()
elseif(event == 'PLAYER_ENTERING_WORLD') then
-- check for any outstanding bonus rolls
for _, info in next, GetSpellConfirmationPromptsInfo() do
if(info and info.spellID) then
local difficultyID = info.difficultyID
if not difficultyID then
local inInstance, instanceType, _ = IsInInstance()
if inInstance and instanceType == 'raid' then
_, _, difficultyID = GetInstanceInfo()
end
end
self:OnEvent('SPELL_CONFIRMATION_PROMPT', info.spellID, info.confirmType, nil, nil, info.currencyID, info.currencyCost, difficultyID or DifficultyUtil.ID.Raid25Normal)
end
end
elseif(event == 'PLAYER_LOGIN') then
-- update anchor position and frame positions
C_Timer.After(3, function() -- wait for db
BonusRollPreviewAnchor:ClearAllPoints()
BonusRollPreviewAnchor:SetPoint(unpack(BonusRollPreviewDB.anchor))
end)
end
end
function addon:ZONE_CHANGED_NEW_AREA()
-- warm up cache
local mapID = C_Map.GetBestMapForUnit('player')
if not mapID or mapID < 1 then
return
end
local _, _, classID = UnitClass('player')
EJ_SetLootFilter(classID, 0) -- all specs for wide cache
if IsInInstance() then
local _, _, difficultyID = GetInstanceInfo()
local instanceID = EJ_GetInstanceForMap(mapID)
if instanceID and pcall(EJ_SelectInstance, instanceID) then
EJ_SetDifficulty(difficultyID or DifficultyUtil.ID.Raid25Normal)
local journalIndex = 1
while journalIndex do
local _, _, encounterID = EJ_GetEncounterInfoByIndex(journalIndex)
if encounterID and encounterID > 0 then
EJ_SelectEncounter(encounterID)
for index = 1, EJ_GetNumLoot() do
C_EncounterJournal.GetLootInfoByIndex(index)
end
journalIndex = journalIndex + 1
else
break
end
end
end
else
for _, info in next, C_EncounterJournal.GetEncountersOnMap(mapID) do
local _, _, _, _, _, instanceID = EJ_GetEncounterInfo(info.encounterID)
if pcall(EJ_SelectInstance, instanceID) then
EJ_SelectEncounter(info.encounterID)
for index = 1, EJ_GetNumLoot() do
C_EncounterJournal.GetLootInfoByIndex(index)
end
end
end
end
end
function BonusRollPreviewMixin:StartEncounter()
-- start the encounter by selecting the encounter
self:RegisterSafeEvent('EJ_DIFFICULTY_UPDATE')
EJ_SelectInstance(self.instanceID)
EJ_SetDifficulty(self.difficultyID) -- this will trigger EJ_DIFFICULTY_UPDATE
EJ_SelectEncounter(self.encounterID) -- must be called last, otherwise breaks loot spec logic
end
local function shouldShowItem(itemID)
if specialItems[itemID] then
return true
end
local favoriteTag = addon.GetFavoriteTag and addon:GetFavoriteTag(itemID)
if BonusRollPreviewDB.favoritesOnly then
return favoriteTag ~= nil, favoriteTag
end
local _, _, _, _, _, itemClass, itemSubClass = GetItemInfoInstant(itemID)
if itemClass == Enum.ItemClass.Weapon then
return true, favoriteTag
elseif itemClass == Enum.ItemClass.Armor then
return true, favoriteTag
elseif itemClass == Enum.ItemClass.Gem and itemSubClass == Enum.ItemArmorSubclass.Relic then
-- azerite
return true, favoriteTag
elseif itemClass == Enum.ItemClass.Miscellaneous and itemSubClass == Enum.ItemMiscellaneousSubclass.Junk then
-- armor tokens
return true, favoriteTag
end
end
function BonusRollPreviewMixin:PrepareButton(index)
local button = self.buttons:Acquire()
button:Show()
-- set journal index
button.index = index
return button
end
function BonusRollPreviewMixin:ProcessItem(button, itemInfo)
if not itemInfo then
-- cache is still cold for some reason, this shouldn't happen
return
end
if itemInfo.encounterID ~= self.encounterID then
-- sometimes the API returns all loot for an entire instance, so we'll need to ignore
-- items from other encounters
return
end
local shouldShow, favoriteTag = shouldShowItem(itemInfo.itemID)
if not shouldShow then
return
end
if favoriteTag then
PlaySound(SOUNDKIT.LFG_REWARDS, 'SFX')
-- sort favorites first
button.index = 0
end
-- update button data
button.itemLink = itemInfo.link
button.itemID = itemInfo.itemID
-- update button widgets
button.Icon:SetTexture(itemInfo.icon)
button.Name:SetText(itemInfo.name)
button.Slot:SetText(itemInfo.slot)
button.Class:SetText(itemInfo.armorType)
button.Fav:SetText(BonusRollPreviewDB.favoriteAlert and favoriteTag and favoriteTag or '')
return true
end
local function sortByIndex(a, b)
return a.index < b.index
end
function BonusRollPreviewMixin:UpdateButtonPositions()
-- TODO: look into using a ScrollUtil instead of managing this ourselves
table.sort(self.itemButtons, sortByIndex)
for index, button in next, self.itemButtons do
button:SetPoint('TOPLEFT', 0, (index - 1) * -40)
button:SetPoint('TOPRIGHT', 0, (index - 1) * -40)
end
end
function BonusRollPreviewMixin:UpdateItems()
self.buttons:ReleaseAll() -- reset and hide all buttons in the pool
self.numShownItems = 0
self.itemButtons:wipe()
for index = 1, EJ_GetNumLoot() do
local itemInfo = C_EncounterJournal.GetLootInfoByIndex(index)
if itemInfo and itemInfo.encounterID == self.encounterID then
local button = self:PrepareButton(index, itemInfo.itemID)
if itemInfo and itemInfo.link and itemInfo.link ~= "" then
if self:ProcessItem(button, itemInfo) then
self.itemButtons:insert(button)
end
end
end
end
-- update box
self:SetHeight(Clamp(10 + (#self.itemButtons * 40), 50, 330))
self:UpdateButtonPositions()
self:UpdateScrolling()
end
function BonusRollPreviewMixin:UpdateItemFilter()
local _, classID = UnitClassBase('player')
local lootSpecialization = GetLootSpecialization() or 0
if(lootSpecialization == 0) then
lootSpecialization = (GetSpecializationInfo(GetSpecialization() or 0)) or 0
end
EJ_SetLootFilter(classID, lootSpecialization)
end
function BonusRollPreviewMixin:RegisterSafeEvent(event)
self:RegisterEvent(event)
if(EncounterJournal) then
-- if the EncounterJournal is loaded, prevent it from getting data and
-- prevent our events from being triggered when we don't want to
EncounterJournal:UnregisterEvent(event)
end
end
function BonusRollPreviewMixin:UnregisterSafeEvent(event)
self:UnregisterEvent(event)
if(EncounterJournal) then
-- if the EncounterJournal is loaded, let it have its events back
EncounterJournal:RegisterEvent(event)
end
end
function BonusRollPreviewMixin:UpdateHandlePosition(collapsed)
local Handle = BonusRollPreviewHandle
Handle:ClearAllPoints()
local downwards = BonusRollPreviewDB.fillDirection == 'DOWN'
if(collapsed) then
if(downwards) then
Handle:SetPoint('TOP', BonusRollFrame, 'BOTTOM', 0, 2)
Handle.Arrow:SetTexCoord(0, 0, 1/2, 0, 0, 1, 1/2, 1)
else
Handle:SetPoint('BOTTOM', BonusRollFrame, 'TOP', 0, -2)
Handle.Arrow:SetTexCoord(1/2, 1, 0, 1, 1/2, 0, 0, 0)
end
else
if(downwards) then
Handle:SetPoint('BOTTOM', self, 0, -14)
Handle.Arrow:SetTexCoord(1/2, 1, 1, 1, 1/2, 0, 1, 0)
else
Handle:SetPoint('TOP', self, 0, 14)
Handle.Arrow:SetTexCoord(1, 0, 1/2, 0, 1, 1, 1/2, 1)
end
end
Handle.TopLeft:SetShown(not downwards)
Handle.TopCenter:SetShown(not downwards)
Handle.TopRight:SetShown(not downwards)
Handle.BottomLeft:SetShown(downwards)
Handle.BottomCenter:SetShown(downwards)
Handle.BottomRight:SetShown(downwards)
end
function BonusRollPreviewMixin:UpdatePosition()
self:ClearAllPoints()
if(BonusRollPreviewDB.fillDirection == 'DOWN') then
self:SetPoint('TOP', self:GetParent(), 'BOTTOM')
else
self:SetPoint('BOTTOM', self:GetParent(), 'TOP')
end
self:UpdateHandlePosition(not self:IsShown())
end
function BonusRollPreviewMixin:ToggleLock()
local Anchor = BonusRollPreviewAnchor
Anchor:SetShown(not Anchor:IsShown())
end
function BonusRollPreviewMixin:UpdateScrolling()
local ScrollFrame = self.ScrollFrame
local numButtons = #self.itemButtons
if numButtons > 8 then
ScrollFrame:EnableMouseWheel(true)
ScrollFrame.ScrollChild:SetWidth(274 - ScrollFrame.Slider:GetWidth())
ScrollFrame.Slider:Show()
ScrollFrame.Slider:SetValue(0) -- reset scroll to top
-- update scroll values
local height = (10 + (numButtons * 40)) - self:GetHeight()
ScrollFrame.Slider:SetMinMaxValues(0, height)
else
ScrollFrame:EnableMouseWheel(false)
ScrollFrame.ScrollChild:SetWidth(274)
ScrollFrame.Slider:Hide()
end
end
function BonusRollPreviewMixin:Toggle()
self:SetShown(not self:IsShown())
self:UpdateHandlePosition(not self:IsShown())
end