-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathKillTrack.lua
889 lines (795 loc) · 27.7 KB
/
KillTrack.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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
--[[
* Copyright (c) 2011-2020 by Adam Hellberg.
*
* This file is part of KillTrack.
*
* KillTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KillTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KillTrack. If not, see <http://www.gnu.org/licenses/>.
--]]
---@type string
local NAME = ...
---@class KillTrack
---@field PlayerName string
---@field PlayerGUID string?
local KT = select(2, ...)
---@class KillTrackMobData
---@field Kills integer
---@field Name string
---@field LastKillAt integer?
---@field AchievCount integer
---@field Exp integer?
---@class KillTrackCharMobData
---@field Kills integer
---@field Name string
---@field LastKillAt integer?
_G[NAME] = KT
-- Upvalue some functions used in CLEU
local IsGUIDInGroup = IsGUIDInGroup
local UnitGUID = UnitGUID
local UnitIsTapDenied = UnitIsTapDenied
local UnitTokenFromGUID = UnitTokenFromGUID
local CombatLogGetCurrentEventInfo = CombatLogGetCurrentEventInfo
local GetServerTime = GetServerTime
local NO_NAME = "<No Name>"
KT.Name = NAME
KT.Version = C_AddOns.GetAddOnMetadata(NAME, "Version")
KT.Events = {}
---@class KillTrackImmediatePosition
---@field POINT string?
---@field RELATIVE string?
---@field X number?
---@field Y number?
---@class KillTrackGlobal
---@field LOAD_MESSAGE boolean
---@field PRINTKILLS boolean
---@field PRINTNEW boolean
---@field ACHIEV_THRESHOLD integer
---@field COUNT_GROUP boolean
---@field SHOW_EXP boolean
---@field MOBS { [integer]: KillTrackMobData }
---@field IMMEDIATE { POSITION: KillTrackImmediatePosition, THRESHOLD: integer, FILTER: string? }
---@field BROKER { SHORT_TEXT: boolean, MINIMAP: { hide: boolean } }
---@field DISABLE_DUNGEONS boolean
---@field DISABLE_RAIDS boolean
---@field TOOLTIP boolean
---@field DATETIME_FORMAT string
KT.Global = {}
---@class KillTrackCharGlobal
---@field MOBS { [integer]: KillTrackCharMobData }
KT.CharGlobal = {}
---@class KillTrackTemp
---@field Threshold integer?
---@field DeleteId integer?
KT.Temp = {}
---@enum KillTrackMobSortMode
KT.Sort = {
Desc = 0,
Asc = 1,
CharDesc = 2,
CharAsc = 3,
AlphaD = 4,
AlphaA = 5,
IdDesc = 6,
IdAsc = 7
}
KT.Session = {
Count = 0,
---@type { [string]: integer? }
Kills = {}
}
KT.Messages = {
Announce = "[KillTrack] Session Length: %s. Session Kills: %d. Kills Per Minute: %.2f."
}
KT.Defaults = {
DateTimeFormat = "%Y-%m-%d %H:%M:%S"
}
---@type KillTrackExpTracker
local ET
local KTT = KT.Tools
-- Upvalue as it's used in CLEU
local GUIDToID = KTT.GUIDToID
---@type { [string]: string? }
local FirstDamage = {} -- Tracks first damage to a mob registered by CLEU
---@type { [string]: string? }
local LastDamage = {} -- Tracks whoever did the most recent damage to a mob
---@type { [string]: boolean? }
local HasPlayerDamage = {} -- Tracks whether the player (or pet) has dealt damage to a mob
---@type { [string]: boolean? }
local HasGroupDamage = {} -- Tracks whether a group member has dealt damage to a mob
---@type { [string]: boolean? }
local DamageValid = {} -- Determines if mob is tapped by player/group
local combat_log_damage_events = {}
do
local prefixes = { "SWING", "RANGE", "SPELL", "SPELL_PERIODIC", "SPELL_BUILDING" }
local suffixes = { "DAMAGE", "DRAIN", "LEECH", "INSTAKILL" }
for _, prefix in pairs(prefixes) do
for _, suffix in pairs(suffixes) do
combat_log_damage_events[prefix .. "_" .. suffix] = true
end
end
end
if KT.Version == "@" .. "project-version" .. "@" then
KT.Version = "Development"
KT.Debug = true
end
if not UnitTokenFromGUID then
local units = {
"player",
"vehicle",
"pet",
"party1", "party2", "party3", "party4",
"partypet1", "partypet2", "partypet3", "partypet4"
}
-- Multiple loops to get the same ordering as mainline API
for i = 1, 40 do
units[#units + 1] = "raid" .. i
end
for i = 1, 40 do
units[#units + 1] = "raidpet" .. i
end
for i = 1, 40 do
units[#units + 1] = "nameplate" .. i
end
for i = 1, 5 do
units[#units + 1] = "arena" .. i
end
for i = 1, 5 do
units[#units + 1] = "arenapet" .. i
end
for i = 1, 8 do
units[#units + 1] = "boss" .. i
end
units[#units + 1] = "target"
units[#units + 1] = "focus"
units[#units + 1] = "npc"
units[#units + 1] = "mouseover"
units[#units + 1] = "softenemy"
units[#units + 1] = "softfriend"
units[#units + 1] = "softinteract"
UnitTokenFromGUID = function(guid)
for _, unit in ipairs(units) do
if UnitGUID(unit) == guid then
return unit
end
end
return nil
end
end
---@param _ Frame
---@param event string
---@param ... any
function KT:OnEvent(_, event, ...)
if self.Events[event] then
self.Events[event](self, ...)
end
end
---@param self KillTrack
---@param name string
function KT.Events.ADDON_LOADED(self, name)
if name ~= NAME then return end
ET = KT.ExpTracker
if type(_G["KILLTRACK"]) ~= "table" then
_G["KILLTRACK"] = {}
end
self.Global = _G["KILLTRACK"]
if type(self.Global.LOAD_MESSAGE) ~= "boolean" then
self.Global.LOAD_MESSAGE = false
end
if type(self.Global.PRINTKILLS) ~= "boolean" then
self.Global.PRINTKILLS = false
end
if type(self.Global.PRINTNEW) ~= "boolean" then
self.Global.PRINTNEW = false
end
if type(self.Global.ACHIEV_THRESHOLD) ~= "number" then
self.Global.ACHIEV_THRESHOLD = 1000
end
if type(self.Global.COUNT_GROUP) ~= "boolean" then
self.Global.COUNT_GROUP = false
end
if type(self.Global.SHOW_EXP) ~= "boolean" then
self.Global.SHOW_EXP = false
end
if type(self.Global.MOBS) ~= "table" then
self.Global.MOBS = {}
end
if type(self.Global.IMMEDIATE) ~= "table" then
self.Global.IMMEDIATE = {}
end
if type(self.Global.IMMEDIATE.POSITION) ~= "table" then
self.Global.IMMEDIATE.POSITION = {}
end
if type(self.Global.IMMEDIATE.THRESHOLD) ~= "number" then
self.Global.IMMEDIATE.THRESHOLD = 0
end
if type(self.Global.BROKER) ~= "table" then
self.Global.BROKER = {}
end
if type(self.Global.BROKER.SHORT_TEXT) ~= "boolean" then
self.Global.BROKER.SHORT_TEXT = false
end
if type(self.Global.BROKER.MINIMAP) ~= "table" then
self.Global.BROKER.MINIMAP = {}
end
if type(self.Global.DISABLE_DUNGEONS) ~= "boolean" then
self.Global.DISABLE_DUNGEONS = false
end
if type(self.Global.DISABLE_RAIDS) ~= "boolean" then
self.Global.DISABLE_RAIDS = false
end
if type(self.Global.TOOLTIP) ~= "boolean" then
self.Global.TOOLTIP = true
end
if type(self.Global.DATETIME_FORMAT) ~= "string" then
self.Global.DATETIME_FORMAT = self.Defaults.DateTimeFormat
end
if type(_G["KILLTRACK_CHAR"]) ~= "table" then
_G["KILLTRACK_CHAR"] = {}
end
self.CharGlobal = _G["KILLTRACK_CHAR"]
if type(self.CharGlobal.MOBS) ~= "table" then
self.CharGlobal.MOBS = {}
end
self.PlayerName = UnitName("player")
self.PlayerGUID = UnitGUID("player")
if self.Global.LOAD_MESSAGE then
self:Msg("AddOn Loaded!")
end
self.Session.Start = time()
self.Broker:OnLoad()
end
---@param self KillTrack
function KT.Events.COMBAT_LOG_EVENT_UNFILTERED(self)
local _, event, _, s_guid, _, _, _, d_guid, d_name, _, _ = CombatLogGetCurrentEventInfo()
if combat_log_damage_events[event] then
if FirstDamage[d_guid] == nil then
-- s_guid is (probably) the player who first damaged this mob and probably has the tag
FirstDamage[d_guid] = s_guid
end
LastDamage[d_guid] = s_guid
if s_guid == self.PlayerGUID or s_guid == UnitGUID("pet") then
HasPlayerDamage[d_guid] = true
elseif self:IsInGroup(s_guid) then
HasGroupDamage[d_guid] = true
end
if not DamageValid[d_guid] then
-- if DamageValid returns true for a GUID, we can tell with 100% certainty that it's valid
-- But this relies on one of the valid unit names currently being the damaged mob
local d_unit = UnitTokenFromGUID(d_guid)
if not d_unit then return end
DamageValid[d_guid] = not UnitIsTapDenied(d_unit)
end
return
end
if event ~= "UNIT_DIED" then return end
-- Perform solo/group checks
local d_id = GUIDToID(d_guid)
local firstDamage = FirstDamage[d_guid]
local lastDamage = LastDamage[d_guid]
local damageValid = DamageValid[d_guid]
local hasPlayerDamage = HasPlayerDamage[d_guid]
local hasGroupDamage = HasGroupDamage[d_guid]
FirstDamage[d_guid] = nil
LastDamage[d_guid] = nil
DamageValid[d_guid] = nil
HasPlayerDamage[d_guid] = nil
HasGroupDamage[d_guid] = nil
-- If we can't identify the mob there's no point continuing
if d_id == nil or d_id == 0 then return end
local petGUID = UnitGUID("pet")
local firstByPlayer = firstDamage == self.PlayerGUID or firstDamage == petGUID
local firstByGroup = self:IsInGroup(firstDamage)
local lastByPlayer = lastDamage == self.PlayerGUID or lastDamage == petGUID
-- The checks when DamageValid is non-false are not 100% failsafe
-- Scenario: You deal the killing blow to an already tapped mob <- Would count as kill with current code
-- If damageValid is false, it means tap is denied on the mob and there is no way the kill would count
if damageValid == false then return end
-- If neither the player not group was involved in the battle, we don't count the kill
if not hasPlayerDamage and not hasGroupDamage then return end
if damageValid == nil and not firstByPlayer and not firstByGroup then
-- If we couldn't get a proper tapped status and the first recorded damage was not by player or group,
-- we can't be sure the kill was valid, so ignore it
return
end
if not lastByPlayer and not self.Global.COUNT_GROUP then
return -- Player or player's pet did not deal the killing blow and addon only tracks player kills
end
self:AddKill(d_id, d_name)
if self.Timer:IsRunning() then
self.Timer:SetData("Kills", self.Timer:GetData("Kills", true) + 1)
end
end
---@param _ KillTrack
---@param message string
function KT.Events.CHAT_MSG_COMBAT_XP_GAIN(_, message)
ET:CheckMessage(message)
end
---@param self KillTrack
---@param size integer
function KT.Events.ENCOUNTER_START(self, _, _, _, size)
if (self.Global.DISABLE_DUNGEONS and size == 5) or (self.Global.DISABLE_RAIDS and size > 5) then
self.Frame:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
self.Frame:UnregisterEvent("UPDATE_MOUSEOVER_UNIT")
self.Frame:UnregisterEvent("CHAT_MSG_COMBAT_XP_GAIN")
end
end
---@param self KillTrack
---@param size integer
function KT.Events.ENCOUNTER_END(self, _, _, _, size)
if (self.Global.DISABLE_DUNGEONS and size == 5) or (self.Global.DISABLE_RAIDS and size > 5) then
self.Frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
self.Frame:RegisterEvent("UPDATE_MOUSEOVER_UNIT")
self.Frame:RegisterEvent("CHAT_MSG_COMBAT_XP_GAIN")
end
end
---@param self GameTooltip
local function tooltip_enhancer(self)
if not KT.Global.TOOLTIP then return end
local _, unit = self:GetUnit()
if not unit then return end
if UnitIsPlayer(unit) then return end
local id = KTT.GUIDToID(UnitGUID(unit))
if not id then return end
if UnitCanAttack("player", unit) then
local mob, charMob = KT:InitMob(id, UnitName(unit))
local gKills, cKills = mob.Kills, charMob.Kills --self:GetKills(id)
local exp = mob.Exp
self:AddLine(("Killed %d (%d) times."):format(cKills, gKills), 1, 1, 1)
if KT.Global.SHOW_EXP and exp then
local toLevel = exp > 0 and math.ceil((UnitXPMax("player") - UnitXP("player")) / exp) or "N/A"
self:AddLine(("EXP: %d (%s kills to level)"):format(exp, toLevel), 1, 1, 1)
end
end
if KT.Debug then
self:AddLine(("ID = %d"):format(id))
end
self:Show()
end
if WOW_PROJECT_ID == WOW_PROJECT_MAINLINE then
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, tooltip_enhancer)
else
GameTooltip:HookScript("OnTooltipSetUnit", tooltip_enhancer)
end
function KT:ToggleLoadMessage()
self.Global.LOAD_MESSAGE = not self.Global.LOAD_MESSAGE
if self.Global.LOAD_MESSAGE then
self:Msg("Now showing message on AddOn load")
else
self:Msg("No longer showing message on AddOn load")
end
end
function KT:ToggleExp()
self.Global.SHOW_EXP = not self.Global.SHOW_EXP
if self.Global.SHOW_EXP then
self:Msg("Now showing EXP on tooltips!")
else
self:Msg("No longer showing EXP on tooltips.")
end
end
function KT:ToggleDebug()
self.Debug = not self.Debug
if self.Debug then
self:Msg("Debug enabled!")
else
self:Msg("Debug disabled!")
end
end
---@param guid string?
---@return boolean
function KT:IsInGroup(guid)
if not guid or guid == "" then return false end
if guid == self.PlayerName or guid == self.PlayerGUID then return true end
return IsGUIDInGroup(guid)
end
---@param threshold integer
function KT:SetThreshold(threshold)
if type(threshold) ~= "number" then
error("KillTrack.SetThreshold: Argument #1 (threshold) must be of type 'number'")
end
self.Global.ACHIEV_THRESHOLD = threshold
if threshold > 0 then
self:ResetAchievCount()
self:Msg(("New kill notice (achievement) threshold set to %d."):format(threshold))
else
self:Msg("Kill notices have been disabled (set threshold to a value greater than 0 to re-enable).")
end
end
---@param threshold integer
function KT:SetImmediateThreshold(threshold)
if type(threshold) ~= "number" then
error("KillTrack.SetImmediateThreshold: Argument #1 (threshold) must be of type 'number'")
end
self.Global.IMMEDIATE.THRESHOLD = threshold
if threshold > 0 then
self:Msg(("New immediate threshold set to %d."):format(threshold))
else
self:Msg("Immediate threshold disabled.")
end
end
---@param filter string
function KT:SetImmediateFilter(filter)
if type(filter) ~= "string" then
error("KillTrack.SetImmediateFilter: Argument #1 (filter) must be of type 'string'")
end
self.Global.IMMEDIATE.FILTER = filter
self:Msg("New immediate filter set to: " .. filter)
end
function KT:ClearImmediateFilter()
self.Global.IMMEDIATE.FILTER = nil
KT:Msg("Immediate filter cleared!")
end
function KT:ToggleCountMode()
self.Global.COUNT_GROUP = not self.Global.COUNT_GROUP
if self.Global.COUNT_GROUP then
self:Msg("Now counting kills for every player in the group (party/raid)!")
else
self:Msg("Now counting your own killing blows ONLY.")
end
end
---@param id integer
---@param name string?
---@return KillTrackMobData
---@return KillTrackCharMobData
function KT:InitMob(id, name)
name = name or NO_NAME
if type(self.Global.MOBS[id]) ~= "table" then
self.Global.MOBS[id] = { Name = name, Kills = 0, AchievCount = 0 }
if self.Global.PRINTNEW then
self:Msg(("Created new entry for %q"):format(name))
end
elseif self.Global.MOBS[id].Name ~= name then
self.Global.MOBS[id].Name = name
end
if type(self.CharGlobal.MOBS[id]) ~= "table" then
self.CharGlobal.MOBS[id] = { Name = name, Kills = 0 }
if self.Global.PRINTNEW then
self:Msg(("Created new entry for %q on this character."):format(name))
end
elseif self.CharGlobal.MOBS[id].Name ~= name then
self.CharGlobal.MOBS[id].Name = name
end
return self.Global.MOBS[id], self.CharGlobal.MOBS[id]
end
---@param id integer
---@param name string?
function KT:AddKill(id, name)
name = name or NO_NAME
local current_time = GetServerTime()
self:InitMob(id, name)
local globalMob = self.Global.MOBS[id]
local charMob = self.CharGlobal.MOBS[id]
globalMob.Kills = self.Global.MOBS[id].Kills + 1
globalMob.LastKillAt = current_time
charMob.Kills = self.CharGlobal.MOBS[id].Kills + 1
charMob.LastKillAt = current_time
if self.Global.PRINTKILLS then
local kills = self.Global.MOBS[id].Kills
local cKills = self.CharGlobal.MOBS[id].Kills
self:Msg(("Updated %q, new kill count: %d. Kill count on this character: %d"):format(name, kills, cKills))
end
self:AddSessionKill(name)
if self.Immediate.Active then
local filter = self.Global.IMMEDIATE.FILTER
local filterPass = not filter or name:match(filter)
if filterPass then
self.Immediate:AddKill()
if self.Global.IMMEDIATE.THRESHOLD > 0 and self.Immediate.Kills % self.Global.IMMEDIATE.THRESHOLD == 0 then
PlaySound(SOUNDKIT.RAID_WARNING)
PlaySound(SOUNDKIT.PVP_THROUGH_QUEUE)
RaidNotice_AddMessage(
RaidWarningFrame,
("%d KILLS!"):format(self.Immediate.Kills),
ChatTypeInfo.SYSTEM)
end
end
end
if self.Global.ACHIEV_THRESHOLD <= 0 then return end
if type(self.Global.MOBS[id].AchievCount) ~= "number" then
self.Global.MOBS[id].AchievCount = floor(self.Global.MOBS[id].Kills / self.Global.ACHIEV_THRESHOLD)
if self.Global.MOBS[id].AchievCount >= 1 then
self:KillAlert(self.Global.MOBS[id])
end
else
local achievCount = self.Global.MOBS[id].AchievCount
self.Global.MOBS[id].AchievCount = floor(self.Global.MOBS[id].Kills / self.Global.ACHIEV_THRESHOLD)
if self.Global.MOBS[id].AchievCount > achievCount then
self:KillAlert(self.Global.MOBS[id])
end
end
end
---@param id integer
---@param name string?
---@param globalCount integer
---@param charCount integer
function KT:SetKills(id, name, globalCount, charCount)
if type(id) ~= "number" then
error("'id' argument must be a number")
end
if type(globalCount) ~= "number" then
error("'globalCount' argument must be a number")
end
if type(charCount) ~= "number" then
error("'charCount' argument must be a number")
end
name = name or NO_NAME
self:InitMob(id, name)
self.Global.MOBS[id].Kills = globalCount
self.CharGlobal.MOBS[id].Kills = charCount
self:Msg(("Updated %q to %d global and %d character kills"):format(name, globalCount, charCount))
end
---@param name string
function KT:AddSessionKill(name)
if self.Session.Kills[name] then
self.Session.Kills[name] = self.Session.Kills[name] + 1
else
self.Session.Kills[name] = 1
end
self.Session.Count = self.Session.Count + 1
end
---@param name string
---@param exp integer|string
function KT:SetExp(name, exp)
for _, mob in pairs(self.Global.MOBS) do
if mob.Name == name then mob.Exp = tonumber(exp) end
end
end
---@param max integer|string?
---@return { Name: string, Kills: integer }[]
function KT:GetSortedSessionKills(max)
max = tonumber(max) or 3
local t = {}
for k,v in pairs(self.Session.Kills) do
t[#t + 1] = {Name = k, Kills = v}
end
table.sort(t, function(a, b) return a.Kills > b.Kills end)
-- Trim table to only contain 3 entries
local trimmed = {}
local c = 0
for i,v in ipairs(t) do
trimmed[i] = v
c = c + 1
if c >= max then break end
end
return trimmed
end
function KT:ResetSession()
wipe(self.Session.Kills)
self.Session.Count = 0
self.Session.Start = time()
end
---@param id integer
---@return integer globalKills
---@return integer charKills
function KT:GetKills(id)
local gKills, cKills = 0, 0
local mob = self.Global.MOBS[id]
if type(mob) == "table" then
gKills = mob.Kills
local cMob = self.CharGlobal.MOBS[id]
if type(cMob) == "table" then
cKills = cMob.Kills
end
end
return gKills, cKills
end
---@return integer
function KT:GetTotalKills()
local count = 0
for _, mob in pairs(self.Global.MOBS) do
count = count + mob.Kills
end
return count
end
---@return integer killsPerSecond
---@return integer killsPerMinute
---@return integer killsPerHour
---@return integer killsThisSession
function KT:GetSessionStats()
if not self.Session.Start then return 0, 0, 0, 0 end
local now = time()
local session = now - self.Session.Start
local kps = session == 0 and 0 or self.Session.Count / session
local kpm = kps * 60
local kph = kpm * 60
return kps, kpm, kph, session
end
---@param identifier string|integer?
function KT:PrintKills(identifier)
local found = false
local name = NO_NAME
local gKills = 0
local cKills = 0
local lastKillAt = nil
if type(identifier) ~= "string" and type(identifier) ~= "number" then identifier = NO_NAME end
for k,v in pairs(self.Global.MOBS) do
if type(v) == "table" and (tostring(k) == tostring(identifier) or v.Name == identifier) then
name = v.Name
gKills = v.Kills
if type(v.LastKillAt) == "number" then
lastKillAt = KTT:FormatDateTime(v.LastKillAt)
end
if self.CharGlobal.MOBS[k] then
cKills = self.CharGlobal.MOBS[k].Kills
end
found = true
end
end
if found then
self:Msg(("You have killed %q %d times in total, %d times on this character"):format(name, gKills, cKills))
if lastKillAt then
self:Msg(("Last killed at %s"):format(lastKillAt))
end
else
if UnitExists("target") and not UnitIsPlayer("target") then
identifier = UnitName("target")
end
self:Msg(("Unable to find %q in mob database."):format(tostring(identifier)))
end
end
---@param target string
function KT:Announce(target)
if target == "GROUP" then
target = ((IsInRaid() and "RAID") or (IsInGroup() and "PARTY")) or "SAY"
end
local _, kpm, _, length = self:GetSessionStats()
local msg = self.Messages.Announce:format(KTT:FormatSeconds(length), self.Session.Count, kpm)
SendChatMessage(msg, target)
end
---@param msg string
function KT:Msg(msg)
DEFAULT_CHAT_FRAME:AddMessage("\124cff00FF00[KillTrack]\124r " .. msg)
end
---@param msg string
function KT:DebugMsg(msg)
if not self.Debug then return end
self:Msg("[DEBUG] " .. msg)
end
---@param mob KillTrackMobData
function KT:KillAlert(mob)
local data = {
Text = ("%d kills on %s!"):format(mob.Kills, mob.Name),
Title = "Kill Record!",
bTitle = "Congratulations!",
Icon = "Interface\\Icons\\ABILITY_Deathwing_Bloodcorruption_Death",
FrameStyle = "GuildAchievement"
}
if C_AddOns.IsAddOnLoaded("Glamour") then
if not _G["GlamourShowAlert"] then
self:Msg("ERROR: GlamourShowAlert == nil! Notify AddOn developer.")
return
end
_G.GlamourShowAlert(500, data)
else
RaidNotice_AddMessage(RaidBossEmoteFrame, data.Text, ChatTypeInfo.SYSTEM)
RaidNotice_AddMessage(RaidBossEmoteFrame, data.Text, ChatTypeInfo.SYSTEM)
end
self:Msg(data.Text)
end
---@param id integer|string
---@return KillTrackMobData|false
---@return KillTrackCharMobData|nil
function KT:GetMob(id)
for k,v in pairs(self.Global.MOBS) do
if type(v) == "table" and (tostring(k) == tostring(id) or v.Name == id) then
return v, self.CharGlobal.MOBS[k]
end
end
return false, nil
end
---@param mode KillTrackMobSortMode|integer?
---@param filter string?
---@param caseSensitive boolean|nil
---@return { Id: integer, Name: string, gKills: integer, cKills: integer }[]
function KT:GetSortedMobTable(mode, filter, caseSensitive)
if not tonumber(mode) then mode = self.Sort.Desc end
if mode < 0 or mode > 7 then mode = self.Sort.Desc end
if filter and filter == "" then filter = nil end
local t = {}
for k,v in pairs(self.Global.MOBS) do
assert(type(v) == "table", "Unexpected mob entry type in db: " .. type(v) .. ". Expected table")
local matches = nil
if filter then
local name = caseSensitive and v.Name or v.Name:lower()
filter = caseSensitive and filter or filter:lower()
local status, result = pcall(string.match, name, filter)
matches = status and result
end
if matches or not filter then
local cKills = 0
if self.CharGlobal.MOBS[k] and type(self.CharGlobal.MOBS[k]) == "table" then
cKills = self.CharGlobal.MOBS[k].Kills
end
local entry = {Id = k, Name = v.Name, gKills = v.Kills, cKills = cKills}
table.insert(t, entry)
end
end
local function compare(a, b)
if mode == self.Sort.Asc then
return a.gKills < b.gKills
elseif mode == self.Sort.CharDesc then
return a.cKills > b.cKills
elseif mode == self.Sort.CharAsc then
return a.cKills < b.cKills
elseif mode == self.Sort.AlphaD then
return a.Name > b.Name
elseif mode == self.Sort.AlphaA then
return a.Name < b.Name
elseif mode == self.Sort.IdDesc then
return a.Id > b.Id
elseif mode == self.Sort.IdAsc then
return a.Id < b.Id
else
return a.gKills > b.gKills -- Descending
end
end
table.sort(t, compare)
return t
end
---@param id integer|string
---@param charOnly boolean?
function KT:Delete(id, charOnly)
id = tonumber(id) --[[@as integer]]
if not id then error(("Expected 'id' param to be number, got %s."):format(type(id))) end
local found = false
local name
if self.Global.MOBS[id] then
name = self.Global.MOBS[id].Name
if not charOnly then self.Global.MOBS[id] = nil end
if self.CharGlobal.MOBS[id] then
self.CharGlobal.MOBS[id] = nil
end
found = true
end
if found then
self:Msg(("Deleted %q (%d) from database."):format(name, id))
StaticPopup_Show("KILLTRACK_FINISH", 1)
else
self:Msg(("ID: %d was not found in the database."):format(id))
end
end
---@param threshold integer
function KT:Purge(threshold)
local count = 0
for k,v in pairs(self.Global.MOBS) do
if type(v) == "table" and v.Kills < threshold then
self.Global.MOBS[k] = nil
count = count + 1
end
end
for k,v in pairs(self.CharGlobal.MOBS) do
if type(v) == "table" and v.Kills < threshold then
self.CharGlobal.MOBS[k] = nil
count = count + 1
end
end
self:Msg(("Purged %d entries with a kill count below %d"):format(count, threshold))
self.Temp.Threshold = nil
StaticPopup_Show("KILLTRACK_FINISH", tostring(count))
end
function KT:Reset()
local count = #self.Global.MOBS + #self.CharGlobal.MOBS
wipe(self.Global.MOBS)
wipe(self.CharGlobal.MOBS)
self:Msg(("%d mob entries have been removed!"):format(count))
StaticPopup_Show("KILLTRACK_FINISH", tostring(count))
end
function KT:ResetAchievCount()
for _,v in pairs(self.Global.MOBS) do
v.AchievCount = floor(v.Kills / self.Global.ACHIEV_THRESHOLD)
end
end
KT.Frame = CreateFrame("Frame")
for k,_ in pairs(KT.Events) do
KT.Frame:RegisterEvent(k)
end
KT.Frame:SetScript("OnEvent", function(_, event, ...) KT:OnEvent(_, event, ...) end)