Refactor commands to separate structure
This commit is contained in:
@@ -123,6 +123,42 @@ function shared.Commander.Init()
|
||||
end
|
||||
return text
|
||||
end
|
||||
local function CountClassPartitionedStinkies()
|
||||
local res = CountClassPartitioned(HeimdallStinkies)
|
||||
if #res == 0 then
|
||||
return { "No stinkies found" }
|
||||
end
|
||||
return res
|
||||
end
|
||||
local function WhoPartitionedStinkies()
|
||||
local res = WhoPartitioned(HeimdallStinkies)
|
||||
if #res == 0 then
|
||||
return { "No stinkies found" }
|
||||
end
|
||||
return res
|
||||
end
|
||||
local function CountPartitionedStinkies()
|
||||
local res = CountPartitioned(HeimdallStinkies)
|
||||
if #res == 0 then
|
||||
return { "No stinkies found" }
|
||||
end
|
||||
return res
|
||||
end
|
||||
local function HelpRu() return helpMessages.ru end
|
||||
local function HelpEn() return helpMessages.en end
|
||||
|
||||
---@class Command
|
||||
---@field keywordRe string
|
||||
---@field commanderOnly boolean
|
||||
---@field callback fun(...: any): string[]
|
||||
|
||||
local commands = {
|
||||
{ keywordRe = "^who", commanderOnly = false, callback = WhoPartitionedStinkies },
|
||||
{ keywordRe = "^howmany", commanderOnly = false, callback = CountPartitionedStinkies },
|
||||
{ keywordRe = "^classes", commanderOnly = false, callback = CountClassPartitionedStinkies },
|
||||
{ keywordRe = "^help", commanderOnly = false, callback = HelpRu },
|
||||
{ keywordRe = "^helpen", commanderOnly = false, callback = HelpEn },
|
||||
}
|
||||
|
||||
local commanderChannelFrame = CreateFrame("Frame")
|
||||
commanderChannelFrame:RegisterEvent("CHAT_MSG_CHANNEL")
|
||||
@@ -132,89 +168,18 @@ function shared.Commander.Init()
|
||||
local _, channelname = GetChannelName(channelId)
|
||||
if channelname ~= Heimdall_Data.config.commander.masterChannel then return end
|
||||
|
||||
if msg == "who" then
|
||||
local messages = WhoPartitioned(HeimdallStinkies)
|
||||
if #messages == 0 then
|
||||
---@type Message
|
||||
local msg = {
|
||||
channel = "CHANNEL",
|
||||
data = channelname,
|
||||
message = "who: No stinkies found"
|
||||
}
|
||||
table.insert(shared.messenger.queue, msg)
|
||||
end
|
||||
for _, message in ipairs(messages) do
|
||||
---@type Message
|
||||
local msg = {
|
||||
channel = "CHANNEL",
|
||||
data = channelname,
|
||||
message = "who: " .. message
|
||||
}
|
||||
table.insert(shared.messenger.queue, msg)
|
||||
end
|
||||
end
|
||||
if msg == "howmany" then
|
||||
local messages = CountPartitioned(HeimdallStinkies)
|
||||
if #messages == 0 then
|
||||
---@type Message
|
||||
local msg = {
|
||||
channel = "CHANNEL",
|
||||
data = channelname,
|
||||
message = "howmany: No stinkies found"
|
||||
}
|
||||
table.insert(shared.messenger.queue, msg)
|
||||
end
|
||||
for _, message in ipairs(messages) do
|
||||
---@type Message
|
||||
local msg = {
|
||||
channel = "CHANNEL",
|
||||
data = channelname,
|
||||
message = "howmany: " .. message
|
||||
}
|
||||
table.insert(shared.messenger.queue, msg)
|
||||
end
|
||||
end
|
||||
if msg == "classes" then
|
||||
local messages = CountClassPartitioned(HeimdallStinkies)
|
||||
if #messages == 0 then
|
||||
---@type Message
|
||||
local msg = {
|
||||
channel = "CHANNEL",
|
||||
data = channelname,
|
||||
message = "classes: No stinkies found"
|
||||
}
|
||||
table.insert(shared.messenger.queue, msg)
|
||||
end
|
||||
for _, message in ipairs(messages) do
|
||||
---@type Message
|
||||
local msg = {
|
||||
channel = "CHANNEL",
|
||||
data = channelname,
|
||||
message = "classes: " .. message
|
||||
}
|
||||
table.insert(shared.messenger.queue, msg)
|
||||
end
|
||||
end
|
||||
if msg == "help" then
|
||||
for _, helpMessage in ipairs(helpMessages.ru) do
|
||||
---@type Message
|
||||
local msg = {
|
||||
channel = "CHANNEL",
|
||||
data = channelname,
|
||||
message = helpMessage
|
||||
}
|
||||
table.insert(shared.messenger.queue, msg)
|
||||
end
|
||||
end
|
||||
if msg == "helpen" then
|
||||
for _, helpMessage in ipairs(helpMessages.en) do
|
||||
---@type Message
|
||||
local msg = {
|
||||
channel = "CHANNEL",
|
||||
data = channelname,
|
||||
message = helpMessage
|
||||
}
|
||||
table.insert(shared.messenger.queue, msg)
|
||||
for _, command in ipairs(commands) do
|
||||
if msg:match(command.keywordRe) then
|
||||
local messages = command.callback({ strsplit(" ", msg) })
|
||||
for _, message in ipairs(messages) do
|
||||
---@type Message
|
||||
local msg = {
|
||||
channel = "CHANNEL",
|
||||
data = channelname,
|
||||
message = message
|
||||
}
|
||||
table.insert(shared.messenger.queue, msg)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user