-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArenaRoundTracker.lua
More file actions
1192 lines (1062 loc) · 51.8 KB
/
ArenaRoundTracker.lua
File metadata and controls
1192 lines (1062 loc) · 51.8 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
local _, ns = ...
local Constants = ns.Constants
local ApiCompat = ns.ApiCompat
local Helpers = ns.Helpers
-- ArenaRoundTracker
-- Owns match and round identity independently of the combat session lifecycle.
-- Blizzard fires ARENA_OPPONENT_UPDATE and ARENA_PREP_OPPONENT_SPECIALIZATIONS
-- before combat begins and between rounds — CombatTracker's session does not
-- exist at those points. This module bridges that gap.
--
-- Responsibilities:
-- • Maintain stable matchKey and roundKey across multi-round arenas
-- • Preserve enemy slot state through "unseen" transitions
-- • Stage unresolved GUIDs from CLEU before unit frames become visible
-- • Compute weighted pressure scores for primaryOpponent selection
-- • Copy final round metadata into session.arena at FinalizeSession time
local ArenaRoundTracker = {}
-- ──────────────────────────────────────────────────────────────────────────────
-- Local helpers
-- ──────────────────────────────────────────────────────────────────────────────
local function nowServer()
return ApiCompat.GetServerTime() or 0
end
local function nowPrecise()
return Helpers and Helpers.Now and Helpers.Now() or GetTime()
end
local function safeGetMapId()
if C_Map and C_Map.GetBestMapForUnit then
return C_Map.GetBestMapForUnit("player")
end
return nil
end
-- Parse "arena1".."arena5" → slot integer, nil otherwise.
local function parseArenaSlot(unitToken)
if type(unitToken) ~= "string" then return nil end
local n = unitToken:match("^arena(%d+)$")
return n and tonumber(n) or nil
end
local function shallowCopy(src)
local dst = {}
for k, v in pairs(src or {}) do dst[k] = v end
return dst
end
-- Maps a GUID to its arena slot via indexed lookup (round.guidToSlot), falling
-- back to a linear scan over all slots. Returns nil when no slot matches.
local function findSlotByGuid(round, guid)
if not round or not guid then return nil end
local indexed = round.guidToSlot and round.guidToSlot[guid]
if indexed and round.slots[indexed] and round.slots[indexed].guid == guid then
return round.slots[indexed]
end
for _, s in pairs(round.slots) do
if s.guid == guid then return s end
end
return nil
end
-- Initializes selection evidence on a slot if not already present. Idempotent.
local function ensureSelectionEvidence(slot)
if not slot.selectionEvidence then
slot.selectionEvidence = {
damageToPlayer = 0,
deathRecap = 0,
identityBias = 0,
visibilityBias = 0,
}
end
return slot.selectionEvidence
end
-- Build a snapshot from a live unit frame. Returns nil if unit not visible.
local function buildUnitSnapshot(unitToken)
if not ApiCompat.UnitExists(unitToken) then return nil end
local guid = ApiCompat.GetUnitGUID(unitToken)
if not guid then return nil end
local localClass, engClass, classId = ApiCompat.GetUnitClass(unitToken)
local localRace, engRace, raceId = ApiCompat.GetUnitRace(unitToken)
return {
guid = guid,
name = ApiCompat.GetUnitName(unitToken),
unitToken = unitToken,
className = localClass,
classFile = engClass,
classId = classId,
raceName = localRace,
raceFile = engRace,
raceId = raceId,
level = ApiCompat.GetUnitLevel(unitToken),
healthMax = ApiCompat.UnitHealthMax(unitToken),
capturedAt = nowPrecise(),
}
end
-- Known non-"unseen" reasons for ARENA_OPPONENT_UPDATE. Any value outside
-- this set is still treated as "visible" (safe fallback), but an unknown
-- reason is recorded in the trace log so it can be investigated.
local KNOWN_VISIBLE_UPDATE_REASONS = {
seen = true,
}
-- ──────────────────────────────────────────────────────────────────────────────
-- Key builders
-- ──────────────────────────────────────────────────────────────────────────────
local function buildMatchKey(matchRecord)
return table.concat({
"player=" .. tostring(ApiCompat.GetPlayerGUID() or "unknown"),
"map=" .. tostring(matchRecord.mapId or 0),
"ctx=" .. tostring(matchRecord.context or Constants.CONTEXT.ARENA),
"sub=" .. tostring(matchRecord.subcontext or "unknown"),
"joined=" .. tostring(matchRecord.joinedAt or 0),
}, "|")
end
-- Roster signature: sorted per-slot entries so the key is stable regardless
-- of slot query order. Incorporates GUID when visible, falls back to prep data.
local function buildRosterSignature(slots)
local rows = {}
for slot = 1, 5 do
local s = slots[slot]
if s then
rows[#rows + 1] = string.format("%d:%s:%s:%s",
slot,
s.guid or "?",
tostring(s.prepSpecId or 0),
s.classFile or "?"
)
end
end
table.sort(rows)
return table.concat(rows, "|")
end
local function buildRoundKey(matchKey, roundIndex, slots)
return string.format("%s|round=%d|roster=%s",
matchKey, roundIndex, buildRosterSignature(slots))
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Slot management
-- ──────────────────────────────────────────────────────────────────────────────
local function ensureSlot(round, slot)
if not round.slots[slot] then
round.slots[slot] = {
slot = slot,
unitToken = "arena" .. tostring(slot),
visible = false,
guid = nil,
name = nil,
className = nil,
classFile = nil,
classId = nil,
prepSpecId = nil,
prepSpecName = nil,
prepSpecIconId = nil,
prepRole = nil,
prepClassFile = nil,
damageToPlayer = 0,
damageTakenFromPlayer = 0,
ccOnPlayer = 0,
killParticipation = 0,
pressureScore = 0,
isDead = false,
updateHistory = {},
fieldConfidence = {},
}
end
return round.slots[slot]
end
local function refreshPressure(slot)
slot.pressureScore =
(slot.damageToPlayer or 0) * 0.45 +
(slot.damageTakenFromPlayer or 0) * 0.30 +
(slot.ccOnPlayer or 0) * 0.15 +
(slot.killParticipation or 0) * 0.10
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Module lifecycle
-- ──────────────────────────────────────────────────────────────────────────────
function ArenaRoundTracker:Initialize()
self.currentMatch = nil
self.inspectQueue = {}
self.inspectBusy = false
self.inspectElapsed = 0
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Match management
-- ──────────────────────────────────────────────────────────────────────────────
function ArenaRoundTracker:BeginMatch(context, subcontext)
local matchRecord = {
context = context or Constants.CONTEXT.ARENA,
subcontext = subcontext,
joinedAt = nowServer(),
mapId = safeGetMapId(),
playerGuid = ApiCompat.GetPlayerGUID(),
prepOpponents = {},
rounds = {},
currentRound = nil,
}
matchRecord.matchKey = buildMatchKey(matchRecord)
self.currentMatch = matchRecord
ns.Addon:Trace("arena_round.match.begin", {
context = matchRecord.context or "nil",
subcontext = matchRecord.subcontext or "nil",
matchKey = matchRecord.matchKey,
})
return matchRecord
end
function ArenaRoundTracker:GetCurrentMatch()
return self.currentMatch
end
function ArenaRoundTracker:GetCurrentRound()
return self.currentMatch and self.currentMatch.currentRound or nil
end
function ArenaRoundTracker:GetSlots()
local round = self:GetCurrentRound()
return round and round.slots or {}
end
function ArenaRoundTracker:EndMatch()
local matchRecord = self.currentMatch
if not matchRecord then return end
-- Close any still-active round (e.g. match went inactive without complete).
if matchRecord.currentRound then
self:EndRound("match_inactive", nil, nil)
end
ns.Addon:Trace("arena_round.match.end", {
rounds = #(matchRecord.rounds),
})
self.currentMatch = nil
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Round management
-- ──────────────────────────────────────────────────────────────────────────────
function ArenaRoundTracker:BeginRound(reason)
-- Lazily create match record if we somehow missed PLAYER_JOINED_PVP_MATCH.
local matchRecord = self.currentMatch
if not matchRecord then
matchRecord = self:BeginMatch(Constants.CONTEXT.ARENA, nil)
end
-- Guard: do not double-open a round.
local existing = matchRecord.currentRound
if existing and existing.state == "active" then
return existing
end
local roundIndex = #matchRecord.rounds + 1
local round = {
roundIndex = roundIndex,
state = "active",
completionState = "complete", -- T044: default; upgraded by EndRound
startReason = reason or "unknown",
startedAt = nowServer(),
slots = {},
guidToSlot = {},
unresolvedEnemyGuids = {},
markers = {},
}
-- Seed slot prep data from any previously captured ARENA_PREP specs.
local seedTs = nowServer()
for slot, prep in pairs(matchRecord.prepOpponents) do
local s = ensureSlot(round, slot)
s.prepSpecId = prep.specId
s.prepSpecName = prep.specName
s.prepSpecIconId = prep.specIconId
s.prepRole = prep.role
s.prepClassFile = prep.classFile
-- T040/T043: Set field confidence for prep-sourced fields.
s.fieldConfidence.spec = "prep"
s.fieldConfidence.specLearnedAt = seedTs
s.fieldConfidence.class = "prep"
s.fieldConfidence.classLearnedAt = seedTs
end
round.rosterSignature = buildRosterSignature(round.slots)
round.roundKey = buildRoundKey(matchRecord.matchKey, roundIndex, round.slots)
matchRecord.rounds[#matchRecord.rounds + 1] = round
matchRecord.currentRound = round
-- T027: Initialize UnitGraphService for this new round so prior-round actor
-- data is archived and the working set starts clean for this session.
local ugs = ns.Addon:GetModule("UnitGraphService")
if ugs and ugs.InitializeForSession then
pcall(ugs.InitializeForSession, ugs)
end
ns.Addon:Trace("arena_round.round.begin", {
reason = reason or "unknown",
roundIndex = roundIndex,
roundKey = round.roundKey,
})
return round
end
function ArenaRoundTracker:EndRound(reason, winner, duration)
local matchRecord = self.currentMatch
local round = matchRecord and matchRecord.currentRound or nil
if not round then return nil end
round.state = "complete"
round.endReason = reason or "unknown"
round.endedAt = nowServer()
round.winner = winner
round.duration = duration or (round.endedAt - (round.startedAt or round.endedAt))
-- Solo Shuffle round-level stat summary (T066-T071).
-- Captures healer identity, partner/opponent specs, and basic totals.
-- Per-round damage/healing is populated later by CopyStateIntoSession
-- from session event data. Here we capture roster composition.
if matchRecord.subcontext == Constants.SUBCONTEXT.SOLO_SHUFFLE then
round.soloShuffle = round.soloShuffle or {}
local archetypes = ns.StaticPvpData and ns.StaticPvpData.SPEC_ARCHETYPES
local partnerSpecs = {}
local opponentSpecs = {}
local healerClass = ""
local healerSpecId = nil
-- In solo shuffle, team composition rotates each round.
-- The roster is in slots; we detect the healer by checking
-- the spec archetype role for each slot.
if archetypes then
for _, s in pairs(round.slots) do
local sid = s.prepSpecId or s.specId
if sid then
local arch = archetypes[sid]
if arch and arch.role == "HEALER" then
healerClass = s.classFile or (arch and arch.classFile) or ""
healerSpecId = sid
end
end
end
end
-- Counters tab: populate the per-round enemy spec list so win rate vs
-- specs met only in Solo Shuffle rounds can be derived later. round.slots
-- holds only arena1-5 (enemy) slots, so every slot is an opponent — no
-- team filter needed. A slot whose spec cannot be resolved flags the
-- round specsIncomplete, so the win-rate scan skips it rather than
-- silently shrinking the sample.
local seenSpec = {}
local specsIncomplete = false
for _, s in pairs(round.slots) do
local sid = s.prepSpecId or s.specId
if sid then
if not seenSpec[sid] then
seenSpec[sid] = true
opponentSpecs[#opponentSpecs + 1] = sid
end
else
specsIncomplete = true
end
end
round.soloShuffle.healerClass = healerClass
round.soloShuffle.healerSpecId = healerSpecId
round.soloShuffle.partnerSpecs = partnerSpecs
round.soloShuffle.opponentSpecs = opponentSpecs
round.soloShuffle.specsIncomplete = specsIncomplete
end
-- T044: Determine completionState based on roster and disconnect signals.
local hasUnresolved = false
local hasDisconnect = false
for slot = 1, 5 do
local s = round.slots[slot]
if s then
-- A slot that was created but never became visible is unresolved.
if not s.visible and not s.guid then
hasUnresolved = true
end
-- Check update history for disconnect-related reasons.
for _, entry in ipairs(s.updateHistory or {}) do
if entry.reason == "disconnect" or entry.reason == "destroyed" then
hasDisconnect = true
end
end
end
end
-- Also treat unresolved enemy GUIDs as evidence of a leaver.
for _ in pairs(round.unresolvedEnemyGuids or {}) do
hasUnresolved = true
break
end
-- T087: Solo Shuffle DC handling — a full Solo Shuffle round requires 6 players.
-- If fewer GUIDs are present, the round is irregular (likely a disconnect).
local soloShuffleIncomplete = false
if matchRecord.subcontext == Constants.SUBCONTEXT.SOLO_SHUFFLE then
local guidCount = 0
for slot = 1, 6 do
local s = round.slots[slot]
if s and s.guid then
guidCount = guidCount + 1
end
end
if guidCount < 6 then
soloShuffleIncomplete = true
end
end
if hasDisconnect then
round.completionState = "partial_disconnect"
elseif soloShuffleIncomplete then
round.completionState = "partial_disconnect"
round.irregular = true
elseif hasUnresolved then
round.completionState = "partial_leaver"
else
round.completionState = "complete"
end
-- Recompute stable keys with full roster data now available.
round.rosterSignature = buildRosterSignature(round.slots)
round.roundKey = buildRoundKey(matchRecord.matchKey, round.roundIndex, round.slots)
-- T089: Show adaptation card between rounds in Solo Shuffle
if matchRecord.subcontext == Constants.SUBCONTEXT.SOLO_SHUFFLE then
local scoutService = ns.Addon:GetModule("ArenaScoutService")
local scoutView = ns.Addon:GetModule("ArenaScoutView")
if scoutService and scoutView then
local previousRoundSession = nil
-- Find the most recent finalized session from CombatStore
local store = ns.Addon:GetModule("CombatStore")
if store then
local sessions = store:GetRecentSessions(1)
previousRoundSession = sessions and sessions[1] or nil
end
local currentPrepState = { slots = round.slots, roundNumber = matchRecord.roundCount or 1 }
local ok, adaptCard = pcall(scoutService.BuildAdaptationCard, scoutService, previousRoundSession, currentPrepState)
if ok and adaptCard then
scoutView:ShowAdaptation(adaptCard)
end
end
end
matchRecord.currentRound = nil
ns.Addon:Trace("arena_round.round.end", {
duration = duration or 0,
reason = reason or "unknown",
roundIndex = round.roundIndex,
roundKey = round.roundKey,
winner = winner or "unknown",
})
return round
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Prep specialization capture
-- Called from CombatTracker:HandleArenaPrepOpponentSpecializations()
-- ──────────────────────────────────────────────────────────────────────────────
function ArenaRoundTracker:CapturePrepSpecs()
-- Lazily create match record; ARENA_PREP fires before PLAYER_JOINED_PVP_MATCH
-- in some queue flows.
local matchRecord = self.currentMatch
if not matchRecord then
matchRecord = self:BeginMatch(Constants.CONTEXT.ARENA, nil)
end
local count = ApiCompat.GetNumArenaOpponentSpecs()
for slot = 1, count do
local specId = ApiCompat.GetArenaOpponentSpec(slot)
if specId and specId > 0 then
local _, specName, _, specIcon, role, classFile =
ApiCompat.GetSpecializationInfoByID(specId)
matchRecord.prepOpponents[slot] = {
slot = slot,
observedAt = nowServer(),
specId = specId,
specName = specName,
specIconId = specIcon,
role = role,
classFile = classFile,
}
end
end
-- Also propagate into active round if one exists.
local round = matchRecord.currentRound
if round then
local ts = nowServer()
for slot, prep in pairs(matchRecord.prepOpponents) do
local s = ensureSlot(round, slot)
s.prepSpecId = s.prepSpecId or prep.specId
s.prepSpecName = s.prepSpecName or prep.specName
s.prepSpecIconId = s.prepSpecIconId or prep.specIconId
s.prepRole = s.prepRole or prep.role
s.prepClassFile = s.prepClassFile or prep.classFile
-- T040/T043: Set field confidence for prep-sourced fields.
s.fieldConfidence.spec = "prep"
s.fieldConfidence.specLearnedAt = ts
s.fieldConfidence.class = "prep"
s.fieldConfidence.classLearnedAt = ts
end
round.rosterSignature = buildRosterSignature(round.slots)
round.roundKey = buildRoundKey(matchRecord.matchKey, round.roundIndex, round.slots)
end
ns.Addon:Trace("arena_round.prep.captured", {
count = count,
})
return matchRecord.prepOpponents
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Opponent unit update
-- Called from CombatTracker:HandleArenaOpponentUpdate()
-- ──────────────────────────────────────────────────────────────────────────────
function ArenaRoundTracker:HandleArenaOpponentUpdate(unitToken, updateReason)
local slot = parseArenaSlot(unitToken)
if not slot then return nil end
-- Ensure a round exists. ARENA_OPPONENT_UPDATE fires during prep before
-- PVP_MATCH_ACTIVE, so BeginRound is called lazily here if needed.
local round = self:GetCurrentRound()
if not round then
round = self:BeginRound("arena_opponent_update")
end
local s = ensureSlot(round, slot)
s.lastUpdateReason = updateReason
s.lastUpdateAt = nowServer()
s.updateHistory[#s.updateHistory + 1] = {
reason = updateReason,
at = s.lastUpdateAt,
}
if updateReason == "unseen" then
s.visible = false
s.hiddenAt = s.lastUpdateAt
-- T034: Propagate visibility-lost into UnitGraphService so it emits
-- the arena_opponent_hidden VISIBILITY lane event.
if s.guid then
local ugsUnseen = ns.Addon:GetModule("UnitGraphService")
if ugsUnseen and ugsUnseen.MarkUnseen then
pcall(ugsUnseen.MarkUnseen, ugsUnseen, s.guid)
end
end
else
-- Negative-space: trace any updateReason that is not in the known-visible
-- set. The existing logic is still correct (treat as visible), but the
-- unknown value is surfaced in the trace log for investigation.
if not KNOWN_VISIBLE_UPDATE_REASONS[updateReason] then
ns.Addon:Trace("arena_round.opponent.unknown_reason", {
unit = unitToken,
reason = tostring(updateReason),
})
end
local snapshot = buildUnitSnapshot(unitToken)
-- Reject snapshots whose GUID is the local player. After a match ends
-- arena unit tokens can briefly become invalid or transition through
-- states where UnitGUID returns the player's own GUID; storing that
-- would cause the player to appear as their own opponent in history.
local myGuid = ApiCompat.GetPlayerGUID()
if snapshot and snapshot.guid ~= myGuid then
s.visible = true
s.guid = snapshot.guid
s.name = snapshot.name
s.className = snapshot.className
s.classFile = snapshot.classFile
s.classId = snapshot.classId
s.raceName = snapshot.raceName
s.raceFile = snapshot.raceFile
s.raceId = snapshot.raceId
s.healthMax = snapshot.healthMax
s.lastSeenAt = snapshot.capturedAt
round.guidToSlot[snapshot.guid] = slot
-- If we had this GUID staged as unresolved, clear it.
round.unresolvedEnemyGuids[snapshot.guid] = nil
-- T041/T043: Set field confidence for visible-sourced fields.
local visTs = nowServer()
s.fieldConfidence.guid = "visible"
s.fieldConfidence.guidLearnedAt = visTs
s.fieldConfidence.name = "visible"
s.fieldConfidence.nameLearnedAt = visTs
s.fieldConfidence.class = "visible"
s.fieldConfidence.classLearnedAt = visTs
-- T027: Push this slot assignment into UnitGraphService so it can
-- use arena_slot_mapping priority for identity resolution.
local ugs = ns.Addon:GetModule("UnitGraphService")
if ugs and ugs.UpdateFromArenaSlot then
pcall(ugs.UpdateFromArenaSlot, ugs,
slot,
snapshot.guid,
snapshot.name,
snapshot.classFile,
s.prepSpecId)
end
end
-- Merge prep data (non-destructive; only fill if not already set).
local prep = self.currentMatch and self.currentMatch.prepOpponents[slot]
if prep then
s.prepSpecId = s.prepSpecId or prep.specId
s.prepSpecName = s.prepSpecName or prep.specName
s.prepSpecIconId = s.prepSpecIconId or prep.specIconId
s.prepRole = s.prepRole or prep.role
s.prepClassFile = s.prepClassFile or prep.classFile
end
end
-- Recompute round keys after any slot change.
round.rosterSignature = buildRosterSignature(round.slots)
round.roundKey = buildRoundKey(self.currentMatch.matchKey, round.roundIndex, round.slots)
-- Queue inspect for PvP talent capture (Task 1.4)
if s.visible and updateReason ~= "unseen" then
self:QueueInspect(unitToken)
end
return s
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Opponent PvP Talent Inspect (Task 1.4)
-- FIFO queue, one inspect per 0.5s via OnUpdate polling.
-- ──────────────────────────────────────────────────────────────────────────────
function ArenaRoundTracker:QueueInspect(unitToken)
if not unitToken or not ApiCompat.UnitExists(unitToken) then return end
-- Avoid duplicates
for _, queued in ipairs(self.inspectQueue) do
if queued == unitToken then return end
end
self.inspectQueue[#self.inspectQueue + 1] = unitToken
end
function ArenaRoundTracker:ProcessInspectQueue(elapsed)
self.inspectElapsed = (self.inspectElapsed or 0) + elapsed
if self.inspectElapsed < 0.5 then return end
self.inspectElapsed = 0
if self.inspectBusy then return end
if #self.inspectQueue == 0 then return end
-- Never call NotifyInspect while in combat lockdown. On Midnight the
-- inspect action is restricted during active combat and calling it from
-- addon code triggers ADDON_ACTION_BLOCKED. We keep the queue intact
-- and retry on the next OnUpdate tick once combat ends.
if InCombatLockdown and InCombatLockdown() then return end
local unitToken = table.remove(self.inspectQueue, 1)
if not ApiCompat.UnitExists(unitToken) then return end
self.inspectBusy = true
self.inspectTarget = unitToken
ApiCompat.NotifyInspect(unitToken)
end
function ArenaRoundTracker:HandleInspectReady()
self.inspectBusy = false
local unitToken = self.inspectTarget
self.inspectTarget = nil
if not unitToken then return end
local round = self:GetCurrentRound()
if not round then return end
local slot = parseArenaSlot(unitToken)
if not slot or not round.slots[slot] then return end
-- Capture PvP talents via inspect API. Wrap in pcall: arena unit
-- tokens may return secret values from inspect APIs during combat.
local pvpTalents = {}
if C_SpecializationInfo and C_SpecializationInfo.GetInspectSelectedPvpTalent then
for talentIndex = 1, 4 do
local ok, talentId = pcall(C_SpecializationInfo.GetInspectSelectedPvpTalent, unitToken, talentIndex)
if ok and talentId and not ApiCompat.IsSecretValue(talentId) and talentId > 0 then
pvpTalents[#pvpTalents + 1] = talentId
end
end
end
-- T042/T043: Timestamp for inspect-sourced field confidence.
local inspTs = nowServer()
if #pvpTalents > 0 then
round.slots[slot].pvpTalents = pvpTalents
-- T042/T043: Field confidence for pvpTalents.
round.slots[slot].fieldConfidence.pvpTalents = "inspect"
round.slots[slot].fieldConfidence.pvpTalentsLearnedAt = inspTs
ns.Addon:Trace("arena_round.inspect.pvp_talents", {
slot = slot,
unit = unitToken,
talents = table.concat(pvpTalents, ","),
})
end
-- Task 2.3: Capture opponent talent build import string via C_Traits (12.0.0+).
-- HasValidInspectData() ensures the inspect cache is populated before reading.
if ApiCompat.HasValidInspectData() then
local importString = ApiCompat.GenerateInspectImportString(unitToken)
if importString and type(importString) == "string" and importString ~= "" then
round.slots[slot].talentImportString = importString
-- T042/T043: Field confidence for talentImportString.
round.slots[slot].fieldConfidence.talentImportString = "inspect"
round.slots[slot].fieldConfidence.talentImportStringLearnedAt = inspTs
ns.Addon:Trace("arena_round.inspect.talent_import", {
slot = slot,
unit = unitToken,
length = #importString,
})
end
end
-- T042/T043: Upgrade spec confidence to "inspect" (from "prep" or "visible").
round.slots[slot].fieldConfidence.spec = "inspect"
round.slots[slot].fieldConfidence.specLearnedAt = inspTs
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Native DR tracking (Task 2.1 — C_SpellDiminish)
-- ──────────────────────────────────────────────────────────────────────────────
--- Handle UNIT_SPELL_DIMINISH_CATEGORY_STATE_UPDATED.
--- Stores native DR state per arena unit per category into the current round.
--- @param unitTarget string — e.g. "arena1"
--- @param trackerInfo table — {category, startTime, duration, showCountdown, isImmune}
function ArenaRoundTracker:HandleDiminishStateUpdated(unitTarget, trackerInfo)
if not unitTarget or not trackerInfo then return end
local slot = parseArenaSlot(unitTarget)
if not slot then return end
local round = self:GetCurrentRound()
if not round or not round.slots[slot] then return end
round.slots[slot].drState = round.slots[slot].drState or {}
-- trackerInfo is a raw WoW API payload and may carry secret/restricted fields
-- under the Midnight taint model. Access each field through pcall to prevent
-- taint propagation from unreadable fields to the addon frame.
local category, startTime, duration, isImmune, showCountdown
local ok, err = pcall(function()
category = trackerInfo.category
startTime = trackerInfo.startTime
duration = trackerInfo.duration
isImmune = trackerInfo.isImmune
showCountdown = trackerInfo.showCountdown
end)
if not ok then
ns.Addon:Trace("arena_round.dr_taint_guard", { unit = unitTarget, err = tostring(err) })
return
end
if category == nil then return end
round.slots[slot].drState[category] = {
startTime = startTime,
duration = duration,
isImmune = isImmune or false,
showCountdown = showCountdown or false,
}
ns.Addon:Trace("arena_round.dr_updated", {
slot = slot,
unit = unitTarget,
category = category,
immune = isImmune and "true" or "false",
})
end
--- Returns the current DR state table for a given unit token (e.g. "arena1").
--- Returns nil when no round or no DR data exists for the given unit.
function ArenaRoundTracker:GetDiminishState(unitToken)
local round = self:GetCurrentRound()
if not round then return nil end
local slot = parseArenaSlot(unitToken)
if not slot or not round.slots[slot] then return nil end
return round.slots[slot].drState
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Session-pressure hydration
-- Called from CopyStateIntoSession before exporting to the session record.
-- Reads post-session evidence (attribution + death recap timeline) and writes
-- per-slot primarySelectionScore and selectionEvidence for GetPrimaryEnemy.
-- ──────────────────────────────────────────────────────────────────────────────
function ArenaRoundTracker:ApplySessionPressure(round, session)
if not round then return end
local myGuid = ApiCompat.GetPlayerGUID()
-- Step 1: Reset all derived selection fields to zero for a clean slate.
for _, s in pairs(round.slots) do
s.primarySelectionScore = 0
ensureSelectionEvidence(s)
s.selectionEvidence.damageToPlayer = 0
s.selectionEvidence.deathRecap = 0
s.selectionEvidence.identityBias = 0
s.selectionEvidence.visibilityBias = 0
end
-- Step 2: Hydrate damageToPlayer from session.attribution.bySource.
-- Each bySource entry is keyed by enemy GUID; totalAmount is the aggregate.
local bySource = session and type(session.attribution) == "table" and session.attribution.bySource
if bySource then
for sourceGuid, sourceData in pairs(bySource) do
if sourceGuid ~= myGuid then
local slot = findSlotByGuid(round, sourceGuid)
if slot then
local ev = ensureSelectionEvidence(slot)
ev.damageToPlayer = ev.damageToPlayer + (sourceData.totalAmount or 0)
end
end
end
end
-- Step 3: Hydrate deathRecap from DM_ENEMY_SPELL timeline events of type "death_recap".
for _, ev in ipairs(session and session.timelineEvents or {}) do
if ev.lane == Constants.TIMELINE_LANE.DM_ENEMY_SPELL and ev.type == "death_recap" then
local sourceGuid = ev.meta and ev.meta.sourceGuid
if sourceGuid and sourceGuid ~= myGuid then
local slot = findSlotByGuid(round, sourceGuid)
if slot then
local sev = ensureSelectionEvidence(slot)
sev.deathRecap = sev.deathRecap + (ev.amount or 0)
end
end
end
end
-- Step 4: Apply identity and visibility biases; compute composite score.
-- preferredOpponentGuid is the slot GUID from the last successful export.
local preferredGuid = round.preferredOpponentGuid
for _, s in pairs(round.slots) do
if s.guid and s.guid ~= myGuid then
local sev = ensureSelectionEvidence(s)
-- Identity bias: +12 when this slot is the previously preferred enemy.
if preferredGuid and s.guid == preferredGuid then
sev.identityBias = 12
end
-- Visibility bias: +1 for currently visible slots.
if s.visible then
sev.visibilityBias = 1
end
-- Composite score: damageToPlayer has highest weight, followed by kill
-- participation, with identity and visibility as tie-breaker adjustments.
s.primarySelectionScore =
sev.damageToPlayer * 0.45 +
(s.killParticipation or 0) * 0.10 +
sev.identityBias +
sev.visibilityBias
end
end
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Primary enemy selection (evidence-based stable ranking)
-- ──────────────────────────────────────────────────────────────────────────────
-- GetPrimaryEnemy(preferredGuid) — returns (slot, strategy).
--
-- Ranking policy (stable, deterministic):
-- 1. Highest primarySelectionScore (set by ApplySessionPressure)
-- 2. Visible slots over hidden
-- 3. Most recently seen (lastSeenAt)
-- 4. Lowest slot index (deterministic tie-breaker)
--
-- 85% sticky GUID rule: when meaningful pressure data exists (bestScore > 0),
-- the previously preferred opponent is retained if its score is >= 85% of the
-- best slot's score, preventing identity flapping between nearly-tied enemies.
--
-- Strategy labels:
-- "preferred_guid_sticky" — retained by sticky rule
-- "highest_score" — normal score-based selection
-- "latest_visible" — fallback: no pressure data, picked by visibility
-- "no_visible_slot" — all eligible slots are invisible or tied at zero
-- "no_round" — no active or recent round exists
--
-- The player's own GUID is always excluded from consideration.
function ArenaRoundTracker:GetPrimaryEnemy(preferredGuid)
local round = self:GetCurrentRound()
if not round then return nil, "no_round" end
local myGuid = ApiCompat.GetPlayerGUID()
-- Collect eligible slots: non-nil GUID, not the local player.
local eligible = {}
for slot = 1, 5 do
local s = round.slots[slot]
if s and s.guid and s.guid ~= myGuid then
eligible[#eligible + 1] = s
end
end
if #eligible == 0 then return nil, "no_visible_slot" end
-- Find the best score across all eligible slots.
local bestScore = 0
for _, s in ipairs(eligible) do
local score = s.primarySelectionScore or 0
if score > bestScore then bestScore = score end
end
-- 85% sticky preferred GUID rule (only when pressure data is meaningful).
if preferredGuid and bestScore > 0 then
for _, s in ipairs(eligible) do
if s.guid == preferredGuid then
local prefScore = s.primarySelectionScore or 0
if prefScore >= bestScore * 0.85 then
return s, "preferred_guid_sticky"
end
break
end
end
end
-- Stable sort: score → visible → recently seen → lowest slot index.
table.sort(eligible, function(a, b)
local aScore = a.primarySelectionScore or 0
local bScore = b.primarySelectionScore or 0
if aScore ~= bScore then return aScore > bScore end
local aVis = a.visible and 1 or 0
local bVis = b.visible and 1 or 0
if aVis ~= bVis then return aVis > bVis end
local aSeen = a.lastSeenAt or 0
local bSeen = b.lastSeenAt or 0
if aSeen ~= bSeen then return aSeen > bSeen end
return (a.slot or 99) < (b.slot or 99)
end)
local best = eligible[1]
if not best then return nil, "no_visible_slot" end
-- Determine strategy label.
local strategy
if (best.primarySelectionScore or 0) > 0 then
strategy = "highest_score"
elseif best.visible then
strategy = "latest_visible"
else
strategy = "no_visible_slot"
end
return best, strategy
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Lightweight identity seed — copies slot GUIDs, names, and specs into the
-- session WITHOUT the pressure scoring (which depends on attribution data that
-- is populated later by the DamageMeter import). Called BEFORE the import so
-- collectExpectedOpponentGuids has populated slot data for candidate scoring.
-- ──────────────────────────────────────────────────────────────────────────────
function ArenaRoundTracker:SeedSessionIdentity(session)
if not session then return end
local matchRecord = self.currentMatch
if not matchRecord then return end
local round = matchRecord.currentRound or (matchRecord.rounds[#matchRecord.rounds])
if not round then return end
session.arena = session.arena or {}
session.arena.matchKey = session.arena.matchKey or matchRecord.matchKey
session.arena.slots = session.arena.slots or {}
session.arena.guidToSlot = session.arena.guidToSlot or {}
for slot, s in pairs(round.slots) do
session.arena.slots[slot] = session.arena.slots[slot] or {}
local dst = session.arena.slots[slot]
dst.guid = dst.guid or s.guid
dst.name = dst.name or s.name
dst.classFile = dst.classFile or s.classFile
dst.className = dst.className or s.className
dst.classId = dst.classId or s.classId
dst.prepSpecId = dst.prepSpecId or s.prepSpecId
dst.prepSpecName = dst.prepSpecName or s.prepSpecName
dst.prepClassFile = dst.prepClassFile or s.prepClassFile
if s.guid then