Refactor Heimdall messenger module to improve structure and utilize ReactiveValue for queue management
This commit is contained in:
24
Heimdall.lua
24
Heimdall.lua
@@ -93,23 +93,6 @@ local function init()
|
|||||||
---@field locale string
|
---@field locale string
|
||||||
---@field debug boolean
|
---@field debug boolean
|
||||||
|
|
||||||
--- Data ---
|
|
||||||
---@class HeimdallMessengerData
|
|
||||||
---@field queue table<string, Message>
|
|
||||||
---@field ticker Timer?
|
|
||||||
|
|
||||||
---@class HeimdallNetworkMessengerData
|
|
||||||
---@field queue table<string, Message>
|
|
||||||
---@field ticker Timer?
|
|
||||||
|
|
||||||
---@class HeimdallWhoData
|
|
||||||
---@field updateTicker Timer?
|
|
||||||
---@field whoTicker Timer?
|
|
||||||
---@field ignored table<string, boolean>
|
|
||||||
|
|
||||||
---@class HeimdallStinkyCacheData
|
|
||||||
---@field stinkies table<string, {value: number, timestamp: number}>
|
|
||||||
|
|
||||||
shared.GetOrDefault = function(table, keys, default)
|
shared.GetOrDefault = function(table, keys, default)
|
||||||
local value = default
|
local value = default
|
||||||
if not table then return value end
|
if not table then return value end
|
||||||
@@ -129,13 +112,6 @@ local function init()
|
|||||||
return value
|
return value
|
||||||
end
|
end
|
||||||
|
|
||||||
shared.messenger = {
|
|
||||||
queue = {},
|
|
||||||
}
|
|
||||||
shared.who = {
|
|
||||||
ignored = {},
|
|
||||||
}
|
|
||||||
|
|
||||||
--/run Heimdall_Data.config.who.queries="g-\"БеспредеЛ\"|ally"
|
--/run Heimdall_Data.config.who.queries="g-\"БеспредеЛ\"|ally"
|
||||||
Heimdall_Data.config = {
|
Heimdall_Data.config = {
|
||||||
debug = shared.GetOrDefault(Heimdall_Data, { "config", "debug" }, false),
|
debug = shared.GetOrDefault(Heimdall_Data, { "config", "debug" }, false),
|
||||||
|
@@ -7,6 +7,10 @@ local ModuleName = "Messenger"
|
|||||||
---@field debug boolean
|
---@field debug boolean
|
||||||
---@field interval number
|
---@field interval number
|
||||||
|
|
||||||
|
---@class HeimdallMessengerData
|
||||||
|
---@field queue ReactiveValue<table<string, Message>>
|
||||||
|
---@field ticker Timer?
|
||||||
|
|
||||||
---@class Message
|
---@class Message
|
||||||
---@field message string
|
---@field message string
|
||||||
---@field channel string
|
---@field channel string
|
||||||
@@ -14,7 +18,13 @@ local ModuleName = "Messenger"
|
|||||||
|
|
||||||
---@class Messenger
|
---@class Messenger
|
||||||
shared.Messenger = {
|
shared.Messenger = {
|
||||||
|
---@param message Message
|
||||||
|
Enqueue = function(message) table.insert(shared.messenger.queue, message) end,
|
||||||
Init = function()
|
Init = function()
|
||||||
|
shared.messenger = {
|
||||||
|
queue = ReactiveValue.new({}),
|
||||||
|
}
|
||||||
|
|
||||||
local function FindOrJoinChannel(channelName, password)
|
local function FindOrJoinChannel(channelName, password)
|
||||||
local channelId = GetChannelName(channelName)
|
local channelId = GetChannelName(channelName)
|
||||||
if channelId == 0 then
|
if channelId == 0 then
|
||||||
@@ -34,14 +44,15 @@ shared.Messenger = {
|
|||||||
return channelId
|
return channelId
|
||||||
end
|
end
|
||||||
|
|
||||||
---@diagnostic disable-next-line: missing-fields
|
|
||||||
if not shared.messenger then shared.messenger = {} end
|
|
||||||
if not shared.messenger.queue then shared.messenger.queue = {} end
|
|
||||||
if not shared.messenger.ticker then
|
if not shared.messenger.ticker then
|
||||||
local function DoMessage()
|
local function DoMessage()
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(
|
print(
|
||||||
string.format("[%s] Processing message queue - Size: %d", ModuleName, #shared.messenger.queue)
|
string.format(
|
||||||
|
"[%s] Processing message queue - Size: %d",
|
||||||
|
ModuleName,
|
||||||
|
#shared.messenger.queue:get()
|
||||||
|
)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -53,7 +64,7 @@ shared.Messenger = {
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@type Message
|
---@type Message
|
||||||
local message = shared.messenger.queue[1]
|
local message = shared.messenger.queue:get()[1]
|
||||||
if not message then
|
if not message then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(string.format("[%s] Message queue empty", ModuleName))
|
print(string.format("[%s] Message queue empty", ModuleName))
|
||||||
@@ -61,65 +72,51 @@ shared.Messenger = {
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then shared.dumpTable(message, "[%s] Processing message:") end
|
||||||
print(
|
|
||||||
string.format(
|
|
||||||
"[%s] Processing message - Channel: %s, Data: %s",
|
|
||||||
ModuleName,
|
|
||||||
message.channel or "nil",
|
|
||||||
message.data or "nil"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
print(string.format("[%s] Message content: %s", ModuleName, message.message or "nil"))
|
|
||||||
end
|
|
||||||
|
|
||||||
if not message.message or message.message == "" then
|
if not message.message or message.message == "" then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(string.format("[%s] Invalid message: empty content", ModuleName))
|
shared.dumpTable(message, string.format("[%s] Invalid message: empty content", ModuleName))
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if not message.channel or message.channel == "" then
|
if not message.channel or message.channel == "" then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(string.format("[%s] Invalid message: no channel specified", ModuleName))
|
shared.dumpTable(
|
||||||
|
message,
|
||||||
|
string.format("[%s] Invalid message: no channel specified", ModuleName)
|
||||||
|
)
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if string.find(message.channel, "^C") then
|
if string.find(message.channel, "^C") then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(string.format("[%s] Converting channel type from C to CHANNEL", ModuleName))
|
shared.dumpTable(
|
||||||
|
message,
|
||||||
|
string.format("[%s] Converting channel type from C to CHANNEL", ModuleName)
|
||||||
|
)
|
||||||
end
|
end
|
||||||
message.channel = "CHANNEL"
|
message.channel = "CHANNEL"
|
||||||
elseif string.find(message.channel, "^W") then
|
elseif string.find(message.channel, "^W") then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(string.format("[%s] Converting channel type from W to WHISPER", ModuleName))
|
shared.dumpTable(
|
||||||
|
message,
|
||||||
|
string.format("[%s] Converting channel type from W to WHISPER", ModuleName)
|
||||||
|
)
|
||||||
end
|
end
|
||||||
message.channel = "WHISPER"
|
message.channel = "WHISPER"
|
||||||
end
|
end
|
||||||
|
|
||||||
if message.channel == "CHANNEL" and message.data and string.match(message.data, "%D") then
|
if message.channel == "CHANNEL" and message.data and string.match(message.data, "%D") then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(
|
shared.dumpTable(message, string.format("[%s] Processing channel message:", ModuleName))
|
||||||
string.format(
|
|
||||||
"[%s] Processing channel message: '%s' to '%s'",
|
|
||||||
ModuleName,
|
|
||||||
message.message,
|
|
||||||
message.data
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
local channelId = GetChannelName(message.data)
|
local channelId = GetChannelName(message.data)
|
||||||
if channelId == 0 then
|
if channelId == 0 then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(
|
shared.dumpTable(message, string.format("[%s] Channel not found, joining:", ModuleName))
|
||||||
string.format(
|
|
||||||
"[%s] Channel not found, attempting to join: %s",
|
|
||||||
ModuleName,
|
|
||||||
message.data
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
channelId = FindOrJoinChannel(message.data)
|
channelId = FindOrJoinChannel(message.data)
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
@@ -132,43 +129,38 @@ shared.Messenger = {
|
|||||||
table.remove(shared.messenger.queue, 1)
|
table.remove(shared.messenger.queue, 1)
|
||||||
if not message.message or message.message == "" then
|
if not message.message or message.message == "" then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(string.format("[%s] Skipping empty message", ModuleName))
|
shared.dumpTable(message, string.format("[%s] Skipping empty message", ModuleName))
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if not message.channel or message.channel == "" then
|
if not message.channel or message.channel == "" then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(string.format("[%s] Skipping message with no channel", ModuleName))
|
shared.dumpTable(message, string.format("[%s] Skipping message with no channel", ModuleName))
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if not message.data or message.data == "" then
|
if not message.data or message.data == "" then
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(string.format("[%s] Skipping message with no data", ModuleName))
|
shared.dumpTable(message, string.format("[%s] Skipping message with no data", ModuleName))
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(
|
shared.dumpTable(message, string.format("[%s] Sending message:", ModuleName))
|
||||||
string.format(
|
|
||||||
"[%s] Sending message: '%s' to %s:%s",
|
|
||||||
ModuleName,
|
|
||||||
message.message,
|
|
||||||
message.channel,
|
|
||||||
message.data
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
if string.len(message.message) > 255 then
|
if string.len(message.message) > 255 then
|
||||||
print(string.format("[%s] Message too long!!!!: %s", ModuleName, message.message))
|
shared.dumpTable(
|
||||||
|
message,
|
||||||
|
string.format("[%s] Message too long!!!!: %s", ModuleName, message.message)
|
||||||
|
)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
SendChatMessage(message.message, message.channel, nil, message.data)
|
SendChatMessage(message.message, message.channel, nil, message.data)
|
||||||
end
|
end
|
||||||
local function Tick()
|
local function Tick()
|
||||||
if Heimdall_Data.config.messenger.debug then
|
if Heimdall_Data.config.messenger.debug then
|
||||||
print(string.format("[%s] Tick - Queue size: %d", ModuleName, #shared.messenger.queue))
|
print(string.format("[%s] Tick - Queue size: %d", ModuleName, #shared.messenger.queue:get()))
|
||||||
end
|
end
|
||||||
DoMessage()
|
DoMessage()
|
||||||
shared.messenger.ticker = C_Timer.NewTimer(Heimdall_Data.config.messenger.interval, Tick, 1)
|
shared.messenger.ticker = C_Timer.NewTimer(Heimdall_Data.config.messenger.interval, Tick, 1)
|
||||||
|
Reference in New Issue
Block a user