Lucas-Bot/src/MessageDispatcher.h

190 lines
5.5 KiB
C
Raw Normal View History

2022-10-08 22:47:18 +08:00
/*
* This file is part of Lucas' Bot.
*
* Lucas' Bot is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
Lucas' Bot is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with Lucas' Bot. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MIRAICPPLUGIN_MESSAGE_DISPATCHER_H
#define MIRAICPPLUGIN_MESSAGE_DISPATCHER_H
#include <MiraiCP.hpp>
#include <string>
#include <unordered_map>
#include <mutex>
#include <thread>
#include <queue>
#include <condition_variable>
#include <functional>
namespace MessageDispatcher {
// 全局类型定义
/**
* @brief
*/
constexpr char instructionPrefix = '.';
/**
* @brief
* MiraiCP的文档说明
*/
enum class MessageType {
MarketFace = -5,
OnlineForwardedMessage = -4,
OnlineAudio = -3,
QuoteReply = -2,
UnSupportMessage = -1,
PlainText = 0,
At = 1,
AtAll = 2,
Image = 3,
App = 4,
Service = 5,
File = 6,
Face = 7,
FlashImage = 8,
MusicShare = 9
};
/**
* @brief
*/
enum class InstructionType{
Repeat, // 复读
CommercialAccount, // 营销号
UselessParagraph, // 废话
Trash, // 垃圾
Sick, // 发病
KFCCrazyThursday, // 疯狂星期四V我50
TouhouProject, // 东方Project
SignOn, // 签到
Fishing, // 钓鱼
Working, // 打工
FiveThousandMega, // 5000兆表情包生成
Menu, // 菜单
About, // 关于
NotImplemented, // 未实现
OtherMessage // 普通聊天消息
};
/**
* @brief SingleInstruction
*
*/
struct SingleInstruction {
const MiraiCP::GroupMessageEvent& event;
InstructionType type;
std::vector<std::string> arguments;
};
/**
* @brief const SingleInstruction& -> void
*/
using InstructionProcedure = std::function<void(const SingleInstruction&)>;
// 全局数据结构定义
/**
* @brief
*/
extern std::queue<MiraiCP::GroupMessageEvent> messageQueue;
/**
* @brief
*/
extern std::mutex messageQueueMutex;
/**
* @brief
*/
extern std::condition_variable msgQueueCondVariable;
/**
* @brief
*/
extern std::unordered_map<InstructionType, InstructionProcedure,
std::hash<InstructionType>> instructionProcedureMap;
// 接口
/**
* @brief produceMessage producer的接口
* @param event
*/
void produceMessage(const MiraiCP::GroupMessageEvent& event);
/**
* @brief translateMessage SingleInstruction结构
* @param event
* @return
*/
SingleInstruction translateMessage(const MiraiCP::GroupMessageEvent& event);
/**
* @brief text字符串对应的指令信息
* @param text
* @return text与某一条指令匹配
* text不与任意一条指令匹配InstructionType::NotImplemented
*/
InstructionType queryInstructionTypeFromText(const std::string& text);
/**
* @brief
* @param instruction
*/
void dispatchInstruction(const SingleInstruction& instruction);
/**
* @brief
* @param instructionType
* @param instructionProcedure
*/
void registerInstructionProcedure(InstructionType instructionType,
InstructionProcedure instructionProcedure);
/**
* @brief
* @param instruction
*/
void defaultInstructionProcedure(const SingleInstruction& instruction);
/**
* @brief 线
*/
[[noreturn]]
void consumerThreadFunction();
/**
* @brief delimstr切割并存储在返回值的vector中
* @param str
* @param delimiter
* @return vector
*/
std::vector<std::string>
split(const std::string& str, char delim);
}
// 打开命名空间std并对InstructionType定义std::hash特化
namespace std{
template<>
struct hash<MessageDispatcher::InstructionType>{
size_t operator() (const MessageDispatcher::InstructionType& instruction) const {
return std::hash<int>()(static_cast<int>(instruction));
}
};
}
#endif //MIRAICPPLUGIN_MESSAGE_DISPATCHER_H