-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelTimingsUI.lua
More file actions
427 lines (363 loc) · 12.7 KB
/
LevelTimingsUI.lua
File metadata and controls
427 lines (363 loc) · 12.7 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
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
-- Copyright © 2020 vzx8. All rights reserved.
-- Licensed under GPLv3 (see license.txt).
local LevelTimingsUI = {
selectedGuid = UnitGUID("player"),
compareGuid = "",
displayRows = {},
fromLevel = 0
}
SLASH_LevelTimings1 = "/leveltimings"
SlashCmdList["LevelTimings"] = function(msg)
LevelTimingsUI:Show()
end
StaticPopupDialogs["LEVELTIMINGS_DELETE_CONFIRMATION"] = {
text = "",
button1 = "Yes",
button2 = "No",
OnAccept = function(self, guid)
LevelTimingsUI:DeleteFromDB(guid)
end,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3
}
function LevelTimingsUI:InitiateDelete()
local myGuid = UnitGUID("player")
local guid = self.selectedGuid
if myGuid == guid then
-- Prevent deletion of this/last character
return
end
if not LevelTimingsDB["players"][guid] then
-- Should never happen but eh you never know
return
end
local item = LevelTimingsDB["players"][guid]
local name = LevelTimingsUI:ColoredName(item)
local realm = GetFactionColor(item.faction):WrapTextInColorCode(item.realm)
StaticPopupDialogs["LEVELTIMINGS_DELETE_CONFIRMATION"].text = "Are you sure you want to permanently delete all level timings for:\n\n"
.. name .. " (" .. realm .. ")" .. "\n\nWARNING: this is irreversible!"
local popup = StaticPopup_Show("LEVELTIMINGS_DELETE_CONFIRMATION")
if popup then
popup.data = guid
end
end
function LevelTimingsUI:DeleteFromDB(guid)
LevelTimingsDB["players"][guid] = nil
LevelTimingsUI:SelectCharacter(UnitGUID("player"), true)
if guid == LevelTimingsUI.compareGuid then
LevelTimingsUI:SelectCompare("", true)
end
LevelTimingsUI:RefreshList()
end
function LevelTimingsUI_ToggleShown()
LevelTimingsUI_Frame:SetShown(not LevelTimingsUI_Frame:IsShown())
end
function LevelTimingsUI:Show()
LevelTimingsUI_Frame:Show()
end
function LevelTimingsUI:SelectCharacter(guid, skipRefresh)
LevelTimingsUI.selectedGuid = guid
LevelTimingsUI:SetSelectedCharacterInDropDown()
if not skipRefresh then
LevelTimingsUI:RefreshList()
end
end
function LevelTimingsUI:SelectCompare(guid, skipRefresh)
LevelTimingsUI.compareGuid = guid
LevelTimingsUI:SetSelectedCompareInDropDown()
if not skipRefresh then
LevelTimingsUI:RefreshList()
end
end
function LevelTimingsUI:SwapCharacters()
local previousCompareGuid = LevelTimingsUI.compareGuid
LevelTimingsUI.compareGuid = LevelTimingsUI.selectedGuid
LevelTimingsUI.selectedGuid = previousCompareGuid
LevelTimingsUI:SetSelectedCharacterInDropDown()
LevelTimingsUI:SetSelectedCompareInDropDown()
LevelTimingsUI:RefreshList()
end
function LevelTimingsUI:SetSelectedCharacterInDropDown()
UIDropDownMenu_SetSelectedValue(LevelTimingsUI_CharactersDropDown, LevelTimingsUI.selectedGuid)
LevelTimingsUI_DeleteCharacterButton:SetEnabled(LevelTimingsUI.selectedGuid ~= UnitGUID("player"))
end
function LevelTimingsUI:SetSelectedCompareInDropDown()
UIDropDownMenu_SetSelectedValue(LevelTimingsUI_CompareDropDown, LevelTimingsUI.compareGuid)
LevelTimingsUI_SwapCharacterButton:SetEnabled(LevelTimingsUI.compareGuid ~= "")
end
function LevelTimingsUI_SetFromLevel(self, level)
LevelTimingsUI_FromLevelText:SetText(level)
LevelTimingsUI.fromLevel = level
LevelTimingsUI:RefreshList()
end
function LevelTimingsUI_RefreshList()
LevelTimingsUI:RefreshList()
end
function LevelTimingsUI:RefreshList()
local guid = LevelTimingsUI.selectedGuid
local entry = LevelTimingsDB["players"][guid]
local compareEntry = nil
local titleText = "Level Timings for " .. LevelTimingsUI:ColoredName(entry)
if LevelTimingsUI.compareGuid ~= "" then
compareEntry = LevelTimingsDB["players"][LevelTimingsUI.compareGuid]
titleText = titleText .. " vs " .. LevelTimingsUI:ColoredName(compareEntry)
LevelTimingsUI_ListFrameColumnHeaderPlayedTotal:SetText(LevelTimingsUI:ColoredName(entry))
LevelTimingsUI_ListFrameColumnHeaderPlayedLevel:SetText(LevelTimingsUI:ColoredName(compareEntry))
LevelTimingsUI_ListFrameColumnHeaderZoneOrDelta:SetText("Difference")
else
LevelTimingsUI_ListFrameColumnHeaderPlayedTotal:SetText("Total played")
LevelTimingsUI_ListFrameColumnHeaderPlayedLevel:SetText("Played on level")
LevelTimingsUI_ListFrameColumnHeaderZoneOrDelta:SetText(ZONE)
end
LevelTimingsUI_FrameTitleText:SetText(titleText)
local sortedRows = LevelTimingsUI:BuildSortedLevelRows(entry, compareEntry, LevelTimingsUI.fromLevel)
LevelTimingsUI.displayRows = LevelTimingsUI:BuildDisplayRows(sortedRows)
HybridScrollFrame_SetOffset(LevelTimingsUI_ScrollFrame, 0)
LevelTimingsUI_ScrollFrame.scrollBar:SetValue(0)
LevelTimingsUI:UpdateList()
end
function LevelTimingsUI:BuildSortedLevelRows(entry, compareEntry, fromLevel)
local timings = entry.timings
local compareTimings = {}
if compareEntry then
compareTimings = compareEntry.timings
end
local sortedTimings = {}
local n = 1
for _, v in ipairs(timings) do
if v.level >= fromLevel then
sortedTimings[n] = v
n = n + 1
end
end
table.sort(sortedTimings, function(l, r)
return l.played < r.played
end)
local levelRows = {}
for i, entry in ipairs(sortedTimings) do
local level = entry.level
local compareEntry = nil
for _, ce in ipairs(compareTimings) do
if ce.level == level and LevelTimingsUI:IsSimilarGameVersion(entry.tocVersion, ce.tocVersion) then
compareEntry = ce
break
end
end
levelRows[i] = {
level = level,
timings = entry,
compareTimings = compareEntry
}
end
return levelRows
end
function LevelTimingsUI:IsSimilarGameVersion(v1, v2)
-- If either entry is nil, assume they are in BfA (toc version 80300)
v1 = v1 or 80300
v2 = v2 or 80300
-- Shadowlands is version 9 (toc version 9xxxx) and had a level squish
-- Entries are similar if they are both either pre-Shadowlands or post-Shadowlands
return (v1 < 90000) == (v2 < 90000)
end
function LevelTimingsUI:BuildDisplayRows(levelRows)
local displayRows = {}
local playedOffset = 0
local compareOffset = 0
for index in ipairs(levelRows) do
local row = levelRows[index]
local level = row.level
local timings = row.timings
local compareTimings = row.compareTimings
if level == LevelTimingsUI.fromLevel then
playedOffset = timings.played
if compareTimings then
compareOffset = compareTimings.played
end
end
local played = timings.played - playedOffset
local entry = {}
displayRows[index] = entry
entry.level = row.level
entry.timestamp = date("%Y-%m-%d %H:%M:%S", timings.timestamp)
entry.played = LevelTimingsUI:FormatPlayed(played)
if LevelTimingsUI.compareGuid ~= "" then
local deltaText = ""
entry.playedLevel = "|cFF808080-|r" -- grey
if compareTimings then
local comparePlayed = compareTimings.played - compareOffset
local delta = comparePlayed - played
entry.playedLevel = LevelTimingsUI:FormatPlayed(comparePlayed)
deltaText = (delta >= 0 and "|cFFFF0000+" or "|cFF00FF00-") .. LevelTimingsUI:FormatPlayed(math.abs(delta)) .. "|r"
end
entry.zoneOrDelta = deltaText
else
entry.playedLevel = "|cFF808080?|r" -- grey
if levelRows[index+1] ~= nil then
local nextPlayed = levelRows[index+1].timings.played
entry.playedLevel = LevelTimingsUI:FormatPlayed(nextPlayed - timings.played)
end
local zone, subzone = timings.zone, timings.subzone
local zoneText = ""
if zone then
zoneText = zone
if subzone and subzone ~= "" then
zoneText = zoneText .. " (" .. subzone .. ")"
end
else
-- grey
zoneText = "|cFF808080" .. (timings.initial and "(Initial entry)" or "(Unknown)") .. "|r"
end
entry.zoneOrDelta = zoneText
end
end
return displayRows
end
function LevelTimingsUI:FormatPlayed(played)
if played == 0 then
return "0s"
end
local remaining = played
local times = {}
times[1] = {amount = math.floor(remaining / 86400), suffix = "d"}
remaining = math.fmod(remaining, 86400)
times[2] = {amount = math.floor(remaining / 3600), suffix = "h"}
remaining = math.fmod(remaining, 3600)
times[3] = {amount = math.floor(remaining / 60), suffix = "m"}
times[4] = {amount = math.fmod(remaining, 60), suffix = "s"}
local display = false
local result = ""
for _, v in ipairs(times) do
if v.amount > 0 then
display = true
end
if display then
if result ~= "" then
result = result .. " "
end
result = result .. v.amount .. v.suffix
end
end
return result
end
function LevelTimingsUI:UpdateList()
local displayRows = LevelTimingsUI.displayRows
local scrollFrame = LevelTimingsUI_ScrollFrame
local offset = HybridScrollFrame_GetOffset(scrollFrame)
local buttons = scrollFrame.buttons
local buttonCount = #buttons
local rowCount = #displayRows
local usedHeight = 0
local buttonHeight = 16
for i = 1, buttonCount do
local button = buttons[i]
local index = offset + i
if index <= rowCount then
local row = displayRows[index]
button.Level:SetText(row.level)
button.Timestamp:SetText(row.timestamp)
button.PlayedTotal:SetText(row.played)
button.PlayedLevel:SetText(row.playedLevel)
button.ZoneOrDelta:SetText(row.zoneOrDelta)
button.index = index
button:Show()
usedHeight = usedHeight + buttonHeight
else
button.index = nil
button:Hide()
end
end
HybridScrollFrame_Update(scrollFrame, rowCount * buttonHeight, usedHeight)
end
function LevelTimingsUI:CharactersDropDown_Initialize()
LevelTimingsUI:PopulateDropDown(function(self) LevelTimingsUI:SelectCharacter(self.value) end)
end
function LevelTimingsUI:CompareDropDown_Initialize()
local onClickFunc = function(self) LevelTimingsUI:SelectCompare(self.value) end
local info = UIDropDownMenu_CreateInfo()
info.text = "-"
info.value = ""
info.func = onClickFunc
info.checked = nil
UIDropDownMenu_AddButton(info)
LevelTimingsUI:PopulateDropDown(onClickFunc)
end
function LevelTimingsUI:PopulateDropDown(onItemClick)
local sortArray = {}
local n = 1
for guid, entry in pairs(LevelTimingsDB["players"]) do
sortArray[n] = {guid = guid, name = entry.name, realm = entry.realm, class = entry.class, faction = entry.faction}
n = n + 1
end
table.sort(sortArray, function(l, r)
if l.realm == r.realm then
return l.name < r.name
else
return l.realm < r.realm
end
end)
local info = UIDropDownMenu_CreateInfo()
for _, item in ipairs(sortArray) do
local name = LevelTimingsUI:ColoredName(item)
local realm = GetFactionColor(item.faction):WrapTextInColorCode(item.realm)
info.text = name .. " (" .. realm .. ")"
info.value = item.guid
info.func = onItemClick
info.checked = nil
UIDropDownMenu_AddButton(info)
end
end
function LevelTimingsUI:ColoredName(item)
return RAID_CLASS_COLORS[item.class]:WrapTextInColorCode(item.name)
end
function LevelTimingsUI_OnLoad(self)
self:RegisterForDrag("LeftButton")
SetPortraitToTexture(LevelTimingsUI_FrameIcon, "Interface\\Icons\\INV_7XP_Inscription_TalentTome01")
LevelTimingsUI_FrameTitleText:SetText("Level Timings")
LevelTimingsUI_ScrollFrame.update = LevelTimingsUI.UpdateList
HybridScrollFrame_CreateButtons(LevelTimingsUI_ScrollFrame, "LevelTimingsUI_ButtonTemplate")
if false then
-- TODO: Debug stuff
self:RegisterEvent("ADDON_LOADED")
self:SetScript("OnEvent", function(self, msg, addonName)
if addonName ~= "LevelTimings" then
return
end
self:UnregisterEvent("ADDON_LOADED")
LevelTimingsUI:Show()
end)
else
-- This will make the frame close when pressing the Escape button
tinsert(UISpecialFrames, self:GetName())
end
end
function LevelTimingsUI_OnShow(self)
LevelTimingsUI:RefreshList()
end
function LevelTimingsUI_CharactersDropDown_OnLoad(self)
UIDropDownMenu_SetWidth(self, 240)
UIDropDownMenu_JustifyText(self, "LEFT")
end
function LevelTimingsUI_CharactersDropDown_OnShow(self)
UIDropDownMenu_Initialize(self, LevelTimingsUI.CharactersDropDown_Initialize)
LevelTimingsUI:SetSelectedCharacterInDropDown()
end
function LevelTimingsUI_CompareDropDown_OnShow(self)
UIDropDownMenu_Initialize(self, LevelTimingsUI.CompareDropDown_Initialize)
LevelTimingsUI:SetSelectedCompareInDropDown()
end
function LevelTimingsUI_DeleteCharacterButton_Click(self)
LevelTimingsUI:InitiateDelete()
end
function LevelTimingsUI_SwapCharacterButton_Click(self)
LevelTimingsUI:SwapCharacters()
end
function LevelTimingsUI_FromLevelSlider_OnLoad(self)
self:OnBackdropLoaded()
self:SetObeyStepOnDrag(true)
local name = self:GetName()
_G[name .. "Low"]:Hide()
_G[name .. "High"]:Hide()
-- Registering it here prevents OnValueChanged from triggering when the default value is set
self:SetScript("OnValueChanged", LevelTimingsUI_SetFromLevel)
end