forked from happenslol/WarpDeplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Events.lua
542 lines (430 loc) · 16.2 KB
/
Events.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
local Util = WarpDeplete.Util
local UPDATE_INTERVAL = 0.1
local sinceLastUpdate = 0
function WarpDeplete:CheckForChallengeMode()
local _, _, difficulty, _, _, _, _, currentZoneID = GetInstanceInfo()
self:PrintDebug("Checking for challenge mode, difficulty: "
.. difficulty .. ", Zone ID: " .. currentZoneID)
local inChallenge = difficulty == 8 and C_ChallengeMode.GetActiveChallengeMapID() ~= nil
self:PrintDebug("Challenge state: inChallenge:"
.. tostring(self.challengeState.inChallenge)
.. ", new value:" .. tostring(inChallenge))
if self.challengeState.inChallenge == inChallenge then return end
if inChallenge then self:StartChallengeMode()
else self:StopChallengeMode() end
end
function WarpDeplete:StartChallengeMode()
if self.challengeState.demoModeActive then
self:Print("Disabling demo mode because a challenge has started.")
self:DisableDemoMode()
end
self:PrintDebug("Starting challenge mode")
self:ResetState()
self:RegisterChallengeEvents()
local gotTimerInfo = self:GetTimerInfo()
local gotKeyInfo = self:GetKeyInfo()
local gotObjectivesInfo = self:GetObjectivesInfo()
self:SetDeaths(C_ChallengeMode.GetDeathCount() or 0)
if not gotKeyInfo or not gotObjectivesInfo or not gotTimerInfo then return end
self.challengeState.inChallenge = true
self:Show()
if not self.timerState.running then
self:StartChallengeTimer()
end
end
function WarpDeplete:StartChallengeTimer()
self:PrintDebug("Challenge timer started")
self.timerState.startTime = GetTime()
self.timerState.running = true
sinceLastUpdate = 0
self.frames.root:SetScript("OnUpdate", function(self, elapsed)
WarpDeplete:OnTimerTick(elapsed)
end)
end
function WarpDeplete:StopChallengeTimer()
sinceLastUpdate = 0
self.frames.root:SetScript("OnUpdate", nil)
end
function WarpDeplete:StopChallengeMode()
self:Hide()
self:ResetState()
self:UnregisterChallengeEvents()
end
function WarpDeplete:CompleteChallengeMode()
--TODO(happens): Refresh information from blizzard timer so we have
-- an accurate finish time. If we load in afterwards this is done automatically,
-- but if we used our own timer we should redo it.
self.challengeState.challengeCompleted = true
self:UpdateTimerDisplay()
self:UpdateObjectivesDisplay()
self:UpdateForcesDisplay()
end
function WarpDeplete:GetTimerInfo()
local mapID = C_ChallengeMode.GetActiveChallengeMapID()
if not mapID then
self:PrintDebug("No map id for timer received")
return false
end
local limit = select(3, C_ChallengeMode.GetMapUIInfo(mapID))
if not limit then
self:PrintDebug("No time limit received")
return false
end
self:SetTimerLimit(limit)
local current = select(2, GetWorldElapsedTime(1))
-- If there already is time elapsed, we're loading into
-- a running key. This means that we're now running on the
-- blizzard timer and should request the current time from
-- someone else.
if current > 2 then --TODO(happens): The WA is using 2 here, is that fine?
self.timerState.isBlizzardTimer = true
-- If we call this without any delay, the timer will be off by 10
-- seconds. The blizzard timer also has this bug and corrects it
-- after the first death. Lmao
C_Timer.After(0.5, function()
local current = select(2, GetWorldElapsedTime(1))
local deaths = C_ChallengeMode.GetDeathCount()
local trueTime = current - deaths * 5
self.timerState.startOffset = trueTime
self.timerState.startTime = GetTime()
self.timerState.isBlizzardTimer = true
self:RequestTimerSync()
self:RequestObjectiveSync()
end)
end
self:SetTimerCurrent(current)
return true
end
function WarpDeplete:GetKeyInfo()
self:PrintDebug("Getting key info")
local level, affixes = C_ChallengeMode.GetActiveKeystoneInfo()
local affixNames = {}
for i, affixID in ipairs(affixes) do
local name = C_ChallengeMode.GetAffixInfo(affixID)
affixNames[i] = name
end
if level <= 0 or #affixNames <= 0 then
self:PrintDebug("No affixes or key level received")
return false
end
self:SetKeyDetails(level or 0, affixNames)
return true
end
function WarpDeplete:GetObjectivesInfo()
self:PrintDebug("Getting objectives info")
local stepCount = select(3, C_Scenario.GetStepInfo())
if stepCount <= 0 then
self:PrintDebug("No steps received, can't update objective info")
return false
end
local currentCount, totalCount = self:GetEnemyForcesCount()
-- The last step will forces, all previous steps are bosses
self:PrintDebug("Got forces info: " .. currentCount .. "/" .. totalCount)
if totalCount <= 0 then
self:PrintDebug("No mob count received")
return false
end
self:SetForcesTotal(totalCount)
self:SetForcesCurrent(currentCount)
local objectives = {}
for i = 1, stepCount - 1 do
local name, _, completed = C_Scenario.GetCriteriaInfo(i)
if not name then break end
name = gsub(name, " defeated", "")
self:PrintDebug("Got boss name for index " .. i .. ": " .. tostring(name))
objectives[i] = { name = name, time = completed and 0 or nil }
end
if #objectives <= 0 then
self:PrintDebug("No objectives received")
return false
end
self:SetObjectives(objectives)
return true
end
function WarpDeplete:GetEnemyForcesCount()
local stepCount = select(3, C_Scenario.GetStepInfo())
local _, _, _, _, totalCount, _, _, mobPointsStr = C_Scenario.GetCriteriaInfo(stepCount)
if not totalCount or not mobPointsStr then return nil, nil end
local currentCountStr = gsub(mobPointsStr, "%%", "")
local currentCount = tonumber(currentCountStr)
return currentCount, totalCount
end
function WarpDeplete:UpdateForces()
if not self.challengeState.inChallenge then return end
local stepCount = select(3, C_Scenario.GetStepInfo())
local currentCount = self:GetEnemyForcesCount()
-- This mostly happens when we have already completed the dungeon
if not currentCount then return end
self:PrintDebug("currentCount: " .. currentCount)
if currentCount >= self.forcesState.totalCount and not self.forcesState.completed then
-- If we just went above the total count (or matched it), we completed it just now
self.forcesState.completed = true
self.forcesState.completedTime = self.timerState.current
end
self:SetForcesCurrent(currentCount)
end
function WarpDeplete:UpdateObjectives()
if not self.challengeState.inChallenge then return end
local objectives = Util.copy(self.objectivesState)
local changed = false
local stepCount = select(3, C_Scenario.GetStepInfo())
for i = 1, stepCount - 1 do
if not objectives[i] or not objectives[i].time then
-- If it wasn't completed before and it is now, we've just completed
-- it and can set the completion time
local completed = select(3, C_Scenario.GetCriteriaInfo(i))
if completed then
objectives[i].time = self.timerState.current
changed = true
end
end
end
if changed then self:SetObjectives(objectives) end
end
function WarpDeplete:ResetCurrentPull()
for k, _ in pairs(self.forcesState.currentPull) do
self.forcesState.currentPull[k] = nil
end
self:SetForcesPull(0)
end
function WarpDeplete:AddDeathDetails(time, name, class)
local len = #self.timerState.deathDetails
self.timerState.deathDetails[len + 1] = {
time = time,
name = name,
class = class
}
end
-- These events are used to detect whether we are in challenge mode
-- or whether we should put a key in the socket, and will always stay registered.
function WarpDeplete:RegisterGlobalEvents()
self:RegisterEvent("PLAYER_ENTERING_WORLD", "OnCheckChallengeMode")
self:RegisterEvent("ZONE_CHANGED_NEW_AREA", "OnCheckChallengeMode")
-- Fired when the countdown hits 0 (and for some reason when we die?)
self:RegisterEvent("WORLD_STATE_TIMER_START", "OnChallengeModeStart")
-- Fired when we open the keystone socket
self:RegisterEvent("CHALLENGE_MODE_KEYSTONE_RECEPTABLE_OPEN", "OnKeystoneOpen")
-- Register tooltip count display
GameTooltip:HookScript("OnTooltipSetUnit", WarpDeplete.DisplayCountInTooltip)
-- Tooltip events
self.frames.deathsTooltip:SetScript("OnEnter", WarpDeplete.TooltipOnEnter)
self.frames.deathsTooltip:SetScript("OnLeave", WarpDeplete.TooltipOnLeave)
end
function WarpDeplete.TooltipOnEnter()
local self = WarpDeplete
if not self.db.profile.showDeathsTooltip then return end
GameTooltip:SetOwner(self.frames.deathsTooltip, "ANCHOR_BOTTOM")
GameTooltip:ClearLines()
local count = #self.timerState.deathDetails
if count == 0 then
GameTooltip:AddLine("No Recorded Player Deaths", 1, 1, 1)
GameTooltip:Show()
return
end
if self.db.profile.deathLogStyle == "time" then
local showFrom = 0
if count > 20 then
showFrom = count - 20
end
GameTooltip:AddLine("Player Deaths", 1, 1, 1)
for i, d in ipairs(self.timerState.deathDetails) do
if i >= showFrom then
local color = select(4, GetClassColor(d.class))
local time = Util.formatTime(d.time)
GameTooltip:AddLine(time .. " - |c" .. color .. d.name .. "|r")
end
end
elseif self.db.profile.deathLogStyle == "count" then
local countTable = {}
for i, d in ipairs(self.timerState.deathDetails) do
if not countTable[d.name] then
countTable[d.name] = {
color = select(4, GetClassColor(d.class)),
count = 0
}
end
countTable[d.name].count = countTable[d.name].count + 1
end
for name, deaths in pairs(countTable) do
GameTooltip:AddLine("|c" .. deaths.color .. name .. "|r|cFFFFFFFF: " .. tostring(deaths.count) .. "|r")
end
end
GameTooltip:Show()
end
function WarpDeplete.TooltipOnLeave()
GameTooltip_Hide()
end
function WarpDeplete.DisplayCountInTooltip()
if not WarpDeplete.timerState.running then return end
if not MDT or not WarpDeplete.db.profile.showTooltipCount then return end
local GUID = UnitGUID("mouseover")
if GUID and MDT then
local npcID = select(6, strsplit("-", GUID))
local count, max = MDT:GetEnemyForces(tonumber(npcID))
if count and max and count ~= 0 and max ~= 0 then
local percentText = ("%.2f"):format(count / max * 100)
local countText = ("%d"):format(count)
local result = WarpDeplete.db.profile.tooltipCountFormat ~= ":custom:" and
WarpDeplete.db.profile.tooltipCountFormat or
WarpDeplete.db.profile.customTooltipCountFormat
result = gsub(result, ":percent:", percentText .. "%%")
result = gsub(result, ":count:", countText)
GameTooltip:AddLine("Count: |cFFFFFFFF" .. result .. "|r")
GameTooltip:Show()
end
end
end
function WarpDeplete:RegisterChallengeEvents()
-- Challenge mode triggers
self:RegisterEvent("CHALLENGE_MODE_START", "OnChallengeModeStart")
self:RegisterEvent("CHALLENGE_MODE_RESET", "OnChallengeModeReset")
self:RegisterEvent("CHALLENGE_MODE_COMPLETED", "OnChallengeModeCompleted")
-- Scenario Triggers
self:RegisterEvent("SCENARIO_POI_UPDATE", "OnScenarioPOIUpdate")
self:RegisterEvent("SCENARIO_CRITERIA_UPDATE", "OnScenarioCriteriaUpdate")
-- Combat triggers
self:RegisterEvent("PLAYER_DEAD", "OnPlayerDead")
self:RegisterEvent("ENCOUNTER_END", "OnResetCurrentPull")
self:RegisterEvent("PLAYER_REGEN_ENABLED", "OnResetCurrentPull")
self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED", "OnCombatLogEvent")
self:RegisterEvent("UNIT_THREAT_LIST_UPDATE", "OnThreatListUpdate")
end
function WarpDeplete:UnregisterChallengeEvents()
self:UnregisterEvent("CHALLENGE_MODE_START")
self:UnregisterEvent("CHALLENGE_MODE_RESET")
self:UnregisterEvent("CHALLENGE_MODE_COMPLETED")
self:UnregisterEvent("SCENARIO_POI_UPDATE")
self:UnregisterEvent("SCENARIO_CRITERIA_UPDATE")
self:UnregisterEvent("PLAYER_DEAD")
self:UnregisterEvent("PLAYER_REGEN_ENABLED")
self:UnregisterEvent("UNIT_THREAT_LIST_UPDATE")
self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
end
function WarpDeplete:OnTimerTick(elapsed)
if not self.challengeState.inChallenge or
self.challengeState.challengeCompleted or
not self.timerState.running then
self:StopChallengeTimer()
return
end
sinceLastUpdate = sinceLastUpdate + elapsed
if sinceLastUpdate <= UPDATE_INTERVAL then return end
sinceLastUpdate = 0
--TODO(happens): We update this a lot, can we do this
-- in a better way so it's not called 10 times a second?
local newDeaths = C_ChallengeMode.GetDeathCount()
if newDeaths ~= self.timerState.deaths then
self:SetDeaths(newDeaths)
end
local deathPenalty = self.timerState.deaths * 5
local current = GetTime() + self.timerState.startOffset - self.timerState.startTime + deathPenalty
-- if current < 0 then return end
self:SetTimerCurrent(current)
end
function WarpDeplete:OnCheckChallengeMode(ev)
self:PrintDebugGlobalEvent(ev)
self:CheckForChallengeMode()
end
function WarpDeplete:OnChallengeModeStart(ev)
self:PrintDebugGlobalEvent(ev)
if self.timerState.running then
self:PrintDebug("Start event received while timer was already running")
return
end
self:StartChallengeTimer()
if not self.challengeState.inChallenge then
self:StartChallengeMode()
end
end
function WarpDeplete:OnPlayerDead(ev)
self:PrintDebugEvent(ev)
self:BroadcastDeath()
self:ResetCurrentPull()
end
function WarpDeplete:OnChallengeModeReset(ev)
self:PrintDebugEvent(ev)
self:ResetState()
end
function WarpDeplete:OnChallengeModeCompleted(ev)
self:PrintDebugEvent(ev)
self:CompleteChallengeMode()
end
function WarpDeplete:OnKeystoneOpen(ev)
self:PrintDebugEvent(ev)
if not self.db.profile.insertKeystoneAutomatically then
return
end
local difficulty = select(3, GetInstanceInfo())
if difficulty ~= 8 and difficulty ~= 23 then
return
end
local found = nil
for bagIndex = 0, NUM_BAG_SLOTS do
for invIndex = 1, GetContainerNumSlots(bagIndex) do
local itemID = GetContainerItemID(bagIndex, invIndex)
if itemID and (itemID == 180653) then
self:PrintDebug("Key found at ("
.. bagIndex .. "," .. invIndex .. ")")
found = {
bagIndex = bagIndex,
invIndex = invIndex
}
break
end
end
if found ~= nil then break end
end
if found ~= nil then
self:PrintDebug("Slotting keystone from ("
.. found.bagIndex .. "," .. found.invIndex .. ")")
PickupContainerItem(found.bagIndex, found.invIndex)
C_Timer.After(0.1, function()
if CursorHasItem() then
C_ChallengeMode.SlotKeystone()
end
end)
end
end
function WarpDeplete:OnScenarioPOIUpdate(ev)
self:PrintDebugEvent(ev)
self:UpdateForces()
self:UpdateObjectives()
end
function WarpDeplete:OnScenarioCriteriaUpdate(ev)
self:PrintDebugEvent(ev)
self:UpdateForces()
self:UpdateObjectives()
end
function WarpDeplete:OnResetCurrentPull(ev)
self:PrintDebugEvent(ev)
self:ResetCurrentPull()
end
function WarpDeplete:OnThreatListUpdate(ev, unit)
self:PrintDebugEvent(ev)
if not MDT then return end
if not InCombatLockdown() or not unit or not UnitExists(unit) then return end
--NOTE(happens): There seem to be cases where a Unit will throw a threat list update
-- after it has died, which falsely re-adds it to the current pull. We set that units'
-- count value to "DEAD" when it dies, and due to the check if the guid already exists
-- in the table, it won't be overwritten after the unit has died.
local guid = UnitGUID(unit)
if not guid or self.forcesState.currentPull[guid] then return end
local npcID = select(6, strsplit("-", guid))
local count = MDT:GetEnemyForces(tonumber(npcID))
if not count or count <= 0 then return end
self:PrintDebug("Adding unit " .. guid .. " to current pull: " .. count)
self.forcesState.currentPull[guid] = count
local pullCount = Util.calcPullCount(self.forcesState.currentPull, self.forcesState.totalCount)
self:SetForcesPull(pullCount)
end
function WarpDeplete:OnCombatLogEvent(ev)
local _, subEv, _, _, _, _, _, guid = CombatLogGetCurrentEventInfo()
if subEv ~= "UNIT_DIED" then return end
self:PrintDebugEvent(ev)
if not guid or not self.forcesState.currentPull[guid] then return end
self:PrintDebug("removing unit " .. guid .. " from current pull")
-- See comment above (OnThreadListUpdate)
self.forcesState.currentPull[guid] = "DEAD"
local pullCount = Util.calcPullCount(self.forcesState.currentPull, self.forcesState.totalCount)
self:SetForcesPull(pullCount)
end