-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegments.lua
More file actions
executable file
·406 lines (363 loc) · 13 KB
/
Copy pathSegments.lua
File metadata and controls
executable file
·406 lines (363 loc) · 13 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
---@class AddOn
local AddOn = (select(2, ...))
local L = AddOn.L
local copy = AddOn.Copy
local format = format
local min = min
local next = next
local setmetatable = setmetatable
local tRemove = table.remove
local tSort = table.sort
local time = time
local After = C_Timer.After
local CallOnAllGroupMembers = AddOn.CallOnAllGroupMembers
local FormatTimestamp = AddOn.FormatTimestamp
local GUIDIsPlayer = C_PlayerInfo.GUIDIsPlayer
local GetActiveChallengeMapID = C_ChallengeMode.GetActiveChallengeMapID
local GetBestMapForUnit = C_Map.GetBestMapForUnit
local GetChallengeCompletionInfo = C_ChallengeMode.GetChallengeCompletionInfo
local GetMapInfo = C_Map.GetMapInfo
local GetMapUIInfo = C_ChallengeMode.GetMapUIInfo
local GetPlayerInfos = AddOn.GetPlayerInfos
local GetPlayerName = AddOn.GetPlayerName
local IsChallengeModeActive = C_ChallengeMode.IsChallengeModeActive
local IsMatchActive = C_PvP.IsMatchActive
local UnitAffectingCombat = UnitAffectingCombat
local UnitGUID = UnitGUID
---@class Segment
---@field endTimestamp? number
---@field name? string
---@field roster? table<string, PlayerInfo>
---@field startTimestamp? number
---@field uiMapId? number
---@field [string] any
local SegmentMeta = {}
AddOn.SegmentMeta = SegmentMeta
SegmentMeta.__index = SegmentMeta
---@return number?
function SegmentMeta:GetStartTimestamp() return self.startTimestamp end
---@return number?
function SegmentMeta:GetEndTimestamp() return self.endTimestamp end
---@return number
function SegmentMeta:GetDuration() return (self.endTimestamp or time()) - (self.startTimestamp or time()) end
---@return string
function SegmentMeta:GetName()
if self.name then
return self.name
else
return format("%s - %s", FormatTimestamp(self.startTimestamp or time()),
FormatTimestamp(self.endTimestamp or time()))
end
end
---@return string
function SegmentMeta:GetMapName()
local info = GetMapInfo(self.uiMapId or 946)
if info and info.name then return info.name end
return L.UNKNOWN
end
---@type Segment[]
local segments = {}
AddOn.Segments = segments
---@type table<any, Segment>
local activeSegments = {}
AddOn.ActiveSegments = activeSegments
---@param key any
---@param timestamp? number
---@param name? string
---@param oldSegment? Segment
---@return Segment?
local function startSegment(key, timestamp, name, oldSegment)
timestamp = timestamp or time()
local activeSegment = activeSegments[key]
if not activeSegment then
if oldSegment then
activeSegment = oldSegment
wipe(oldSegment)
oldSegment.startTimestamp = timestamp
oldSegment.uiMapId = GetBestMapForUnit("player")
oldSegment.name = name
else
activeSegment = setmetatable({
startTimestamp = timestamp,
uiMapId = GetBestMapForUnit("player"),
name = name,
}, SegmentMeta)
end
else
activeSegment.startTimestamp = min(activeSegment.startTimestamp, timestamp)
end
activeSegments[key] = activeSegment
local roster = activeSegment.roster
if not roster then
roster = {}
activeSegment.roster = roster
end
CallOnAllGroupMembers(function(unitToken)
local guid = UnitGUID(unitToken)
if guid then
local playerInfos = roster[guid]
if not playerInfos then
playerInfos = {}
roster[guid] = playerInfos
end
GetPlayerInfos(guid, playerInfos)
end
end)
return activeSegment
end
AddOn.StartSegment = startSegment
---@param a Segment
---@param b Segment
---@return boolean
local function segmentComp(a, b)
local endTimestampA = a.endTimestamp or time()
local endTimestampB = b.endTimestamp or time()
if endTimestampA == endTimestampB then
local startTimestampA = a.startTimestamp or time()
local startTimestampB = b.startTimestamp or time()
if startTimestampA == startTimestampB then
return a:GetName() < b:GetName()
else
return startTimestampA < startTimestampB
end
else
return endTimestampA < endTimestampB
end
end
---@param key any
---@param timestamp? number
---@param name? string
---@param saveCopy? boolean
---@return Segment?
local function stopSegment(key, timestamp, name, saveCopy)
timestamp = timestamp or time()
local activeSegment = activeSegments[key]
if activeSegment then
activeSegments[key] = nil
activeSegment.endTimestamp = timestamp
activeSegment.name = name or activeSegment.name
if activeSegment:GetDuration() >= 5 then
activeSegment = saveCopy and copy(activeSegment) or activeSegment
segments[#segments + 1] = activeSegment
tSort(segments, segmentComp)
end
end
return activeSegment
end
AddOn.StopSegment = stopSegment
AddOn.RegisterEvent("PLAYER_LOGIN", --
function() startSegment("session", time(), L.SESSION) end)
AddOn.RegisterEvent("PLAYER_LOGOUT", --
function()
stopSegment("encounter")
stopSegment("challenge")
stopSegment("pvp")
end)
AddOn.RegisterEvent("PLAYER_ENTERING_WORLD", --
---@param isInitialLogin boolean
---@param isReloadingUi boolean
function(isInitialLogin, isReloadingUi)
if not IsChallengeModeActive() then stopSegment("challenge") end
if IsMatchActive() then
startSegment("pvp")
After(1, function()
local activeSegment = activeSegments.pvp
if activeSegment then
activeSegment.uiMapId = GetBestMapForUnit("player")
activeSegment.name = activeSegment:GetMapName()
end
end)
else
stopSegment("pvp")
end
end)
---@type Segment
local combatSegment = setmetatable({name = L.CURRENT}, SegmentMeta)
---@param timestamp number
local function startCombat(timestamp)
if not activeSegments.combat then startSegment("combat", timestamp, L.CURRENT, combatSegment) end
end
AddOn.StartCombat = startCombat
---@return Segment
function AddOn.GetCombatSegment() return combatSegment end
AddOn.RegisterEvent("PLAYER_REGEN_DISABLED", --
function() startCombat(time()) end)
C_Timer.NewTicker(3, function(self)
if CallOnAllGroupMembers(UnitAffectingCombat, true) then return end
local activeSegment = stopSegment("combat", time(), nil, true)
if activeSegment then
activeSegment.name = nil
---@type DamageDone?
local damageDone = activeSegment.damageDone
if not damageDone then return end
local targets = {}
for target, targetData in next, damageDone.targets, nil do
targets[target] = (targets[target] or 0) + (targetData.amount or 0)
targets[target] = (targets[target] or 0) + (targetData.petAmount or 0)
end
local maxTarget
local maxAmount = 0
for target, amount in next, targets, nil do
if amount > maxAmount then
maxAmount = amount
maxTarget = target
end
end
if maxTarget then
local name
if GUIDIsPlayer(maxTarget) then
local name2, fullName, shortName = GetPlayerName(maxTarget)
name = fullName
else
name = L[maxTarget]
end
activeSegment.name = name
end
end
end)
local getActiveKeystoneInfo = C_ChallengeMode.GetActiveKeystoneInfo
local getDifficultyInfo = GetDifficultyInfo
AddOn.RegisterEvent("ENCOUNTER_START", --
---@param encounterID number
---@param encounterName string
---@param difficultyID number
---@param groupSize number
function(encounterID, encounterName, difficultyID, groupSize)
if difficultyID == 8 then -- Mythic Keystone
local activeKeystoneLevel, activeAffixIDs, wasActiveKeystoneCharged = getActiveKeystoneInfo()
startSegment("encounter", time(),
format("%s (%s - %d) |TInterface\\RAIDFRAME\\ReadyCheck-Waiting:0|t", encounterName,
(getDifficultyInfo(difficultyID)), activeKeystoneLevel))
else
startSegment("encounter", time(), format("%s (%s) |TInterface\\RAIDFRAME\\ReadyCheck-Waiting:0|t",
encounterName, (getDifficultyInfo(difficultyID))))
end
end)
AddOn.RegisterEvent("ENCOUNTER_END", --
---@param encounterID number
---@param encounterName string
---@param difficultyID number
---@param groupSize number
---@param success number
function(encounterID, encounterName, difficultyID, groupSize, success)
if difficultyID == 8 then -- Mythic Keystone
local activeKeystoneLevel, activeAffixIDs, wasActiveKeystoneCharged = getActiveKeystoneInfo()
stopSegment("encounter", time(),
format("%s (%s) |TInterface\\RAIDFRAME\\ReadyCheck-%s:0|t", encounterName,
format(L.MYTHIC_LEVEL, activeKeystoneLevel),
success and success == 1 and "Ready" or "NotReady"))
else
stopSegment("encounter", time(),
format("%s (%s) |TInterface\\RAIDFRAME\\ReadyCheck-%s:0|t", encounterName,
(getDifficultyInfo(difficultyID)), success and success == 1 and "Ready" or "NotReady"))
end
end)
AddOn.RegisterEvent("CHALLENGE_MODE_START", --
---@param mapID number
function(mapID)
startSegment("challenge", time(), L.CHALLENGE_MODE)
After(1, function()
local activeSegment = activeSegments.challenge
if activeSegment then
local mapChallengeModeID = GetActiveChallengeMapID()
if mapChallengeModeID then
local name, id, timeLimit, texture, backgroundTexture = GetMapUIInfo(mapChallengeModeID)
if name then
local activeKeystoneLevel, activeAffixIDs, wasActiveKeystoneCharged = getActiveKeystoneInfo()
activeSegment.name = format("%s (%s) |TInterface\\RAIDFRAME\\ReadyCheck-Waiting:0|t", name,
format(L.MYTHIC_LEVEL, activeKeystoneLevel))
end
end
end
end)
end)
AddOn.RegisterEvent("CHALLENGE_MODE_COMPLETED", --
function()
local activeSegment = activeSegments.challenge
stopSegment("challenge", time())
if activeSegment then
local info = GetChallengeCompletionInfo()
if info.mapChallengeModeID then
if not info.practiceRun then
local name, id, timeLimit, texture, backgroundTexture = GetMapUIInfo(info.mapChallengeModeID)
if name then
activeSegment.name = format("%s (%s) |TInterface\\RAIDFRAME\\ReadyCheck-%s:0|t", name,
format(L.MYTHIC_LEVEL, info.level), info.onTime and "Ready" or "NotReady")
end
end
end
end
end)
AddOn.RegisterEvent("PVP_MATCH_STATE_CHANGED", --
function()
if IsMatchActive() then
startSegment("pvp")
After(1, function()
local activeSegment = activeSegments.pvp
if activeSegment then
activeSegment.uiMapId = GetBestMapForUnit("player")
activeSegment.name = activeSegment:GetMapName()
end
end)
else
stopSegment("pvp")
end
end)
AddOn.RegisterEvent("PVP_MATCH_COMPLETE", --
---@param winner number
---@param duration number
function(winner, duration) stopSegment("pvp") end)
---@param segment Segment
function AddOn.DeleteSegment(segment)
for key, activeSegment in next, activeSegments, nil do
if activeSegment == segment then
local name = activeSegment.name
local uiMapId = activeSegment.uiMapId
wipe(activeSegment)
activeSegment.name = name
activeSegment.uiMapId = uiMapId
activeSegment.startTimestamp = time()
return
end
end
for i = 1, #segments, 1 do
if segments[i] == segment then
tRemove(segments, i)
return
end
end
end
function AddOn.DeleteAllSegments()
for key, activeSegment in next, activeSegments, nil do
local name = activeSegment.name
local uiMapId = activeSegment.uiMapId
wipe(activeSegment)
activeSegment.name = name
activeSegment.uiMapId = uiMapId
activeSegment.startTimestamp = time()
end
wipe(segments)
end
AddOn.RegisterEvent("GROUP_ROSTER_UPDATE", --
function()
local roster = {}
CallOnAllGroupMembers(function(unitToken)
local guid = UnitGUID(unitToken)
if guid then
local playerInfos = {}
roster[guid] = playerInfos
GetPlayerInfos(guid, playerInfos)
end
end)
for key, activeSegment in next, activeSegments, nil do
if not activeSegment.roster then activeSegment.roster = {} end
for guid, infos in next, roster, nil do
local playerInfos = activeSegment.roster[guid]
if not playerInfos then
playerInfos = infos
activeSegment.roster[guid] = playerInfos
end
GetPlayerInfos(guid, playerInfos)
end
end
end)