Files
wow-Heimdall/Modules/DeathReporter.lua

205 lines
7.5 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
local location = Heimdall_Data.config.deathReporter.zoneOverride
if not location or location == "" then
location = string.format("%s (%s)", GetZoneText(), GetSubZoneText())
end
local x, y = GetPlayerMapPosition("player")
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Player coordinates: %.2f, %.2f", ModuleName, x * 100, y * 100))
end
SetMapToCurrentZone()
SetMapByID(GetCurrentMapAreaID())
local zoneId = GetCurrentMapAreaID()
---@type Message
local msg = {
channel = "CHANNEL",
data = Heimdall_Data.config.deathReporter.masterChannel,
message = string.format(shared.L.en.killed,
source,
destination,
spellName,
location,
zoneId,
x * 100, y * 100)
}
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)
local zone, subZone = GetZoneText(), GetSubZoneText()
if not shared.L.ru.zones[zone] then
print(string.format("[%s] Zone %s not found in ru.zones", ModuleName, zone))
end
zone = shared.L.ru.zones[zone] or zone
if not shared.L.ru.zones[subZone] then
print(string.format("[%s] Subzone %s not found in ru.zones", ModuleName, subZone))
end
subZone = shared.L.ru.zones[subZone] or subZone
if Heimdall_Data.config.echoToRussian then
-- Russian message
local ruMsg = {
channel = "CHANNEL",
data = Heimdall_Data.config.deathReporter.masterChannel .. "ru",
message = string.format(shared.L.ru.killed,
source,
destination,
spellName,
string.format("%s (%s)", zone, subZone),
zoneId,
x * 100, y * 100)
}
table.insert(shared.messenger.queue, ruMsg)
end
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 not Heimdall_Data.config.deathReporter.enabled then return end
local source, destination = string.match(msg, "([^ ]+) has defeated ([^ ]+) in a duel")
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Received system message: %s", ModuleName, msg))
print(string.format("[%s] Source: %s, Destination: %s", ModuleName, tostring(source), tostring(destination)))
end
if not source or not destination then return end
source = string.match(source, "([^-]+)")
destination = string.match(destination, "([^-]+)")
if source and destination then
if Heimdall_Data.config.deathReporter.debug then
print(string.format("[%s] Detected duel between %s and %s", ModuleName, source, destination))
end
local now = GetTime()
recentDuels[source] = now
recentDuels[destination] = now
end
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