-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddon.lua
More file actions
executable file
·464 lines (393 loc) · 13.2 KB
/
Addon.lua
File metadata and controls
executable file
·464 lines (393 loc) · 13.2 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
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
--[[--------------------------------------------------------------------
Broker_PlayedTime
DataBroker plugin to track played time across all your characters.
Copyright (c) 2010-2016 Phanx <addons@phanx.net>. All rights reserved.
http://www.wowinterface.com/downloads/info16711-BrokerPlayedTime.html
https://mods.curse.com/addons/wow/broker-playedtime
https://github.com/Phanx/Broker_PlayedTime
----------------------------------------------------------------------]]
local ADDON, L = ...
local floor, format, gsub, ipairs, pairs, sort, tinsert, type, wipe = floor, format, gsub, ipairs, pairs, sort, tinsert, type, wipe
local db, myDB
local timePlayed, timeUpdated = 0, 0
local sortedFactions, sortedPlayers, sortedRealms = { "Horde", "Alliance" }, {}, {}
local currentFaction = UnitFactionGroup("player")
local currentPlayer = UnitName("player")
local currentRealm = GetRealmName()
local MAX_LEVEL = MAX_PLAYER_LEVEL_TABLE[GetAccountExpansionLevel()]
local factionIcons = {
Alliance = [[|TInterface\BattlefieldFrame\Battleground-Alliance:16:16:0:0:32:32:4:26:4:27|t ]],
Horde = [[|TInterface\BattlefieldFrame\Battleground-Horde:16:16:0:0:32:32:5:25:5:26|t ]],
}
local classIcons = {}
for class, t in pairs(CLASS_ICON_TCOORDS) do
local offset, left, right, bottom, top = 0.025, unpack(t)
classIcons[class] = format([[|TInterface\Glues\CharacterCreate\UI-CharacterCreate-Classes:14:14:0:0:256:256:%s:%s:%s:%s|t ]], (left + offset) * 256, (right - offset) * 256, (bottom + offset) * 256, (top - offset) * 256)
end
local CLASS_COLORS = { UNKNOWN = "|cffcccccc" }
for k, v in pairs(RAID_CLASS_COLORS) do
CLASS_COLORS[k] = format("|cff%02x%02x%02x", v.r * 255, v.g * 255, v.b * 255)
end
------------------------------------------------------------------------
local FormatTime
do
local DAY_ABBR, HOUR_ABBR, MIN_ABBR = gsub(DAY_ONELETTER_ABBR, "%%d%s*", ""), gsub(HOUR_ONELETTER_ABBR, "%%d%s*", ""), gsub(MINUTE_ONELETTER_ABBR, "%%d%s*", "")
local DHM = format("|cffffffff%s|r|cffffcc00%s|r |cffffffff%s|r|cffffcc00%s|r |cffffffff%s|r|cffffcc00%s|r", "%d", DAY_ABBR, "%02d", HOUR_ABBR, "%02d", MIN_ABBR)
local DH = format("|cffffffff%s|r|cffffcc00%s|r |cffffffff%s|r|cffffcc00%s|r", "%d", DAY_ABBR, "%02d", HOUR_ABBR)
local HM = format("|cffffffff%s|r|cffffcc00%s|r |cffffffff%s|r|cffffcc00%s|r", "%d", HOUR_ABBR, "%02d", MIN_ABBR)
local H = format("|cffffffff%s|r|cffffcc00%s|r", "%d", HOUR_ABBR)
local M = format("|cffffffff%s|r|cffffcc00%s|r", "%d", MIN_ABBR)
function FormatTime(t, noMinutes)
if not t then return end
local d, h, m = floor(t / 86400), floor((t % 86400) / 3600), floor((t % 3600) / 60)
if d > 0 then
return noMinutes and format(DH, d, h) or format(DHM, d, h, m)
elseif h > 0 then
return noMinutes and format(H, h) or format(HM, h, m)
else
return format(M, m)
end
end
end
------------------------------------------------------------------------
local BuildSortedLists
do
local function SortPlayers(a, b)
if a == currentPlayer then
return true
elseif b == currentPlayer then
return false
end
return a < b
end
local function SortRealms(a, b)
if a == currentRealm then
return true
elseif b == currentRealm then
return false
end
return a < b
end
function BuildSortedLists()
wipe(sortedRealms)
for realm in pairs(db) do
if type(db[realm]) == "table" and (realm == currentRealm or not db.onlyCurrentRealm) then
tinsert(sortedRealms, realm)
sortedPlayers[realm] = wipe(sortedPlayers[realm] or {})
for faction in pairs(db[realm]) do
sortedPlayers[realm][faction] = wipe(sortedPlayers[realm][faction] or {})
for name in pairs(db[realm][faction]) do
tinsert(sortedPlayers[realm][faction], name)
end
if realm == currentRealm and faction == currentFaction then
sort(sortedPlayers[realm][faction], SortPlayers)
else
sort(sortedPlayers[realm][faction])
end
end
end
end
sort(sortedRealms, SortRealms)
end
end
------------------------------------------------------------------------
local BrokerPlayedTime = CreateFrame("Frame")
BrokerPlayedTime:SetScript("OnEvent", function(self, event, ...) return self[event] and self[event](self, ...) or self:SaveTimePlayed() end)
BrokerPlayedTime:RegisterEvent("PLAYER_LOGIN")
function BrokerPlayedTime:PLAYER_LOGIN()
local function copyTable(src, dst)
if type(src) ~= "table" then return {} end
if type(dst) ~= "table" then dst = {} end
for k, v in pairs(src) do
if type(v) == "table" then
dst[k] = copyTable(v, dst[k])
elseif type(v) ~= type(dst[k]) then
dst[k] = v
end
end
return dst
end
local defaults = {
classIcons = false,
factionIcons = false,
levels = false,
onlyCurrentRealm = false,
[currentRealm] = {
[currentFaction] = {
[currentPlayer] = {
class = (select(2, UnitClass("player"))),
level = UnitLevel("player"),
timePlayed = 0,
timeUpdated = 0,
},
}
}
}
BrokerPlayedTimeDB = BrokerPlayedTimeDB or {}
db = copyTable(defaults, BrokerPlayedTimeDB)
myDB = db[currentRealm][currentFaction][currentPlayer]
BuildSortedLists()
if CUSTOM_CLASS_COLORS then
local function UpdateClassColors()
for k, v in pairs(CUSTOM_CLASS_COLORS) do
CLASS_COLORS[k] = format("|cff%02x%02x%02x", v.r * 255, v.g * 255, v.b * 255)
end
end
UpdateClassColors()
CUSTOM_CLASS_COLORS:RegisterCallback(UpdateClassColors)
end
self:UnregisterEvent("PLAYER_LOGIN")
self:RegisterEvent("PLAYER_LEVEL_UP")
self:RegisterEvent("PLAYER_LOGOUT")
self:RegisterEvent("PLAYER_REGEN_ENABLED")
self:RegisterEvent("PLAYER_UPDATE_RESTING")
self:RegisterEvent("TIME_PLAYED_MSG")
self:UpdateTimePlayed()
end
local requesting
local o = ChatFrame_DisplayTimePlayed
ChatFrame_DisplayTimePlayed = function(...)
if requesting then
requesting = false
return
end
return o(...)
end
function BrokerPlayedTime:UpdateTimePlayed()
requesting = true
RequestTimePlayed()
end
function BrokerPlayedTime:SaveTimePlayed()
local now = time()
myDB.timePlayed = timePlayed + now - timeUpdated
myDB.timeUpdated = now
self:UpdateText()
self:SetUpdateInterval(timePlayed < 3600)
end
function BrokerPlayedTime:PLAYER_LEVEL_UP(level)
myDB.level = level or UnitLevel("player")
self:SaveTimePlayed()
end
function BrokerPlayedTime:TIME_PLAYED_MSG(t)
timePlayed = t
timeUpdated = time()
self:SaveTimePlayed()
end
------------------------------------------------------------------------
local BrokerPlayedTimeMenu = CreateFrame("Frame", "BrokerPlayedTimeMenu", nil, "UIDropDownMenuTemplate")
BrokerPlayedTimeMenu.displayMode = "MENU"
BrokerPlayedTimeMenu.info = {}
BrokerPlayedTimeMenu.GetClassIcons = function() return db.classIcons end
BrokerPlayedTimeMenu.SetClassIcons = function() db.classIcons = not db.classIcons end
BrokerPlayedTimeMenu.GetFactionIcons = function() return db.factionIcons end
BrokerPlayedTimeMenu.SetFactionIcons = function() db.factionIcons = not db.factionIcons end
BrokerPlayedTimeMenu.GetLevels = function() return db.levels end
BrokerPlayedTimeMenu.SetLevels = function() db.levels = not db.levels end
BrokerPlayedTimeMenu.GetHideOtherRealms = function() return db.onlyCurrentRealm end
BrokerPlayedTimeMenu.SetHideOtherRealms = function()
db.onlyCurrentRealm = not db.onlyCurrentRealm
BuildSortedLists()
end
BrokerPlayedTimeMenu.CloseDropDownMenus = function() CloseDropDownMenus() end
BrokerPlayedTimeMenu.RemoveCharacter = function(button)
local value = button and button.value or UIDROPDOWNMENU_MENU_VALUE
local realm, faction, name = string.split("#", value)
if realm and faction and name and db[realm] and db[realm][faction] and db[realm][faction][name] then
db[realm][faction][name] = nil
local nf = 0
for k in pairs(db[realm][faction]) do
nf = nf + 1
end
if nf == 0 then
db[realm][faction] = nil
end
local nr = 0
for k in pairs(db[realm]) do
nr = nr + 1
end
if nr == 0 then
db[realm] = nil
sortedRealms[realm] = nil
end
BuildSortedLists()
CloseDropDownMenus()
end
end
BrokerPlayedTimeMenu.initialize = function(self, level)
if not level then return end
local info = wipe(self.info)
if level == 1 then
info.text = L["Played Time"]
info.isTitle = 1
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = " "
info.disabled = 1
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = L["Character levels"]
info.checked = self.GetLevels
info.func = self.SetLevels
info.keepShownOnClick = 1
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = L["Class icons"]
info.checked = self.GetClassIcons
info.func = self.SetClassIcons
info.keepShownOnClick = 1
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = L["Faction icons"]
info.keepShownOnClick = 1
info.checked = self.GetFactionIcons
info.func = self.SetFactionIcons
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = " "
info.disabled = 1
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = L["Current realm only"]
info.keepShownOnClick = 1
info.checked = self.GetHideOtherRealms
info.func = self.SetHideOtherRealms
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = " "
info.disabled = 1
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = L["Remove character"]
info.hasArrow = 1
info.keepShownOnClick = 1
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = " "
info.disabled = 1
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
wipe(self.info)
info.text = CLOSE
info.func = self.CloseDropDownMenus
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
elseif level == 2 then
for _, realm in ipairs(sortedRealms) do
info.text = realm
info.value = realm
info.hasArrow = 1
info.keepShownOnClick = 1
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
end
elseif level == 3 then
local factions = 0
for i, faction in ipairs(sortedFactions) do
info.value = nil
info.colorCode = nil
info.func = nil
info.keepShownOnClick = nil
local realm = UIDROPDOWNMENU_MENU_VALUE
local rfp = sortedPlayers[realm][faction]
if rfp then
factions = factions + 1
if factions > 1 then
info.text = " "
info.disabled = 1
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
end
info.disabled = nil
info.text = faction
info.isTitle = 1
UIDropDownMenu_AddButton(info, level)
info.isTitle = nil
for j, name in ipairs(rfp) do
local cdata = db[realm][faction][name]
info.text = name
info.value = format("%s#%s#%s", realm, faction, name)
info.colorCode = CLASS_COLORS[cdata and cdata.class or "UNKNOWN"]
info.disabled = (name == currentPlayer and realm == currentRealm)
info.func = self.RemoveCharacter
UIDropDownMenu_AddButton(info, level)
end
end
end
end
end
------------------------------------------------------------------------
local function OnTooltipShow(tooltip)
local total = 0
tooltip:AddLine(L["Time Played"])
for _, realm in ipairs(sortedRealms) do
tooltip:AddLine(" ")
if #sortedRealms > 1 then
tooltip:AddLine(realm)
end
for _, faction in ipairs(sortedFactions) do
local nfr = sortedPlayers[realm][faction]
if nfr and #nfr > 0 then
for _, name in ipairs(nfr) do
local data = db[realm][faction][name]
if data then
local t
if realm == currentRealm and name == currentPlayer then
t = data.timePlayed + time() - data.timeUpdated
else
t = data.timePlayed
end
if t > 0 then
if db.levels then
tooltip:AddDoubleLine(format("%s%s%s%s (%s)|r", db.factionIcons and factionIcons[faction] or "", db.classIcons and classIcons[data.class] or "", CLASS_COLORS[data.class] or GRAY, name, data.level), FormatTime(t))
else
tooltip:AddDoubleLine(format("%s%s%s%s|r", db.factionIcons and factionIcons[faction] or "", db.classIcons and classIcons[data.class] or "", CLASS_COLORS[data.class] or GRAY, name), FormatTime(t))
end
total = total + t
end
end
end
end
end
end
tooltip:AddLine(" ")
tooltip:AddDoubleLine(L["Total"], FormatTime(total))
end
------------------------------------------------------------------------
BrokerPlayedTime.dataObject = LibStub("LibDataBroker-1.1"):NewDataObject(L["Time Played"], {
type = "data source",
icon = [[Interface\Icons\Spell_Nature_TimeStop]],
text = UNKNOWN,
OnTooltipShow = OnTooltipShow,
OnClick = function(self, button)
if button == "RightButton" then
ToggleDropDownMenu(1, nil, BrokerPlayedTimeMenu, self, 0, 0, nil, nil, 15)
if BrokerPlayedTimeMenu ~= UIDROPDOWNMENU_OPEN_MENU then
self:GetScript("OnEnter")(self)
end
end
end,
})
function BrokerPlayedTime:UpdateText()
local t = myDB.timePlayed + time() - myDB.timeUpdated
self.dataObject.text = FormatTime(t, t > 3600)
end
do
local t
local function UpdateText()
BrokerPlayedTime:UpdateText()
C_Timer.After(t, UpdateText)
end
function BrokerPlayedTime:SetUpdateInterval(fast)
local o = t
t = fast and 30 or 300
if not o then
C_Timer.After(t, UpdateText)
end
end
end
------------------------------------------------------------------------