local _, data = ... ---@cast data HeimdallData if not data.config.spotter.enabled then return end local function FormatHP(hp) if hp > 1e9 then return string.format("%.1fB", hp / 1e9) elseif hp > 1e6 then return string.format("%.1fM", hp / 1e6) elseif hp > 1e3 then return string.format("%.1fK", hp / 1e3) else return hp end end ---@type table local throttleTable = {} ---@param unit string ---@return string? local function NotifySpotted(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 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 err = NotifySpotted(unit) if err then print(string.format("Error notifying %s: %s", tostring(unit), tostring(err))) end end)