-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
748 lines (675 loc) · 20.5 KB
/
Core.lua
File metadata and controls
748 lines (675 loc) · 20.5 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
local _, core = ...
local ControlFreak = LibStub("AceAddon-3.0"):NewAddon("ControlFreak", "AceConsole-3.0", "AceEvent-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("ControlFreak")
local LSM = LibStub("LibSharedMedia-3.0")
--------------------------------------------------------------------------------
-- UPVALUES
--------------------------------------------------------------------------------
local CombatLogGetCurrentEventInfo = CombatLogGetCurrentEventInfo
local UnitGUID = UnitGUID
local UnitName = UnitName
local UnitInParty = UnitInParty
local UnitInRaid = UnitInRaid
local IsInGroup = IsInGroup
local IsInRaid = IsInRaid
local IsInInstance = IsInInstance
local SendChatMessage = SendChatMessage
local PlaySoundFile = PlaySoundFile
local string_format = string.format
local string_find = string.find
local bit_band = bit.band
local GetRealmName = GetRealmName
local GetTime = GetTime
local pairs, ipairs = pairs, ipairs
local PLAYER_REALM = GetRealmName()
local PLAYER_GUID = UnitGUID("player")
local projectVersion = "@project-version@"
if projectVersion:find("project") then
projectVersion = "Dev"
end
ControlFreak.version = projectVersion
local petOwnerCache = {}
local spamThrottle = {}
local badPetHistory = {
instance = {},
boss = {}
}
local CLASS_HEX = {
DEATHKNIGHT = "C41F3B",
DRUID = "FF7D0A",
HUNTER = "ABD473",
MAGE = "40C7EB",
PALADIN = "F58CBA",
PRIEST = "FFFFFF",
ROGUE = "FFF569",
SHAMAN = "0070DE",
WARLOCK = "9482C9",
WARRIOR = "C79C6E",
DEFAULT = "CCCCCC"
}
--------------------------------------------------------------------------------
-- INITIALIZATION
--------------------------------------------------------------------------------
function ControlFreak:OnInitialize()
local defaults = {
profile = {
blacklist = {},
general = {
globalEnable = true,
notInGroup = false,
inParty = true,
inRaid = true,
openWorld = false,
instances = true,
battlegrounds = false,
arenas = false
},
own = {
enable = true,
rank = 2,
print = true,
channel = "SELF",
audio = true,
sound = "Punch",
aoeAudio = true,
aoeSound = "Boxing Arena Gong",
failPrint = true,
failChannel = "GROUP",
failAudio = true,
failSound = "Synth Chord"
},
group = {
enable = true,
rank = 2,
print = true,
channel = "SELF",
audio = true,
sound = "Punch",
aoeAudio = true,
aoeSound = "Boxing Arena Gong",
failPrint = true,
failChannel = "SELF",
failAudio = true,
failSound = "Synth Chord"
},
other = {
enable = false,
rank = 2,
print = false,
channel = "SELF",
audio = false,
sound = "Punch",
aoeAudio = false,
aoeSound = "Boxing Arena Gong",
failPrint = false,
failChannel = "SELF",
failAudio = false,
failSound = "Synth Chord"
},
pets = {
enable = true,
rank = 2,
print = true,
channel = "SELF",
audio = false,
sound = "Bite",
badPetEnable = false,
badPetFreq = "BOSS"
},
fears = {
enable = true,
rank = 3,
print = true,
channel = "SELF",
audio = true,
sound = "Voice: Idiot"
},
annoyances = {
enable = true,
print = true,
channel = "SELF",
audio = false,
sound = "Voice: Idiot"
},
items = {
enable = true,
print = true,
channel = "SELF",
audio = true,
sound = "Squeaky Toy Short"
}
}
}
self.db = LibStub("AceDB-3.0"):New("ControlFreakDB", defaults, true)
if not self.db.profile then
self.db.profile = defaults.profile
end
self.db.RegisterCallback(self, "OnProfileChanged", "RefreshConfig")
self.db.RegisterCallback(self, "OnProfileCopied", "RefreshConfig")
self.db.RegisterCallback(self, "OnProfileReset", "RefreshConfig")
self:SetupOptions()
self.SpellMap = {}
if core.SpellDB then
for id, data in pairs(core.SpellDB.Taunts or {}) do
self.SpellMap[id] = {type = "Taunt", data = data}
end
for id, data in pairs(core.SpellDB.Pets or {}) do
self.SpellMap[id] = {type = "Pet", data = data}
end
for id, data in pairs(core.SpellDB.Fears or {}) do
self.SpellMap[id] = {type = "Fear", data = data}
end
for id, data in pairs(core.SpellDB.Items or {}) do
self.SpellMap[id] = {type = "Item", data = data}
end
for id, data in pairs(core.SpellDB.Annoyances or {}) do
self.SpellMap[id] = {type = "Annoyance", data = data}
end
end
end
function ControlFreak:RefreshConfig()
end
function ControlFreak:OnEnable()
PLAYER_GUID = UnitGUID("player")
self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
self:RegisterEvent("UNIT_PET")
self:RegisterEvent("GROUP_ROSTER_UPDATE", "ScanAllPets")
self:RegisterEvent("ENCOUNTER_END")
self:RegisterEvent("PLAYER_ENTERING_WORLD")
self:ScanAllPets()
end
--------------------------------------------------------------------------------
-- PET CACHING & HISTORY
--------------------------------------------------------------------------------
function ControlFreak:UNIT_PET(_, unitId)
local petGUID = UnitGUID(unitId .. "pet")
if petGUID then
petOwnerCache[petGUID] = {
ownerName = GetUnitName(unitId, true),
ownerGUID = UnitGUID(unitId)
}
end
end
function ControlFreak:ScanAllPets()
petOwnerCache = {}
self:UNIT_PET(nil, "player")
if IsInRaid() then
for i = 1, 40 do
self:UNIT_PET(nil, "raid" .. i)
end
elseif IsInGroup() then
for i = 1, 4 do
self:UNIT_PET(nil, "party" .. i)
end
end
end
function ControlFreak:ENCOUNTER_END()
badPetHistory.boss = {}
end
function ControlFreak:PLAYER_ENTERING_WORLD()
badPetHistory.instance = {}
badPetHistory.boss = {}
end
function ControlFreak:CheckFrequency(guid, spellID)
local freq = self.db.profile.pets.badPetFreq or "BOSS"
if freq == "ALWAYS" then
return true
end
local key = guid .. "-" .. spellID
if freq == "INSTANCE" then
if badPetHistory.instance[key] then
return false
end
badPetHistory.instance[key] = true
return true
end
if freq == "BOSS" then
if badPetHistory.boss[key] then
return false
end
badPetHistory.boss[key] = true
return true
end
return true
end
function ControlFreak:SendBadPetWhisper(ownerName, petName, spellID, spellName, destName)
if not ownerName then
return
end
local safeDest = destName or "Unknown Target"
local spellLink = "|cff71d5ff|Hspell:" .. spellID .. ":0|h[" .. spellName .. "]|h|r"
local msg = string_format(L["BAD_PET_WHISPER"], petName, spellLink, safeDest)
SendChatMessage(msg, "WHISPER", nil, ownerName)
end
--------------------------------------------------------------------------------
-- HELPERS
--------------------------------------------------------------------------------
local function IsZoneEnabled(db)
if not db.general.globalEnable then
return false
end
local _, instanceType = IsInInstance()
if instanceType == "pvp" then
return db.general.battlegrounds
elseif instanceType == "arena" then
return db.general.arenas
elseif instanceType == "party" or instanceType == "raid" then
return db.general.instances
end
if not db.general.openWorld then
return false
end
if IsInRaid() then
return db.general.inRaid
elseif IsInGroup() then
return db.general.inParty
else
return db.general.notInGroup
end
end
local function CheckRank(guid, minRankSetting)
local rank = minRankSetting or 3
if rank == 3 then
return true
end
if not guid or guid == "" then
return false
end
local classification = "normal"
local tokens = {"target", "focus", "mouseover"}
for _, unit in ipairs(tokens) do
if UnitGUID(unit) == guid then
classification = UnitClassification(unit)
break
end
end
if rank == 1 then
return classification == "worldboss"
elseif rank == 2 then
return (classification == "worldboss" or classification == "rareelite" or classification == "elite")
end
return false
end
local function PlayAlertSound(soundName)
if not soundName or soundName == "None" then
return
end
local soundFile = LSM:Fetch("sound", soundName)
if soundFile then
PlaySoundFile(soundFile, "Master")
end
end
local function FormatNameWithRealm(name, guid)
if not name then
return "?"
end
if string_find(name, "-") then
return name
end
if guid and string_find(guid, "^Player") then
return name .. "-" .. PLAYER_REALM
end
return name
end
local function GetRaidIconFromFlags(destRaidFlags)
if not destRaidFlags then
return ""
end
if bit_band(destRaidFlags, COMBATLOG_OBJECT_RAIDTARGET8) > 0 then
return "{rt8} "
end
if bit_band(destRaidFlags, COMBATLOG_OBJECT_RAIDTARGET7) > 0 then
return "{rt7} "
end
if bit_band(destRaidFlags, COMBATLOG_OBJECT_RAIDTARGET6) > 0 then
return "{rt6} "
end
if bit_band(destRaidFlags, COMBATLOG_OBJECT_RAIDTARGET5) > 0 then
return "{rt5} "
end
if bit_band(destRaidFlags, COMBATLOG_OBJECT_RAIDTARGET4) > 0 then
return "{rt4} "
end
if bit_band(destRaidFlags, COMBATLOG_OBJECT_RAIDTARGET3) > 0 then
return "{rt3} "
end
if bit_band(destRaidFlags, COMBATLOG_OBJECT_RAIDTARGET2) > 0 then
return "{rt2} "
end
if bit_band(destRaidFlags, COMBATLOG_OBJECT_RAIDTARGET1) > 0 then
return "{rt1} "
end
return ""
end
local function GetThemeColor(sourceGUID, spellName, spellInfo)
if spellInfo.type == "Pet" then
if string_find(spellName, "Growl") then
return CLASS_HEX.HUNTER
elseif string_find(spellName, "Torment") or string_find(spellName, "Suffering") then
return CLASS_HEX.WARLOCK
end
end
if sourceGUID then
local _, englishClass = GetPlayerInfoByGUID(sourceGUID)
if englishClass and CLASS_HEX[englishClass] then
return CLASS_HEX[englishClass]
end
end
return CLASS_HEX.DEFAULT
end
local function GetSpellDisplay(spellID, spellName)
return "|cff71d5ff|Hspell:" .. spellID .. ":0|h[" .. spellName .. "]|h|r"
end
local function SendSmartGroupMessage(msg)
if IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then
SendChatMessage(msg, "INSTANCE_CHAT")
elseif IsInRaid() then
SendChatMessage(msg, "RAID")
elseif IsInGroup() then
SendChatMessage(msg, "PARTY")
end
end
--------------------------------------------------------------------------------
-- COMBAT LOG HANDLER
--------------------------------------------------------------------------------
function ControlFreak:COMBAT_LOG_EVENT_UNFILTERED()
if not IsZoneEnabled(self.db.profile) then
return
end
local _,
eventType,
_,
sourceGUID,
sourceName,
_,
_,
destGUID,
destName,
_,
destRaidFlags,
spellID,
spellName,
_,
missType = CombatLogGetCurrentEventInfo()
local spellInfo = self.SpellMap[spellID]
if not spellInfo then
return
end
if self.db.profile.blacklist[spellID] then
return
end
local spellType = spellInfo.type
local isAoE = spellInfo.data.aoe
local isSuccess = false
local isFail = false
if eventType == "SPELL_CAST_SUCCESS" then
isSuccess = true
elseif eventType == "SPELL_MISSED" or eventType == "RANGE_MISSED" then
isFail = true
end
if not isSuccess and not isFail then
return
end
local categoryKey = "other"
local db = self.db.profile
local cfg
local petOwnerName = nil
local isMine = (sourceGUID == PLAYER_GUID)
local isGroup = (UnitInParty(sourceName) or UnitInRaid(sourceName))
if spellType == "Fear" then
cfg = db.fears
elseif spellType == "Annoyance" then
cfg = db.annoyances
if not (isMine or isGroup) then
return
end
elseif spellType == "Pet" then
cfg = db.pets
local ownerData = petOwnerCache[sourceGUID]
if ownerData then
if ownerData.ownerGUID == PLAYER_GUID then
isMine = true
end
if UnitInParty(ownerData.ownerName) or UnitInRaid(ownerData.ownerName) then
isGroup = true
end
petOwnerName = ownerData.ownerName
end
if not (isMine or isGroup) then
return
end
elseif spellType == "Item" then
cfg = db.items
if spellID == 4054 and not db.items.targetDummy then
return
end
if spellID == 4068 and not db.items.advTargetDummy then
return
end
if not (isMine or isGroup) then
return
end
else
if isMine then
categoryKey = "own"
elseif isGroup then
categoryKey = "group"
else
categoryKey = "other"
end
cfg = db[categoryKey]
end
if not cfg.enable then
return
end
if spellType ~= "Item" then
if not (isAoE and isSuccess) then
if not CheckRank(destGUID, cfg.rank or 3) then
return
end
end
end
local now = GetTime()
local spamKey = (sourceGUID or "nil") .. (spellID or "0")
if spamThrottle[spamKey] and (now - spamThrottle[spamKey] < 0.5) then
return
end
spamThrottle[spamKey] = now
if isAoE and isSuccess then
destName = nil
end
if isSuccess then
local playSound = cfg.audio
local whichSound = cfg.sound
if isAoE and cfg.aoeAudio ~= nil then
playSound = cfg.aoeAudio
whichSound = cfg.aoeSound
end
if playSound then
PlayAlertSound(whichSound)
end
if spellType == "Pet" and cfg.badPetEnable and not isMine and petOwnerName then
if self:CheckFrequency(sourceGUID, spellID) then
self:SendBadPetWhisper(petOwnerName, sourceName, spellID, spellName, destName)
end
end
if cfg.print then
self:ConstructAndSend(
cfg.channel,
sourceGUID,
sourceName,
destName,
destRaidFlags,
spellID,
spellName,
spellInfo,
spellType,
true,
nil
)
end
elseif isFail then
if cfg.failAudio then
PlayAlertSound(cfg.failSound)
end
if cfg.failPrint then
self:ConstructAndSend(
cfg.failChannel,
sourceGUID,
sourceName,
destName,
destRaidFlags,
spellID,
spellName,
spellInfo,
spellType,
false,
missType
)
end
end
end
--------------------------------------------------------------------------------
-- MESSAGE CONSTRUCTION
--------------------------------------------------------------------------------
function ControlFreak:ConstructAndSend(
channel,
sourceGUID,
sourceName,
destName,
destRaidFlags,
spellID,
spellName,
spellInfo,
spellType,
isSuccess,
missType)
local spellDisplay = GetSpellDisplay(spellID, spellName)
local raidIcon = GetRaidIconFromFlags(destRaidFlags)
local sourceNameFull = FormatNameWithRealm(sourceName, sourceGUID)
if spellType == "Pet" then
local ownerData = petOwnerCache[sourceGUID]
if ownerData then
sourceNameFull = sourceName .. " (" .. FormatNameWithRealm(ownerData.ownerName, ownerData.ownerGUID) .. ")"
else
if string_find(spellName, "Growl") then
sourceNameFull = sourceName .. " (Unknown Hunter)"
elseif string_find(spellName, "Torment") or string_find(spellName, "Suffering") then
sourceNameFull = sourceName .. " (Unknown Warlock)"
end
end
end
local prefix = L["PREFIX_TAUNT"]
if spellType == "Fear" then
prefix = L["PREFIX_FEAR"]
elseif spellType == "Pet" then
prefix = L["PREFIX_PET"]
elseif spellType == "Annoyance" then
prefix = L["PREFIX_ANNOYANCE"]
elseif spellInfo.data.aoe and isSuccess then
prefix = L["PREFIX_TAUNT_AOE"]
end
local failAction = L["ACTION_MISSED"]
if not isSuccess and missType then
if missType == "IMMUNE" then
failAction = L["ACTION_IMMUNE"]
elseif missType == "RESIST" then
failAction = L["ACTION_RESISTED"]
end
prefix = L["PREFIX_TAUNT_FAIL"]
end
if channel == "GROUP" and not (IsInGroup() or IsInRaid()) then
channel = "SELF"
end
local hex = GetThemeColor(sourceGUID, spellName, spellInfo)
local msg = ""
if channel == "SELF" then
if isSuccess then
local action = L["ACTION_USED"]
if destName then
msg =
string_format(
L["FMT_SUCCESS_TARGET"],
hex,
prefix,
sourceNameFull,
action,
spellDisplay,
raidIcon,
destName,
L["BRAND_SUFFIX"]
)
else
msg =
string_format(
L["FMT_SUCCESS_NO_TARGET"],
hex,
prefix,
sourceNameFull,
action,
spellDisplay,
raidIcon,
"",
L["BRAND_SUFFIX"]
)
end
else
local safeDest = destName or "Unknown Target"
msg =
string_format(
L["FMT_FAIL"],
hex,
prefix,
raidIcon,
safeDest,
failAction,
sourceNameFull,
spellDisplay,
L["BRAND_SUFFIX"]
)
end
print(msg)
elseif channel == "GROUP" then
if isSuccess then
local action = L["ACTION_USED"]
if destName then
msg =
string_format(
L["CLEAN_SUCCESS_TARGET"],
prefix,
sourceNameFull,
action,
spellDisplay,
raidIcon,
destName,
L["BRAND_SUFFIX"]
)
else
msg =
string_format(
L["CLEAN_SUCCESS_NO_TARGET"],
prefix,
sourceNameFull,
action,
spellDisplay,
raidIcon,
L["BRAND_SUFFIX"]
)
end
else
local safeDest = destName or "Unknown Target"
msg =
string_format(
L["CLEAN_FAIL"],
prefix,
raidIcon,
safeDest,
failAction,
sourceNameFull,
spellDisplay,
L["BRAND_SUFFIX"]
)
end
SendSmartGroupMessage(msg)
end
end