From 834c8a9a0325181c7ed92b3dcb78cb944484baf5 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 12 Dec 2024 13:18:32 +0100 Subject: [PATCH] Rework spotter to be a little more betterer --- Heimdall.lua | 42 ++++++++++++++++++++++++++- Spotter.lua | 82 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 95 insertions(+), 29 deletions(-) diff --git a/Heimdall.lua b/Heimdall.lua index 79cd58e..ee94872 100644 --- a/Heimdall.lua +++ b/Heimdall.lua @@ -6,6 +6,7 @@ if not Heimdall_Data then Heimdall_Data = {} end ---@class HeimdallData ---@field config HeimdallConfig ---@field raceMap table +---@field stinkies table ---@field dumpTable fun(table: any, depth?: number): nil ---@field messenger {queue: table, ticker: number} @@ -13,7 +14,46 @@ if not Heimdall_Data then Heimdall_Data = {} end ---@field spotter HeimdallSpotterConfig ---@class HeimdallSpotterConfig +---@field enabled boolean ---@field allyOnly boolean ---@field stinkyOnly boolean +---@field notifyChannel string +---@field zoneOverride string? +---@field throttleTime number -data.raceMap \ No newline at end of file +data.config = { + spotter = { + enabled = true, + allyOnly = false, + stinkyOnly = false, + notifyChannel = "Foobar", + zoneOverride = nil, + throttleTime = 10 + } +} + +data.raceMap = { + ["Orc"] = "Horde", + ["Undead"] = "Horde", + ["Tauren"] = "Horde", + ["Troll"] = "Horde", + ["Blood Elf"] = "Horde", + ["Goblin"] = "Horde", + ["Human"] = "Alliance", + ["Dwarf"] = "Alliance", + ["Night Elf"] = "Alliance", + ["Gnome"] = "Alliance", + ["Draenei"] = "Alliance", + ["Worgen"] = "Alliance", + ["Vulpera"] = "Horde", + ["Nightborne"] = "Horde", + ["Zandalari Troll"] = "Horde", + ["Kul Tiran"] = "Alliance", + ["Dark Iron Dwarf"] = "Alliance", + ["Void Elf"] = "Alliance", + ["Lightforged Draenei"] = "Alliance", + ["Mechagnome"] = "Alliance", + ["Mag'har Orc"] = "Horde" +} + +data.stinkies = {} diff --git a/Spotter.lua b/Spotter.lua index 9c827b7..c42c6a4 100644 --- a/Spotter.lua +++ b/Spotter.lua @@ -1,5 +1,7 @@ ----@type HeimdallData local _, data = ... +---@cast data HeimdallData + +if not data.config.spotter.enabled then return end local function FormatHP(hp) if hp > 1e9 then @@ -13,47 +15,71 @@ local function FormatHP(hp) end end +---@type table +local throttleTable = {} + ---@param unit string ---@return string? local function NotifySpotted(unit) - print(unit) - local name = UnitName(unit) - local race = UnitRace(unit) - local hp = UnitHealth(unit) - local maxHp = UnitHealthMax(unit) + if not unit then return string.format("Could not find unit %s", tostring(unit)) end + if not UnitIsPlayer(unit) then return nil end + local name = UnitName(unit) if not name then return string.format("Could not find name for unit %s", tostring(unit)) end + + local time = GetTime() + if throttleTable[name] and time - throttleTable[name] < data.config.spotter.throttleTime then + return string.format("Throttled %s", tostring(name)) + end + throttleTable[name] = time + + local race = UnitRace(unit) if not race then return string.format("Could not find race for unit %s", tostring(unit)) end + local faction = data.raceMap[race] + if not faction then return string.format("Could not find faction for race %s", tostring(race)) end + + local doNotify = true + if data.config.spotter.allyOnly then + doNotify = false + if faction == "Alliance" then doNotify = true end + end + if data.config.spotter.stinkyOnly then + doNotify = false + if data.stinkies[name] then doNotify = true end + end + if not doNotify then return string.format("Not notifying %s", tostring(name)) end + + local hp = UnitHealth(unit) if not hp then return string.format("Could not find hp for unit %s", tostring(unit)) end + + local maxHp = UnitHealthMax(unit) if not maxHp then return string.format("Could not find maxHp for unit %s", tostring(unit)) end - local msg = string.format("%s (%s) - %s/%s", name, race, FormatHP(hp), FormatHP(maxHp)) - print(msg) + local location = data.config.spotter.zoneOverride + if not location then + local zone = GetZoneText() + if not zone then return string.format("Could not find zone for unit %s", tostring(unit)) end + local subzone = GetSubZoneText() + if not subzone then subzone = "" end + location = string.format("%s (%s)", zone, subzone) + end + + local text = string.format("I see %s of race (%s) with health %s/%s at %s", name, race, FormatHP(hp), FormatHP(maxHp), location) + ---@type Message + local msg = { + channel = "CHANNEL", + data = data.config.spotter.notifyChannel, + message = text + } + table.insert(data.messenger.queue, msg) end local frame = CreateFrame("Frame") frame:RegisterEvent("NAME_PLATE_UNIT_ADDED") frame:RegisterEvent("TARGET_UNIT_CHANGED") frame:SetScript("OnEvent", function(self, event, unit) - local name = UnitName(unit) - if name and UnitIsPlayer(unit) then - local race = UnitRace(unit) - local doNotify = true - if data.config.spotter.allyOnly then - doNotify = false - if data.raceMap[race] == "Alliance" then - doNotify = true - end - end - -- Stinkies overwrite allyOnly - if data.config.spotter.stinkyOnly then - doNotify = false - if data.stinkies[name] then - doNotify = true - end - end - if doNotify then - NotifySpotted(unit) - end + local err = NotifySpotted(unit) + if err then + print(string.format("Error notifying %s: %s", tostring(unit), tostring(err))) end end)