Refactor AgentTracker and related modules to improve agent management and logging
This commit is contained in:
@@ -2,118 +2,136 @@ local _, shared = ...
|
||||
---@cast shared HeimdallShared
|
||||
local ModuleName = "AgentTracker"
|
||||
|
||||
---@diagnostic disable-next-line: missing-fields
|
||||
shared.AgentTracker = {}
|
||||
function shared.AgentTracker.Init()
|
||||
--/run Heimdall_Data.config.agents["Cyheuraeth"]=date("%Y-%m-%dT%H:%M:%S")
|
||||
---@type table<string, boolean>
|
||||
local channelRosterFrame = CreateFrame("Frame")
|
||||
channelRosterFrame:RegisterEvent("CHANNEL_ROSTER_UPDATE")
|
||||
channelRosterFrame:SetScript("OnEvent", function(self, event, index)
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Channel roster update received", ModuleName))
|
||||
end
|
||||
if not Heimdall_Data.config.agentTracker.enabled then
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Module disabled, ignoring roster update", ModuleName))
|
||||
end
|
||||
return
|
||||
end
|
||||
local name = GetChannelDisplayInfo(index)
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Processing channel update: %s (index: %d)", ModuleName, name or "nil", index))
|
||||
end
|
||||
if name ~= Heimdall_Data.config.agentTracker.masterChannel then
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Ignoring non-master channel: %s", ModuleName, name or "nil"))
|
||||
end
|
||||
return
|
||||
end
|
||||
local count = select(5, GetChannelDisplayInfo(index))
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Processing %d members in channel", ModuleName, count))
|
||||
end
|
||||
---@class AgentTracker
|
||||
---@field Init fun(): nil
|
||||
---@field Track fun(name: string): boolean
|
||||
---@field IsAgent fun(name: string): boolean
|
||||
---@field OnChange fun(callback: fun(name: string)): nil
|
||||
---@field ForEach fun(callback: fun(name: string)): nil
|
||||
|
||||
local newAgents = 0
|
||||
for i = 1, count do
|
||||
name = GetChannelRosterInfo(index, i)
|
||||
if name then
|
||||
local isNewAgent = not Heimdall_Data.config.agents[name]
|
||||
Heimdall_Data.config.agents[name] = date("%Y-%m-%dT%H:%M:%S")
|
||||
if isNewAgent then newAgents = newAgents + 1 end
|
||||
---@class AgentTrackerData
|
||||
---@field agents ReactiveValue<table<string, string>>
|
||||
|
||||
shared.agentTracker.agents = ReactiveValue(Heimdall_Data.config.agents)
|
||||
|
||||
---@class AgentTracker
|
||||
shared.AgentTracker = {
|
||||
Track = function(name)
|
||||
if not name then return false end
|
||||
local exists = shared.AgentTracker.IsAgent(name)
|
||||
if exists then return false end
|
||||
shared.agentTracker.agents[name] = date("%Y-%m-%dT%H:%M:%S")
|
||||
-- Heimdall_Data.config.agents[name] = date("%Y-%m-%dT%H:%M:%S")
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Tracking new agent: %s", ModuleName, name))
|
||||
shared.dumpTable(shared.agentTracker.agents)
|
||||
end
|
||||
return true
|
||||
end,
|
||||
IsAgent = function(name)
|
||||
if not name then return false end
|
||||
return shared.agentTracker.agents[name] ~= nil
|
||||
end,
|
||||
OnChange = function(callback) shared.agentTracker.agents:onChange(callback) end,
|
||||
ForEach = function(callback)
|
||||
---@type table<string, string>
|
||||
local agents = shared.agentTracker.agents:get()
|
||||
for name, _ in pairs(agents) do
|
||||
callback(name)
|
||||
end
|
||||
end,
|
||||
Init = function()
|
||||
--/run Heimdall_Data.config.agents["Cyheuraeth"]=date("%Y-%m-%dT%H:%M:%S")
|
||||
---@type table<string, boolean>
|
||||
local channelRosterFrame = CreateFrame("Frame")
|
||||
channelRosterFrame:RegisterEvent("CHANNEL_ROSTER_UPDATE")
|
||||
channelRosterFrame:SetScript("OnEvent", function(self, event, index)
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Channel roster update received", ModuleName))
|
||||
end
|
||||
if not Heimdall_Data.config.agentTracker.enabled then
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(
|
||||
string.format(
|
||||
"[%s] %s agent: %s",
|
||||
ModuleName,
|
||||
isNewAgent and "Added new" or "Updated existing",
|
||||
name
|
||||
)
|
||||
)
|
||||
print(string.format("[%s] Module disabled, ignoring roster update", ModuleName))
|
||||
end
|
||||
return
|
||||
end
|
||||
local name = GetChannelDisplayInfo(index)
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Processing channel update: %s (index: %d)", ModuleName, name or "nil", index))
|
||||
end
|
||||
if name ~= Heimdall_Data.config.agentTracker.masterChannel then
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Ignoring non-master channel: %s", ModuleName, name or "nil"))
|
||||
end
|
||||
return
|
||||
end
|
||||
local count = select(5, GetChannelDisplayInfo(index))
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Processing %d members in channel", ModuleName, count))
|
||||
end
|
||||
|
||||
local newAgents = 0
|
||||
for i = 1, count do
|
||||
name = GetChannelRosterInfo(index, i)
|
||||
shared.AgentTracker.Track(name)
|
||||
end
|
||||
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Roster update complete - Added %d new agents", ModuleName, newAgents))
|
||||
end
|
||||
end)
|
||||
|
||||
local agentTrackerChannelSniffer = CreateFrame("Frame")
|
||||
agentTrackerChannelSniffer:RegisterEvent("CHAT_MSG_CHANNEL")
|
||||
agentTrackerChannelSniffer:SetScript("OnEvent", function(self, event, msg, sender, ...)
|
||||
-- if Heimdall_Data.config.agentTracker.debug then
|
||||
-- print(string.format("[%s] Channel message received from: %s", ModuleName, sender))
|
||||
-- end
|
||||
if not Heimdall_Data.config.agentTracker.enabled then
|
||||
-- if Heimdall_Data.config.agentTracker.debug then
|
||||
-- print(string.format("[%s] Module disabled, ignoring channel message", ModuleName))
|
||||
-- end
|
||||
return
|
||||
end
|
||||
|
||||
local channelId = select(6, ...)
|
||||
local _, channelname = GetChannelName(channelId)
|
||||
local ok = false
|
||||
for _, channel in pairs(Heimdall_Data.config.agentTracker.channels) do
|
||||
if channel == channelname then
|
||||
ok = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Roster update complete - Added %d new agents", ModuleName, newAgents))
|
||||
end
|
||||
end)
|
||||
|
||||
local agentTrackerChannelSniffer = CreateFrame("Frame")
|
||||
agentTrackerChannelSniffer:RegisterEvent("CHAT_MSG_CHANNEL")
|
||||
agentTrackerChannelSniffer:SetScript("OnEvent", function(self, event, msg, sender, ...)
|
||||
-- if Heimdall_Data.config.agentTracker.debug then
|
||||
-- print(string.format("[%s] Channel message received from: %s", ModuleName, sender))
|
||||
-- end
|
||||
if not Heimdall_Data.config.agentTracker.enabled then
|
||||
-- if Heimdall_Data.config.agentTracker.debug then
|
||||
-- print(string.format("[%s] Module disabled, ignoring channel message", ModuleName))
|
||||
-- end
|
||||
return
|
||||
end
|
||||
|
||||
local channelId = select(6, ...)
|
||||
local _, channelname = GetChannelName(channelId)
|
||||
local ok = false
|
||||
for _, channel in pairs(Heimdall_Data.config.agentTracker.channels) do
|
||||
if channel == channelname then
|
||||
ok = true
|
||||
break
|
||||
if not ok then
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Channel name does not match any of the channels", ModuleName))
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
if not ok then
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Channel name does not match any of the channels", ModuleName))
|
||||
print(string.format("[%s] Processing message from master channel: %s", ModuleName, sender))
|
||||
end
|
||||
return
|
||||
end
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Processing message from master channel: %s", ModuleName, sender))
|
||||
shared.dumpTable(Heimdall_Data.config.agentTracker)
|
||||
end
|
||||
|
||||
sender = string.match(sender, "^[^-]+")
|
||||
local isNewAgent = not Heimdall_Data.config.agents[sender]
|
||||
Heimdall_Data.config.agents[sender] = date("%Y-%m-%dT%H:%M:%S")
|
||||
sender = string.match(sender, "^[^-]+")
|
||||
local new = shared.AgentTracker.Track(sender)
|
||||
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(
|
||||
string.format(
|
||||
"[%s] %s agent from message: %s",
|
||||
ModuleName,
|
||||
isNewAgent and "Added new" or "Updated existing",
|
||||
sender
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(
|
||||
string.format(
|
||||
"[%s] %s agent from message: %s",
|
||||
ModuleName,
|
||||
new and "Added new" or "Updated existing",
|
||||
sender
|
||||
)
|
||||
)
|
||||
)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
local count = 0
|
||||
for _ in pairs(Heimdall_Data.config.agents) do
|
||||
count = count + 1
|
||||
if Heimdall_Data.config.agentTracker.debug then
|
||||
print(string.format("[%s] Module initialized", ModuleName))
|
||||
shared.dumpTable(shared.agentTracker.agents:get(), "Agents")
|
||||
end
|
||||
print(string.format("[%s] Module initialized - Tracking %d agents", ModuleName, count))
|
||||
end
|
||||
print("[Heimdall] AgentTracker loaded")
|
||||
end
|
||||
print("[Heimdall] AgentTracker loaded")
|
||||
end,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user