Refactor macroer to include "I see"

This commit is contained in:
2025-01-05 19:30:37 +01:00
parent c00ddd410a
commit bb1acd5003

View File

@@ -5,86 +5,124 @@ local addonname, shared = ...
---@diagnostic disable-next-line: missing-fields
shared.Macroer = {}
function shared.Macroer.Init()
---@class stinky
---@field name string
---@field class string
---@field seenAt number
---@class stinky
---@field name string
---@field class string
---@field seenAt number
---@field hostile boolean
---@type table<string, stinky>
local recentStinkies = {}
---@type table<string, stinky>
local recentStinkies = {}
local function FindOrCreateMacro(macroName)
local idx = GetMacroIndexByName(macroName)
if idx == 0 then
CreateMacro(macroName, "INV_Misc_QuestionMark", "")
end
idx = GetMacroIndexByName(macroName)
return idx
end
local function FindOrCreateMacro(macroName)
local idx = GetMacroIndexByName(macroName)
if idx == 0 then
CreateMacro(macroName, "INV_Misc_QuestionMark", "")
end
idx = GetMacroIndexByName(macroName)
return idx
end
---@param stinkies table<string, stinky>
local function FixMacro(stinkies)
local priorityMap = {}
for priority, className in ipairs(Heimdall_Data.config.macroer.priority) do
priorityMap[className] = priority
end
local minPriority = #Heimdall_Data.config.macroer.priority + 1
---@param stinkies table<string, stinky>
local function FixMacro(stinkies)
local priorityMap = {}
for priority, className in ipairs(Heimdall_Data.config.macroer.priority) do
priorityMap[className] = priority
end
local minPriority = #Heimdall_Data.config.macroer.priority + 1
local sortedStinkies = {}
for _, stinky in pairs(stinkies) do
table.insert(sortedStinkies, stinky)
end
local sortedStinkies = {}
for _, stinky in pairs(stinkies) do
table.insert(sortedStinkies, stinky)
end
table.sort(sortedStinkies, function(a, b)
local aPriority = priorityMap[a.class] or minPriority
local bPriority = priorityMap[b.class] or minPriority
return aPriority > bPriority
end)
table.sort(sortedStinkies, function(a, b)
local aPriority = priorityMap[a.class] or minPriority
local bPriority = priorityMap[b.class] or minPriority
return aPriority > bPriority
end)
local lines = { "/targetenemy" }
for _, stinky in pairs(sortedStinkies) do
if stinky.seenAt > GetTime() - 600 then
print(string.format("Macroing %s", stinky.name))
lines[#lines + 1] = string.format("/tar %s", stinky.name)
end
end
local lines = {"/targetenemy"}
for _, stinky in pairs(sortedStinkies) do
if stinky.seenAt > GetTime() - 600 then
print(string.format("Macroing %s", stinky.name))
lines[#lines + 1] = string.format("/tar %s", stinky.name)
end
end
local idx = FindOrCreateMacro("HeimdallTarget")
local body = strjoin("\n", unpack(lines))
EditMacro(idx, "HeimdallTarget", "INV_Misc_QuestionMark", body)
end
local idx = FindOrCreateMacro("HeimdallTarget")
local body = strjoin("\n", unpack(lines))
EditMacro(idx, "HeimdallTarget", "INV_Misc_QuestionMark", body)
end
local frame = CreateFrame("Frame")
frame:RegisterEvent("CHAT_MSG_CHANNEL")
frame:SetScript("OnEvent", function(self, event, msg, sender, ...)
if not Heimdall_Data.config.macroer.enabled then return end
local doUpdate = false
if string.find(msg, "^who:") then
for name, class in string.gmatch(msg, "([^ -/]+)-?%w*/(%w+)") do
recentStinkies[name] = {
name = name,
class = class,
seenAt = GetTime(),
}
doUpdate = true
end
end
local name, class = string.match(msg, "I see (Hostile) ([^ -/]+)-?%w*/(%w+) of")
if not name then
name, class = string.match(msg, "^([^ -/]+)-?%w* of class (%w+)")
end
if name then
name = strtrim(name)
name = string.match(name, "^(.*)-?")
recentStinkies[name] = {
name = name,
class = class,
seenAt = GetTime(),
}
doUpdate = true
end
if doUpdate then FixMacro(recentStinkies) end
end)
---@param msg string
---@return table<string, stinky>
local function ParseWho(msg)
local stinkies = {}
for name, class in string.gmatch(msg, "([^ -/]+)-?%w*/(%w+)") do
stinkies[name] = {
name = name,
class = class,
seenAt = GetTime(),
hostile = true
}
end
return stinkies
end
---@param msg string
---@return table<string, stinky>
local function ParseSee(msg)
local stinkies = {}
local aggression, name, class = string.match(msg, "I see %((%w+)%) ([^ -/]+)-?%w*/(%w+)")
local stinky = {
name = name,
class = class,
seenAt = GetTime(),
hostile = aggression == "Hostile"
}
stinkies[name] = stinky
return stinkies
end
---@param msg string
---@return table<string, stinky>
local function ParseArrived(msg)
local stinkies = {}
return stinkies
end
print("Heimdall - Macroer loaded")
local frame = CreateFrame("Frame")
frame:RegisterEvent("CHAT_MSG_CHANNEL")
frame:SetScript("OnEvent", function(self, event, msg, sender, ...)
if not Heimdall_Data.config.macroer.enabled then
return
end
local doUpdate = false
if string.find(msg, "^who:") then
local whoStinkies = ParseWho(msg)
for name, stinky in pairs(whoStinkies) do
if stinky.hostile then
recentStinkies[name] = stinky
doUpdate = true
end
end
end
if string.find(msg, "^I see") then
local seeStinkies = ParseSee(msg)
for name, stinky in pairs(seeStinkies) do
if stinky.hostile then
recentStinkies[name] = stinky
doUpdate = true
end
if not stinky.hostile then
recentStinkies[name] = nil
doUpdate = true
end
end
end
if doUpdate then
FixMacro(recentStinkies)
end
end)
print("Heimdall - Macroer loaded")
end