Refactor commands to separate structure
This commit is contained in:
@@ -123,6 +123,42 @@ function shared.Commander.Init()
|
|||||||
end
|
end
|
||||||
return text
|
return text
|
||||||
end
|
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")
|
local commanderChannelFrame = CreateFrame("Frame")
|
||||||
commanderChannelFrame:RegisterEvent("CHAT_MSG_CHANNEL")
|
commanderChannelFrame:RegisterEvent("CHAT_MSG_CHANNEL")
|
||||||
@@ -132,89 +168,18 @@ function shared.Commander.Init()
|
|||||||
local _, channelname = GetChannelName(channelId)
|
local _, channelname = GetChannelName(channelId)
|
||||||
if channelname ~= Heimdall_Data.config.commander.masterChannel then return end
|
if channelname ~= Heimdall_Data.config.commander.masterChannel then return end
|
||||||
|
|
||||||
if msg == "who" then
|
for _, command in ipairs(commands) do
|
||||||
local messages = WhoPartitioned(HeimdallStinkies)
|
if msg:match(command.keywordRe) then
|
||||||
if #messages == 0 then
|
local messages = command.callback({ strsplit(" ", msg) })
|
||||||
---@type Message
|
for _, message in ipairs(messages) do
|
||||||
local msg = {
|
---@type Message
|
||||||
channel = "CHANNEL",
|
local msg = {
|
||||||
data = channelname,
|
channel = "CHANNEL",
|
||||||
message = "who: No stinkies found"
|
data = channelname,
|
||||||
}
|
message = message
|
||||||
table.insert(shared.messenger.queue, msg)
|
}
|
||||||
end
|
table.insert(shared.messenger.queue, msg)
|
||||||
for _, message in ipairs(messages) do
|
end
|
||||||
---@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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user