-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.lua
345 lines (288 loc) · 9.75 KB
/
main.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
local LDB = LibStub("LibDataBroker-1.1")
local BrokerClassicTraining = LibStub("AceAddon-3.0"):NewAddon('Broker_Classic_Training', "AceEvent-3.0");
local UPDATEPERIOD, elapsed = 2, 0
BrokerClassicTraining.kbDEBUG = true
BrokerClassicTraining.constants = {
newTrainingAlert = 'badge',
}
-- The data feed
BrokerClassicTraining.Feed = {
ClassName = '',
level = 0,
newSpells = 0,
spells = {},
newBooks = 0,
books = {},
newTalents = 0
}
local f = CreateFrame("frame")
f:SetScript("OnUpdate", function(self, elap)
elapsed = elapsed + elap
if elapsed < UPDATEPERIOD then return end
BrokerClassicTraining:UpdateLabel()
elapsed = 0
end)
-- Addon Debug
function BrokerClassicTraining:Dump(str, obj)
if ViragDevTool_AddData and BrokerClassicTraining.kbDEBUG then
ViragDevTool_AddData(obj, str)
end
end
-- Class Colours
function BrokerClassicTraining:GetClassColourHex(class)
if class ~= nil then
class = UnitClass('player')
end
if class == 'PALADIN' then
return 'F58CBA'
elseif class == 'WARRIOR' then
return 'C79C6E'
elseif class == 'HUNTER' then
return 'ABD473'
elseif class == 'SHAMAN' then
return '0070DE'
elseif class == 'DRUID' then
return 'FF7D0A'
elseif class == 'ROGUE' then
return 'FFF569'
elseif class == 'MAGE' then
return '40C7EB'
elseif class == 'PRIEST' then
return 'FFFFFF'
elseif class == 'WARLOCK' then
return '8787ED'
end
return 'FFFFFF'
end
-- Update the label
function BrokerClassicTraining:UpdateLabel()
return BrokerClassicTraining:GetLabels()
end
-- Get the labels
function BrokerClassicTraining:GetLabels()
local label = 'Training'
local new = 0
if (BrokerClassicTraining.Feed.newSpells > 0) then
new = new + BrokerClassicTraining.Feed.newSpells
end
if (BrokerClassicTraining.Feed.newBooks > 0) then
new = new + BrokerClassicTraining.Feed.newBooks
end
if (BrokerClassicTraining.constants.newTrainingAlert == 'badge') then
if (new > 0) then
return label .. ' (' .. new .. ')'
end
end
return label
end
function BrokerClassicTraining:BuildTrainingData(self, level)
level = level or UnitLevel("player")
-- Get the list of player learnable spells
local localizedClass, englishClass, classIndex = UnitClass("player")
BrokerClassicTraining.Feed.ClassName = englishClass
local hex = BrokerClassicTraining:GetClassColourHex(englishClass)
------------
-- SPELLS --
------------
-- -- Get the spells for the class
local classKey = 'Broker_Classic_Training_'..englishClass
local spells = _G[classKey]
local talents = _G[classKey .. '_Talents']
-- Filter and format spells
if (spells ~= nil) then
BrokerClassicTraining:FilterSpells(spells, level, talents)
BrokerClassicTraining:FormatSpells(self, hex)
end
-----------------------
-- SPELL BOOKS/TOMES --
-----------------------
-- Get the spell books for the class
local classBooksKey = 'Broker_Classic_Training_'..englishClass..'_Tomes'
local books = _G[classBooksKey]
local EscapeColour = '|cFF'..hex
-- Filter and format spells
if (books ~= nil) then
BrokerClassicTraining:FilterSpellBooks(books, level)
BrokerClassicTraining:FormatSpellBooks(self, hex)
end
end
function BrokerClassicTraining:FilterSpells(spells, level, talents)
-- new table to hold learnable spells
local spellsLearnable = {}
local new = 0
level = level or UnitLevel("player")
BrokerClassicTraining.Feed.newSpells = 0
if (level > 0) then
for i=1,level do
local levelSpells = spells[i]
if (levelSpells ~= nil) then
for _,spell in pairs(levelSpells) do
-- Spell = v
if spell.id ~= nil and spell.id ~= 0 then -- Abort if spell id is zero or nil
local isKnown = IsSpellKnown(spell.id)
if (isKnown == false) then
local addSpell = true
-- Spells without mana and thus can't downrank use the replaced_by property to defer is known to later ranks.
if (spell.replaced_by ~= nil) then
for _,id in ipairs(spell.replaced_by) do
if (IsSpellKnown(id)) then
addSpell = false
end
end
end
-- Spells which are ranks of talented abilities need an aditional check if the talent isn't known
if (spell.talent ~= nil) then
local talent = talents[spell.talent]
BrokerClassicTraining:Dump('talent', talent)
if (IsSpellKnown(talent.spell_id)) then
-- Do nothing
else
addSpell = false
end
end
if (addSpell == true) then
BrokerClassicTraining.Feed.newSpells = BrokerClassicTraining.Feed.newSpells + 1
table.insert(spellsLearnable, spell)
new = new + 1
end
end
end
end
end
end
end
BrokerClassicTraining.Feed.spells = spellsLearnable
BrokerClassicTraining.Feed.newSpells = new
end
function BrokerClassicTraining:FormatSpells(self, hex)
if (self.AddLine ~= nil and BrokerClassicTraining.Feed.newSpells > 0) then
local totalCost = 0
local EscapeColour = '|cFF'..hex
local title = string.format("%sSpells", '|cFFFFFFFF')
self:AddLine(title)
for _,spell in pairs(BrokerClassicTraining.Feed.spells) do
-- self:AddLine(spell.level .. ' ' .. spell.name)
local cost = ''
if (spell.cost == 'quest') then
cost = 'Quest'
elseif (spell.cost == 'free' or spell.cost == nil) then
cost = 'free'
elseif (spell.cost == 'unknown') then
-- if you find one, please raise a PR
else
totalCost = totalCost + spell.cost
cost = GetCoinTextureString(spell.cost)
end
if (spell.rank == nil) then
BrokerClassicTraining:Dump('spell without rank', spell)
return
end
local spellOutput = ''
if spell.rank == 0 then
spellOutput = string.format('%d %s', spell.level, spell.name)
else
spellOutput = string.format('%d %s %s', spell.level, spell.name, 'Rank ' .. spell.rank)
end
self:AddDoubleLine(spellOutput, cost)
if (spell.cost == 'quest' and spell.quest_name ~= nil) then
self:AddLine('- ' ..spell.quest_name)
end
end
-- Add total
self:AddDoubleLine('|cFF808080Total: ', GetCoinTextureString(totalCost))
end
end
-----------------
-- Spell Books --
-----------------
function BrokerClassicTraining:FilterSpellBooks(books, level)
-- new table to hold learnable spells
local booksLearnable = {}
local new = 0
level = level or UnitLevel("player")
BrokerClassicTraining.Feed.newBooks = 0
for _,book in pairs(books) do
if book.spell_id ~= nil and book.spell_id ~= 0 then -- Abort if spell id is zero or nil
if (book.level <= level) then
local isKnown = IsSpellKnown(book.spell_id)
if (isKnown == false) then
BrokerClassicTraining.Feed.newBooks = BrokerClassicTraining.Feed.newBooks + 1
table.insert(booksLearnable, book)
new = new + 1
end
end
end
end
BrokerClassicTraining.Feed.books = booksLearnable
BrokerClassicTraining.Feed.newBooks = new
end
function BrokerClassicTraining:FormatSpellBooks(self, hex)
local EscapeColour = '|cFF'..hex
if (self.AddLine ~= nil and BrokerClassicTraining.Feed.newBooks > 0) then
if (self.AddLine ~= nil) then
self:AddLine(EscapeColour..'Class Books')
end
for _,book in pairs(BrokerClassicTraining.Feed.books) do
self:AddDoubleLine(book.name, book.source)
-- Add drop source
local difficulty = ''
local leftText = ''
local rightText = ''
if (book.source == 'drop') then
if book.source_drop == 'dungeon' then
difficulty = '|cFFF0F000'
leftText = string.format('%s - Dungeon', difficulty)
rightText = string.format('%s%s', difficulty, book.source_drop_dungeon)
self:AddDoubleLine(leftText, rightText)
elseif book.source_drop == 'raid' then
difficulty = '|cFFF08000'
leftText = string.format('%s - Raid', difficulty)
rightText = string.format('%s%s', difficulty, book.source_drop_raid)
self:AddDoubleLine(leftText, rightText)
end
end
end
end
end
--------------------------------------
-- Register the LDB plugin launcher --
--------------------------------------
local dataObj = LDB:NewDataObject( 'Training', {
type = "data source",
text = BrokerClassicTraining:GetLabels(),
icon = "Interface\\AddOns\\Broker_Classic_Training\\Icons\\" .. select(2, UnitClass('player')),
OnClick = function(clickedframe, button)
--
end,
} )
-- TOOLTIP EVENTS
function dataObj:OnTooltipShow()
BrokerClassicTraining:BuildTrainingData(self)
end
function dataObj:OnEnter()
GameTooltip:SetOwner(self, "ANCHOR_NONE")
GameTooltip:SetPoint("TOPLEFT", self, "BOTTOMLEFT")
GameTooltip:ClearLines()
dataObj.OnTooltipShow(GameTooltip)
GameTooltip:Show()
end
function dataObj:OnLeave()
GameTooltip:Hide()
end
-- Game Events
function BrokerClassicTraining:PLAYER_ENTERING_WORLD()
BrokerClassicTraining:BuildTrainingData(self)
dataObj.text = BrokerClassicTraining:UpdateLabel()
end
function BrokerClassicTraining:PLAYER_LEVEL_UP(event, newLevel, ...)
BrokerClassicTraining:BuildTrainingData(self, newLevel)
dataObj.text = BrokerClassicTraining:UpdateLabel()
end
function BrokerClassicTraining:SPELLS_CHANGED(event, ...)
BrokerClassicTraining:BuildTrainingData(self)
dataObj.text = BrokerClassicTraining:UpdateLabel()
end
BrokerClassicTraining:RegisterEvent('PLAYER_ENTERING_WORLD')
BrokerClassicTraining:RegisterEvent('PLAYER_LEVEL_UP')
BrokerClassicTraining:RegisterEvent('SPELLS_CHANGED')
_G['BrokerClassicTraining'] = BrokerClassicTraining