-
Notifications
You must be signed in to change notification settings - Fork 13
/
WhatsTraining.lua
470 lines (450 loc) · 16.8 KB
/
WhatsTraining.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
local addonName, wt = ...
local ignoreStore = LibStub:GetLibrary("FusionIgnoreStore-1.0")
local AVAILABLE_KEY = "available"
local MISSINGREQS_KEY = "missingReqs"
local NEXTLEVEL_KEY = "nextLevel"
local NOTLEVEL_KEY = "notLevel"
local MISSINGTALENT_KEY = "missingTalent"
local IGNORED_KEY = "ignored"
local IGNORED_PET_KEY = "ignoredPet"
local KNOWN_KEY = "known"
local KNOWN_PET_KEY = "knownPet"
local PET_KEY = "pet"
local COMINGSOON_FONT_COLOR_CODE = "|cff82c5ff"
local MISSINGTALENT_FONT_COLOR_CODE = "|cffffffff"
local PET_FONT_COLOR_CODE = "|cffffffff"
local function isPreviouslyLearnedAbility(spellId)
if wt.overriddenSpellsMap == nil or not wt.overriddenSpellsMap[spellId] then
return false
end
local spellIndex, knownIndex = 0, 0
for i, otherId in ipairs(wt.overriddenSpellsMap[spellId]) do
if otherId == spellId then spellIndex = i end
if (IsSpellKnown(otherId) or IsPlayerSpell(otherId)) then
knownIndex = i
end
end
return spellIndex <= knownIndex
end
local function isAbilityKnown(spellId)
if (IsSpellKnown(spellId) or IsPlayerSpell(spellId) or
isPreviouslyLearnedAbility(spellId)) then return true end
if (not wt:IsPetAbility(spellId)) then return false end
local info = wt:SpellInfo(spellId)
if info.subText == nil or wt.learnedPetAbilityMap[info.name] == nil then
return false
end
return wt.learnedPetAbilityMap[info.name][info.subText]
end
local headers = {
{
name = wt.L.AVAILABLE_HEADER,
color = GREEN_FONT_COLOR_CODE,
hideLevel = true,
key = AVAILABLE_KEY
}, {
name = wt.L.MISSINGREQS_HEADER,
color = ORANGE_FONT_COLOR_CODE,
hideLevel = true,
key = MISSINGREQS_KEY
}, {
name = wt.L.NEXTLEVEL_HEADER,
color = COMINGSOON_FONT_COLOR_CODE,
key = NEXTLEVEL_KEY
}, {
name = wt.L.NOTLEVEL_HEADER,
color = RED_FONT_COLOR_CODE,
key = NOTLEVEL_KEY
}, {
name = wt.L.PET_HEADER,
color = PET_FONT_COLOR_CODE,
key = PET_KEY
-- nameSort = true
}, {
name = wt.L.MISSINGTALENT_HEADER,
color = MISSINGTALENT_FONT_COLOR_CODE,
key = MISSINGTALENT_KEY,
nameSort = true
}, {
name = wt.L.IGNORED_HEADER,
color = LIGHTYELLOW_FONT_COLOR_CODE,
costFormat = wt.L.TOTALSAVINGS_FORMAT,
costColor = GREEN_FONT_COLOR_CODE,
key = IGNORED_KEY,
nameSort = true
}, {
name = wt.L.IGNORED_PET_HEADER,
color = LIGHTYELLOW_FONT_COLOR_CODE,
costFormat = wt.L.TOTALSAVINGS_FORMAT,
costColor = GREEN_FONT_COLOR_CODE,
key = IGNORED_PET_KEY,
nameSort = true
},{
name = wt.L.KNOWN_HEADER,
color = GRAY_FONT_COLOR_CODE,
hideLevel = true,
key = KNOWN_KEY,
costFormat = wt.L.TOTALSPENT_FORMAT,
costColor = RED_FONT_COLOR_CODE,
nameSort = true
}, {
name = wt.L.KNOWN_PET_HEADER,
color = GRAY_FONT_COLOR_CODE,
hideLevel = true,
key = KNOWN_PET_KEY,
costFormat = wt.L.TOTALSPENT_FORMAT,
costColor = RED_FONT_COLOR_CODE,
nameSort = true
}
}
local function makeCategories(headers)
return {
_spellsByCategoryKey = {},
Insert = function(self, key, spellInfo)
if self._spellsByCategoryKey[key] ~= nil then tinsert(self._spellsByCategoryKey[key], spellInfo) end
end,
Initialize = function(self)
for _, cat in ipairs(headers) do
cat.spells = {}
self._spellsByCategoryKey[cat.key] = cat.spells
cat.formattedName = cat.color and cat.color .. cat.name .. FONT_COLOR_CODE_CLOSE or cat.name
cat.isHeader = true
tinsert(self, cat)
end
end,
ClearSpells = function(self)
for _, cat in ipairs(self) do
cat.cost = 0
wipe(cat.spells)
end
end
}
end
local categories = makeCategories(headers)
categories:Initialize()
local brokerHeaders = {
{
name = wt.L.AVAILABLE_HEADER,
color = GREEN_FONT_COLOR_CODE,
hideLevel = true,
key = AVAILABLE_KEY,
maxDisplayEntries = 10,
}, {
name = wt.L.MISSINGREQS_HEADER,
color = ORANGE_FONT_COLOR_CODE,
hideLevel = true,
key = MISSINGREQS_KEY,
maxDisplayEntries = 5,
}, {
name = wt.L.NEXTLEVEL_HEADER,
color = COMINGSOON_FONT_COLOR_CODE,
key = NEXTLEVEL_KEY,
maxDisplayEntries = 10,
}, {
name = wt.L.NOTLEVEL_HEADER,
color = RED_FONT_COLOR_CODE,
key = NOTLEVEL_KEY,
maxDisplayEntries = 5,
}, {
name = wt.L.PET_HEADER,
color = PET_FONT_COLOR_CODE,
key = PET_KEY,
maxDisplayEntries = 5,
}
}
local brokerCategories = makeCategories(brokerHeaders)
brokerCategories:Initialize()
wt.data = {}
wt.brokerData = {}
wt.filter = ''
local function matchesFilter(spellOrItem)
if wt.filter == '' then return true end
return strfind(spellOrItem, wt.filter, 1, true)
end
local warlockPetOrder = {'Imp', 'Voidwalker', 'Succubus', 'Incubus', 'Felhunter'}
local function rebuildData(playerLevel, isLevelUpEvent)
categories:ClearSpells()
brokerCategories:ClearSpells()
wipe(wt.data)
wipe(wt.brokerData)
if wt.TomesByLevel then
for level, tomesAtLevel in pairs(wt.TomesByLevel) do
for _, tome in ipairs(tomesAtLevel) do
local itemInfo = wt:ItemInfo(tome.id)
if itemInfo ~= nil then
local key = PET_KEY
if wt:IsPetAbilityLearned(tome.id) then
key = KNOWN_PET_KEY
elseif ignoreStore:IsIgnored(tome.id) then
key = IGNORED_PET_KEY
end
if matchesFilter(itemInfo.searchText) then
categories:Insert(key, itemInfo)
end
brokerCategories:Insert(key, itemInfo)
end
end
end
end
for level, spellsAtLevel in pairs(wt.SpellsByLevel) do
for _, spell in ipairs(spellsAtLevel) do
local spellInfo = wt:SpellInfo(spell.id)
if spellInfo ~= nil then
local categoryKey
if (isAbilityKnown(spellInfo.id)) then
categoryKey = wt:IsPetAbility(spellInfo.id) and
KNOWN_PET_KEY or KNOWN_KEY
elseif (ignoreStore:IsIgnored(spellInfo.id)) then
categoryKey = wt:IsPetAbility(spellInfo.id) and
IGNORED_PET_KEY or IGNORED_KEY
elseif (wt:IsPetAbility(spellInfo.id)) then
categoryKey = PET_KEY
elseif (spell.requiredTalentId ~= nil and
not isAbilityKnown(spell.requiredTalentId)) then
categoryKey = MISSINGTALENT_KEY
elseif level > playerLevel then
categoryKey = level <= playerLevel + 2 and NEXTLEVEL_KEY or
NOTLEVEL_KEY
else
local hasReqs = true
if spell.requiredIds ~= nil then
for _, reqId in ipairs(spell.requiredIds) do
hasReqs = hasReqs and isAbilityKnown(reqId)
end
end
categoryKey = hasReqs and AVAILABLE_KEY or MISSINGREQS_KEY
end
if matchesFilter(spellInfo.searchText) then
categories:Insert(categoryKey, spellInfo)
end
brokerCategories:Insert(categoryKey, spellInfo)
end
end
end
local function byLevelThenName(a, b)
if a.level == b.level then return a.name < b.name end
return a.level < b.level
end
local function byNameThenLevel(a, b)
if a.name == b.name then return a.level < b.level end
return a.name < b.name
end
local function getSpellLevelColor(spell)
local effectiveLevel = spell.level
-- when a player levels up and this is triggered from that event, GetQuestDifficultyColor won't
-- have the correct player level, it will be off by 1 for whatever reason (just like UnitLevel)
if isLevelUpEvent then
effectiveLevel = effectiveLevel - 1
end
return GetQuestDifficultyColor(effectiveLevel)
end
local function categorySort(category)
local sortFunc = category.nameSort and byNameThenLevel or byLevelThenName
sort(category.spells, sortFunc)
end
for _, category in ipairs(categories) do
if #category.spells > 0 then
tinsert(wt.data, category)
categorySort(category)
local totalCost = 0
if (category.key == PET_KEY and wt.needsBeastTraining()) then
tinsert(wt.data, {
formattedName = ORANGE_FONT_COLOR_CODE ..
wt.L.OPEN_BEAST_TRAINING .. FONT_COLOR_CODE_CLOSE,
isHeader = true,
cost = 0,
tooltip = wt.L.CLICK_TO_OPEN,
click = function()
if InCombatLockdown() then
print(wt.L.OPEN_BEAST_IN_COMBAT)
else
wt.openBeastTraining()
end
end
})
end
if WT_ShowLearnedNotice == true and category.key == PET_KEY and wt.currentClass == "WARLOCK" then
tinsert(wt.data, {
formattedName = wt.L.RIGHT_CLICK_LEARNED,
isHeader = true,
cost = 0,
tooltip = wt.L.CLICK_TO_DISMISS,
click = function()
WT_ShowLearnedNotice = false
wt:RebuildData()
end
})
end
if category.key == PET_KEY and wt.currentClass == "WARLOCK" then
-- split by family, then weave sub-headers in
local byEnglishFamily = {}
for _, s in ipairs(category.spells) do
s.levelColor = getSpellLevelColor(s)
-- don't use the alt icon for things in the Pet category
s.useAltIcon = false
if not byEnglishFamily[s.family] then
byEnglishFamily[s.family] = {localFamily = s.localFamily, cost = 0}
end
local familyTable = byEnglishFamily[s.family]
tinsert(familyTable, s)
familyTable.cost = familyTable.cost + s.cost
totalCost = totalCost + s.cost
end
for _, englishFamily in ipairs(warlockPetOrder) do
local family = byEnglishFamily[englishFamily]
if family and #family > 0 then
tinsert(wt.data, {
formattedName = family.localFamily,
isHeader = true,
cost = family.cost
})
for _, s in ipairs(family) do
tinsert(wt.data, s)
end
end
end
else
for _, s in ipairs(category.spells) do
s.levelColor = getSpellLevelColor(s)
s.hideLevel = category.hideLevel
-- warlock succubus/incubus tomes should show the appropriate icon when in the ignored or known categories
s.useAltIcon = wt.currentClass == "WARLOCK"
totalCost = totalCost + s.cost
tinsert(wt.data, s)
end
end
category.cost = totalCost
end
end
if #wt.data == 0 and wt.filter ~= '' then
tinsert(wt.data, {
formattedName = wt.L.SEARCH_NO_RESULTS,
isHeader = true,
cost = 0
})
end
local brokerAvailable = 0
local brokerComing = 0
for _, category in ipairs(brokerCategories) do
if #category.spells > 0 then
category.costFormat = "%s"
tinsert(wt.brokerData, category)
categorySort(category)
local displayedCost = 0
local hiddenCost = 0
local totalCost = 0
category.displayedSpells = {}
for _, s in ipairs(category.spells) do
s.levelColor = getSpellLevelColor(s)
s.hideLevel = category.hideLevel
s.costFormat = "%s"
if #category.displayedSpells < category.maxDisplayEntries then
tinsert(category.displayedSpells, s)
displayedCost = displayedCost + s.cost
else
hiddenCost = hiddenCost + s.cost
end
totalCost = totalCost + s.cost
end
category.cost = totalCost
category.displayed = {cost = displayedCost, costFormat = "%s"}
category.hidden = {cost = hiddenCost, costFormat = "%s"}
if category.key == AVAILABLE_KEY then
brokerAvailable = #category.spells
end
if category.key == NEXTLEVEL_KEY then
brokerComing = #category.spells
end
end
end
if wt.updateBroker ~= nil then wt.updateBroker(brokerAvailable, brokerComing) end
end
local function rebuildIfNotCached(fromCache)
if fromCache or wt.MainFrame == nil then return end
rebuildData(UnitLevel("player"))
end
function wt:RebuildData()
rebuildData(UnitLevel("player"))
if (self.MainFrame and self.MainFrame:IsVisible()) then
self.Update(self.MainFrame, true)
end
end
function wt.afterPetUpdate()
WT_NeedsToOpenBeastTraining = false
wt:RebuildData()
end
function wt.onSpellLearned(name)
local petAbility = wt:PetAbility(name)
if petAbility == nil then return end
if petAbility.subText then
if wt.learnedPetAbilityMap[petAbility.name] == nil then
wt.learnedPetAbilityMap[petAbility.name] = {}
end
wt.learnedPetAbilityMap[petAbility.name][petAbility.subText] = true
else
WT_NeedsToOpenBeastTraining = true
end
wt:RebuildData()
end
if wt.TomesByLevel then
for level, tomesByLevel in pairs(wt.TomesByLevel) do
for _, tome in ipairs(tomesByLevel) do
local taughtSpellId = wt.TomeTaughtSpells[tome.itemId]
if not taughtSpellId then print("NO SPELL ID?", tome.itemId) end
wt:CacheSpell({
id = taughtSpellId,
cost = tome.cost,
}, level, function(_, spellInfo)
wt:CacheItem(tome, level, rebuildIfNotCached, spellInfo)
end)
end
end
end
for level, spellsByLevel in pairs(wt.SpellsByLevel) do
for _, spell in ipairs(spellsByLevel) do
wt:CacheSpell(spell, level, rebuildIfNotCached)
end
end
ignoreStore:AddSubscription(function()
wt:RebuildData()
end)
local eventFrame = CreateFrame("Frame")
eventFrame:SetScript("OnEvent", function(self, event, ...)
if event == "ADDON_LOADED" and ... == addonName then
if WT_ShowLearnedNotice == nil then
WT_ShowLearnedNotice = true
end
if WT_ShowIgnoreNotice == nil then
WT_ShowIgnoreNotice = true
end
if WT_IgnoredSpells == nil then
WT_IgnoredSpells = {}
end
ignoreStore:MigrateOrUse(WT_IgnoredSpells)
if WT_LearnedPetAbilities == nil then
WT_LearnedPetAbilities = {}
WT_NeedsToOpenBeastTraining = wt.currentClass == "HUNTER"
end
wt.learnedPetAbilityMap = WT_LearnedPetAbilities
if WT_NeedsToOpenBeastTraining == nil and wt.currentClass == "HUNTER" then
WT_NeedsToOpenBeastTraining = true
end
self:UnregisterEvent("ADDON_LOADED")
elseif event == "PLAYER_ENTERING_WORLD" then
local isLogin, isReload = ...
if isLogin or isReload then
rebuildData(UnitLevel("player"))
wt.CreateFrame()
end
elseif event == "LEARNED_SPELL_IN_TAB" or event == "PLAYER_LEVEL_UP" then
local isLevelUp = event == "PLAYER_LEVEL_UP"
rebuildData(isLevelUp and ... or UnitLevel("player"), isLevelUp)
if (wt.MainFrame and wt.MainFrame:IsVisible()) then
wt.Update(wt.MainFrame, true)
end
end
end)
eventFrame:RegisterEvent("ADDON_LOADED")
eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
eventFrame:RegisterEvent("PLAYER_LEVEL_UP")