From fb4c3481d2977a7285e92ee539925f82605cde90 Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Wed, 15 Sep 2021 00:44:00 +0200 Subject: [PATCH] Implement help information loader --- bin/config_sample/text/commandhelp.json | 7 +++++ core/include/config_manager.h | 28 +++++++++++++++++ core/src/config_manager.cpp | 41 ++++++++++++++++++++++++- core/src/server.cpp | 3 ++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 bin/config_sample/text/commandhelp.json diff --git a/bin/config_sample/text/commandhelp.json b/bin/config_sample/text/commandhelp.json new file mode 100644 index 0000000..f6cbde8 --- /dev/null +++ b/bin/config_sample/text/commandhelp.json @@ -0,0 +1,7 @@ +[ + { + "name": "foo", + "usage": "/foo [baz|qux]", + "text": "A sample explanation." + } +] \ No newline at end of file diff --git a/core/include/config_manager.h b/core/include/config_manager.h index e6db121..eedcf42 100644 --- a/core/include/config_manager.h +++ b/core/include/config_manager.h @@ -77,6 +77,13 @@ class ConfigManager { */ static QStringList musiclist(); + /** + * @brief Loads help information into m_help_information. + * + * @return See short description. + */ + static void loadcommandHelp(); + /** * @brief Returns the duration of a song in the songlist. * @param The name of the song where duration is requested @@ -422,6 +429,22 @@ class ConfigManager { */ static qint64 uptime(); + /** + * @brief A struct that contains the help information for a command. + * It's split in the syntax and the explanation text. + */ + struct help { + QString usage; + QString text; + }; + + /** + * @brief Returns a struct with the help information of the command. + * + * @return See short description. + */ + static help commandHelp(QString f_command_name); + /** * @brief Sets the server's authorization type. * @@ -500,6 +523,11 @@ private: */ static QHash* m_musicList; + /** + * @brief QHash containing the help information for all commands registered to the server. + */ + static QHash* m_commands_help; + /** * @brief Returns a stringlist with the contents of a .txt file from config/text/. * diff --git a/core/src/config_manager.cpp b/core/src/config_manager.cpp index 5e21cc6..0935e9c 100644 --- a/core/src/config_manager.cpp +++ b/core/src/config_manager.cpp @@ -25,6 +25,7 @@ QSettings* ConfigManager::m_areas = new QSettings("config/areas.ini", QSettings: ConfigManager::CommandSettings* ConfigManager::m_commands = new CommandSettings(); QElapsedTimer* ConfigManager::m_uptimeTimer = new QElapsedTimer; QHash* ConfigManager::m_musicList = new QHash; +QHash* ConfigManager::m_commands_help = new QHash; bool ConfigManager::verifyServerConfig() { @@ -39,7 +40,8 @@ bool ConfigManager::verifyServerConfig() // Verify config files QStringList l_config_files{"config/config.ini", "config/areas.ini", "config/backgrounds.txt", "config/characters.txt", "config/music.json", - "config/discord.ini", "config/text/8ball.txt", "config/text/gimp.txt", "config/text/praise.txt", "config/text/reprimands.txt"}; + "config/discord.ini", "config/text/8ball.txt", "config/text/gimp.txt", "config/text/praise.txt", + "config/text/reprimands.txt","config/text/commandhelp.json"}; for (const QString &l_file : l_config_files) { if (!fileExists(QFileInfo(l_file))) { qCritical() << l_file + " does not exist!"; @@ -173,6 +175,38 @@ QStringList ConfigManager::musiclist() return l_musiclist; } +void ConfigManager::loadcommandHelp() +{ + QFile l_music_json("config/text/commandhelp.json"); + l_music_json.open(QIODevice::ReadOnly | QIODevice::Text); + + QJsonParseError l_error; + QJsonDocument l_music_list_json = QJsonDocument::fromJson(l_music_json.readAll(), &l_error); + if (!(l_error.error == QJsonParseError::NoError)) { //Non-Terminating error. + qWarning() << "Unable to help information. The following error was encounted : " + l_error.errorString(); + } + + // Akashi expects the helpfile to contain multiple entires, so it always checks for an array first. + QJsonArray l_Json_root_array = l_music_list_json.array(); + QJsonObject l_child_obj; + + for (int i = 0; i <= l_Json_root_array.size() -1; i++){ + l_child_obj = l_Json_root_array.at(i).toObject(); + QString l_name = l_child_obj["name"].toString(); + QString l_usage = l_child_obj["usage"].toString(); + QString l_text = l_child_obj["text"].toString(); + + if (!l_name.isEmpty()) { + help l_help_information; + l_help_information.usage = l_usage; + l_help_information.text = l_text; + + m_commands_help->insert(l_name,l_help_information); + qDebug() << commandHelp("foo").text; + } + } +} + int ConfigManager::songInformation(const QString &f_songName) { return m_musicList->value(f_songName); @@ -540,6 +574,11 @@ qint64 ConfigManager::uptime() return m_uptimeTimer->elapsed(); } +ConfigManager::help ConfigManager::commandHelp(QString f_command_name) +{ + return m_commands_help->value(f_command_name); +} + void ConfigManager::setMotd(const QString f_motd) { m_settings->setValue("Options/motd", f_motd); diff --git a/core/src/server.cpp b/core/src/server.cpp index 8ff9212..7d92017 100644 --- a/core/src/server.cpp +++ b/core/src/server.cpp @@ -97,6 +97,9 @@ void Server::start() this, QOverload::of(&Server::broadcast)); } + //Loads the command help information. This is not stored inside the server. + ConfigManager::loadcommandHelp(); + //Rate-Limiter for IC-Chat connect(&next_message_timer, SIGNAL(timeout()), this, SLOT(allowMessage())); }