Files
wow-Heimdall/Modules/DeathReporter.lua
2025-01-09 11:15:44 +01:00

195 lines
7.0 KiB
Lua

local addonname, shared = ...
---@cast shared HeimdallShared
---@cast addonname string
local ModuleName = "DeathReporter"
---@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 Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Processing death event - Source: %s, Target: %s, Spell: %s",
ModuleName, source, destination, spellName))
end
if not Heimdall_Data.config.deathReporter.enabled then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Module disabled, ignoring death event", ModuleName))
end
return
end
if recentDeaths[destination] and GetTime() - recentDeaths[destination] < Heimdall_Data.config.deathReporter.throttle then
if Heimdall_Data.config.deathReporter.debug then
local timeLeft = Heimdall_Data.config.deathReporter.throttle - (GetTime() - recentDeaths[destination])
print(string.format("[%s] Death report throttled for %s (%.1f seconds remaining)",
ModuleName, destination, timeLeft))
end
return
end
if recentDuels[destination] and GetTime() - recentDuels[destination] < Heimdall_Data.config.deathReporter.duelThrottle then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Ignoring death report - Recent duel detected for target: %s", ModuleName, destination))
end
return
end
if recentDuels[source] and GetTime() - recentDuels[source] < Heimdall_Data.config.deathReporter.duelThrottle then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Ignoring death report - Recent duel detected for source: %s", ModuleName, source))
end
return
end
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Recording death for %s", ModuleName, destination))
end
recentDeaths[destination] = GetTime()
C_Timer.NewTimer(3, function()
if recentDuels[destination] and GetTime() - recentDuels[destination] < Heimdall_Data.config.deathReporter.duelThrottle then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Cancelling delayed death report - Recent duel detected for: %s", ModuleName, destination))
end
return
end
if recentDuels[source] and GetTime() - recentDuels[source] < Heimdall_Data.config.deathReporter.duelThrottle then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Cancelling delayed death report - Recent duel detected for: %s", ModuleName, source))
end
return
end
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Sending death report - %s killed %s with %s",
ModuleName, source, destination, spellName))
end
---@type Message
local msg = {
channel = "CHANNEL",
data = Heimdall_Data.config.deathReporter.masterChannel,
message = string.format("%s killed %s with %s", source, destination, spellName)
}
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Adding message to queue: %s", ModuleName, msg.message))
end
table.insert(shared.messenger.queue, msg)
end)
end
local cleuFrame = CreateFrame("Frame")
cleuFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
cleuFrame:SetScript("OnEvent", function(self, event, ...)
-- if Heimdall_Data.config.deathReporter.debug then
-- print(string.format("[%s] Received combat log event", ModuleName))
-- end
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"
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Error getting source name", ModuleName))
end
end
local destination, err = CLEUParser.GetDestName(...)
if err then
destination = "unknown"
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Error getting destination name", ModuleName))
end
end
local spellName, err = CLEUParser.GetSpellName(...)
if err then
spellName = "unknown"
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Error getting spell name", ModuleName))
end
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 Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Received system message: %s", ModuleName, msg))
end
if not Heimdall_Data.config.deathReporter.enabled then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Module disabled, ignoring event", ModuleName))
end
return
end
if event == "DUEL_REQUESTED" then
local opponent = ...
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Duel requested with: %s", ModuleName, opponent))
end
recentDuels[opponent] = GetTime()
recentDuels[UnitName("player")] = GetTime()
return
end
if event == "DUEL_FINISHED" or event == "DUEL_CANCELLED" then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Duel ended: %s", ModuleName, event))
end
return
end
local subevent = select(2, ...)
if subevent ~= "PARTY_KILL" and subevent ~= "UNIT_DIED" then return end
local source, err = CLEUParser.GetSourceName(...)
if err then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Error getting source: %s", ModuleName, err))
end
return
end
local destination, err = CLEUParser.GetDestName(...)
if err then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Error getting destination: %s", ModuleName, err))
end
return
end
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Death event detected - Source: %s, Target: %s", ModuleName, source, destination))
end
local spellId = CLEUParser.GetSpellId(...)
local spellName = GetSpellInfo(spellId) or "Unknown Spell"
RegisterDeath(source, destination, spellName)
end)
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Module initialized with throttle: %.1fs, duel throttle: %.1fs",
ModuleName, Heimdall_Data.config.deathReporter.throttle, Heimdall_Data.config.deathReporter.duelThrottle))
end
print("[Heimdall] DeathReporter loaded")
end