Files
wow-Heimdall/Modules/DeathReporter.lua

117 lines
4.1 KiB
Lua

local addonname, shared = ...
---@cast shared HeimdallShared
---@cast addonname string
---@diagnostic disable-next-line: missing-fields
shared.DeathReporter = {}
function shared.DeathReporter.Init()
---@type table<string, number>
local recentDeaths = {}
---@type table<string, number>
local recentDuels = {}
---@param source string
---@param destination string
---@param spellName string
local function RegisterDeath(source, destination, spellName)
if not Heimdall_Data.config.deathReporter.enabled then return end
if recentDeaths[destination]
and GetTime() - recentDeaths[destination] < Heimdall_Data.config.deathReporter.throttle then
return
end
if recentDuels[destination]
and GetTime() - recentDuels[destination] < Heimdall_Data.config.deathReporter.duelThrottle then
print(string.format("Cancelling death reports for %s and %s because of recent duel", source, destination))
return
end
if recentDuels[source]
and GetTime() - recentDuels[source] < Heimdall_Data.config.deathReporter.duelThrottle then
print(string.format("Cancelling death reports for %s and %s because of recent duel", source, destination))
return
end
recentDeaths[destination] = GetTime()
C_Timer.NewTimer(3, function()
if recentDuels[destination]
and GetTime() - recentDuels[destination] < Heimdall_Data.config.deathReporter.duelThrottle then
print(string.format("Cancelling death reports for %s and %s because of recent duel", source, destination))
return
end
if recentDuels[source]
and GetTime() - recentDuels[source] < Heimdall_Data.config.deathReporter.duelThrottle then
print(string.format("Cancelling death reports for %s and %s because of recent duel", source, destination))
return
end
local zone = Heimdall_Data.config.deathReporter.zoneOverride
if zone == nil or zone == "" then
zone = string.format("%s (%s)", GetZoneText(), GetSubZoneText())
end
local text = string.format("%s killed %s with %s in %s",
tostring(source),
tostring(destination),
tostring(spellName),
tostring(zone))
---@type Message
local msg = {
channel = "CHANNEL",
data = Heimdall_Data.config.deathReporter.notifyChannel,
message = text,
}
table.insert(shared.messenger.queue, msg)
if Heimdall_Data.config.deathReporter.doWhisper then
for _, name in pairs(Heimdall_Data.config.whisperNotify) do
local msg = {
channel = "WHISPER",
data = name,
message = text,
}
table.insert(shared.messenger.queue, msg)
end
end
end)
end
local cleuFrame = CreateFrame("Frame")
cleuFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
cleuFrame:SetScript("OnEvent", function(self, event, ...)
if not Heimdall_Data.config.deathReporter.enabled then return end
local overkill, err = CLEUParser.GetOverkill(...)
if not err and overkill > 0 then
local source, err = CLEUParser.GetSourceName(...)
if err then source = "unknown" end
local destination, err = CLEUParser.GetDestName(...)
if err then destination = "unknown" end
local spellName, err = CLEUParser.GetSpellName(...)
if err then spellName = "unknown" end
local sourceGUID, err = CLEUParser.GetSourceGUID(...)
if err or not string.match(sourceGUID, "Player") then return end
local destinationGUID, err = CLEUParser.GetDestGUID(...)
if err or not string.match(destinationGUID, "Player") then return end
RegisterDeath(source, destination, spellName)
end
end)
local systemMessageFrame = CreateFrame("Frame")
systemMessageFrame:RegisterEvent("CHAT_MSG_SYSTEM")
systemMessageFrame:SetScript("OnEvent", function(self, event, msg)
if not Heimdall_Data.config.deathReporter.enabled then return end
local source, destination = string.match(msg, "([^ ]+) has defeated ([^ ]+) in a duel")
if not source or not destination then return end
source = string.match(source, "([^-]+)")
destination = string.match(destination, "([^-]+)")
if source and destination then
print(string.format("Detected duel between %s and %s", source, destination))
local now = GetTime()
recentDuels[source] = now
recentDuels[destination] = now
end
end)
print("Heimdall - DeathReporter loaded")
end