From 1129d787b54f362e8cc9413839b3c08f4735cd59 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 18 May 2025 11:43:40 +0200 Subject: [PATCH] Refactor BonkDetector and Bully modules for improved structure and clarity --- Modules/BonkDetector.lua | 214 ++++++++++++++++++++------------------- Modules/Bully.lua | 18 ++-- 2 files changed, 123 insertions(+), 109 deletions(-) diff --git a/Modules/BonkDetector.lua b/Modules/BonkDetector.lua index 5f0750e..1b8767f 100644 --- a/Modules/BonkDetector.lua +++ b/Modules/BonkDetector.lua @@ -1,134 +1,142 @@ local _, shared = ... ---@cast shared HeimdallShared + +---@class HeimdallBonkDetectorConfig +---@field enabled boolean +---@field debug boolean +---@field channels string[] +---@field throttle number + local ModuleName = "BonkDetector" ----@diagnostic disable-next-line: missing-fields -shared.BonkDetector = {} -function shared.BonkDetector.Init() - ---@type table - local lastReportTime = {} +shared.BonkDetector = { + ---@return nil + Init = function() + ---@type table + local lastReportTime = {} - local frame = CreateFrame("Frame") - frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED") - frame:SetScript("OnEvent", function(self, event, ...) - -- if Heimdall_Data.config.bonkDetector.debug then - -- print(string.format("[%s] Combat log event received", ModuleName)) - -- end - if not Heimdall_Data.config.bonkDetector.enabled then - -- if Heimdall_Data.config.bonkDetector.debug then - -- print(string.format("[%s] Module disabled, ignoring combat event", ModuleName)) - -- end - return - end + local frame = CreateFrame("Frame") + frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED") + frame:SetScript("OnEvent", function(self, event, ...) + -- if Heimdall_Data.config.bonkDetector.debug then + -- print(string.format("[%s] Combat log event received", ModuleName)) + -- end + if not Heimdall_Data.config.bonkDetector.enabled then + -- if Heimdall_Data.config.bonkDetector.debug then + -- print(string.format("[%s] Module disabled, ignoring combat event", ModuleName)) + -- end + return + end - local subevent = select(2, ...) - if not subevent:find("_DAMAGE") then - if Heimdall_Data.config.bonkDetector.debug then - print(string.format("[%s] Not a damage event, ignoring: %s", ModuleName, subevent)) + local subevent = select(2, ...) + if not subevent:find("_DAMAGE") then + if Heimdall_Data.config.bonkDetector.debug then + print(string.format("[%s] Not a damage event, ignoring: %s", ModuleName, subevent)) + end + return end - return - end - ---@type string|nil, string, string, string, string - local err, source, sourceGUID, destination, destinationGUID + ---@type string|nil, string, string, string, string + local err, source, sourceGUID, destination, destinationGUID - source, err = CLEUParser.GetSourceName(...) - if err then - if Heimdall_Data.config.bonkDetector.debug then - print(string.format("[%s] Error getting source name: %s", ModuleName, err)) + source, err = CLEUParser.GetSourceName(...) + if err then + if Heimdall_Data.config.bonkDetector.debug then + print(string.format("[%s] Error getting source name: %s", ModuleName, err)) + end + return end - return - end - sourceGUID, err = CLEUParser.GetSourceGUID(...) - if err then - if Heimdall_Data.config.bonkDetector.debug then - print(string.format("[%s] Error getting source GUID: %s", ModuleName, err)) + sourceGUID, err = CLEUParser.GetSourceGUID(...) + if err then + if Heimdall_Data.config.bonkDetector.debug then + print(string.format("[%s] Error getting source GUID: %s", ModuleName, err)) + end + return end - return - end - if not string.find(sourceGUID, "Player") then - if Heimdall_Data.config.bonkDetector.debug then - print(string.format("[%s] Source %s is not a player, nothing to do", ModuleName, source)) + if not string.find(sourceGUID, "Player") then + if Heimdall_Data.config.bonkDetector.debug then + print(string.format("[%s] Source %s is not a player, nothing to do", ModuleName, source)) + end + return end - return - end - destination, err = CLEUParser.GetDestName(...) - if err then - if Heimdall_Data.config.bonkDetector.debug then - print(string.format("[%s] Error getting destination name: %s", ModuleName, err)) + destination, err = CLEUParser.GetDestName(...) + if err then + if Heimdall_Data.config.bonkDetector.debug then + print(string.format("[%s] Error getting destination name: %s", ModuleName, err)) + end + return end - return - end - destinationGUID, err = CLEUParser.GetDestGUID(...) - if err then - if Heimdall_Data.config.bonkDetector.debug then - print(string.format("[%s] Error getting destination GUID: %s", ModuleName, err)) + destinationGUID, err = CLEUParser.GetDestGUID(...) + if err then + if Heimdall_Data.config.bonkDetector.debug then + print(string.format("[%s] Error getting destination GUID: %s", ModuleName, err)) + end + return end - return - end - if not string.find(destinationGUID, "Player") then - if Heimdall_Data.config.bonkDetector.debug then - print(string.format("[%s] Destination %s is not a player, nothing to do", ModuleName, destination)) + if not string.find(destinationGUID, "Player") then + if Heimdall_Data.config.bonkDetector.debug then + print(string.format("[%s] Destination %s is not a player, nothing to do", ModuleName, destination)) + end + return end - return - end - if source == destination then - if Heimdall_Data.config.bonkDetector.debug then - print(string.format("[%s] Source and destination are the same, ignoring event", ModuleName)) + if source == destination then + if Heimdall_Data.config.bonkDetector.debug then + print(string.format("[%s] Source and destination are the same, ignoring event", ModuleName)) + end + return end - return - end - local currentTime = GetTime() - local throttle = Heimdall_Data.config.bonkDetector.throttle + local currentTime = GetTime() + local throttle = Heimdall_Data.config.bonkDetector.throttle + + if lastReportTime[source] and (currentTime - lastReportTime[source]) < throttle then + if Heimdall_Data.config.bonkDetector.debug then + local timeLeft = throttle - (currentTime - lastReportTime[source]) + print( + string.format( + "[%s] Damage report throttled for %s (%.1f seconds remaining)", + ModuleName, + source, + timeLeft + ) + ) + end + return + end + + lastReportTime[source] = currentTime - if lastReportTime[source] and (currentTime - lastReportTime[source]) < throttle then if Heimdall_Data.config.bonkDetector.debug then - local timeLeft = throttle - (currentTime - lastReportTime[source]) print( string.format( - "[%s] Damage report throttled for %s (%.1f seconds remaining)", + "[%s] Processing damage event - Source: %s, Target: %s, Type: %s", ModuleName, source, - timeLeft + destination, + subevent ) ) end - return - end - lastReportTime[source] = currentTime - - if Heimdall_Data.config.bonkDetector.debug then - print( - string.format( - "[%s] Processing damage event - Source: %s, Target: %s, Type: %s", - ModuleName, - source, - destination, - subevent - ) - ) - end - - for _, channel in pairs(Heimdall_Data.config.bonkDetector.channels) do - local locale = shared.GetLocaleForChannel(channel) - local msg = string.format(shared._L("bonkDetected", locale), source, destination, subevent) - ---@type Message - local message = { - channel = "C", - data = channel, - message = msg, - } - if Heimdall_Data.config.bonkDetector.debug then - print(string.format("[%s] Queuing bonk detector message", ModuleName)) - shared.dumpTable(message) + for _, channel in pairs(Heimdall_Data.config.bonkDetector.channels) do + local locale = shared.GetLocaleForChannel(channel) + local msg = string.format(shared._L("bonkDetected", locale), source, destination, subevent) + ---@type Message + local message = { + channel = "C", + data = channel, + message = msg, + } + if Heimdall_Data.config.bonkDetector.debug then + print(string.format("[%s] Queuing bonk detector message", ModuleName)) + shared.dumpTable(message) + end + table.insert(shared.messenger.queue, message) end - table.insert(shared.messenger.queue, message) - end - end) + end) - print("[Heimdall] BonkDetector loaded") -end + print("[Heimdall] BonkDetector loaded") + end, +} diff --git a/Modules/Bully.lua b/Modules/Bully.lua index 9dbf230..0dc02e0 100644 --- a/Modules/Bully.lua +++ b/Modules/Bully.lua @@ -1,10 +1,16 @@ local _, shared = ... ---@cast shared HeimdallShared + +---@class HeimdallBullyConfig +---@field enabled boolean +---@field debug boolean + local ModuleName = "Bully" ----@diagnostic disable-next-line: missing-fields -shared.Bully = {} -function shared.Bully.Init() - if Heimdall_Data.config.bully.debug then print(string.format("[%s] Module initialized", ModuleName)) end - print("[Heimdall] Bully loaded") -end +shared.Bully = { + ---@return nil + Init = function() + if Heimdall_Data.config.bully.debug then print(string.format("[%s] Module initialized", ModuleName)) end + print("[Heimdall] Bully loaded") + end, +}