Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
846 lines (729 loc) · 26 KB
/
Copy pathCore.lua
File metadata and controls
846 lines (729 loc) · 26 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
-- Raid Locks - data layer.
--
-- Instance, difficulty and reset time come from GetSavedInstanceInfo, the same
-- as on any 3.3.5 client. Boss progress comes from Ascension's own loot-lockout
-- API, which their client ships as Lua inside patch-B.MPQ rather than as an exe
-- export - the client's own Raid Info tooltip is built on it:
--
-- GetEncounterDatasForMapAndDifficulty("player", mapID, difficulty)
-- -> { [encounterID] = { EncounterName = ..., TimeRemaining = ... } }
-- TimeRemaining > 0 means locked to that boss
-- GetLootLockouts("player") -> mapID -> difficultyID (0 based) -> lock
-- C_LootLockout.QueryInstanceBinds() -> ask the server to resend them
-- GetMapID(name) / GetMapName(mapID)
--
-- That is exact, per boss, with its own expiry, available at any time and
-- without walking into the raid. Everything below is written to prefer it and
-- to keep working if a future client drops it:
-- 1. the loot-lockout API (authoritative)
-- 2. the zone-in lock query, GetInstanceLockTimeRemainingEncounter
-- 3. the combat log, for kills during the run
-- 4. shift-click in the panel
local ADDON, RaidLocks = ...
RaidLocks.name = ADDON
local DAY = 86400
-- A lockout only ever reports what is left, never how long it was, so the row
-- depletion bar needs a total: the largest remaining time ever seen for this
-- lock, rounded up to one of the lengths the server actually hands out.
local LOCK_BUCKETS = { DAY, 3 * DAY, 5 * DAY, 7 * DAY, 14 * DAY }
local defaults = {
locks = {},
raidsOnly = true,
autoRecord = true,
showOpen = true,
view = "locks", -- "locks" | "history"
history = {},
sort = "time",
point = "CENTER",
x = 0,
y = 0,
shown = false,
minimap = { hide = false },
}
--------------------------------------------------------------------
-- Helpers
--------------------------------------------------------------------
function RaidLocks:Print(msg)
DEFAULT_CHAT_FRAME:AddMessage("|cff8fb8ffRaid Locks|r " .. tostring(msg))
end
-- "2d 14h", "19h 05m", "42m". Not SecondsToTime: the panel needs a fixed
-- two-unit shape so the right-hand column stays aligned down the list.
function RaidLocks:FormatDuration(seconds)
if not seconds or seconds <= 0 then return "expired" end
local d = math.floor(seconds / DAY)
local h = math.floor((seconds % DAY) / 3600)
local m = math.floor((seconds % 3600) / 60)
if d > 0 then
return string.format("%dd %dh", d, h)
elseif h > 0 then
return string.format("%dh %02dm", h, m)
end
return string.format("%dm", m)
end
function RaidLocks:FormatResetClock(seconds)
if not seconds or seconds <= 0 then return "" end
return date("%a %H:%M", time() + seconds)
end
local function BucketFor(seconds)
for _, bucket in ipairs(LOCK_BUCKETS) do
if seconds <= bucket then return bucket end
end
return seconds
end
function RaidLocks:LockKey(name, difficulty, instanceID)
return string.format("%s|%d|%d", name or "?", difficulty or 0, instanceID or 0)
end
-- Boss list for an instance, from the client's own DungeonEncounter.dbc.
function RaidLocks:EncountersFor(instanceName)
return self.encounters and self.encounters[instanceName]
end
-- The roster the panel draws: the live loot-lockout list when the client offers
-- one, then whatever the zone-in query reported, then the static list.
function RaidLocks:RosterFor(lock)
if lock.roster and #lock.roster > 0 then
return lock.roster, true
end
if lock.saved.roster and #lock.saved.roster > 0 then
return lock.saved.roster, true
end
return self:EncountersFor(lock.name), false
end
-- What kind of content this lockout is. Ascension's single-encounter PvE
-- bosses sit in the raid list next to real raids, and they behave nothing like
-- them, so they get their own class.
function RaidLocks:KindOf(lock)
if not lock.isRaid then return "dungeon" end
local roster = self:RosterFor(lock)
if roster then
if #roster == 1 then return "worldboss" end
return "raid"
end
-- No boss list to count: Ascension tags these encounters in the name.
if string.find(lock.name, "(PvE)", 1, true) then return "worldboss" end
return "raid"
end
--------------------------------------------------------------------
-- History
--
-- A lockout that resets vanishes from the server's list and takes its boss
-- progress with it, so the week you just cleared leaves no trace at all. Every
-- lock with kills on it is archived on the way out, which is what makes "what
-- did I actually clear last week" answerable.
--------------------------------------------------------------------
local HISTORY_MAX = 40
local HISTORY_KEEP_DAYS = 60
function RaidLocks:Archive(saved)
if not saved or not saved.name then return end
local killed = saved.killed or 0
if killed <= 0 then return end
local db = self.db
db.history = db.history or {}
table.insert(db.history, 1, {
name = saved.name,
difficultyName = saved.difficultyName,
difficulty = saved.difficulty,
killed = killed,
total = saved.total_bosses,
full = saved.total_bosses and killed >= saved.total_bosses or false,
clearedAt = saved.lastSeen or time(),
})
-- Newest first, capped, and nothing older than two months: this is a
-- reminder of recent weeks, not a career log.
local cutoff = time() - (HISTORY_KEEP_DAYS * DAY)
local kept = {}
for _, entry in ipairs(db.history) do
if #kept < HISTORY_MAX and (entry.clearedAt or 0) >= cutoff then
kept[#kept + 1] = entry
end
end
db.history = kept
end
function RaidLocks:GetHistory()
return self.db and self.db.history or {}
end
function RaidLocks:ClearHistory()
if self.db then self.db.history = {} end
if self.OnDataChanged then self:OnDataChanged() end
end
-- "3d ago", "5h ago", "just now" - a lockout resets on a schedule, so how long
-- ago reads better here than a date.
function RaidLocks:FormatAgo(when)
if not when then return "" end
local elapsed = time() - when
if elapsed < 300 then return "just now" end
if elapsed < 3600 then return string.format("%dm ago", math.floor(elapsed / 60)) end
if elapsed < DAY then return string.format("%dh ago", math.floor(elapsed / 3600)) end
return string.format("%dd ago", math.floor(elapsed / DAY))
end
-- Which expansion each raid belongs to. Hand maintained on purpose: the DBC
-- carries no expansion field for these maps, and Ascension's own raids belong
-- to none of them. Anything not listed here falls into "custom", which is the
-- right home for a bespoke Ascension encounter.
RaidLocks.expansionOf = {
-- Classic
["Molten Core"] = "vanilla",
["Blackwing Lair"] = "vanilla",
["Ahn'Qiraj Temple"] = "vanilla",
["Ruins of Ahn'Qiraj"] = "vanilla",
["Onyxia's Lair"] = "vanilla",
["Zul'Gurub"] = "vanilla",
["Azuregos (PvE)"] = "vanilla",
["Lord Kazzak (PvE)"] = "vanilla",
["Emeriss (PvE)"] = "vanilla",
["Lethon (PvE)"] = "vanilla",
["Taerar (PvE)"] = "vanilla",
["Ysondre (PvE)"] = "vanilla",
-- The Burning Crusade
["Karazhan"] = "tbc",
["Gruul's Lair"] = "tbc",
["Magtheridon's Lair"] = "tbc",
["Coilfang: Serpentshrine Cavern"] = "tbc",
["Tempest Keep"] = "tbc",
["The Battle for Mount Hyjal"] = "tbc",
["Black Temple"] = "tbc",
["The Sunwell"] = "tbc",
["Zul'Aman"] = "tbc",
-- Wrath of the Lich King
["Naxxramas"] = "wotlk",
["The Obsidian Sanctum"] = "wotlk",
["The Eye of Eternity"] = "wotlk",
["Vault of Archavon"] = "wotlk",
["Ulduar"] = "wotlk",
["Trial of the Crusader"] = "wotlk",
["Icecrown Citadel"] = "wotlk",
["The Ruby Sanctum"] = "wotlk",
}
RaidLocks.expansionOrder = { "vanilla", "tbc", "wotlk", "custom" }
function RaidLocks:ExpansionOf(name)
return (self.expansionOf and self.expansionOf[name]) or "custom"
end
-- What is still open: every raid-type instance this client knows that the
-- player holds no lock on, at any difficulty. The other half of the question a
-- lockout panel is asked - not just what is spent, but what is left tonight.
function RaidLocks:OpenInstances()
local raids, bosses = {}, {}
if not self.raidKinds then return raids, bosses end
local locked = {}
for _, lock in ipairs(self.locks) do
if lock.locked then locked[lock.name] = true end
end
for name, kind in pairs(self.raidKinds) do
if not locked[name] then
if kind == "worldboss" then
bosses[#bosses + 1] = name
else
raids[#raids + 1] = name
end
end
end
table.sort(raids)
table.sort(bosses)
return raids, bosses
end
-- The same list, cut by expansion. A player asking "what can I still run
-- tonight" is nearly always asking it about one expansion at a time, and a
-- single 30-name column answers nothing. Returns an ordered array of
-- { key = "vanilla", entries = { { name = ..., kind = "raid" }, ... } }
-- with raids ahead of world bosses inside each expansion.
function RaidLocks:OpenByExpansion()
local raids, bosses = self:OpenInstances()
local buckets = {}
local function add(name, kind)
local key = self:ExpansionOf(name)
buckets[key] = buckets[key] or { raid = {}, worldboss = {} }
local list = buckets[key][kind]
list[#list + 1] = name
end
for _, name in ipairs(raids) do add(name, "raid") end
for _, name in ipairs(bosses) do add(name, "worldboss") end
local groups = {}
for _, key in ipairs(self.expansionOrder) do
local bucket = buckets[key]
if bucket then
local entries = {}
for _, kind in ipairs({ "raid", "worldboss" }) do
for _, name in ipairs(bucket[kind]) do
entries[#entries + 1] = { name = name, kind = kind }
end
end
if #entries > 0 then
groups[#groups + 1] = { key = key, entries = entries }
end
end
end
return groups
end
--------------------------------------------------------------------
-- Ascension's loot-lockout API
--------------------------------------------------------------------
function RaidLocks:HasLockoutAPI()
return type(GetEncounterDatasForMapAndDifficulty) == "function"
and type(GetLootLockouts) == "function"
and type(GetMapID) == "function"
end
-- pairs() over an encounterID-keyed table has no useful order, so put the
-- bosses back in the order the client's own encounter table lists them and
-- append anything unrecognised behind it.
local function OrderEncounters(self, instanceName, entries)
local known = self:EncountersFor(instanceName)
local roster, seen = {}, {}
if known then
for _, boss in ipairs(known) do
if entries[boss] then
roster[#roster + 1] = boss
seen[boss] = true
end
end
end
local rest = {}
for boss in pairs(entries) do
if not seen[boss] then rest[#rest + 1] = boss end
end
table.sort(rest, function(a, b) return entries[a].id < entries[b].id end)
for _, boss in ipairs(rest) do roster[#roster + 1] = boss end
return roster
end
-- Fills lock.roster and lock.bossInfo from the loot lockouts. Returns false if
-- the client has nothing to say about this instance.
function RaidLocks:ApplyLootLockouts(lock)
if not self:HasLockoutAPI() then return false end
local mapID = lock.mapID
if not mapID then
local ok, id = pcall(GetMapID, lock.name)
mapID = ok and id or nil
-- GetMapID works off the client's own map names, which do not always
-- match the name a lockout carries; our generated table covers the rest.
if not mapID and self.mapIDs then mapID = self.mapIDs[lock.name] end
lock.mapID = mapID
end
if not mapID then return false end
local ok, data = pcall(GetEncounterDatasForMapAndDifficulty, "player", mapID, lock.difficulty)
if not ok or type(data) ~= "table" then return false end
local entries, count = {}, 0
for encounterID, info in pairs(data) do
local name = info and info.EncounterName
if name and name ~= "" then
local remaining = tonumber(info.TimeRemaining) or 0
entries[name] = { id = tonumber(encounterID) or 0, remaining = remaining }
count = count + 1
end
end
if count == 0 then return false end
lock.roster = OrderEncounters(self, lock.name, entries)
lock.bossInfo = entries
lock.source = "lockouts"
return true
end
-- Ascension can lock you to bosses without a classic instance save, and those
-- never appear in GetSavedInstanceInfo. The client's own Raid Info window folds
-- them in the same way (C_Instance:GetInstanceLocks), so this panel does too.
function RaidLocks:AddLootOnlyLockouts(list, seen)
if not self:HasLockoutAPI() then return end
local ok, lockouts = pcall(GetLootLockouts, "player")
if not ok or type(lockouts) ~= "table" then return end
for mapID, difficulties in pairs(lockouts) do
if type(difficulties) == "table" then
for difficultyID in pairs(difficulties) do
-- GetLootLockouts counts difficulties from zero, everything
-- else on this client counts from one.
local difficulty = (tonumber(difficultyID) or 0) + 1
local nameOk, name = pcall(GetMapName, mapID)
name = nameOk and name or nil
if name then
local key = self:LockKey(name, difficulty, -1)
local duplicate = false
for _, existing in ipairs(list) do
if existing.name == name and existing.difficulty == difficulty then
duplicate = true
break
end
end
if not duplicate then
local saved = self.db.locks[key]
if not saved then
saved = { bosses = {}, total = 0 }
self.db.locks[key] = saved
end
local entry = {
key = key,
name = name,
mapID = mapID,
instanceID = -1,
difficulty = difficulty,
difficultyName = _G["RAID_DIFFICULTY" .. difficulty],
locked = true,
extended = false,
isRaid = true,
lootOnly = true,
reset = 0,
saved = saved,
}
if self:ApplyLootLockouts(entry) then
-- No instance save means no reset time of its own;
-- the longest boss lock is when it actually clears.
local longest = 0
for _, info in pairs(entry.bossInfo) do
if info.remaining > longest then longest = info.remaining end
end
entry.reset = longest
if longest > (saved.total or 0) then saved.total = longest end
seen[key] = true
list[#list + 1] = entry
else
self.db.locks[key] = nil
end
end
end
end
end
end
end
--------------------------------------------------------------------
-- Scanning
--------------------------------------------------------------------
RaidLocks.locks = {}
RaidLocks.lastScan = 0
local function CompareByTime(a, b)
if a.locked ~= b.locked then return a.locked end
if a.reset == b.reset then return a.name < b.name end
return a.reset < b.reset
end
local function CompareByName(a, b)
if a.name == b.name then return (a.maxPlayers or 0) < (b.maxPlayers or 0) end
return a.name < b.name
end
function RaidLocks:Scan()
local db = self.db
if not db then return end
-- GetLootLockouts("player") is one of the calls that crashes the client
-- before the player exists, so a scan waits for the world like Refresh does.
if not self:IsWorldReady() then return end
local seen, list = {}, {}
for index = 1, GetNumSavedInstances() do
local name, instanceID, reset, difficulty, locked, extended, _, isRaid, maxPlayers, difficultyName =
GetSavedInstanceInfo(index)
if name then
local key = self:LockKey(name, difficulty, instanceID)
seen[key] = true
local saved = db.locks[key]
if not saved then
saved = { bosses = {}, total = 0 }
db.locks[key] = saved
end
if reset and reset > (saved.total or 0) then
saved.total = BucketFor(reset)
end
local entry = {
index = index,
key = key,
name = name,
instanceID = instanceID,
reset = reset or 0,
difficulty = difficulty or 0,
difficultyName = (difficultyName and difficultyName ~= "" and difficultyName) or nil,
locked = locked and true or false,
extended = extended and true or false,
isRaid = isRaid and true or false,
maxPlayers = maxPlayers,
saved = saved,
}
self:ApplyLootLockouts(entry)
-- Snapshot enough to archive this lock after it resets: once the
-- server stops reporting it, none of this is recoverable.
local killed, bosses = self:KillCount(entry)
saved.name = name
saved.difficulty = difficulty
saved.difficultyName = entry.difficultyName
saved.killed = killed
saved.total_bosses = bosses
saved.lastSeen = time()
list[#list + 1] = entry
end
end
self:AddLootOnlyLockouts(list, seen)
-- A lockout the server no longer reports has reset. Its boss progress is
-- about to be dropped, so anything worth remembering is archived first -
-- that archive is the only record that the week was cleared at all.
--
-- An empty list on the login scan is not proof of a reset: the server has
-- not sent the instance list yet. Pruning then would archive and delete
-- every live lockout, so the first scan only prunes once the server has
-- actually answered with UPDATE_INSTANCE_INFO.
if #list > 0 or self.instanceInfoSeen then
for key, saved in pairs(db.locks) do
if not seen[key] then
self:Archive(saved)
db.locks[key] = nil
end
end
end
table.sort(list, db.sort == "name" and CompareByName or CompareByTime)
self.locks = list
self.lastScan = GetTime()
self:ApplyPending()
if self.OnDataChanged then self:OnDataChanged() end
return list
end
function RaidLocks:VisibleLocks()
local out = {}
for _, lock in ipairs(self.locks) do
if lock.isRaid or not self.db.raidsOnly then
out[#out + 1] = lock
end
end
return out
end
-- True when this boss is already down. The loot lockout is the truth when the
-- client provides one; our own records answer for everything else.
function RaidLocks:IsBossKilled(lock, bossName)
if lock.bossInfo then
local info = lock.bossInfo[bossName]
if info then return info.remaining > 0 end
end
return lock.saved.bosses[bossName] and true or false
end
-- Seconds left on one boss's own lock, when the client tracks them separately.
function RaidLocks:BossRemaining(lock, bossName)
local info = lock.bossInfo and lock.bossInfo[bossName]
return info and info.remaining or nil
end
function RaidLocks:KillCount(lock)
local roster = self:RosterFor(lock)
local killed = 0
if roster then
for _, boss in ipairs(roster) do
if self:IsBossKilled(lock, boss) then killed = killed + 1 end
end
return killed, #roster
end
for _ in pairs(lock.saved.bosses) do killed = killed + 1 end
return killed, nil
end
function RaidLocks:SetBossKilled(lock, bossName, killed)
-- Overriding a lockout the server itself reported would only ever make the
-- panel lie, so the override stops at the door.
if lock.bossInfo and lock.bossInfo[bossName] then
self:Print("that boss comes straight from the server - nothing to correct.")
return
end
lock.saved.bosses[bossName] = killed and true or nil
lock.saved.source = "manual"
if self.OnDataChanged then self:OnDataChanged() end
end
-- Every lockout call below reaches into the player object inside the client.
-- Asked for before the player exists - ADDON_LOADED fires while the world is
-- still loading - the client dereferences a null pointer and takes the whole
-- game down with a #132 ACCESS_VIOLATION, which no pcall can catch because the
-- fault is in C, not in Lua. Nothing touches the server data until this is set.
RaidLocks.worldReady = false
function RaidLocks:IsWorldReady()
if not self.worldReady then return false end
-- Belt and braces: the flag says the event fired, this says the object the
-- client is about to dereference is actually there.
return UnitGUID and UnitGUID("player") ~= nil
end
function RaidLocks:Refresh()
if not self:IsWorldReady() then return end
RequestRaidInfo()
-- Ask for the loot lockouts as well; the client's own UI does this whenever
-- it needs them fresh.
if C_LootLockout and type(C_LootLockout.QueryInstanceBinds) == "function" then
pcall(C_LootLockout.QueryInstanceBinds)
end
end
--------------------------------------------------------------------
-- Source 1: the lock query
--------------------------------------------------------------------
local function FindLock(self, name, difficulty)
for _, lock in ipairs(self.locks) do
if lock.name == name and lock.difficulty == difficulty then return lock end
end
end
function RaidLocks:StoreCapture(lock, roster, killed)
lock.saved.roster = roster
lock.saved.bosses = {}
for _, boss in ipairs(roster) do
if killed[boss] then lock.saved.bosses[boss] = true end
end
lock.saved.source = "server"
lock.saved.capturedAt = time()
if self.OnDataChanged then self:OnDataChanged() end
end
-- Reads whatever the lock query is currently holding. Returns true once real
-- encounter data is in hand, which is the signal for the poller to stop.
function RaidLocks:CaptureLockDetail()
-- Only a fallback: with the loot-lockout API present the panel already has
-- better data than this query can give.
if self:HasLockoutAPI() then return true end
if not GetInstanceLockTimeRemaining then return false end
local ok, timeLeft, _, encountersTotal, encountersComplete = pcall(GetInstanceLockTimeRemaining)
if not ok or type(timeLeft) ~= "number" or timeLeft <= 0 then return false end
if not encountersTotal or encountersTotal <= 0 then return false end
local instanceName, _, difficulty = GetInstanceInfo()
if not instanceName then return false end
local roster, killed = {}, {}
for i = 1, encountersTotal do
local bossOk, bossName, _, isKilled = pcall(GetInstanceLockTimeRemainingEncounter, i)
if bossOk and bossName and bossName ~= "" then
roster[#roster + 1] = bossName
if isKilled then killed[bossName] = true end
end
end
if #roster == 0 then return false end
local lock = FindLock(self, instanceName, difficulty)
if not lock then
-- The lockout itself may not have reached us yet; ask for it and apply
-- this on the next scan.
self.pendingCapture = {
name = instanceName,
difficulty = difficulty,
roster = roster,
killed = killed,
at = time(),
}
self:Refresh()
return true
end
self:StoreCapture(lock, roster, killed)
self:Print(string.format("%s: %d of %d bosses already down, straight from the server.",
instanceName, encountersComplete or 0, encountersTotal))
return true
end
-- The lock query lands a moment after the zone-in, so watch for it rather than
-- guessing a delay, and give up quietly when nothing arrives.
local poller = CreateFrame("Frame")
poller.elapsed = 0
poller.deadline = 0
poller:Hide()
poller:SetScript("OnUpdate", function(self, elapsed)
self.elapsed = self.elapsed + elapsed
if self.elapsed < 1 then return end
self.elapsed = 0
if RaidLocks:CaptureLockDetail() or GetTime() > self.deadline then
self:Hide()
end
end)
function RaidLocks:WatchForLockDetail(seconds)
poller.elapsed = 0
poller.deadline = GetTime() + (seconds or 30)
poller:Show()
end
--------------------------------------------------------------------
-- Source 2: the combat log
--------------------------------------------------------------------
-- A boss can die before the lockout exists - the first kill is what saves you -
-- so kills land here and are applied on the next scan.
RaidLocks.pending = {}
function RaidLocks:RecordKill(instanceName, difficulty, bossName)
self.pending[#self.pending + 1] = {
name = instanceName,
difficulty = difficulty,
boss = bossName,
at = time(),
}
self:Refresh()
end
function RaidLocks:ApplyPending()
local capture = self.pendingCapture
if capture then
local lock = FindLock(self, capture.name, capture.difficulty)
if lock then
self:StoreCapture(lock, capture.roster, capture.killed)
self.pendingCapture = nil
elseif (time() - capture.at) > 180 then
self.pendingCapture = nil
end
end
if #self.pending == 0 then return end
local unmatched = {}
for _, kill in ipairs(self.pending) do
local lock = FindLock(self, kill.name, kill.difficulty)
if lock then
lock.saved.bosses[kill.boss] = true
if lock.saved.roster then
local known = false
for _, boss in ipairs(lock.saved.roster) do
if boss == kill.boss then known = true break end
end
if not known then table.insert(lock.saved.roster, kill.boss) end
end
elseif (time() - kill.at) < 180 then
-- Keep it briefly: the save may still be on its way.
unmatched[#unmatched + 1] = kill
end
end
self.pending = unmatched
end
--------------------------------------------------------------------
-- Events
--------------------------------------------------------------------
local driver = CreateFrame("Frame")
local function SafeRegister(event)
-- This client throws on an event name it does not know, which would abort
-- the rest of the file, so every registration is guarded.
pcall(driver.RegisterEvent, driver, event)
end
driver:SetScript("OnEvent", function(self, event, ...)
if event == "ADDON_LOADED" then
local addon = ...
if addon ~= ADDON then return end
RaidLocksDB = RaidLocksDB or {}
RaidLocks.db = RaidLocksDB
for k, v in pairs(defaults) do
if RaidLocks.db[k] == nil then RaidLocks.db[k] = v end
end
RaidLocks.db.locks = RaidLocks.db.locks or {}
RaidLocks.db.minimap = RaidLocks.db.minimap or { hide = false }
if RaidLocks.OnDatabaseReady then RaidLocks:OnDatabaseReady() end
return
end
if event == "PLAYER_LOGIN" then
RaidLocks.worldReady = true
RaidLocks:Scan()
RaidLocks:Refresh()
return
end
if event == "PLAYER_ENTERING_WORLD" then
RaidLocks.worldReady = true
RaidLocks:Scan()
RaidLocks:Refresh()
if IsInInstance() then RaidLocks:WatchForLockDetail(30) end
return
end
if event == "UPDATE_INSTANCE_INFO" then
RaidLocks.instanceInfoSeen = true
RaidLocks:Scan()
return
end
if event == "RAID_INSTANCE_WELCOME" then
RaidLocks:Refresh()
RaidLocks:WatchForLockDetail(30)
return
end
if event == "COMBAT_LOG_EVENT_UNFILTERED" then
if not RaidLocks.db or not RaidLocks.db.autoRecord then return end
-- The loot lockouts already know, exactly, who is down.
if RaidLocks:HasLockoutAPI() then return end
local _, subEvent, _, _, _, _, destName = ...
if subEvent ~= "UNIT_DIED" or not destName then return end
if not IsInInstance() then return end
local instanceName, _, difficulty = GetInstanceInfo()
if not instanceName then return end
local bosses = RaidLocks:EncountersFor(instanceName)
if not bosses then return end
for _, boss in ipairs(bosses) do
if boss == destName then
RaidLocks:RecordKill(instanceName, difficulty, boss)
return
end
end
return
end
end)
SafeRegister("ADDON_LOADED")
SafeRegister("PLAYER_LOGIN")
SafeRegister("PLAYER_ENTERING_WORLD")
SafeRegister("UPDATE_INSTANCE_INFO")
SafeRegister("RAID_INSTANCE_WELCOME")
SafeRegister("COMBAT_LOG_EVENT_UNFILTERED")
RaidLocks.driver = driver