-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuraReconciliationService.lua
More file actions
355 lines (311 loc) · 14.6 KB
/
Copy pathAuraReconciliationService.lua
File metadata and controls
355 lines (311 loc) · 14.6 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
local _, ns = ...
local Constants = ns.Constants
local ApiCompat = ns.ApiCompat
local Helpers = ns.Helpers
local AuraReconciliationService = {}
-- ---------------------------------------------------------------------------
-- Internal helpers
-- ---------------------------------------------------------------------------
local function getTimelineProducer()
return ns.Addon:GetModule("TimelineProducer")
end
-- T025: Resolve source identity (GUID, name, classFile, arenaSlot) for the
-- given unit token using UnitGraphService as the primary source with a direct
-- ApiCompat.GetUnitGUID fallback. Returns four values; all nil on failure.
local function resolveActorIdentity(unitToken)
local ugs = ns.Addon:GetModule("UnitGraphService")
local guid, name, classFile, arenaSlot
local ok = pcall(function()
if ugs then
guid = ugs:GetGUIDForToken(unitToken)
if guid then
local node = ugs:GetNode(guid)
if node then
name = node.name
classFile = node.classFile
arenaSlot = node.arenaSlot
end
end
end
-- Fallback: direct API lookup when UGS did not resolve the GUID.
if not guid then
guid = ApiCompat.GetUnitGUID(unitToken)
end
end)
if not ok then
guid = nil; name = nil; classFile = nil; arenaSlot = nil
end
return guid, name, classFile, arenaSlot
end
-- C2: When an aura's sourceUnit is unknown (secret/nil on arena opponents),
-- infer the caster by correlating with the nearest preceding cast_succeeded
-- of the same spellId within a small time window. Returns inferred identity
-- fields when a match is found (caller lowers the confidence tier).
local CASTER_CORRELATION_WINDOW = 3.0 -- seconds
local function inferCasterFromCasts(session, spellId, auraT)
if not session or not session.timelineEvents or not spellId then return nil end
local events = session.timelineEvents
-- Order-independent: timelineEvents is appended in insertion order, which
-- is NOT guaranteed to be sorted by `t` (DM/summary rows carry computed
-- offsets). Scan all events and keep the closest *preceding* realtime cast
-- of this spellId within the window, instead of breaking on assumed order.
local best
local bestT
for i = 1, #events do
local ev = events[i]
if ev.type == "cast_succeeded"
and ev.spellId == spellId
and ev.t
and ev.chronology == "realtime"
then
local dt = auraT - ev.t
if dt >= 0 and dt <= CASTER_CORRELATION_WINDOW then
if not bestT or ev.t > bestT then
best = ev
bestT = ev.t
end
end
end
end
if not best then return nil end
return {
guid = best.sourceGuid,
name = best.sourceName,
classFile = best.sourceClassFile,
slot = best.sourceSlot,
unitToken = best.sourceUnitToken,
}
end
-- Enumerate all current auras on a unit from live game state.
-- Returns a list of raw aura data tables. Each table is wrapped in pcall to
-- avoid crashing on secret values present on arena opponent auras in Midnight.
local function enumLiveAuras(unitToken)
local results = {}
if not AuraUtil or not AuraUtil.ForEachAura then return results end
for _, filter in ipairs({ "HELPFUL", "HARMFUL" }) do
local ok = pcall(function()
AuraUtil.ForEachAura(unitToken, filter, nil, function(auraData)
if auraData then
results[#results + 1] = auraData
end
end)
end)
if not ok then
ns.Addon:Trace("aura_reconciliation.enum_failed", { unit = unitToken, filter = filter })
end
end
return results
end
-- ---------------------------------------------------------------------------
-- Public API
-- ---------------------------------------------------------------------------
--- Incremental update path — called from the coalesced UNIT_AURA handler
--- (Events.lua _flushPendingAura). Processes addedAuras and
--- removedAuraInstanceIDs from the merged updateInfo for a single unit.
function AuraReconciliationService:HandleUnitAura(unitTarget, updateInfo)
if not unitTarget or not Constants.TRACKED_UNITS[unitTarget] then return end
local tp = getTimelineProducer()
if not tp then return end
local session = tp:GetCurrentSession()
if not session then return end
if not updateInfo then return end
local now = Helpers.Now()
local t = now - (session.startedAt or now)
-- Process added auras.
if updateInfo.addedAuras then
for _, auraData in ipairs(updateInfo.addedAuras) do
local okAura, auraEvent = pcall(function()
local auraSpellId = auraData.spellId
local auraName = auraData.name
local auraInstanceID = auraData.auraInstanceID
local isHelpful = auraData.isHelpful
local sourceUnit = auraData.sourceUnit
local duration = auraData.duration
local expirationTime = auraData.expirationTime
auraSpellId = ApiCompat.SanitizeNumber(auraSpellId)
auraName = ApiCompat.SanitizeString(auraName)
auraInstanceID = ApiCompat.SanitizeNumber(auraInstanceID)
-- T025: Resolve actor identity for the unit that has the aura.
local srcGuid, srcName, srcClassFile, srcSlot = resolveActorIdentity(unitTarget)
-- C2: holder identity is the aura target, not the caster. When
-- the real sourceUnit is unknown (secret/nil on arena enemies),
-- infer the caster from a preceding cast_succeeded of the same
-- spellId and expose it in distinct caster* fields.
local safeSourceUnit = ApiCompat.SanitizeString(sourceUnit)
local casterConfidence = Constants.ATTRIBUTION_CONFIDENCE.confirmed
local casterGuid, casterName, casterClassFile, casterSlot, casterToken
if not safeSourceUnit or safeSourceUnit == "" then
local inferred = inferCasterFromCasts(
session, auraSpellId ~= 0 and auraSpellId or nil, t)
if inferred then
casterGuid = inferred.guid
casterName = inferred.name
casterClassFile = inferred.classFile
casterSlot = inferred.slot
casterToken = inferred.unitToken
casterConfidence = Constants.ATTRIBUTION_CONFIDENCE.inferred
end
end
return {
t = t,
lane = Constants.TIMELINE_LANE.VISIBLE_AURA,
type = "applied",
source = Constants.PROVENANCE_SOURCE.VISIBLE_UNIT,
confidence = casterConfidence,
sourceGuid = srcGuid,
sourceName = srcName,
sourceClassFile = srcClassFile,
sourceSlot = srcSlot,
sourceUnitToken = unitTarget,
-- C2: inferred caster identity (distinct from aura holder).
casterGuid = casterGuid,
casterName = casterName,
casterClassFile = casterClassFile,
casterSlot = casterSlot,
casterUnitToken = casterToken,
spellId = auraSpellId ~= 0 and auraSpellId or nil,
spellName = auraName,
unitToken = unitTarget,
meta = {
auraInstanceID = auraInstanceID ~= 0 and auraInstanceID or nil,
isHelpful = isHelpful and true or false,
sourceUnit = safeSourceUnit,
duration = ApiCompat.SanitizeNumber(duration),
expirationTime = ApiCompat.SanitizeNumber(expirationTime),
},
}
end)
if okAura and auraEvent then
tp:AppendTimelineEvent(session, auraEvent)
end
end
end
-- Process removed auras.
if updateInfo.removedAuraInstanceIDs then
for _, instanceID in ipairs(updateInfo.removedAuraInstanceIDs) do
local okRemoved, removedEvent = pcall(function()
local sanitizedId = ApiCompat.SanitizeNumber(instanceID)
-- T025: Resolve actor identity for the unit whose aura was removed.
local srcGuid, srcName, srcClassFile, srcSlot = resolveActorIdentity(unitTarget)
return {
t = t,
lane = Constants.TIMELINE_LANE.VISIBLE_AURA,
type = "removed",
source = Constants.PROVENANCE_SOURCE.VISIBLE_UNIT,
confidence = Constants.ATTRIBUTION_CONFIDENCE.confirmed,
sourceGuid = srcGuid,
sourceName = srcName,
sourceClassFile = srcClassFile,
sourceSlot = srcSlot,
sourceUnitToken = unitTarget,
unitToken = unitTarget,
meta = {
auraInstanceID = sanitizedId ~= 0 and sanitizedId or nil,
},
}
end)
if okRemoved and removedEvent then
tp:AppendTimelineEvent(session, removedEvent)
end
end
end
end
--- Full-rescan path — enumerate all live auras on unitToken from game state.
--- Records pre-existing auras as confidence = "observed_active" so that no
--- fabricated application time is emitted for auras that were already active
--- when the unit entered observation.
--- Called by: T019 NAME_PLATE_UNIT_ADDED hook, arena slot resolution.
function AuraReconciliationService:HandleFullRescan(unitToken)
if not unitToken then return end
local tp = getTimelineProducer()
if not tp then return end
local session = tp:GetCurrentSession()
if not session then return end
local now = Helpers.Now()
local t = now - (session.startedAt or now)
local liveAuras = enumLiveAuras(unitToken)
for _, auraData in ipairs(liveAuras) do
local okAura, auraEvent = pcall(function()
local auraSpellId = auraData.spellId
local auraName = auraData.name
local auraInstanceID = auraData.auraInstanceID
local isHelpful = auraData.isHelpful
local sourceUnit = auraData.sourceUnit
local duration = auraData.duration
local expirationTime = auraData.expirationTime
auraSpellId = ApiCompat.SanitizeNumber(auraSpellId)
auraName = ApiCompat.SanitizeString(auraName)
auraInstanceID = ApiCompat.SanitizeNumber(auraInstanceID)
-- T025: Resolve actor identity; confidence = "inferred" for rescan-
-- detected auras (aura was already active, application not witnessed).
local srcGuid, srcName, srcClassFile, srcSlot = resolveActorIdentity(unitToken)
return {
t = t,
lane = Constants.TIMELINE_LANE.VISIBLE_AURA,
type = "observed_active",
source = Constants.PROVENANCE_SOURCE.VISIBLE_UNIT,
-- confidence = "inferred": aura was already present when observed;
-- application time is unknown. Actor identity inferred from unit state.
confidence = Constants.ATTRIBUTION_CONFIDENCE.inferred,
sourceGuid = srcGuid,
sourceName = srcName,
sourceClassFile = srcClassFile,
sourceSlot = srcSlot,
sourceUnitToken = unitToken,
spellId = auraSpellId ~= 0 and auraSpellId or nil,
spellName = auraName,
unitToken = unitToken,
meta = {
auraInstanceID = auraInstanceID ~= 0 and auraInstanceID or nil,
isHelpful = isHelpful and true or false,
sourceUnit = ApiCompat.SanitizeString(sourceUnit),
duration = ApiCompat.SanitizeNumber(duration),
expirationTime = ApiCompat.SanitizeNumber(expirationTime),
},
}
end)
if okAura and auraEvent then
tp:AppendTimelineEvent(session, auraEvent)
end
end
ns.Addon:Trace("aura_reconciliation.full_rescan", {
unit = unitToken,
count = #liveAuras,
offset = t,
})
end
--- Unit-removed path — emits a visibility-lost marker so timeline consumers
--- know that subsequent aura data for this unit may be stale.
--- Called by: T019 NAME_PLATE_UNIT_REMOVED hook.
function AuraReconciliationService:HandleUnitRemoved(unitToken)
if not unitToken then return end
local tp = getTimelineProducer()
if not tp then return end
local session = tp:GetCurrentSession()
if not session then return end
local now = Helpers.Now()
local t = now - (session.startedAt or now)
-- T025: Resolve actor identity for the departing unit.
local srcGuid, srcName, srcClassFile, srcSlot = resolveActorIdentity(unitToken)
tp:AppendTimelineEvent(session, {
t = t,
lane = Constants.TIMELINE_LANE.VISIBLE_AURA,
type = "unit_left_visibility",
source = Constants.PROVENANCE_SOURCE.VISIBLE_UNIT,
confidence = Constants.ATTRIBUTION_CONFIDENCE.confirmed,
sourceGuid = srcGuid,
sourceName = srcName,
sourceClassFile = srcClassFile,
sourceSlot = srcSlot,
sourceUnitToken = unitToken,
unitToken = unitToken,
})
ns.Addon:Trace("aura_reconciliation.unit_removed", {
unit = unitToken,
offset = t,
})
end
-- ---------------------------------------------------------------------------
-- Module registration
-- ---------------------------------------------------------------------------
ns.Addon:RegisterModule("AuraReconciliationService", AuraReconciliationService)