-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.lua
616 lines (503 loc) · 16.8 KB
/
Core.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
local NAME, S = ...
local RSD = ReadySetDing
local ACR = LibStub("AceConfigRegistry-3.0")
local ACD = LibStub("AceConfigDialog-3.0")
local L = S.L
local options = S.options
local player = S.player
local cd = S.cd
local args = S.args
local profile, member, char
local time = time
S.lastPlayed = time()
S.totalTPM, S.curTPM = 0, 0
local curTPM2, totalTPM2
local pairs, ipairs = pairs, ipairs
local format, gsub = format, gsub
local IsInGuild = IsInGuild
---------------------------
--- Ace3 Initialization ---
---------------------------
local appKey = {
"ReadySetDing_Main",
"ReadySetDing_Advanced",
}
local appValue = {
ReadySetDing_Main = options.args.main,
ReadySetDing_Advanced = options.args.advanced,
ReadySetDing_Data = options.args.data,
}
function RSD:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("ReadySetDingDB", S.defaults, true)
self.db.RegisterCallback(self, "OnProfileChanged", "RefreshDB")
self.db.RegisterCallback(self, "OnProfileCopied", "RefreshDB")
self.db.RegisterCallback(self, "OnProfileReset", "RefreshDB")
self:RefreshDB()
ACR:RegisterOptionsTable("ReadySetDing_Parent", options)
ACD:AddToBlizOptions("ReadySetDing_Parent", NAME)
ACD:SetDefaultSize("ReadySetDing_Parent", 600, 620)
-- setup profiles now, self reminder: requires db to be already defined
options.args.profiles = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
local profiles = options.args.profiles
profiles.order = 6
profiles.name = "|TInterface\\Icons\\INV_Misc_Note_01:16:16:-2:-1"..S.crop.."|t "..profiles.name
tinsert(appKey, "ReadySetDing_Profiles")
appValue.ReadySetDing_Profiles = profiles
for _, v in ipairs(appKey) do
ACR:RegisterOptionsTable(v, appValue[v])
ACD:AddToBlizOptions(v, appValue[v].name, NAME)
end
----------------------
--- Slash Commands ---
----------------------
for _, v in ipairs({"rsd", "readyset", "readysetding"}) do
self:RegisterChatCommand(v, "SlashCommand")
end
-----------------------------
--- Custom SavedVariables ---
-----------------------------
char.LevelTimeList = char.LevelTimeList or {}
char.TotalTimeList = char.TotalTimeList or {}
char.DateStampList = char.DateStampList or {}
char.UnixTimeList = char.UnixTimeList or {}
self.db.global.maxxp = self.db.global.maxxp or {}
self.db.global.member = setmetatable(self.db.global.member or {}, {__index = function(t, k)
local v = {}
rawset(t, k, v)
return v
end
})
member = self.db.global.member
-- character specific
char.timeAFK = char.timeAFK or 0
char.totalAFK = char.totalAFK or 0
-- level 1 init
if player.level == 1 then
char.DateStampList[1] = char.DateStampList[1] or date("%Y.%m.%d %H:%M:%S")
char.UnixTimeList[1] = char.UnixTimeList[1] or time()
end
-- v1.16: remove pre-connected realms guild member data
wipe(self.db.realm)
end
local f = CreateFrame("Frame")
local delay = 0
-- wait 4 sec first for any other AddOns that want to request /played too
function f:WaitPlayed(elapsed)
delay = delay + elapsed
if delay > 4 then
if S.totalTPM == 0 then
RequestTimePlayed()
end
self:SetScript("OnUpdate", nil)
end
end
function RSD:OnEnable()
for k, v in pairs(S.events) do
if type(v) == "table" then
for _, event in ipairs(v) do
self:RegisterEvent(event, k)
end
else
self:RegisterEvent(v)
end
end
-- filter /played message
S.filterPlayed = true
-- support [Class Colors] by Phanx
if CUSTOM_CLASS_COLORS then
CUSTOM_CLASS_COLORS:RegisterCallback("WipeCache", self)
end
--------------
--- Timers ---
--------------
-- standalone OnUpdate is more stable than AceTimer on client startup imho
f:SetScript("OnUpdate", f.WaitPlayed)
self:ScheduleRepeatingTimer(function()
-- the returns of UnitLevel() aren't yet updated on UNIT_LEVEL
if profile.ShowGroup then
self:UNIT_LEVEL()
end
if profile.ShowGuild and IsInGuild() then
C_GuildInfo.GuildRoster() -- fires GUILD_ROSTER_UPDATE
end
-- FRIENDLIST_UPDATE doesn't fire on actual friend levelups
-- the returns of GetFriendInfo() only get updated when FRIENDLIST_UPDATE fires
if profile.ShowFriend then
-- C_FriendList.ShowFriends() -- fires FRIENDLIST_UPDATE
-- self:FRIENDLIST_UPDATE()
-- BN_FRIEND_INFO_CHANGED doesn't fire on login; but it does on actual levelups; just to be sure
self:BN_FRIEND_INFO_CHANGED()
end
end, 11)
end
function RSD:OnDisable()
self:UnregisterAllEvents()
self:CancelAllTimers()
if CUSTOM_CLASS_COLORS then
CUSTOM_CLASS_COLORS:UnregisterCallback("WipeCache", self)
end
end
function RSD:RefreshDB()
-- table shortcuts
profile = self.db.profile
char = self.db.char
-- update table references in other files
for i = 1, 2 do
self["RefreshDB"..i](self)
end
-- init random messages
self["SetupRandomDing"](self, profile["NumRandomDing"])
-- stopwatch (only on changing profiles; it's not yet initialized at start)
-- but don't hide stopwatch, we'd want a library for that lol
local v = S.curTPM + time() - S.lastPlayed
if profile.Stopwatch and S.curTPM > 0 and S.CanUseStopwatch(v) then
S.StopwatchStart(v)
end
-- graphs
if ReadySetDing_LevelGraph then
ReadySetDing_LevelGraph[profile.LevelGraph and "Show" or "Hide"](ReadySetDing_LevelGraph)
end
-- clear random message preview
wipe(S.activePreview)
end
----------------------
--- Slash Commands ---
----------------------
local enable = {
["1"] = true,
on = true,
enable = true,
load = true,
}
local disable = {
["0"] = true,
off = true,
disable = true,
unload = true,
}
function RSD:SlashCommand(input)
if enable[input] then
self:Enable()
self:Print("|cffADFF2F"..VIDEO_OPTIONS_ENABLED.."|r")
elseif disable[input] then
self:Disable()
self:Print("|cffFF2424"..VIDEO_OPTIONS_DISABLED.."|r")
else
ACD:Open("ReadySetDing_Parent")
end
end
----------------
--- Level Up ---
----------------
local playerDinged
function RSD:PLAYER_LEVEL_UP(event, ...)
local level = ...
player.level = level -- on another note, UnitLevel is not yet updated
playerDinged = true
S.filterPlayed = true
RequestTimePlayed() -- TIME_PLAYED_MSG
end
local requiresHw = {
SAY = true,
YELL = true,
CHANNEL = true,
}
local function CanSendChatMessage(chatType)
local _, instanceType = IsInInstance()
if instanceType == "none" and requiresHw[chatType or "SAY"] then
return false
end
return true
end
function RSD:TIME_PLAYED_MSG(event, ...)
S.totalTPM, S.curTPM = ...
S.lastPlayed = time()
if playerDinged then
playerDinged = false
local levelTime
local prevTime = char.TotalTimeList[player.level-1]
if prevTime then
-- TotalTime @ Ding - TotalTime @ previous Ding
levelTime = S.totalTPM - prevTime
else
-- undinged LevelTime + (dinged TotalTime - undinged TotalTime)
levelTime = curTPM2 + (S.totalTPM - totalTPM2)
end
-- update player data
local level = player.level
char.LevelTimeList[level] = levelTime
char.TotalTimeList[level] = S.totalTPM
char.UnixTimeList[level] = time()
char.DateStampList[level] = date("%Y.%m.%d %H:%M:%S")
self.db.global.maxxp[level-1] = player.maxxp
-- Language
local _, langId
if profile.Language == 2 then
_, langId = GetLanguageByIndex(random(GetNumLanguages()))
elseif profile.Language > 2 then
_, langId = GetLanguageByIndex(profile.Language-2)
end
local text = self:ChatDing(levelTime)
-- Party/Raid Announce
if profile.ChatGroup then
local isBattleground = select(2, IsInInstance()) == "pvp"
local chan = (IsPartyLFG() or isBattleground) and "INSTANCE_CHAT" or IsInRaid() and "RAID" or IsInGroup() and "PARTY" or CanSendChatMessage() and "SAY" or "EMOTE"
-- send in two messages
if strlen(text) > 255 then
SendChatMessage(strsub(text, 1, 255), chan, langId)
SendChatMessage(strsub(text, 256), chan, langId)
else
SendChatMessage(text, chan, langId)
end
else -- for people that rather like a local notification instead of announce
RaidNotice_AddMessage(RaidWarningFrame, text, S.white)
end
-- Guild Announce
if profile.ChatGuild and IsInGuild() then
SendChatMessage(text, "GUILD", langId)
end
-- Screenshot
if profile.Screenshot then
self:ScheduleTimer(function()
Screenshot()
end, 1)
end
if profile.Stopwatch and S.CanUseStopwatch(S.curTPM) then
-- temporarily pause
Stopwatch_Pause()
self:ScheduleTimer(function()
-- play again
S.StopwatchStart(S.curTPM + time() - S.lastPlayed)
end, 60)
end
-- reset current level specific data
char.timeAFK = 0
--------------
--- Graphs ---
--------------
local level = ReadySetDing_LevelGraph
local total = ReadySetDing_TotalGraph
if level and profile.LevelGraph then
level:ResetData()
total:ResetData()
self:UpdateGraph(level, total)
end
else
-- Blizzard_TimeManager is not yet loaded, but we're being delayed anyway
if profile.Stopwatch and S.CanUseStopwatch(S.curTPM) then
S.StopwatchStart(S.curTPM)
end
end
-- update config if currently shown
if ACD.OpenFrames.ReadySetDing_Parent then
ACR:NotifyChange("ReadySetDing_Parent")
end
-- update for next levelup
totalTPM2, curTPM2 = S.totalTPM, S.curTPM
-- UnitXPMax is not yet readily updated
self:ScheduleTimer(function()
player.maxxp = UnitXPMax("player")
end, 2)
end
function RSD:ChatDing(levelTime)
local args = args
args.level = player.level
args["level-"] = player.level - 1
args["level#"] = player.maxlevel
args["level%"] = player.maxlevel - player.level
args.time = self:Time(levelTime)
args.total = self:Time(S.totalTPM)
args.date = date("%Y.%m.%d %H:%M:%S")
args.date2 = date("%m/%d/%y %H:%M:%S")
args.afk = self:Time(char.timeAFK)
args["afk+"] = self:Time(char.totalAFK)
args.zone = GetRealZoneText() or GetSubZoneText() or ZONE
-- fallback to default in case of blank (nil) random message
local msg = profile.DingRandom and profile.DingMsg[random(profile.NumRandomDing)] or profile.DingMsg[1]
return self:ReplaceArgs(msg, args)
end
-------------
--- Group ---
-------------
local group = {}
function RSD:UNIT_LEVEL()
if not profile.ShowGroup then return end
local isRaid = IsInRaid()
local isParty = IsInGroup()
local numGroup = isRaid and GetNumGroupMembers() or isParty and GetNumSubgroupMembers() or 0
local groupType = isRaid and "raid" or isParty and "party"
local chan = isRaid and "|cffFF7F00"..RAID.."|r" or isParty and "|cffA8A8FF"..PARTY.."|r"
for i = 1, numGroup do
local guid = UnitGUID(groupType..i)
local name, realm = UnitName(groupType..i)
local level = UnitLevel(groupType..i)
-- level can return as 0 when party members are not yet in the instance/zone
if guid and level and level > 0 then
if group[guid] and group[guid] > 0 and level > group[guid] and name ~= player.name then
local class = select(2, UnitClass(groupType..i))
local race = select(2, UnitRace(groupType..i))
local sex = UnitSex(groupType..i)
local raceIcon = S.GetRaceIcon(race, sex, 1, 1)
local classIcon = S.GetClassIcon(class, 1, 1)
args.icon = raceIcon..classIcon
args.chan = chan
local classColor = S.classCache[select(2, UnitClass(groupType..i))]
args.name = format("|cff%s|Hplayer:%s|h%s|h|r", classColor, name..(realm and "-"..realm or ""), name)
args.level = "|cffADFF2F"..level.."|r"
self:ShowLevelup(profile.ShowMsg, args)
end
group[guid] = level
end
end
end
-------------
--- Guild ---
-------------
-- before secondGRU show anything as guild diff
-- after secondGRU show realtime (around +20 sec earliest)
local firstGRU, secondGRU, showedHeader
-- now this is an even bigger mess than before ._.
function RSD:GUILD_ROSTER_UPDATE(event)
if not profile.ShowGuild then return end
if not (time() > (cd.guild or 0)) then return end
cd.guild = time() + 10
local chan = "|cff40FF40"..GUILD.."|r"
for i = 1, GetNumGuildMembers() do
local fullName, rank, _, level, class, zone, _, _, _, _, englishClass = GetGuildRosterInfo(i)
local charName, charRealm = strmatch(fullName or "", "(.+)%-(.+)") -- is this different on non-connected realms?..
if charName and charRealm then -- sanity checks
local p = member[charRealm][charName]
-- sanity checks everywhere~
if p and #p > 0 and level > p[1] and charName ~= player.name then
if secondGRU then
local realtime = 0
if level-1 ~= 1 and p[level-1] then
realtime = time() - p[level-1][1]
end
-- args for ShowGuild specifically
args.icon = S.GetClassIcon(englishClass, 1, 1)
args.chan = chan
args.name = format("|cff%s|Hplayer:%s|h%s|h|r", S.classCache[englishClass], fullName, charName)
args.level = "|cffADFF2F"..level.."|r"
args.zone = zone
args.realtime = realtime
if profile.ShowGuild then
self:ShowLevelup(profile.ShowMsg, args)
end
-- save time & date
p[level] = {time(), date("%Y.%m.%d %H:%M:%S")}
else
-- level changed while user was offline
if profile.GuildChangelog then
if not showedHeader and char.LastCheck then
self:Print(format("|cffF6ADC6[%s]|r - |cffADFF2F[%s]|r", char.LastCheck, date("%Y.%m.%d %H:%M:%S")))
showedHeader = true; char.LastCheck = nil
end
print(format("|cff%s|Hplayer:%s|h[%s]|h|r %s |cffF6ADC6%s|r - |cffADFF2F%s|r (+|cff71D5FF%s|r)",
S.classCache[englishClass], fullName, charName, LEVEL, p[1], level, level-p[1]))
end
-- don't got time & date
p[level] = false
end
end
-- dont save already maxed characters
if level ~= S.maxlevel or (p and #p > 0) then
member[charRealm][charName] = member[charRealm][charName] or {}
member[charRealm][charName][1] = level
end
end
end
-- even more awful delaying
-- a lot of bunnies were sacrificed for this
if firstGRU then secondGRU = true end
firstGRU = true
end
---------------
--- Friends ---
---------------
friend = {}
function RSD:FRIENDLIST_UPDATE(event)
--[=[
if not profile.ShowFriend then return end
if time() > (cd.friend or 0) then
cd.friend = time() + 1
local chan = FRIENDS_WOW_NAME_COLOR_CODE..FRIEND.."|r"
for i = 1, C_FriendList.GetNumOnlineFriends() do
local info = C_FriendList.GetFriendInfoByIndex(i)
local name, level, class = info.name, info.level, info.className
-- print(name, level)
if name then -- name is sometimes nil
if friend[name] and level > 0 and level > friend[name] then
args.icon = S.GetClassIcon(S.revLOCALIZED_CLASS_NAMES[class], 1, 1)
args.chan = chan
args.name = format("|cff%s|Hplayer:%s|h%s|h|r", S.classCache[S.revLOCALIZED_CLASS_NAMES[class]], name, name)
args.level = "|cffADFF2F"..level.."|r"
self:ShowLevelup(profile.ShowMsg, args)
end
friend[name] = level
end
end
end
]=]
end
------------------
--- Battle.net ---
------------------
local realid = {}
function RSD:BN_FRIEND_INFO_CHANGED()
if not profile.ShowFriend then return end
if time() > (cd.realid or 0) then
cd.realid = time() + 1
local chan = FRIENDS_BNET_NAME_COLOR_CODE..BATTLENET_FRIEND.."|r"
for i = 1, select(2, BNGetNumFriends()) do
local info = C_BattleNet.GetFriendAccountInfo(i)
local presenceID, presenceName = info.gameAccountInfo.gameAccountID, info.accountName
-- ToDo: add support for multiple online toons / BNGetFriendToonInfo
local ginfo = C_BattleNet.GetGameAccountInfoByID(presenceID)
local toonName, client, realm, class, level = ginfo.characterName, ginfo.clientProgram, ginfo.realmName, ginfo.className, ginfo.characterLevel
if not realm or not info.gameAccountInfo.isOnline then return end -- sanity checks
-- avoid misrecognizing characters that share the same name, but are from different servers
realid[realm] = realid[realm] or {}
local bnet = realid[realm]
if client == BNET_CLIENT_WOW then
level = tonumber(level) -- why is level a string type
if toonName and bnet[toonName] and bnet[toonName] > 0 and level and level > bnet[toonName] then
args.icon = S.GetClassIcon(S.revLOCALIZED_CLASS_NAMES[class], 1, 1)
args.chan = chan
-- "|Kg49|k00000000|k": f BNplayer; g firstname; s surname; default f in 5.0.4
-- the "BNplayer" hyperlink might maybe taint whatever it calls on right-click
args.name = format("|cff%s|HBNplayer:%s:%s|h%s|r |cff%s%s|h|r", "82C5FF", presenceName, presenceID, presenceName, S.classCache[S.revLOCALIZED_CLASS_NAMES[class]], toonName)
args.level = "|cffADFF2F"..level.."|r"
self:ShowLevelup(profile.ShowMsg, args)
end
bnet[toonName] = level
end
end
end
end
----------------
--- AFK Time ---
----------------
local afk
local function LeaveAFK()
char.timeAFK = char.timeAFK + (time() - afk)
char.totalAFK = char.totalAFK + (time() - afk)
end
local MARKED_AFK_MESSAGE = gsub(MARKED_AFK_MESSAGE, "%%s", ".+")
function RSD:CHAT_MSG_SYSTEM(event, msg)
-- entering afk
if msg == MARKED_AFK or strfind(msg, MARKED_AFK_MESSAGE) then
afk = time()
-- leaving afk
elseif msg == CLEARED_AFK and afk then
LeaveAFK()
end
end
function RSD:PLAYER_LEAVING_WORLD(event)
-- logging out while afk
if UnitIsAFK("player") and afk then
LeaveAFK()
end
-- time/date at logout for showing levelDiffs
char.LastCheck = date("%Y.%m.%d %H:%M:%S")
end