-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTotalQuestXP.lua
420 lines (329 loc) · 12.2 KB
/
TotalQuestXP.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
-- NAMESPACE: TotalQuestXP
local ADDON_NAME = "TotalQuestXP"
TotalQuestXP = {}
local questCache = {}
local DEFAULT_BAR_WIDTH = 200
local DEFAULT_BAR_HEIGHT = 20
local defaults = {
["cache"] = {},
["unlocked"] = false,
["includeSpeakQuests"] = true,
["showProjectedRewards"] = true,
["barWidth"] = DEFAULT_BAR_WIDTH,
["barHeight"] = DEFAULT_BAR_HEIGHT,
["barLeft"] = GetScreenWidth() / 2 - DEFAULT_BAR_WIDTH/2,
["barTop"] = -20,
}
-- STATE
local TotalQuestXPRoot = CreateFrame("Frame") -- Root frame
local totalQuestXpBar = CreateFrame("StatusBar", ADDON_NAME.."Statusbar", UIParent)
local projectedRewards = {}
local previousProjectionsLeftOffset = 0
local missingXp = 0
local xpBarFull = false
-- REGISTER EVENT LISTENERS
TotalQuestXPRoot:RegisterEvent("ADDON_LOADED")
TotalQuestXPRoot:RegisterEvent("PLAYER_ENTERING_WORLD")
TotalQuestXPRoot:RegisterEvent("QUEST_LOG_UPDATE")
TotalQuestXPRoot:RegisterEvent("PLAYER_XP_UPDATE")
TotalQuestXPRoot:RegisterEvent("PLAYER_LOGIN")
TotalQuestXPRoot:RegisterEvent("QUEST_COMPLETE")
TotalQuestXPRoot:RegisterEvent("QUEST_FINISHED")
TotalQuestXPRoot:RegisterEvent("QUEST_DETAIL")
TotalQuestXPRoot:RegisterEvent("QUEST_ACCEPTED")
TotalQuestXPRoot:RegisterEvent("QUEST_DETAIL")
TotalQuestXPRoot:RegisterEvent("QUEST_ACCEPTED")
TotalQuestXPRoot:RegisterEvent("QUEST_REMOVED")
TotalQuestXPRoot:SetScript("OnEvent", function(self, event, arg1, ...)
TotalQuestXP:onEvent(self, event, arg1, ...)
end);
function TotalQuestXP:onEvent(self, event, arg1, ...)
if event == "ADDON_LOADED" then
if arg1 == ADDON_NAME then
TotalQuestXP:PrintHelp()
end
end
if event == "QUEST_DETAIL" then
TotalQuestXP:OnQuestDetails(arg1, ...)
end
if event == "QUEST_ACCEPTED" then
TotalQuestXP:OnQuestAccepted(arg1, ...)
TotalQuestXP:Update()
end
if event == "QUEST_COMPLETE" then
TotalQuestXP:Update()
end
if event == "QUEST_REMOVED" then
TotalQuestXP:OnQuestRemoved(arg1, ...)
end
if event == "QUEST_LOG_UPDATE" then
TotalQuestXP:Update()
end
if event == "PLAYER_XP_UPDATE" then
TotalQuestXP:Update()
end
if event == "PLAYER_ENTERING_WORLD" then
TotalQuestXP:Init()
TotalQuestXP:Update()
end
end
function TotalQuestXP:Init()
TotalQuestXP:LoadOptions()
TotalQuestXP:Update()
end
function TotalQuestXP:LoadOptions()
TotalQuestXP_Options = TotalQuestXP_Options or AddonUtils:deepcopy(defaults)
for key,value in pairs(defaults) do
if (TotalQuestXP_Options[key] == nil) then
TotalQuestXP_Options[key] = value
end
end
TotalQuestXP_Options.unlocked = false
end
function TotalQuestXP:Update()
-- UPDATE PRIMARY BAR
TotalQuestXP:UpdateTotalQuestXpBar()
-- GET REWARDS
local questRewards = TotalQuestXP:GetQuestRewards()
-- CALCULATE SUM
local xpSum = TotalQuestXP:CalculateXPSum(questRewards)
-- DETERMINE PROJECTED REWARDS
TotalQuestXP:RefreshProjectedRewards(questRewards, xpSum)
-- DETERMINE WHETHER OR NOT ENOUGH XP HAS BEEN ACQUIRED
if (xpSum >= TotalQuestXP:GetRemainingXP()) then
local msg = "Turn in quests to DING!"
totalQuestXpBar:SetValue(xpSum)
totalQuestXpBar.value:SetText(msg)
totalQuestXpBar:SetStatusBarColor(0, 0.80, 0)
else
totalQuestXpBar:SetValue(xpSum)
totalQuestXpBar.value:SetText("Total XP Reward: "..string.format("%.0f", xpSum).."/"..TotalQuestXP:GetRemainingXP())
totalQuestXpBar:SetStatusBarColor(0.80, 0, 0.80)
end
end
function TotalQuestXP:RefreshProjectedRewards(questRewards, xpSum)
previousProjectionsLeftOffset = 0
missingXp = TotalQuestXP:GetRemainingXP() - xpSum
for key,statusbar in pairs(projectedRewards) do
projectedRewards[key]:Hide()
projectedRewards[key]:SetUserPlaced(false)
projectedRewards[key] = nil
end
if (TotalQuestXP_Options.showProjectedRewards and (not TotalQuestXP_Options.unlocked)) then
for key,quest in pairs(questRewards) do
if (quest.completed) then
if (not projectedRewards[quest.title] == nil) then
projectedRewards[quest.title]:Hide()
end
else
if (projectedRewards[quest.title] == nil) then
projectedRewards[quest.title] = TotalQuestXP:CreateStatusbarForProjectedQuest(quest, xpSum)
end
end
end
end
end
function TotalQuestXP:UpdateTotalQuestXpBar()
-- POSITION, SIZE
totalQuestXpBar:ClearAllPoints()
totalQuestXpBar:SetWidth(TotalQuestXP_Options.barWidth)
totalQuestXpBar:SetHeight(TotalQuestXP_Options.barHeight)
totalQuestXpBar:SetPoint("TOPLEFT", TotalQuestXP_Options.barLeft, TotalQuestXP_Options.barTop)
totalQuestXpBar:SetFrameLevel(1)
-- DRAGGING
totalQuestXpBar:SetScript("OnMouseDown", function(self, button) TotalQuestXP:onMouseDown(button); end)
totalQuestXpBar:SetScript("OnMouseUp", function(self, button) TotalQuestXP:onMouseUp(button); end)
totalQuestXpBar:SetMovable(true)
totalQuestXpBar:SetResizable(true)
totalQuestXpBar:EnableMouse(TotalQuestXP_Options.unlocked)
totalQuestXpBar:SetClampedToScreen(true)
-- VALUE
totalQuestXpBar:SetMinMaxValues(0, TotalQuestXP:GetRequiredXP())
-- FOREGROUND
totalQuestXpBar:SetStatusBarTexture("Interface\\TARGETINGFRAME\\UI-StatusBar")
totalQuestXpBar:GetStatusBarTexture():SetHorizTile(false)
totalQuestXpBar:GetStatusBarTexture():SetVertTile(false)
totalQuestXpBar:SetStatusBarColor(0.80, 0, 0.80)
-- BACKGROUND
if (totalQuestXpBar.bg == nil) then
totalQuestXpBar.bg = totalQuestXpBar:CreateTexture(nil, "BACKGROUND")
totalQuestXpBar.bg:SetTexture("Interface\\TARGETINGFRAME\\UI-StatusBar")
totalQuestXpBar.bg:SetAllPoints(true)
totalQuestXpBar.bg:SetVertexColor(0.50, 0.50, 0.50)
totalQuestXpBar.bg:SetAlpha(0.5)
end
-- TEXT
if (totalQuestXpBar.value == nil) then
totalQuestXpBar.value = totalQuestXpBar:CreateFontString(nil, "OVERLAY")
totalQuestXpBar.value:SetPoint("LEFT", totalQuestXpBar, "LEFT", 4, 0)
totalQuestXpBar.value:SetFont("Fonts\\FRIZQT__.TTF", 16, "OUTLINE")
totalQuestXpBar.value:SetJustifyH("LEFT")
totalQuestXpBar.value:SetShadowOffset(1, -1)
totalQuestXpBar.value:SetTextColor(0.95, 0.95, 0.95)
end
TotalQuestXP:UpdateFont()
-- SET REMAINING XP
totalQuestXpBar:SetMinMaxValues(0, TotalQuestXP:GetRemainingXP())
totalQuestXpBar:Show()
end
function TotalQuestXP:CreateStatusbarForProjectedQuest(quest, xpSum)
if (missingXp <= 0) then
return nil
end
-- MAPPING
local totalWidth = TotalQuestXP_Options.barWidth
local maxXp = TotalQuestXP:GetRemainingXP()
local completedWidth = AddonUtils:map(xpSum, 0, maxXp, 0, totalWidth)
local width = AddonUtils:map(quest.reward, 0, maxXp, 0, totalWidth)
local left = completedWidth + previousProjectionsLeftOffset
local isLast = missingXp - quest.reward <= 0
local statusbar = CreateFrame("StatusBar", ADDON_NAME..quest.title, totalQuestXpBar)
statusbar:SetMovable(true)
statusbar:SetResizable(true)
statusbar:SetUserPlaced(false)
statusbar:SetClampedToScreen(true)
statusbar:SetFrameLevel(1)
statusbar:ClearAllPoints()
statusbar:SetHeight(TotalQuestXP_Options.barHeight)
if (isLast) then
width = AddonUtils:map(missingXp, 0, maxXp, 0, totalWidth)
statusbar:SetWidth(width)
statusbar:SetPoint("TOPLEFT", totalQuestXpBar, left, 0)
else
statusbar:SetWidth(width)
statusbar:SetPoint("TOPLEFT", totalQuestXpBar, left, 0)
end
-- VALUE
statusbar:SetMinMaxValues(0, 1)
statusbar:SetValue(1)
-- FOREGROUND
statusbar:SetStatusBarTexture("Interface\\TARGETINGFRAME\\UI-StatusBar")
statusbar:GetStatusBarTexture():SetHorizTile(false)
statusbar:GetStatusBarTexture():SetVertTile(false)
statusbar:SetStatusBarColor(0, 0, 1, 0.3)
statusbar:Show()
previousProjectionsLeftOffset = previousProjectionsLeftOffset + width
missingXp = missingXp - quest.reward
return statusbar
end
-- GETTERS
function TotalQuestXP:GetQuestRewards()
local oldQuest = GetQuestLogSelection()
local questCount = GetNumQuestLogEntries()
local rewards = {}
for i = 1,questCount do
SelectQuestLogEntry(i)
local title, level, suggestedGroup, isHeader, isCollapsed, isComplete, frequency, questID, startEvent, displayQuestID, isOnMap, hasLocalPOI, isTask, isStory = GetQuestLogTitle(i);
local questDescription, questObjectives = GetQuestLogQuestText();
if (not isHeader) and questID ~= nil and questID > 0 then
local speakQuest = TotalQuestXP_Options.includeSpeakQuests and string.match(string.lower(questObjectives), "speak with")
local cachedQuest = TotalQuestXP_Options.cache[questID]
if cachedQuest then
local quest = {
title = title,
reward = cachedQuest.rewardXP,
completed = isComplete == 1 or speakQuest
}
table.insert(rewards, quest)
end
end
end
SelectQuestLogEntry(oldQuest)
return rewards
end
function TotalQuestXP:CalculateXPSum(questRewards)
local xpSum = 0
for key,quest in pairs(questRewards) do
if (quest.completed) then
xpSum = xpSum + quest.reward
end
end
return xpSum
end
function TotalQuestXP:GetCurrentXP()
return UnitXP("player")
end
function TotalQuestXP:GetRequiredXP()
return UnitXPMax("player")
end
function TotalQuestXP:GetRemainingXP()
return TotalQuestXP:GetRequiredXP() - TotalQuestXP:GetCurrentXP()
end
-- UI HELPERS
function TotalQuestXP:UpdateFont()
local height = totalQuestXpBar:GetHeight()
local remainder = AddonUtils:modulus(height, 2)
local px = height - remainder - 10
px = math.min(px, 12)
px = math.max(px, 1)
if (px < 8) then
totalQuestXpBar.value:SetTextColor(0, 0, 0, 0)
else
totalQuestXpBar.value:SetTextColor(0.95, 0.95, 0.95)
end
totalQuestXpBar.value:SetFont("Fonts\\FRIZQT__.TTF", px, "OUTLINE")
end
-- DRAG LISTENERS
function TotalQuestXP:onMouseDown(button)
if button == "LeftButton" then
totalQuestXpBar:StartMoving();
elseif button == "RightButton" then
totalQuestXpBar:StartSizing("BOTTOMRIGHT");
totalQuestXpBar.resizing = 1
end
end
function TotalQuestXP:onMouseUp()
TotalQuestXP:UpdateFont()
totalQuestXpBar:StopMovingOrSizing();
TotalQuestXP_Options.barLeft = totalQuestXpBar:GetLeft()
TotalQuestXP_Options.barTop = -1 * (GetScreenHeight() - totalQuestXpBar:GetTop())
TotalQuestXP_Options.barWidth = totalQuestXpBar:GetWidth()
TotalQuestXP_Options.barHeight = totalQuestXpBar:GetHeight()
TotalQuestXP:Update()
TotalQuestXP.OptionsPanelFrame:UpdateOptionValues()
end
-- COMMANDS
function TotalQuestXP:unlock()
TotalQuestXP_Options.unlocked = true
totalQuestXpBar:EnableMouse(true)
for key,statusbar in pairs(projectedRewards) do
statusbar:Hide()
end
end
function TotalQuestXP:lock()
TotalQuestXP_Options.unlocked = false
totalQuestXpBar:EnableMouse(false)
totalQuestXpBar:StopMovingOrSizing();
totalQuestXpBar.resizing = nil
TotalQuestXP:Update()
end
function TotalQuestXP:reset()
totalQuestXpBar:SetUserPlaced(false)
TotalQuestXP_Options = AddonUtils:deepcopy(defaults)
TotalQuestXP:Init()
end
function TotalQuestXP:PrintHelp()
local colorHex = "9575cd"
print("|cff"..colorHex.."TotalQuestXP loaded - /tqxp")
end
-- DB HANDLING
function TotalQuestXP:OnQuestDetails()
local questID = GetQuestID()
if questID > 0 then
questCache[questID] = {
rewardXP = GetRewardXP()
}
end
end
function TotalQuestXP:OnQuestAccepted(questIndex, questID)
if questCache[questID] then
TotalQuestXP_Options.cache[questID] = questCache[questID]
else
TotalQuestXP_Options.cache[questID] = {
rewardXP = GetRewardXP()
}
end
end
function TotalQuestXP:OnQuestRemoved(questID)
TotalQuestXP_Options.cache[questID] = nil
end