Repository navigation
Expand file tree
/
Copy pathSimpleMeters_Combat.lua
More file actions
1531 lines (1311 loc) · 44.2 KB
/
Copy pathSimpleMeters_Combat.lua
File metadata and controls
1531 lines (1311 loc) · 44.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
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- SimpleMeters v0.5
-- Build date: 2026-07-18
local addon = _G.SimpleMeters
if not addon then
return
end
local bitmod = bit
local band = bitmod.band
local bor = bitmod.bor
local pairs = pairs
local max = math.max
local sort = table.sort
local table_insert = table.insert
local table_remove = table.remove
local type = type
local tostring = tostring
local strsub = string.sub
local UnitExists = UnitExists
local UnitGUID = UnitGUID
local UnitName = UnitName
local UnitClass = UnitClass
local IsInRaid = IsInRaid
local GetNumGroupMembers = GetNumGroupMembers
local GetNumSubgroupMembers = GetNumSubgroupMembers
local GetTime = GetTime
local date = date
local CombatLogGetCurrentEventInfo = CombatLogGetCurrentEventInfo
local GetPlayerInfoByGUID = GetPlayerInfoByGUID
local GetSpellTexture = GetSpellTexture
local UNKNOWNOBJECT = UNKNOWNOBJECT or "Unknown"
local AFFILIATION_MASK = bor(
COMBATLOG_OBJECT_AFFILIATION_MINE,
COMBATLOG_OBJECT_AFFILIATION_PARTY,
COMBATLOG_OBJECT_AFFILIATION_RAID
)
local PET_TYPE_MASK = bor(COMBATLOG_OBJECT_TYPE_PET or 0, COMBATLOG_OBJECT_TYPE_GUARDIAN or 0)
local SELF_DAMAGE_SPELLS = {
[32379] = true, -- Shadow Word: Death
[129176] = true, -- Shadow Word: Death
}
local FIGHT_END_DELAY = 3
local FIGHT_FINALIZE_FAILSAFE = 30
local ENABLE_DEBUG_INJECT = false
local PERSIST_VERSION = 1
local PERSIST_SAVE_THROTTLE = 5
local ICON_MELEE = "Interface\\Icons\\INV_Sword_04"
local ICON_ENV = "Interface\\Icons\\Ability_Creature_Cursed_02"
local ICON_PET = "Interface\\Icons\\Ability_Hunter_BeastCall"
local ICON_UNKNOWN = "Interface\\Icons\\INV_Misc_QuestionMark"
local state = addon.state or {}
addon.state = state
state.actors = state.actors or {}
state.rosterClassByGUID = state.rosterClassByGUID or {}
state.rosterColorByGUID = state.rosterColorByGUID or {}
state.rosterNameByGUID = state.rosterNameByGUID or {}
state.petOwnerByGUID = state.petOwnerByGUID or {}
state.pendingGUIDLookupSet = state.pendingGUIDLookupSet or state.pendingGUIDLookup or {}
state.pendingGUIDLookupQueue = state.pendingGUIDLookupQueue or {}
state.pendingGUIDLookupHead = tonumber(state.pendingGUIDLookupHead) or 1
state.pendingGUIDLookupTail = tonumber(state.pendingGUIDLookupTail) or 0
state.pendingGUIDLookup = state.pendingGUIDLookupSet
state.guidLookupRetryAt = state.guidLookupRetryAt or {}
state.spellMeta = state.spellMeta or {}
state.persistDirtyActors = state.persistDirtyActors or {}
state.persistDirtySpellMeta = state.persistDirtySpellMeta or {}
state.persistBossHistoryDirty = state.persistBossHistoryDirty == true
state.bossHistory = state.bossHistory or {}
state.activeBossGUIDs = state.activeBossGUIDs or {}
state.pendingBossKills = state.pendingBossKills or {}
state.nextBossEntryId = state.nextBossEntryId or 1
state.selectedBossId = state.selectedBossId
state.lastBossKillName = state.lastBossKillName
state.lastBossKillTime = state.lastBossKillTime or 0
state.maxBossHistory = state.maxBossHistory or 24
state.groupMemberGUIDs = state.groupMemberGUIDs or {}
state.groupSnapshotReady = state.groupSnapshotReady == true
state.dirty = state.dirty ~= false
state.dataDirty = state.dataDirty == true or state.dirty
state.inFight = state.inFight == true
state.awaitingFightEnd = state.awaitingFightEnd == true
state.fightEndAt = state.fightEndAt or 0
state.pendingFinalizeAt = state.pendingFinalizeAt or 0
state.oocConfirmedAt = state.oocConfirmedAt or 0
state.lastRelevantTime = state.lastRelevantTime or 0
state.lastDamageTime = state.lastDamageTime or 0
state.fightId = state.fightId or 0
state.fightStartTime = state.fightStartTime or 0
state.fightActiveTime = state.fightActiveTime or 0
state.lastFightDuration = state.lastFightDuration or 1
state.totalActiveTime = state.totalActiveTime or 0
state.totalDamage = state.totalDamage or 0
state.uiDirty = state.uiDirty == true
state.renderDirty = state.renderDirty == true or state.uiDirty
state.nextTotalRefreshAt = state.nextTotalRefreshAt or 0
state.resetAt = state.resetAt or GetTime()
state.persistDirty = state.persistDirty == true
state.lastPersistAt = tonumber(state.lastPersistAt) or 0
if state.pendingGUIDLookupTail < state.pendingGUIDLookupHead then
state.pendingGUIDLookupHead = 1
state.pendingGUIDLookupTail = 0
end
if state.pendingGUIDLookupTail == 0 then
for guid in pairs(state.pendingGUIDLookupSet) do
state.pendingGUIDLookupTail = state.pendingGUIDLookupTail + 1
state.pendingGUIDLookupQueue[state.pendingGUIDLookupTail] = guid
end
state.pendingGUIDLookupHead = 1
end
local function ClearTable(tbl)
for key in pairs(tbl) do
tbl[key] = nil
end
end
local function MarkDataDirty()
state.dataDirty = true
state.dirty = true
end
local function MarkRenderDirty()
state.renderDirty = true
state.uiDirty = true
end
local function ClearPendingFinalize()
state.awaitingFightEnd = false
state.fightEndAt = 0
state.pendingFinalizeAt = 0
state.oocConfirmedAt = 0
end
local function IsPlayerGUID(guid)
return guid and strsub(guid, 1, 7) == "Player-"
end
local function QueueGUIDLookup(guid)
if not guid or state.pendingGUIDLookupSet[guid] then
return
end
local retryAt = state.guidLookupRetryAt[guid]
if retryAt and retryAt > GetTime() then
return
end
-- Class fallback is only meaningful for player GUIDs.
if not IsPlayerGUID(guid) then
return
end
state.pendingGUIDLookupSet[guid] = true
state.pendingGUIDLookup = state.pendingGUIDLookupSet
state.pendingGUIDLookupTail = state.pendingGUIDLookupTail + 1
state.pendingGUIDLookupQueue[state.pendingGUIDLookupTail] = guid
end
local function IsGroupGUID(guid)
return guid and state.groupMemberGUIDs and state.groupMemberGUIDs[guid] == true
end
local function IsAffiliated(flags)
return flags and band(flags, AFFILIATION_MASK) ~= 0
end
local function IsPetOrGuardian(flags)
return flags and band(flags, PET_TYPE_MASK) ~= 0
end
local function IsShadowWordDeath(spellId, spellName)
if spellId and SELF_DAMAGE_SPELLS[spellId] then
return true
end
return spellName == "Shadow Word: Death"
end
local function IsFriendlyDamage(srcGUID, srcFlags, destGUID, destFlags, spellId, spellName)
if srcGUID and destGUID and srcGUID == destGUID then
return true
end
if IsShadowWordDeath(spellId, spellName) and IsGroupGUID(destGUID) then
return true
end
local sourceOwnerGUID = state.petOwnerByGUID[srcGUID]
local sourceIsKnownFriendly = IsGroupGUID(srcGUID) or IsGroupGUID(sourceOwnerGUID) or (IsPlayerGUID(srcGUID) and IsAffiliated(srcFlags))
local destIsKnownFriendly = IsGroupGUID(destGUID) or IsAffiliated(destFlags)
return sourceIsKnownFriendly and destIsKnownFriendly
end
local function ResolveTrackedSource(srcGUID, srcName, srcFlags)
if not srcGUID or not srcFlags then
return nil
end
local ownerGUID = state.petOwnerByGUID[srcGUID]
if ownerGUID then
if IsGroupGUID(ownerGUID) or IsAffiliated(srcFlags) then
return ownerGUID, srcName
end
return nil
end
if IsPetOrGuardian(srcFlags) then
-- Keep orphan pet/guardian events out of rankings until owner mapping is known.
return nil
end
if IsGroupGUID(srcGUID) or (IsPlayerGUID(srcGUID) and IsAffiliated(srcFlags)) then
return srcGUID, nil
end
return nil
end
local function BuildGroupDiff(oldMap, newMap)
local joined = 0
local left = 0
for guid in pairs(newMap) do
if not oldMap[guid] then
joined = joined + 1
end
end
for guid in pairs(oldMap) do
if not newMap[guid] then
left = left + 1
end
end
return (joined > 0 or left > 0), joined, left
end
local function EnsureSpellMeta(key, name, icon, spellId)
local meta = state.spellMeta[key]
local changed = false
if not meta then
meta = {
name = name,
icon = icon,
spellId = spellId,
}
state.spellMeta[key] = meta
changed = true
else
if name and not meta.name then
meta.name = name
changed = true
end
if icon and not meta.icon then
meta.icon = icon
changed = true
end
if spellId and not meta.spellId then
meta.spellId = spellId
changed = true
end
end
if changed then
state.persistDirtySpellMeta[key] = true
end
return meta
end
local function EnsureFightMaps(actor)
if actor.lastSeenFightId ~= state.fightId then
actor.fightDamage = 0
actor.lastSeenFightId = state.fightId
ClearTable(actor.spellFight)
ClearTable(actor.petFight)
ClearTable(actor.petSpellFight)
end
end
local function GetOrCreateActor(guid, fallbackName)
local actor = state.actors[guid]
if actor then
actor.guid = actor.guid or guid
if fallbackName and (not actor.name or actor.name == UNKNOWNOBJECT) then
actor.name = fallbackName
end
if not actor.petSpellTotal then
actor.petSpellTotal = {}
end
if not actor.petSpellFight then
actor.petSpellFight = {}
end
if not actor.classFile then
local classFile = state.rosterClassByGUID[guid]
if classFile then
actor.classFile = classFile
actor.color = state.rosterColorByGUID[guid]
else
QueueGUIDLookup(guid)
end
end
return actor
end
local classFile = state.rosterClassByGUID[guid]
actor = {
guid = guid,
name = state.rosterNameByGUID[guid] or fallbackName or UNKNOWNOBJECT,
classFile = classFile,
color = classFile and state.rosterColorByGUID[guid] or nil,
totalDamage = 0,
fightDamage = 0,
lastSeenFightId = 0,
spellTotal = {},
spellFight = {},
petTotal = {},
petFight = {},
petSpellTotal = {},
petSpellFight = {},
}
state.actors[guid] = actor
if not classFile then
QueueGUIDLookup(guid)
end
return actor
end
local function AddSpellDamage(actor, spellKey, spellName, icon, spellId, amount)
if not spellKey then
return
end
local totalMap = actor.spellTotal
totalMap[spellKey] = (totalMap[spellKey] or 0) + amount
local fightMap = actor.spellFight
fightMap[spellKey] = (fightMap[spellKey] or 0) + amount
EnsureSpellMeta(spellKey, spellName, icon, spellId)
end
local function AddPetDamage(actor, petName, amount)
if not petName then
return
end
local totalMap = actor.petTotal
totalMap[petName] = (totalMap[petName] or 0) + amount
local fightMap = actor.petFight
fightMap[petName] = (fightMap[petName] or 0) + amount
end
local function AddPetSpellDamage(actor, spellKey, petName, amount)
if not petName or not spellKey then
return
end
local totalBySpell = actor.petSpellTotal[spellKey]
if not totalBySpell then
totalBySpell = {}
actor.petSpellTotal[spellKey] = totalBySpell
end
totalBySpell[petName] = (totalBySpell[petName] or 0) + amount
local fightBySpell = actor.petSpellFight[spellKey]
if not fightBySpell then
fightBySpell = {}
actor.petSpellFight[spellKey] = fightBySpell
end
fightBySpell[petName] = (fightBySpell[petName] or 0) + amount
end
local function EffectiveDamage(amount, overkill)
amount = tonumber(amount) or 0
overkill = tonumber(overkill) or 0
if overkill < 0 then
overkill = 0
end
amount = amount - overkill
if amount < 0 then
return 0
end
return amount
end
local function AddDamage(actor, amount, spellKey, spellName, spellIcon, spellId, petName)
if not amount or amount <= 0 then
return
end
actor.totalDamage = actor.totalDamage + amount
state.totalDamage = (state.totalDamage or 0) + amount
EnsureFightMaps(actor)
actor.fightDamage = actor.fightDamage + amount
AddSpellDamage(actor, spellKey, spellName, spellIcon, spellId, amount)
AddPetDamage(actor, petName, amount)
AddPetSpellDamage(actor, spellKey, petName, amount)
state.persistDirty = true
state.persistDirtyActors[actor.guid] = true
MarkDataDirty()
end
-- SWING_DAMAGE payload offsets (after destRaidFlags):
-- a1=amount, a2=overkill, a3=school, a4=resisted, a5=blocked
local function HandleSWING_DAMAGE(actor, _, _, _, petName, a1, a2)
local amount = EffectiveDamage(a1, a2)
AddDamage(actor, amount, 0, "Melee", ICON_MELEE, nil, petName)
end
-- SPELL_DAMAGE / SPELL_PERIODIC_DAMAGE / RANGE_DAMAGE payload offsets:
-- a1=spellId, a2=spellName, a3=spellSchool, a4=amount, a5=overkill
local function HandleSPELL_DAMAGE(actor, _, _, _, petName, a1, a2, _, a4, a5)
local amount = EffectiveDamage(a4, a5)
AddDamage(actor, amount, a1, a2, nil, a1, petName)
end
local ENV_KEY_BY_TYPE = {}
local ENV_NAME_BY_TYPE = {}
-- ENVIRONMENTAL_DAMAGE payload offsets:
-- a1=environmentalType, a2=amount, a3=overkill, a4=school, a5=resisted
local function HandleENVIRONMENTAL_DAMAGE(actor, _, _, _, petName, a1, a2, a3)
local envType = a1 or "Unknown"
local envName = ENV_NAME_BY_TYPE[envType]
if not envName then
envName = tostring(envType)
ENV_NAME_BY_TYPE[envType] = envName
end
local key = ENV_KEY_BY_TYPE[envType]
if not key then
key = "ENV:" .. envName
ENV_KEY_BY_TYPE[envType] = key
end
local amount = EffectiveDamage(a2, a3)
AddDamage(actor, amount, key, envName, ICON_ENV, nil, petName)
end
local SUBEVENT_DISPATCH = {
SWING_DAMAGE = HandleSWING_DAMAGE,
SPELL_DAMAGE = HandleSPELL_DAMAGE,
SPELL_PERIODIC_DAMAGE = HandleSPELL_DAMAGE,
RANGE_DAMAGE = HandleSPELL_DAMAGE,
DAMAGE_SHIELD = HandleSPELL_DAMAGE,
ENVIRONMENTAL_DAMAGE = HandleENVIRONMENTAL_DAMAGE,
}
local DAMAGE_SUBEVENTS = {
SWING_DAMAGE = true,
SPELL_DAMAGE = true,
SPELL_PERIODIC_DAMAGE = true,
RANGE_DAMAGE = true,
DAMAGE_SHIELD = true,
ENVIRONMENTAL_DAMAGE = true,
}
local function UpdateUnitCache(unit)
if not UnitExists(unit) then
return nil
end
local guid = UnitGUID(unit)
if not guid then
return nil
end
local name = UnitName(unit)
if name then
state.rosterNameByGUID[guid] = name
end
local _, classFile = UnitClass(unit)
if classFile then
state.rosterClassByGUID[guid] = classFile
state.rosterColorByGUID[guid] = addon:GetClassColorTable(classFile)
end
local actor = state.actors[guid]
if actor then
local actorChanged = false
if name and actor.name ~= name then
actor.name = name
actorChanged = true
end
if classFile and actor.classFile ~= classFile then
actor.classFile = classFile
actor.color = state.rosterColorByGUID[guid]
actorChanged = true
end
if actorChanged then
state.persistDirtyActors[guid] = true
state.persistDirty = true
end
end
return guid
end
local function MapPetOwner(petUnit, ownerUnit)
if not UnitExists(petUnit) then
return
end
local petGUID = UnitGUID(petUnit)
local ownerGUID = UnitGUID(ownerUnit)
if petGUID and ownerGUID then
state.petOwnerByGUID[petGUID] = ownerGUID
end
end
local function CompareSnapshotActors(a, b)
if a.damage == b.damage then
return (a.name or UNKNOWNOBJECT) < (b.name or UNKNOWNOBJECT)
end
return a.damage > b.damage
end
local function CopyDamageMap(src)
local copy = {}
for key, value in pairs(src or {}) do
copy[key] = value
end
return copy
end
local function CopyNestedDamageMap(src)
local copy = {}
for key, value in pairs(src or {}) do
local nested = {}
for subKey, amount in pairs(value or {}) do
nested[subKey] = amount
end
copy[key] = nested
end
return copy
end
local function GetFightEndTimestamp(now)
now = now or GetTime()
local startTime = state.fightStartTime or 0
local endTime
if state.inFight then
endTime = state.lastDamageTime
else
endTime = state.lastDamageTime
if not endTime or endTime <= 0 then
endTime = state.lastRelevantTime
end
end
if not endTime or endTime <= 0 then
endTime = now
end
if startTime > 0 and endTime < startTime then
endTime = now
end
return endTime
end
local function BuildBossSnapshot(now)
local duration
if state.inFight and state.fightStartTime > 0 then
duration = GetFightEndTimestamp(now) - state.fightStartTime
else
duration = state.lastFightDuration or 1
end
duration = max(1, duration)
local actors = {}
local totalDamage = 0
for guid, actor in pairs(state.actors) do
if actor.lastSeenFightId == state.fightId and (actor.fightDamage or 0) > 0 then
local damage = actor.fightDamage
totalDamage = totalDamage + damage
actors[#actors + 1] = {
guid = guid,
name = actor.name or UNKNOWNOBJECT,
classFile = actor.classFile,
damage = damage,
spells = CopyDamageMap(actor.spellFight),
petSpells = CopyNestedDamageMap(actor.petSpellFight),
pets = CopyDamageMap(actor.petFight),
}
end
end
if #actors > 1 then
sort(actors, CompareSnapshotActors)
end
return actors, totalDamage, duration
end
local function RecordBossKillNow(bossName, bossGUID, now)
if bossGUID and state.activeBossGUIDs[bossGUID] then
bossName = state.activeBossGUIDs[bossGUID]
state.activeBossGUIDs[bossGUID] = nil
end
bossName = bossName or UNKNOWNOBJECT
now = now or GetTime()
if state.lastBossKillName == bossName and (now - state.lastBossKillTime) < 2 then
return
end
state.lastBossKillName = bossName
state.lastBossKillTime = now
local actors, totalDamage, duration = BuildBossSnapshot(now)
local entry = {
id = state.nextBossEntryId,
name = bossName,
fightId = state.fightId,
totalDamage = totalDamage,
duration = duration,
time = date("%H:%M"),
actors = actors,
}
state.nextBossEntryId = state.nextBossEntryId + 1
table_insert(state.bossHistory, 1, entry)
if #state.bossHistory > state.maxBossHistory then
table_remove(state.bossHistory)
end
if not state.selectedBossId then
state.selectedBossId = entry.id
end
state.persistDirty = true
state.persistBossHistoryDirty = true
MarkDataDirty()
MarkRenderDirty()
end
function addon:InitializeCombat()
EnsureSpellMeta(0, "Melee", ICON_MELEE, nil)
state.resetAt = state.resetAt or GetTime()
MarkDataDirty()
end
function addon:ClearPersistedCombat()
if self.db then
self.db.combatPersist = nil
end
ClearTable(state.persistDirtyActors)
ClearTable(state.persistDirtySpellMeta)
state.persistBossHistoryDirty = false
state.persistDirty = false
state.lastPersistAt = 0
end
function addon:SavePersistedCombat(force)
if not self.db then
return
end
local now = GetTime()
if not force then
if not state.persistDirty then
return
end
if state.lastPersistAt > 0 and (now - state.lastPersistAt) < PERSIST_SAVE_THROTTLE then
return
end
end
local persistedActiveTime = state.totalActiveTime or 0
if state.inFight and state.fightStartTime > 0 then
local activeNow = GetFightEndTimestamp(now) - state.fightStartTime
if activeNow > 0 then
persistedActiveTime = persistedActiveTime + activeNow
end
end
local persist = self.db.combatPersist
local fullSync = force or type(persist) ~= "table" or persist.version ~= PERSIST_VERSION
if fullSync then
persist = {
version = PERSIST_VERSION,
spellMeta = {},
actors = {},
bossHistory = {},
}
self.db.combatPersist = persist
for key in pairs(state.spellMeta) do
state.persistDirtySpellMeta[key] = true
end
for guid in pairs(state.actors) do
state.persistDirtyActors[guid] = true
end
state.persistBossHistoryDirty = true
end
persist.spellMeta = type(persist.spellMeta) == "table" and persist.spellMeta or {}
persist.actors = type(persist.actors) == "table" and persist.actors or {}
persist.bossHistory = type(persist.bossHistory) == "table" and persist.bossHistory or {}
persist.resetAt = state.resetAt or GetTime()
persist.totalActiveTime = persistedActiveTime
persist.totalDamage = state.totalDamage or 0
persist.nextBossEntryId = state.nextBossEntryId or 1
persist.selectedBossId = state.selectedBossId
for key in pairs(state.persistDirtySpellMeta) do
local meta = state.spellMeta[key]
if type(meta) == "table" then
persist.spellMeta[key] = {
name = meta.name,
icon = meta.icon,
spellId = meta.spellId,
}
else
persist.spellMeta[key] = nil
end
end
for guid in pairs(state.persistDirtyActors) do
local actor = state.actors[guid]
local totalDamage = actor and tonumber(actor.totalDamage) or 0
if actor and totalDamage > 0 then
persist.actors[guid] = {
name = actor.name,
classFile = actor.classFile,
totalDamage = totalDamage,
spellTotal = CopyDamageMap(actor.spellTotal),
petTotal = CopyDamageMap(actor.petTotal),
petSpellTotal = CopyNestedDamageMap(actor.petSpellTotal),
}
else
persist.actors[guid] = nil
end
end
if state.persistBossHistoryDirty then
ClearTable(persist.bossHistory)
for i = 1, #state.bossHistory do
local entry = state.bossHistory[i]
if type(entry) == "table" then
local savedEntry = {
id = entry.id,
name = entry.name,
fightId = entry.fightId,
totalDamage = entry.totalDamage,
duration = entry.duration,
time = entry.time,
actors = {},
}
for j = 1, #(entry.actors or {}) do
local actor = entry.actors[j]
if type(actor) == "table" then
savedEntry.actors[#savedEntry.actors + 1] = {
guid = actor.guid,
name = actor.name,
classFile = actor.classFile,
damage = actor.damage,
spells = CopyDamageMap(actor.spells),
petSpells = CopyNestedDamageMap(actor.petSpells),
pets = CopyDamageMap(actor.pets),
}
end
end
persist.bossHistory[#persist.bossHistory + 1] = savedEntry
end
end
end
ClearTable(state.persistDirtyActors)
ClearTable(state.persistDirtySpellMeta)
state.persistBossHistoryDirty = false
state.persistDirty = false
state.lastPersistAt = now
end
function addon:RestorePersistedCombat()
if not self.db then
return
end
local persist = self.db.combatPersist
if type(persist) ~= "table" then
return
end
local persistVersion = tonumber(persist.version) or 0
if persistVersion ~= PERSIST_VERSION then
-- Drop incompatible snapshots to avoid stale/corrupt restore state.
self.db.combatPersist = nil
return
end
ClearTable(state.actors)
ClearTable(state.spellMeta)
ClearTable(state.bossHistory)
ClearTable(state.activeBossGUIDs)
ClearTable(state.pendingBossKills)
ClearTable(state.pendingGUIDLookupSet)
ClearTable(state.pendingGUIDLookupQueue)
ClearTable(state.guidLookupRetryAt)
state.pendingGUIDLookupHead = 1
state.pendingGUIDLookupTail = 0
state.pendingGUIDLookup = state.pendingGUIDLookupSet
ClearTable(state.persistDirtyActors)
ClearTable(state.persistDirtySpellMeta)
state.persistBossHistoryDirty = false
state.inFight = false
ClearPendingFinalize()
state.lastRelevantTime = 0
state.lastDamageTime = 0
state.fightStartTime = 0
state.fightActiveTime = 0
state.lastFightDuration = 1
state.resetAt = tonumber(persist.resetAt) or GetTime()
state.totalActiveTime = max(0, tonumber(persist.totalActiveTime) or 0)
state.totalDamage = max(0, tonumber(persist.totalDamage) or 0)
state.nextBossEntryId = max(1, tonumber(persist.nextBossEntryId) or 1)
state.selectedBossId = tonumber(persist.selectedBossId)
state.nextTotalRefreshAt = 0
for key, meta in pairs(persist.spellMeta or {}) do
if type(meta) == "table" then
state.spellMeta[key] = {
name = meta.name,
icon = meta.icon,
spellId = meta.spellId,
}
end
end
EnsureSpellMeta(0, "Melee", ICON_MELEE, nil)
local recomputedTotal = 0
for guid, savedActor in pairs(persist.actors or {}) do
if type(savedActor) == "table" then
local classFile = savedActor.classFile
local actor = {
guid = guid,
name = savedActor.name or state.rosterNameByGUID[guid] or UNKNOWNOBJECT,
classFile = classFile,
color = classFile and addon:GetClassColorTable(classFile) or nil,
totalDamage = max(0, tonumber(savedActor.totalDamage) or 0),
fightDamage = 0,
lastSeenFightId = state.fightId,
spellTotal = CopyDamageMap(savedActor.spellTotal),
spellFight = {},
petTotal = CopyDamageMap(savedActor.petTotal),
petFight = {},
petSpellTotal = CopyNestedDamageMap(savedActor.petSpellTotal),
petSpellFight = {},
}
recomputedTotal = recomputedTotal + actor.totalDamage
state.actors[guid] = actor
end
end
if state.totalDamage <= 0 and recomputedTotal > 0 then
state.totalDamage = recomputedTotal
end
for i = 1, #(persist.bossHistory or {}) do
local entry = persist.bossHistory[i]
if type(entry) == "table" then
local restored = {
id = tonumber(entry.id) or i,
name = entry.name or UNKNOWNOBJECT,
fightId = tonumber(entry.fightId) or 0,
totalDamage = max(0, tonumber(entry.totalDamage) or 0),
duration = max(1, tonumber(entry.duration) or 1),
time = entry.time,
actors = {},
}
for j = 1, #(entry.actors or {}) do
local actor = entry.actors[j]
if type(actor) == "table" then
restored.actors[#restored.actors + 1] = {
guid = actor.guid,
name = actor.name or UNKNOWNOBJECT,
classFile = actor.classFile,
damage = max(0, tonumber(actor.damage) or 0),
spells = CopyDamageMap(actor.spells),
petSpells = CopyNestedDamageMap(actor.petSpells),
pets = CopyDamageMap(actor.pets),
}
end
end
state.bossHistory[#state.bossHistory + 1] = restored
if restored.id >= state.nextBossEntryId then
state.nextBossEntryId = restored.id + 1
end
end
end
if not state.selectedBossId and #state.bossHistory > 0 then
state.selectedBossId = state.bossHistory[1].id
end
state.persistDirty = false
state.lastPersistAt = 0
MarkDataDirty()
MarkRenderDirty()
end
function addon:RefreshRosterCache()
local newGroupMembers = {}
ClearTable(state.rosterClassByGUID)
ClearTable(state.rosterColorByGUID)
ClearTable(state.rosterNameByGUID)
ClearTable(state.petOwnerByGUID)
local guid = UpdateUnitCache("player")
if guid then
newGroupMembers[guid] = true
end
MapPetOwner("pet", "player")
if IsInRaid() then
local count = GetNumGroupMembers()
for i = 1, count do
local unit = "raid" .. i
guid = UpdateUnitCache(unit)
if guid then
newGroupMembers[guid] = true
end
MapPetOwner("raidpet" .. i, unit)
end
else
local count = GetNumSubgroupMembers()
for i = 1, count do
local unit = "party" .. i
guid = UpdateUnitCache(unit)
if guid then
newGroupMembers[guid] = true
end
MapPetOwner("partypet" .. i, unit)
end
end
local changed, joined, left = false, 0, 0
if state.groupSnapshotReady then
changed, joined, left = BuildGroupDiff(state.groupMemberGUIDs, newGroupMembers)
end
state.groupMemberGUIDs = newGroupMembers
state.groupSnapshotReady = true
MarkRenderDirty()
return changed, joined, left
end
function addon:StartFight(now)
now = now or GetTime()
if state.inFight then
state.lastRelevantTime = now
ClearPendingFinalize()
return
end
state.inFight = true
ClearPendingFinalize()
state.lastRelevantTime = now
state.lastDamageTime = now
state.fightStartTime = now
state.fightActiveTime = 0
state.lastFightDuration = 1
state.fightId = state.fightId + 1
MarkDataDirty()
MarkRenderDirty()