-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefileAlert.lua
More file actions
592 lines (518 loc) · 18.7 KB
/
DefileAlert.lua
File metadata and controls
592 lines (518 loc) · 18.7 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
local GetTime = GetTime
local UnitGUID = UnitGUID
local UnitName = UnitName
local UnitExists = UnitExists
local UnitIsUnit = UnitIsUnit
local GetNumRaidMembers = GetNumRaidMembers
local IsRaidLeader = IsRaidLeader
local IsRaidOfficer = IsRaidOfficer
local SendChatMessage = SendChatMessage
local PlaySoundFile = PlaySoundFile
local CreateFrame = CreateFrame
local select = select
local tonumber = tonumber
local pairs = pairs
local format = string.format
local strsub = string.sub
local strfind = string.find
local strtrim = strtrim
local strlower = string.lower
local print = print
local type = type
local UIParent = UIParent
local SetMapToCurrentZone = SetMapToCurrentZone
local GetCurrentMapAreaID = GetCurrentMapAreaID
local ICC_MAP_ID = 605
local ADDON_VERSION = "1.2.0"
local DEFILE_IDS = {
[72762] = true,
[73708] = true,
[73709] = true,
[73710] = true,
}
local LK_NPC_ID = 36597
local EV_CLEU = "COMBAT_LOG_EVENT_UNFILTERED"
local EV_SPELL_CAST_START = "SPELL_CAST_START"
local EV_SPELL_SUMMON = "SPELL_SUMMON"
local DEBOUNCE_SEC = 2.0
local SCAN_TIMEOUT = 0.4
local COUNTDOWN_PRE = 5.0
local COUNTDOWN_SOUND = "Interface\\AddOns\\DefileAlert\\Sounds\\DefileCountdown.ogg"
local ALERT_SOUND = "Interface\\AddOns\\DefileAlert\\Sounds\\AirHorn.ogg"
local pending = false
local lastAnnounce = 0
local lkGUID = nil
local zoneActive = false
local detected = false
local pendingGUID = nil
local pendingStart = 0
local diagMode = false
local castDetectTime = 0
local scanStartTarget = nil
local nextCountdownTime = 0
local countdownPlayed = false
local dbmRegistered = false
local TARG = {
boss1 = "boss1target",
boss2 = "boss2target",
boss3 = "boss3target",
boss4 = "boss4target",
focus = "focustarget",
target = "targettarget",
}
local defaults = {
announceEnabled = true,
announceChannel = "RAID_WARNING",
downgradeToRaid = true,
whisperTarget = true,
flashSelf = true,
flashOther = false,
soundEnabled = true,
centerText = true,
selfFlashColor = { r = 1, g = 0, b = 0, a = 0.45 },
otherFlashColor = { r = 0, g = 0.8, b = 0, a = 0.35 },
centerTextColor = { r = 1, g = 1, b = 0 },
selfTextColor = { r = 1, g = 0.1, b = 0.1 },
raidMessage = "{skull} DEFILE >> %s << {skull}",
whisperMessage = "{skull} DEFILE ON YOU — MOVE! {skull}",
soundFile = "Interface\\AddOns\\DefileAlert\\Sounds\\AirHorn.ogg",
textScale = 46,
flashDuration = 0.9,
textDuration = 3.5,
countdownEnabled = true,
}
local db
DefileAlertAPI = {}
local function IsInICC()
SetMapToCurrentZone()
local mapID = GetCurrentMapAreaID()
return mapID == ICC_MAP_ID
end
local function IsGUID(str)
if not str then return true end
if str == "" then return true end
return strfind(str, "^0x") ~= nil
end
local function GUIDtoNPC(guid)
if not guid then return 0 end
return tonumber(strsub(guid, 9, 12), 16) or 0
end
local function GUIDtoPlayerName(guid)
if not guid then return nil end
local n = GetNumRaidMembers()
if n > 0 then
for i = 1, n do
local uid = "raid" .. i
if UnitGUID(uid) == guid then
return UnitName(uid)
end
end
else
if UnitGUID("player") == guid then
return UnitName("player")
end
if UnitExists("party1") then
for i = 1, 4 do
local uid = "party" .. i
if UnitExists(uid) and UnitGUID(uid) == guid then
return UnitName(uid)
end
end
end
end
return nil
end
local function FindLKUnit(knownGUID)
for i = 1, 4 do
local uid = "boss" .. i
if UnitExists(uid) then
local g = UnitGUID(uid)
if g and ((knownGUID and g == knownGUID)
or GUIDtoNPC(g) == LK_NPC_ID) then
return uid
end
end
end
if UnitExists("focus") then
local g = UnitGUID("focus")
if g and ((knownGUID and g == knownGUID)
or GUIDtoNPC(g) == LK_NPC_ID) then
return "focus"
end
end
if UnitExists("target") then
local g = UnitGUID("target")
if g and ((knownGUID and g == knownGUID)
or GUIDtoNPC(g) == LK_NPC_ID) then
return "target"
end
end
return nil
end
local function ReadBossTargetName(lkUnit)
if not lkUnit then return nil end
local t = TARG[lkUnit]
if t then return UnitName(t) end
return UnitName(lkUnit .. "target")
end
local function RegisterDBMCallback()
if dbmRegistered then return end
if not DBM or not DBM.RegisterCallback then return end
DBM:RegisterCallback("DBM_TimerStart", function(event, id, msg, timer, icon, timerType, spellId)
local sid = tonumber(spellId) or 0
if not DEFILE_IDS[sid] then return end
if not db or not db.countdownEnabled then return end
nextCountdownTime = GetTime() + timer - COUNTDOWN_PRE
countdownPlayed = false
if diagMode then
print("|cff00ffff[DIAG]|r DBM timer: " .. tostring(timer) .. "s, voice at -5s")
end
end)
DBM:RegisterCallback("DBM_TimerStop", function(event, id)
if id and (tostring(id):find("72762") or tostring(id):find("73708")
or tostring(id):find("73709") or tostring(id):find("73710")
or tostring(id):find("efile")) then
nextCountdownTime = 0
countdownPlayed = false
end
end)
dbmRegistered = true
end
local function InitDB()
if type(DefileAlertDB) ~= "table" then DefileAlertDB = {} end
for k, v in pairs(defaults) do
if DefileAlertDB[k] == nil then
if type(v) == "table" then
DefileAlertDB[k] = {}
for k2, v2 in pairs(v) do DefileAlertDB[k][k2] = v2 end
else
DefileAlertDB[k] = v
end
end
end
db = DefileAlertDB
if db.soundFile == "Sound\\interface\\RaidWarning.wav" then
db.soundFile = defaults.soundFile
end
if not db.soundFile or db.soundFile == "" then
db.soundFile = defaults.soundFile
end
if db.otherFlashColor and db.otherFlashColor.r == 0
and db.otherFlashColor.g == 0.3 and db.otherFlashColor.b == 1 then
db.otherFlashColor.r = defaults.otherFlashColor.r
db.otherFlashColor.g = defaults.otherFlashColor.g
db.otherFlashColor.b = defaults.otherFlashColor.b
db.otherFlashColor.a = defaults.otherFlashColor.a
end
DefileAlertAPI.db = db
DefileAlertAPI.defaults = defaults
DefileAlertAPI.version = ADDON_VERSION
DefileAlertAPI.zoneActive = function() return zoneActive end
DefileAlertAPI.lkUnit = function() return lkGUID end
end
local flashFrame = CreateFrame("Frame", "DefileAlertFlash", UIParent)
flashFrame:SetFrameStrata("TOOLTIP")
flashFrame:SetAllPoints(UIParent)
flashFrame:Hide()
local flashTex = flashFrame:CreateTexture(nil, "BACKGROUND")
flashTex:SetAllPoints(flashFrame)
local flashElapsed = 0
flashFrame:SetScript("OnUpdate", function(self, dt)
flashElapsed = flashElapsed + dt
local dur = db and db.flashDuration or 0.9
local fade = dur * 0.33
if flashElapsed >= dur then
self:Hide()
return
end
if flashElapsed > fade then
local alpha = flashTex._baseAlpha or 0.4
flashTex:SetAlpha(alpha * (dur - flashElapsed) / (dur - fade))
end
end)
local function FlashScreen(r, g, b, a)
flashTex:SetTexture(r, g, b, a or 0.4)
flashTex:SetAlpha(a or 0.4)
flashTex._baseAlpha = a or 0.4
flashElapsed = 0
flashFrame:Show()
end
DefileAlertAPI.FlashScreen = FlashScreen
local textFrame = CreateFrame("Frame", "DefileAlertText", UIParent)
textFrame:SetFrameStrata("FULLSCREEN_DIALOG")
textFrame:SetAllPoints(UIParent)
textFrame:Hide()
local alertText = textFrame:CreateFontString(nil, "OVERLAY")
alertText:SetFont("Fonts\\FRIZQT__.TTF", 46, "OUTLINE")
alertText:SetPoint("CENTER", 0, 180)
alertText:SetShadowOffset(2, -2)
alertText:SetShadowColor(0, 0, 0, 1)
local textElapsed = 0
textFrame:SetScript("OnUpdate", function(self, dt)
textElapsed = textElapsed + dt
local dur = db and db.textDuration or 3.5
local fade = dur - 1.0
if textElapsed >= dur then self:Hide(); return end
if textElapsed > fade then alertText:SetAlpha(dur - textElapsed) end
end)
local function ShowCenterText(text, r, g, b)
local size = db and db.textScale or 46
alertText:SetFont("Fonts\\FRIZQT__.TTF", size, "OUTLINE")
alertText:SetText(text)
alertText:SetTextColor(r or 1, g or 1, b or 0, 1)
alertText:SetAlpha(1)
textElapsed = 0
textFrame:Show()
end
DefileAlertAPI.ShowCenterText = ShowCenterText
local function AnnounceDefile(targetName, source)
if not targetName or targetName == "" then return end
if IsGUID(targetName) then return end
local now = GetTime()
if (now - lastAnnounce) < DEBOUNCE_SEC then return end
lastAnnounce = now
detected = true
if diagMode and source then
local delay = (castDetectTime > 0) and format("%.0fms", (now - castDetectTime) * 1000) or "?"
print("|cff00ffff[DIAG]|r Announced via: |cff00ff00" .. source .. "|r delay: " .. delay)
end
local isMe = UnitIsUnit(targetName, "player")
if db.centerText then
if isMe then
local c = db.selfTextColor
ShowCenterText(">>> DEFILE ON YOU <<<", c.r, c.g, c.b)
else
local c = db.centerTextColor
ShowCenterText("Defile: " .. targetName, c.r, c.g, c.b)
end
end
if isMe and db.flashSelf then
local c = db.selfFlashColor
FlashScreen(c.r, c.g, c.b, c.a)
elseif not isMe and db.flashOther then
local c = db.otherFlashColor
FlashScreen(c.r, c.g, c.b, c.a)
end
if db.soundEnabled and isMe then
local path = db.soundFile
if not path or path == "" then path = ALERT_SOUND end
PlaySoundFile(path)
end
if db.announceEnabled and GetNumRaidMembers() > 0 then
local channel = db.announceChannel
if channel == "RAID_WARNING"
and not IsRaidOfficer() and not IsRaidLeader() then
channel = db.downgradeToRaid and "RAID" or nil
end
if channel then
SendChatMessage(format(db.raidMessage, targetName), channel)
end
end
if db.whisperTarget and not isMe and GetNumRaidMembers() > 0 then
SendChatMessage(db.whisperMessage, "WHISPER", nil, targetName)
end
print("|cffff4444[DefileAlert]|r " .. targetName
.. (isMe and " |cffff0000(YOU!)|r" or ""))
end
DefileAlertAPI.AnnounceDefile = AnnounceDefile
DefileAlertAPI.TestSelf = function()
lastAnnounce = 0
AnnounceDefile(UnitName("player"), "TEST")
end
DefileAlertAPI.TestOther = function()
lastAnnounce = 0
AnnounceDefile("TestRaider", "TEST")
end
local core = CreateFrame("Frame", "DefileAlertCore")
core:SetScript("OnEvent", function(self, event, ...)
if event == EV_CLEU then
local _, etype = ...
if etype == EV_SPELL_CAST_START then
local _, _, srcGUID, srcName, _, destGUID, destName, _, spellId = ...
if not DEFILE_IDS[spellId] then return end
castDetectTime = GetTime()
lkGUID = srcGUID
if detected and (GetTime() - lastAnnounce) < DEBOUNCE_SEC then
return
end
if destName and destName ~= "" and not IsGUID(destName) then
if diagMode then
print("|cff00ffff[DIAG]|r CLEU destName: " .. destName)
end
AnnounceDefile(destName, "CLEU_destName")
pending = false
scanStartTarget = nil
return
end
if destGUID and destGUID ~= "" then
local name = GUIDtoPlayerName(destGUID)
if name then
if diagMode then
print("|cff00ffff[DIAG]|r CLEU destGUID resolved: " .. name)
end
AnnounceDefile(name, "CLEU_destGUID")
pending = false
scanStartTarget = nil
return
end
pendingGUID = destGUID
end
local lkUnit = FindLKUnit(srcGUID)
scanStartTarget = lkUnit and ReadBossTargetName(lkUnit) or nil
if diagMode then
print("|cff00ffff[DIAG]|r Boss target at cast: " .. tostring(scanStartTarget) .. " (skipping, scanning for change...)")
end
pending = true
pendingStart = GetTime()
return
end
if etype == EV_SPELL_SUMMON and pending then
local spellId = select(9, ...)
if DEFILE_IDS[spellId] then
pending = false
pendingGUID = nil
scanStartTarget = nil
end
end
return
end
if event == "ZONE_CHANGED_NEW_AREA"
or event == "ZONE_CHANGED"
or event == "PLAYER_ENTERING_WORLD" then
if IsInICC() then
if not zoneActive then
zoneActive = true
self:RegisterEvent(EV_CLEU)
RegisterDBMCallback()
end
else
if zoneActive then
zoneActive = false
pending = false
detected = false
pendingGUID = nil
lkGUID = nil
scanStartTarget = nil
nextCountdownTime = 0
countdownPlayed = false
self:UnregisterEvent(EV_CLEU)
end
end
return
end
if event == "ADDON_LOADED" then
if ... ~= "DefileAlert" then return end
InitDB()
self:UnregisterEvent("ADDON_LOADED")
RegisterDBMCallback()
if IsInICC() then
zoneActive = true
self:RegisterEvent(EV_CLEU)
end
print("|cffff4444[DefileAlert]|r v" .. ADDON_VERSION .. " loaded"
.. (zoneActive and " — |cff00ff00ACTIVE|r" or "")
.. (dbmRegistered and " — |cff00ff00DBM|r" or "")
.. " — /da config")
end
end)
core:SetScript("OnUpdate", function(self, dt)
local now = GetTime()
if pending then
local elapsed = now - pendingStart
if elapsed >= SCAN_TIMEOUT then
if diagMode then
print("|cff00ffff[DIAG]|r SCAN TIMEOUT after " .. format("%.0fms", elapsed * 1000))
end
pending = false
pendingGUID = nil
scanStartTarget = nil
return
end
if elapsed >= 0.02 then
if pendingGUID then
local name = GUIDtoPlayerName(pendingGUID)
if name then
if diagMode then
print("|cff00ffff[DIAG]|r GUID resolved: " .. name .. " at " .. format("%.0fms", elapsed * 1000))
end
pending = false
pendingGUID = nil
scanStartTarget = nil
AnnounceDefile(name, "GUID_retry")
return
end
end
local lkUnit = FindLKUnit(lkGUID)
if lkUnit then
local name = ReadBossTargetName(lkUnit)
if name and name ~= "" and not IsGUID(name) then
if not scanStartTarget or name ~= scanStartTarget then
if diagMode then
print("|cff00ffff[DIAG]|r Target CHANGED: " .. tostring(scanStartTarget) .. " -> " .. name .. " at " .. format("%.0fms", elapsed * 1000))
end
pending = false
pendingGUID = nil
scanStartTarget = nil
AnnounceDefile(name, "BossTargetScan")
return
end
end
end
end
end
if db and db.countdownEnabled and nextCountdownTime > 0 and not countdownPlayed then
if now >= nextCountdownTime then
countdownPlayed = true
PlaySoundFile(COUNTDOWN_SOUND)
if diagMode then
print("|cff00ffff[DIAG]|r Countdown voice triggered")
end
end
end
end)
core:RegisterEvent("ADDON_LOADED")
core:RegisterEvent("ZONE_CHANGED_NEW_AREA")
core:RegisterEvent("ZONE_CHANGED")
core:RegisterEvent("PLAYER_ENTERING_WORLD")
SLASH_DEFILEALERT1 = "/defilealert"
SLASH_DEFILEALERT2 = "/da"
SlashCmdList["DEFILEALERT"] = function(input)
if not db then InitDB() end
input = strtrim(strlower(input or ""))
if input == "config" or input == "options" or input == "opt"
or input == "gui" or input == "settings" or input == "" then
if DefileAlertOptions_Toggle then
DefileAlertOptions_Toggle()
else
print("|cffff4444[DefileAlert]|r Options panel not loaded.")
end
elseif input == "test" then
DefileAlertAPI.TestSelf()
elseif input == "testother" then
DefileAlertAPI.TestOther()
elseif input == "diag" then
diagMode = not diagMode
if diagMode then
print("|cff00ffff[DefileAlert DIAG]|r Diagnostic mode |cff00ff00ON|r")
print("|cff00ffff[DefileAlert DIAG]|r DBM: " .. (dbmRegistered and "|cff00ff00connected|r" or "|cffff0000not found|r"))
else
print("|cff00ffff[DefileAlert DIAG]|r Diagnostic mode |cffff0000OFF|r")
end
elseif input == "status" then
print("|cffff4444[DefileAlert]|r v" .. ADDON_VERSION .. " Status:")
print(" Zone: " .. (zoneActive and "|cff00ff00ICC|r" or "|cffaaaaaanot ICC|r"))
print(" LK GUID: " .. (lkGUID or "none"))
print(" DBM: " .. (dbmRegistered and "|cff00ff00connected|r" or "not found"))
print(" Countdown: " .. (db.countdownEnabled and "ON" or "OFF"))
print(" Channel: " .. (db.announceEnabled and db.announceChannel or "OFF"))
print(" Whisper: " .. (db.whisperTarget and "ON" or "OFF"))
elseif input == "help" then
print("|cffff4444[DefileAlert]|r Commands:")
print(" /da — open config panel")
print(" /da test|testother — test alerts")
print(" /da diag — toggle diagnostic mode")
print(" /da status — detailed info")
else
print("|cffff4444[DefileAlert]|r Unknown command. /da help")
end
end