-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelTimings.lua
More file actions
200 lines (170 loc) · 6.45 KB
/
LevelTimings.lua
File metadata and controls
200 lines (170 loc) · 6.45 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
-- Copyright © 2020 vzx8. All rights reserved.
-- Licensed under GPLv3 (see license.txt).
local LEVEL_TIMINGS_CURRENT_DB_VERSION = 2
local addonName = ...
local LevelTimings = {}
-- timePlayedRequested is initialized to true so that if another addon requests time played before us, we handle that
local timePlayedRequested = true
-- levelUps is an array because the player can get multiple level ups at one time and we want to record them all
local levelUps = {}
local eventsFrame = CreateFrame("Frame")
eventsFrame:RegisterEvent("ADDON_LOADED")
eventsFrame:RegisterEvent("PLAYER_LEVEL_UP")
eventsFrame:RegisterEvent("TIME_PLAYED_MSG")
local function handleEvents(self, event, ...)
if event == "ADDON_LOADED" then
self:UnregisterEvent("ADDON_LOADED")
LevelTimings:handleLoaded(...)
elseif event == "PLAYER_LEVEL_UP" then
LevelTimings:handleLevelUp(...)
elseif event == "TIME_PLAYED_MSG" then
LevelTimings:handleTimePlayed(...)
end
end
eventsFrame:SetScript("OnEvent", handleEvents)
function LevelTimings:handleLoaded(...)
local loadedAddonName = ...
if loadedAddonName ~= addonName then
return
end
local guid = LevelTimings:playerGuid()
if LevelTimingsDB ~= nil then
LevelTimings:UpgradeDB()
local playerEntry = LevelTimingsDB["players"][guid]
if playerEntry ~= nil then
-- The DB has already been initialized for this character
timePlayedRequested = false
-- Update base character data in case of some changes (eg. name or faction change)
playerEntry.name = UnitFullName("player")
playerEntry.realm = GetRealmName()
playerEntry.class = select(2, UnitClass("player"))
playerEntry.faction = UnitFactionGroup("player")
return
end
end
-- if another addon requested time played first, timePlayedRequested will be false here
if timePlayedRequested then
RequestTimePlayed()
end
end
function LevelTimings:handleLevelUp(...)
local level = ...
timePlayedRequested = true
local isFirstLevelUp = #levelUps == 0
table.insert(levelUps, level)
if isFirstLevelUp then
-- When there are multiple level ups at once, only request time played the first time (when the array was still empty)
-- Assumption here is that all level up events are fired and handled before the time played message is handled
RequestTimePlayed()
end
end
function LevelTimings:handleTimePlayed(...)
local totalTimePlayedSec = ...
if not timePlayedRequested then
return
end
timePlayedRequested = false
if #levelUps == 0 then
-- Message is fired during ADDON_LOADED
LevelTimings:handleTimePlayedLoaded(totalTimePlayedSec)
return
end
-- Message was fired during PLAYER_LEVEL_UP
LevelTimings:handleTimePlayedLevelUp(totalTimePlayedSec, levelUps)
levelUps = {}
end
function LevelTimings:handleTimePlayedLoaded(totalTimePlayedSec)
if LevelTimingsDB == nil then
-- If the DB does not exist at all yet, initialize it to an empty table first
LevelTimingsDB = {version = LEVEL_TIMINGS_CURRENT_DB_VERSION, players = {}}
end
local guid = LevelTimings:playerGuid()
if LevelTimingsDB["players"][guid] ~= nil then
return
end
-- No entry for the player, initialize entry
local name = UnitFullName("player")
-- GetRealmName() returns with spaces (eg. "Aerie Peak"), while UnitFullName() returns without (eg. "AeriePeak")
local realm = GetRealmName()
local currentLevel = UnitLevel("player")
local class = select(2, UnitClass("player"))
local faction = UnitFactionGroup("player")
local timestamp = time()
local gameVersion, _, _, tocVersion = GetBuildInfo()
LevelTimingsDB["players"][guid] = {
name = name,
realm = realm,
class = class,
faction = faction,
timings = {{
initial = true,
level = currentLevel,
timestamp = timestamp,
played = totalTimePlayedSec,
gameVersion = gameVersion,
tocVersion = tocVersion
}}
}
end
function LevelTimings:handleTimePlayedLevelUp(totalTimePlayedSec, newLevels)
local timestamp = time()
local guid = LevelTimings:playerGuid()
local zone = GetRealZoneText()
local subZone = GetSubZoneText()
for _, newLevel in ipairs(newLevels) do
-- Record the data into the database
local timings = LevelTimingsDB["players"][guid]["timings"]
local gameVersion, _, _, tocVersion = GetBuildInfo()
table.insert(timings, {
level = newLevel,
timestamp = timestamp,
played = totalTimePlayedSec,
zone = zone,
subzone = subZone,
gameVersion = gameVersion,
tocVersion = tocVersion
})
-- Show a nice message to the player
local prevEntry = timings[#timings - 1]
if prevEntry ~= nil then
local secondsNeededToReachThislevel = totalTimePlayedSec - prevEntry.played
end
-- Refresh the UI in case it is open at this time
LevelTimingsUI_RefreshList()
end
end
-- Converts the DB of an old format which was a table of level to entry, to a flat array
function LevelTimings:UpgradeDB()
if LevelTimingsDB["version"] == LEVEL_TIMINGS_CURRENT_DB_VERSION then
return
end
-- The new DB format now has a version number and player timings are in the 'players' entry rather than the top level
local newDb = {version = LEVEL_TIMINGS_CURRENT_DB_VERSION, players = {}}
local newDbPlayers = newDb["players"]
for guid, playerData in pairs(LevelTimingsDB) do
-- Timings are now a flat array, not indexed by level; the level is now inside the entry
local newTimings = {}
for level, entry in pairs(playerData["timings"]) do
entry["level"] = level
table.insert(newTimings, entry)
end
playerData["timings"] = newTimings
newDbPlayers[guid] = playerData
end
LevelTimingsDB = newDb
end
function LevelTimings:playerGuid()
return UnitGUID("player")
end
function LevelTimings_GetConfig(key)
if not LevelTimingsConfig then
LevelTimingsConfig = {}
end
return LevelTimingsConfig[key]
end
function LevelTimings_SetConfig(key, value)
if not LevelTimingsConfig then
LevelTimingsConfig = {}
end
LevelTimingsConfig[key] = value
end