From 03827f8976b1c74520da55748c89bbf65764bd56 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Sat, 31 Dec 2016 14:05:24 +0100 Subject: [PATCH 01/66] Added first version of Database implementation (MySQL C++ connector). --- CMakeLists.txt | 5 +- config.dist.yaml | 8 +++ install.sh | 4 +- plugins/LocalRecords/CMakeLists.txt | 19 +++++++ plugins/LocalRecords/build.sh | 4 ++ .../LocalRecords/src/LocalRecordsPlugin.cpp | 28 ++++++++++ plugins/LocalRecords/src/LocalRecordsPlugin.h | 16 ++++++ plugins/LocalRecords/src/main.cpp | 25 +++++++++ src/Config/Config.cpp | 8 +++ src/Config/Config.h | 16 ++++++ src/Database/Database.cpp | 23 ++++++++ src/Database/Database.h | 24 +++++++++ src/ManiaPP.cpp | 54 +++++++++++++++---- src/ManiaPP.h | 8 +++ src/Plugins/Plugin.h | 25 +++++++-- src/Plugins/PluginManager.cpp | 4 +- src/Plugins/PluginManager.h | 5 +- src/Utils/Logging.h | 15 ++++++ 18 files changed, 270 insertions(+), 21 deletions(-) create mode 100644 plugins/LocalRecords/CMakeLists.txt create mode 100755 plugins/LocalRecords/build.sh create mode 100644 plugins/LocalRecords/src/LocalRecordsPlugin.cpp create mode 100644 plugins/LocalRecords/src/LocalRecordsPlugin.h create mode 100644 plugins/LocalRecords/src/main.cpp create mode 100644 src/Database/Database.cpp create mode 100644 src/Database/Database.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b0d975d0..2e6389f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,4 +27,7 @@ file(GLOB_RECURSE SOURCES src/*.cpp) add_executable(Mania++ ${SOURCES} lib/pugixml/libpugixml.a) -target_link_libraries(Mania++ yaml-cpp ${EXECUTABLE_OUTPUT_PATH}/lib/pugixml/libpugixml.a dl) +target_link_libraries(Mania++ yaml-cpp + ${EXECUTABLE_OUTPUT_PATH}/lib/pugixml/libpugixml.a + dl + mysqlcppconn) diff --git a/config.dist.yaml b/config.dist.yaml index 93ae0021..e2b2c499 100644 --- a/config.dist.yaml +++ b/config.dist.yaml @@ -5,3 +5,11 @@ config: authentication: username: 'SuperAdmin' password: '***' + + database: + address: 'localhost' + port: 3306 + authentication: + username: '***' + password: '***' + database: 'maniapp' diff --git a/install.sh b/install.sh index 4b7e00d8..37ea3d14 100755 --- a/install.sh +++ b/install.sh @@ -1,5 +1,5 @@ -# Install Boost and xerces -sudo apt-get -y install libboost-dev +# Install Boost and MySQL +sudo apt-get -y install libboost-dev libmysqlcppconn-dev # Go to libraries directory cd ./lib/ diff --git a/plugins/LocalRecords/CMakeLists.txt b/plugins/LocalRecords/CMakeLists.txt new file mode 100644 index 00000000..edabdde3 --- /dev/null +++ b/plugins/LocalRecords/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required (VERSION 2.6) +project (LocalRecordsPlugin) + +add_definitions(-std=c++11) +add_definitions(-Wno-deprecated) +add_definitions(-Wl,--export-dynamic) + +set (LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}) +link_directories(${PROJECT_SOURCE_DIR}/../../lib/yaml/build) +include_directories("${PROJECT_SOURCE_DIR}/../../lib/yaml/include") +set (PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src) + +include_directories("${PROJECT_BINARY_DIR}") +include_directories("${PROJECT_SOURCE_DIR}/../../../src") + +file(GLOB_RECURSE SOURCES src/*.cpp) + +add_library(LocalRecordsPlugin SHARED ${SOURCES}) +target_link_libraries(LocalRecordsPlugin yaml-cpp mysqlcppconn) diff --git a/plugins/LocalRecords/build.sh b/plugins/LocalRecords/build.sh new file mode 100755 index 00000000..8f88dbb9 --- /dev/null +++ b/plugins/LocalRecords/build.sh @@ -0,0 +1,4 @@ +cmake -DCMAKE_BUILD_TYPE=Debug -H. -Bbuild + +cd ./build +make diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp new file mode 100644 index 00000000..56f66d57 --- /dev/null +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp @@ -0,0 +1,28 @@ +#include "LocalRecordsPlugin.h" + +LocalRecordsPlugin::LocalRecordsPlugin() +{ + Version = "0.1.0"; + Author = "TheM"; + + BeginMap.push_back([this](Map map) { OnBeginMap(map); }); +} + +void LocalRecordsPlugin::Init() +{ + +} + +void LocalRecordsPlugin::OnBeginMap(Map map) +{ + std::cout << "PLUGIN Begin map: " << map.Name << "!" << std::endl; + + sql::PreparedStatement* pstmt; + pstmt = database->prepareStatement("SELECT * FROM `maps` WHERE `Uid` = ?"); + pstmt->setString(1, map.UId); + sql::ResultSet* result = pstmt->executeQuery(); + result->next(); + std::cout << "ID: " << result->getInt("Id") << std::endl; + + delete pstmt; pstmt = NULL; +} diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.h b/plugins/LocalRecords/src/LocalRecordsPlugin.h new file mode 100644 index 00000000..6669f504 --- /dev/null +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.h @@ -0,0 +1,16 @@ +#ifndef LOCALRECORDSPLUGIN_H_ +#define LOCALRECORDSPLUGIN_H_ + +#include +#include "Plugins/Plugin.h" + +class LocalRecordsPlugin : public Plugin +{ +public: + LocalRecordsPlugin(); + + void Init(); + void OnBeginMap(Map map); +}; + +#endif // LOCALRECORDSPLUGIN_H_ diff --git a/plugins/LocalRecords/src/main.cpp b/plugins/LocalRecords/src/main.cpp new file mode 100644 index 00000000..d24c1446 --- /dev/null +++ b/plugins/LocalRecords/src/main.cpp @@ -0,0 +1,25 @@ +#include "LocalRecordsPlugin.h" + +/** + * Mania++ is a Server Controller for TrackMania 2 servers, written in C++. + * Copyright (C) 2016 Max Klaversma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +extern "C" Plugin* startPlugin() +{ + LocalRecordsPlugin* plugin = new LocalRecordsPlugin(); + return (Plugin*)plugin; +} diff --git a/src/Config/Config.cpp b/src/Config/Config.cpp index f1220177..5e0a9943 100644 --- a/src/Config/Config.cpp +++ b/src/Config/Config.cpp @@ -17,6 +17,7 @@ void Config::parseConfig() std::cout << "[ ] Loading configuration file '" << configFile << "' ... " << '\r' << std::flush; YAML::Node config = YAML::LoadFile(configFile); YAML::Node server = config["config"]["server"]; + YAML::Node database = config["config"]["database"]; std::cout << "[ \033[0;32mOK.\033[0;0m" << std::endl; Server = new ServerConfig(); @@ -24,4 +25,11 @@ void Config::parseConfig() Server->port = server["port"].as(); Server->username = server["authentication"]["username"].as(); Server->password = server["authentication"]["password"].as(); + + Database = new DatabaseConfig(); + Database->address = database["address"].as(); + Database->port = database["port"].as(); + Database->username = database["authentication"]["username"].as(); + Database->password = database["authentication"]["password"].as(); + Database->database = database["database"].as(); } diff --git a/src/Config/Config.h b/src/Config/Config.h index 99b4b345..42fef0ae 100644 --- a/src/Config/Config.h +++ b/src/Config/Config.h @@ -20,6 +20,21 @@ struct ServerConfig std::string password; /**< \brief Password with which to authenticate on the server. */ }; +//* DatabaseConfig +/** + * \brief Database connection settings. + * + * Editing information in this struct will not result in a change in the configuration file. + */ +struct DatabaseConfig +{ + std::string address; /**< \brief Database address (either hostname or IP Address). */ + int port; /**< \brief Database serverport. */ + std::string username; /**< \brief Username with which to authenticate on the database server. */ + std::string password; /**< \brief Password with which to authenticate on the database server. */ + std::string database; /**< \brief Name of the database that should be used. */ +}; + //* Config /** * \brief Reads and stores configuration information. @@ -40,6 +55,7 @@ class Config ~Config(); ServerConfig* Server; /**< \brief Instance of ServerConfig which stores the server connection settings. */ + DatabaseConfig* Database; /**< \brief Instance of DatabaseConfig which stores the database server connection settings. */ private: /*! diff --git a/src/Database/Database.cpp b/src/Database/Database.cpp new file mode 100644 index 00000000..b9691f9d --- /dev/null +++ b/src/Database/Database.cpp @@ -0,0 +1,23 @@ +#include "Database.h" + +Database::Database(std::string serveraddress, int serverport) +{ + address = serveraddress; + port = serverport; +} + +sql::Connection* Database::Connect(std::string username, std::string password, std::string database) +{ + sql::Driver* driver; + sql::Connection* connection; + + std::stringstream host; + host << "tcp://" << address << ":" << port; + + driver = get_driver_instance(); + connection = driver->connect(host.str(), username, password); + + connection->setSchema(database); + + return connection; +} diff --git a/src/Database/Database.h b/src/Database/Database.h new file mode 100644 index 00000000..254b484b --- /dev/null +++ b/src/Database/Database.h @@ -0,0 +1,24 @@ +#ifndef DATABASE_H_ +#define DATABASE_H_ + +#include +#include + +#include "mysql_connection.h" +#include +#include +#include +#include + +class Database +{ +public: + Database(std::string serveraddress, int serverport = 3306); + sql::Connection* Connect(std::string username, std::string password, std::string database); + +private: + std::string address; + int port; +}; + +#endif // DATABASE_H_ diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index 4472ebfa..d00c7a74 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -10,11 +10,6 @@ ManiaPP::ManiaPP() methods = new Methods(server); players = new std::map(); maps = new std::map(); - - events = new EventManager(); - plugins = new PluginManager(logging, methods, players, maps); - plugins->SetEventManager(events); - callbacks = new CallBackManager(server, events, players, maps); } ManiaPP::~ManiaPP() @@ -83,12 +78,24 @@ bool ManiaPP::ConnectToServer() { logging->PrintOKFlush(); - plugins->LoadPlugins(); - plugins->InitializePlugins(); - - PrintServerInfo(); - - return true; + if(ConnectToDatabase()) + { + events = new EventManager(); + plugins = new PluginManager(logging, methods, players, maps, database); + plugins->SetEventManager(events); + callbacks = new CallBackManager(server, events, players, maps); + + plugins->LoadPlugins(); + plugins->InitializePlugins(); + + PrintServerInfo(); + + return true; + } + else + { + return false; + } } } } @@ -103,6 +110,31 @@ bool ManiaPP::ConnectToServer() return false; } +bool ManiaPP::ConnectToDatabase() +{ + std::cout << "[ ] Connecting to the database on '" << config->Database->address << ":" << config->Database->port << "' ... " << '\r' << std::flush; + try + { + Database db = Database(config->Database->address, config->Database->port); + sql::Connection* dbConnection = db.Connect(config->Database->username, config->Database->password, config->Database->database); + if(dbConnection != NULL) + { + logging->PrintOKFlush(); + database = dbConnection; + return true; + } + } + catch(sql::SQLException &e) + { + logging->PrintFailedFlush(); + logging->PrintError(e.getErrorCode(), e.what()); + return false; + } + + logging->PrintFailedFlush(); + return false; +} + void ManiaPP::PrintServerInfo() { std::cout << "###############################################################################" << std::endl; diff --git a/src/ManiaPP.h b/src/ManiaPP.h index c7012765..3f922112 100644 --- a/src/ManiaPP.h +++ b/src/ManiaPP.h @@ -6,6 +6,7 @@ #include "CallBacks/CallBackManager.h" #include "Config/Version.h" #include "Config/Config.h" +#include "Database/Database.h" #include "Events/EventManager.h" #include "GbxRemote/GbxRemote.h" #include "Methods/Methods.h" @@ -36,6 +37,11 @@ class ManiaPP */ bool ConnectToServer(); + /*! + * \brief Connects and authenticates with the database server. + */ + bool ConnectToDatabase(); + /*! * \brief Prints information about the server. */ @@ -63,6 +69,8 @@ class ManiaPP CallBackManager* callbacks; /**< \brief Contains the callback manager. */ Methods* methods; /**< \brief Server method caller. */ + sql::Connection* database = NULL; /**< \brief Contains database driver. */ + ServerVersion serverVersion; /**< \brief Struct with server version information. */ SystemInfo systemInfo; /**< \brief Struct with system information. */ diff --git a/src/Plugins/Plugin.h b/src/Plugins/Plugin.h index 1eb57a6b..5215d467 100644 --- a/src/Plugins/Plugin.h +++ b/src/Plugins/Plugin.h @@ -9,6 +9,12 @@ #include #include +#include +#include +#include +#include +#include + #include "../Events/Structs.h" #include "../Methods/Methods.h" #include "../Objects/Player.h" @@ -30,7 +36,7 @@ class Plugin /*! * \brief Sets the logging instance. * - * \param loggingPtr Pointer to the logging instance. + * \param loggingPtr Pointer to the logging instance. */ void SetLogging(Logging* loggingPtr) { @@ -40,7 +46,7 @@ class Plugin /*! * \brief Sets the server instance. * - * \param methodsPtr Pointer to the Methods instance. + * \param methodsPtr Pointer to the Methods instance. */ void SetMethods(Methods* methodsPtr) { @@ -50,7 +56,7 @@ class Plugin /*! * \brief Sets the playerlist instance. * - * \param playersPtr Pointer to the playerlist instance. + * \param playersPtr Pointer to the playerlist instance. */ void SetPlayers(std::map* playersPtr) { @@ -60,13 +66,23 @@ class Plugin /*! * \brief Sets the maplist instance. * - * \param mapsPtr Pointer to the maplist instance. + * \param mapsPtr Pointer to the maplist instance. */ void SetMaps(std::map* mapsPtr) { maps = mapsPtr; } + /*! + * \brief Sets the database instance. + * + * \param databasePtr Pointer to the database instance. + */ + void SetDatabase(sql::Connection* databasePtr) + { + database = databasePtr; + } + std::vector> PlayerConnect; /**< \brief Vector with functions for the PlayerConnect event. */ std::vector> PlayerDisconnect; /**< \brief Vector with functions for the PlayerDisconnect event. */ std::vector> PlayerChat; /**< \brief Vector with functions for the PlayerChat event. */ @@ -93,6 +109,7 @@ class Plugin Methods* methods; /**< \brief Methods instance. */ std::map* players; /**< \brief Playerlist instance. */ std::map* maps; /**< \brief Maplist instance. */ + sql::Connection* database; /**< \brief Database instance. */ }; #endif // PLUGIN_H_ diff --git a/src/Plugins/PluginManager.cpp b/src/Plugins/PluginManager.cpp index ed757b79..9ead7e51 100644 --- a/src/Plugins/PluginManager.cpp +++ b/src/Plugins/PluginManager.cpp @@ -1,6 +1,6 @@ #include "PluginManager.h" -PluginManager::PluginManager(Logging* loggingPtr, Methods* methodsPtr, std::map* playersPtr, std::map* mapsPtr) +PluginManager::PluginManager(Logging* loggingPtr, Methods* methodsPtr, std::map* playersPtr, std::map* mapsPtr, sql::Connection* databasePtr) { plugins = std::map(); events = NULL; @@ -9,6 +9,7 @@ PluginManager::PluginManager(Logging* loggingPtr, Methods* methodsPtr, std::map< methods = methodsPtr; players = playersPtr; maps = mapsPtr; + database = databasePtr; } PluginManager::~PluginManager() @@ -75,6 +76,7 @@ void PluginManager::LoadPlugins(std::string pluginsFolder) plugin->SetMethods(methods); plugin->SetPlayers(players); plugin->SetMaps(maps); + plugin->SetDatabase(database); std::cout << "[ \033[0;32mOK.\033[0;0m ] Loaded plugin '" << pluginId->first << "': v" << plugin->Version << " by " << plugin->Author << "." << std::endl; diff --git a/src/Plugins/PluginManager.h b/src/Plugins/PluginManager.h index 3852c8e8..2b1a5618 100644 --- a/src/Plugins/PluginManager.h +++ b/src/Plugins/PluginManager.h @@ -40,7 +40,7 @@ class PluginManager * \param playersPtr Current instance of the playerlist. * \param mapsPtr Current instance of the maplist. */ - PluginManager(Logging* loggingPtr, Methods* methodsPtr, std::map* playersPtr, std::map* mapsPtr); + PluginManager(Logging* loggingPtr, Methods* methodsPtr, std::map* playersPtr, std::map* mapsPtr, sql::Connection* databasePtr); /*! * \brief Destructor closes all open plugins. @@ -76,9 +76,10 @@ class PluginManager EventManager* events; /**< \brief Current instance of the EventManager. */ Logging* logging; /**< \brief Current instance of Logging. */ - Methods* methods; /**< \brief Current instance of Methods. */ + Methods* methods; /**< \brief Current instance of Methods. */ std::map* players; /**< \brief Current instance of the playerlist. */ std::map* maps; /**< \brief Current instance of the maplist. */ + sql::Connection* database; /**< \brief Current instance of the database. */ }; #endif // PLUGINMANAGER_H_ diff --git a/src/Utils/Logging.h b/src/Utils/Logging.h index e27c04b5..d6d944ab 100644 --- a/src/Utils/Logging.h +++ b/src/Utils/Logging.h @@ -51,6 +51,21 @@ class Logging } } + /*! + * \brief Prints error to console. + * + * \param error Error structure. + */ + static void PrintError(int number, std::string message) + { + GbxError* error = new GbxError(); + error->number = number; + error->message = message; + PrintError(error); + + delete error; error = NULL; + } + /*! * \brief Prints OK. in [ ]-spaces in console. */ From 6a4895e96936c54708ca7cdffb31d9e829e4d7d7 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Sat, 31 Dec 2016 16:40:33 +0100 Subject: [PATCH 02/66] Added FormatTime function to convert ssssttt to m:ss.ttt. --- .../LocalRecords/src/LocalRecordsPlugin.cpp | 18 +++++++-- plugins/LocalRecords/src/LocalRecordsPlugin.h | 1 + src/Utils/Time.h | 40 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/Utils/Time.h diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp index 56f66d57..01e7d2f4 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp @@ -15,14 +15,26 @@ void LocalRecordsPlugin::Init() void LocalRecordsPlugin::OnBeginMap(Map map) { - std::cout << "PLUGIN Begin map: " << map.Name << "!" << std::endl; - sql::PreparedStatement* pstmt; pstmt = database->prepareStatement("SELECT * FROM `maps` WHERE `Uid` = ?"); pstmt->setString(1, map.UId); sql::ResultSet* result = pstmt->executeQuery(); result->next(); - std::cout << "ID: " << result->getInt("Id") << std::endl; + int mapId = result->getInt("Id"); + + pstmt = database->prepareStatement("SELECT * FROM `records` WHERE `MapId` = ? ORDER BY `Score` ASC LIMIT ?"); + pstmt->setInt(1, mapId); + pstmt->setInt(2, 100); + result = pstmt->executeQuery(); + + std::cout << "Records for " << map.Name << ":" << std::endl; + int recordIndex = 1; + while(result->next()) + { + std::cout << "#" << recordIndex << ": " << Time::FormatTime(result->getInt("Score")) << " by " << result->getInt("PlayerId") << std::endl; + recordIndex++; + } delete pstmt; pstmt = NULL; + delete result; result = NULL; } diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.h b/plugins/LocalRecords/src/LocalRecordsPlugin.h index 6669f504..e35c661c 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.h +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.h @@ -3,6 +3,7 @@ #include #include "Plugins/Plugin.h" +#include "Utils/Time.h" class LocalRecordsPlugin : public Plugin { diff --git a/src/Utils/Time.h b/src/Utils/Time.h new file mode 100644 index 00000000..4688f5ad --- /dev/null +++ b/src/Utils/Time.h @@ -0,0 +1,40 @@ +#ifndef TIME_H_ +#define TIME_H_ + +#include +#include +#include + +class Time +{ +public: + /*! + * \brief Formats time to 0:00.000 format. + * + * \param time Time to be formatted. + */ + static std::string FormatTime(int time) + { + std::string formattedTime = "0:00.000"; + + if(time > 0) + { + std::stringstream timeAsStringstream; + timeAsStringstream << time; + std::string timeAsString = timeAsStringstream.str(); + + int minutes = floor(time / (1000 * 60)); + int seconds = floor((time - (minutes * 60 * 1000)) / 1000); + int tseconds = atoi(timeAsString.substr((timeAsString.length() - 3)).c_str()); + + char formattedCharArray[8]; + sprintf(formattedCharArray, "%d:%02d:%03d", minutes, seconds, tseconds); + + return std::string(formattedCharArray); + } + + return formattedTime; + } +}; + +#endif // TIME_H_ From 7e608808178fc0599e5986baf01754fe8017748d Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Sun, 1 Jan 2017 17:43:00 +0100 Subject: [PATCH 03/66] Now retrieving detailed player information on connect and start-up. --- src/CallBacks/CallBackManager.cpp | 3 ++ src/ManiaPP.cpp | 12 +++++- src/Objects/Map.h | 8 ++-- src/Objects/Player.h | 68 +++++++++++++++++++++++++++---- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/src/CallBacks/CallBackManager.cpp b/src/CallBacks/CallBackManager.cpp index d38dd8cf..bc527dfb 100644 --- a/src/CallBacks/CallBackManager.cpp +++ b/src/CallBacks/CallBackManager.cpp @@ -97,6 +97,9 @@ void CallBackManager::HandlePlayerConnect(std::vector para GbxMessage* message = new GbxMessage("GetPlayerInfo", params); server->Query(message); Player newPlayer = Player(server->GetResponse()->GetParameters().at(0).GetStruct()); + message = new GbxMessage("GetDetailedPlayerInfo", params); + newPlayer.PlayerDetailed(server->GetResponse()->GetParameters().at(0).GetStruct()); + players->insert(std::pair(newPlayer.Login, newPlayer)); std::cout << "Player connected: " << newPlayer.Login << " (# players: " << players->size() << ")" << std::endl; diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index d00c7a74..691a59b2 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -84,7 +84,7 @@ bool ManiaPP::ConnectToServer() plugins = new PluginManager(logging, methods, players, maps, database); plugins->SetEventManager(events); callbacks = new CallBackManager(server, events, players, maps); - + plugins->LoadPlugins(); plugins->InitializePlugins(); @@ -188,8 +188,18 @@ void ManiaPP::retrievePlayerList() std::map player = playerList.at(playerId).GetStruct(); if(player.find("Login")->second.GetString() != systemInfo.ServerLogin) { + GbxParameters* params = new GbxParameters(); + std::string login = player.find("Login")->second.GetString(); + params->Put(&(login)); + + GbxMessage* message = new GbxMessage("GetDetailedPlayerInfo", params); + server->Query(message); Player newPlayer = Player(player); + newPlayer.PlayerDetailed(server->GetResponse()->GetParameters().at(0).GetStruct()); players->insert(std::pair(newPlayer.Login, newPlayer)); + + //Player newPlayer = Player(player); + //players->insert(std::pair(newPlayer.Login, newPlayer)); } } diff --git a/src/Objects/Map.h b/src/Objects/Map.h index 641c6fa4..83028fc0 100644 --- a/src/Objects/Map.h +++ b/src/Objects/Map.h @@ -95,8 +95,8 @@ struct Map */ void setDetailedInfo(std::map serverStruct) { - UId = serverStruct.find("UId")->second.GetString().c_str(); - Name = serverStruct.find("Name")->second.GetString().c_str(); + UId = serverStruct.find("UId")->second.GetString(); + Name = serverStruct.find("Name")->second.GetString(); FileName = serverStruct.find("FileName")->second.GetString(); Environment = serverStruct.find("Environnement")->second.GetString(); Author = serverStruct.find("Author")->second.GetString(); @@ -104,8 +104,8 @@ struct Map GoldTime = atoi(serverStruct.find("GoldTime")->second.GetString().c_str()); CopperPrice = atoi(serverStruct.find("CopperPrice")->second.GetString().c_str()); - MapType = serverStruct.find("MapType")->second.GetString().c_str(); - MapStyle = serverStruct.find("MapStyle")->second.GetString().c_str(); + MapType = serverStruct.find("MapType")->second.GetString(); + MapStyle = serverStruct.find("MapStyle")->second.GetString(); std::string Mood = serverStruct.find("Mood")->second.GetString(); int BronzeTime = atoi(serverStruct.find("BronzeTime")->second.GetString().c_str()); diff --git a/src/Objects/Player.h b/src/Objects/Player.h index c4c7935d..6090b5b2 100644 --- a/src/Objects/Player.h +++ b/src/Objects/Player.h @@ -9,6 +9,7 @@ */ struct Player { + // === Basic information === int PlayerId; /**< \brief Player identifier (on server). */ int TeamId; /**< \brief Team identifier (on server). */ @@ -20,6 +21,16 @@ struct Player int LadderRanking; /**< \brief Current ladder ranking of the player. */ + // === Detailed information === + std::string IPAddress; /**< \brief IP Address of the player. */ + int DownloadRate; /**< \brief Download rate in Kbps. */ + int UploadRate; /**< \brief Upload rate in Kbps. */ + + std::string Language; /**< \brief Application language. */ + + bool IsSpectator; /**< \brief Is in spectatormode? */ + bool IsInOfficialMode; /**< \brief Is in official mode? */ + /*! * \brief Constructs a Player object without input. */ @@ -37,17 +48,58 @@ struct Player { if(serverStruct.find("Login") != serverStruct.end()) { - PlayerId = atoi(serverStruct.find("PlayerId")->second.GetString().c_str()); - TeamId = atoi(serverStruct.find("TeamId")->second.GetString().c_str()); + setBasicInfo(serverStruct); + } + } - Login = serverStruct.find("Login")->second.GetString(); - NickName = serverStruct.find("NickName")->second.GetString(); + /*! + * \brief Formats a server response into a usable form. + * + * \param serverStruct The struct with detailed player information received from the server. + */ + void PlayerDetailed(std::map serverStruct) + { + if(serverStruct.find("Language") != serverStruct.end()) + { + setDetailedInfo(serverStruct); + } + } - SpectatorStatus = atoi(serverStruct.find("SpectatorStatus")->second.GetString().c_str()); - Flags = atoi(serverStruct.find("Flags")->second.GetString().c_str()); +private: + /*! + * \brief Sets the basic player information from the struct. + * + * \param serverStruct The struct with player information received from the server. + */ + void setBasicInfo(std::map serverStruct) + { + PlayerId = atoi(serverStruct.find("PlayerId")->second.GetString().c_str()); + TeamId = atoi(serverStruct.find("TeamId")->second.GetString().c_str()); - LadderRanking = atoi(serverStruct.find("LadderRanking")->second.GetString().c_str()); - } + Login = serverStruct.find("Login")->second.GetString(); + NickName = serverStruct.find("NickName")->second.GetString(); + + SpectatorStatus = atoi(serverStruct.find("SpectatorStatus")->second.GetString().c_str()); + Flags = atoi(serverStruct.find("Flags")->second.GetString().c_str()); + + LadderRanking = atoi(serverStruct.find("LadderRanking")->second.GetString().c_str()); + } + + /*! + * \brief Sets the detailed player information from the struct. + * + * \param serverStruct The struct with detailed player information received from the server. + */ + void setDetailedInfo(std::map serverStruct) + { + IPAddress = serverStruct.find("IPAddress")->second.GetString(); + DownloadRate = atoi(serverStruct.find("DownloadRate")->second.GetString().c_str()); + UploadRate = atoi(serverStruct.find("UploadRate")->second.GetString().c_str()); + + Language = serverStruct.find("Language")->second.GetString(); + + std::istringstream(serverStruct.find("IsSpectator")->second.GetString()) >> IsSpectator; + std::istringstream(serverStruct.find("IsInOfficialMode")->second.GetString()) >> IsInOfficialMode; } }; From 73543e0d229f6a2efd793342bac7c6dfb356fcf8 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Sun, 1 Jan 2017 19:46:11 +0100 Subject: [PATCH 04/66] Now linking database identifiers to players and maps. --- .../LocalRecords/src/LocalRecordsPlugin.cpp | 13 ++----- src/CallBacks/CallBackManager.cpp | 21 ++++++++-- src/CallBacks/CallBackManager.h | 10 ++++- src/ManiaPP.cpp | 38 +++++++++++++++---- src/ManiaPP.h | 4 ++ src/Objects/Map.h | 13 +++++++ src/Objects/Player.h | 13 +++++++ 7 files changed, 91 insertions(+), 21 deletions(-) diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp index 01e7d2f4..12f90f03 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp @@ -15,17 +15,10 @@ void LocalRecordsPlugin::Init() void LocalRecordsPlugin::OnBeginMap(Map map) { - sql::PreparedStatement* pstmt; - pstmt = database->prepareStatement("SELECT * FROM `maps` WHERE `Uid` = ?"); - pstmt->setString(1, map.UId); - sql::ResultSet* result = pstmt->executeQuery(); - result->next(); - int mapId = result->getInt("Id"); - - pstmt = database->prepareStatement("SELECT * FROM `records` WHERE `MapId` = ? ORDER BY `Score` ASC LIMIT ?"); - pstmt->setInt(1, mapId); + sql::PreparedStatement* pstmt = database->prepareStatement("SELECT * FROM `records` WHERE `MapId` = ? ORDER BY `Score` ASC LIMIT ?"); + pstmt->setInt(1, map.Id); pstmt->setInt(2, 100); - result = pstmt->executeQuery(); + sql::ResultSet* result = pstmt->executeQuery(); std::cout << "Records for " << map.Name << ":" << std::endl; int recordIndex = 1; diff --git a/src/CallBacks/CallBackManager.cpp b/src/CallBacks/CallBackManager.cpp index bc527dfb..b56c221b 100644 --- a/src/CallBacks/CallBackManager.cpp +++ b/src/CallBacks/CallBackManager.cpp @@ -1,9 +1,10 @@ #include "CallBackManager.h" -CallBackManager::CallBackManager(GbxRemote* serverPtr, EventManager* eventManagerPtr, std::map* playerList, std::map* mapList) +CallBackManager::CallBackManager(GbxRemote* serverPtr, EventManager* eventManagerPtr, sql::Connection* databasePtr, std::map* playerList, std::map* mapList) { server = serverPtr; events = eventManagerPtr; + database = databasePtr; players = playerList; maps = mapList; @@ -100,6 +101,18 @@ void CallBackManager::HandlePlayerConnect(std::vector para message = new GbxMessage("GetDetailedPlayerInfo", params); newPlayer.PlayerDetailed(server->GetResponse()->GetParameters().at(0).GetStruct()); + if(database != NULL) + { + sql::PreparedStatement* pstmt; + pstmt = database->prepareStatement("SELECT * FROM `players` WHERE `Login` = ?"); + pstmt->setString(1, newPlayer.Login); + sql::ResultSet* result = pstmt->executeQuery(); + if(result->next()) + { + newPlayer.SetId(result->getInt("Id")); + } + } + players->insert(std::pair(newPlayer.Login, newPlayer)); std::cout << "Player connected: " << newPlayer.Login << " (# players: " << players->size() << ")" << std::endl; @@ -213,14 +226,16 @@ void CallBackManager::HandleEndMatch(std::vector parameter void CallBackManager::HandleBeginMap(std::vector parameters) { - Map map = Map(); + std::map mapStruct = parameters.at(0).GetStruct(); + Map map = maps->at(mapStruct.at("UId").GetString()); map.MapDetailed(parameters.at(0).GetStruct()); events->CallBeginMap(map); } void CallBackManager::HandleEndMap(std::vector parameters) { - Map map = Map(); + std::map mapStruct = parameters.at(0).GetStruct(); + Map map = maps->at(mapStruct.at("UId").GetString()); map.MapDetailed(parameters.at(0).GetStruct()); events->CallEndMap(map); } diff --git a/src/CallBacks/CallBackManager.h b/src/CallBacks/CallBackManager.h index f6970425..9f3c5ce5 100644 --- a/src/CallBacks/CallBackManager.h +++ b/src/CallBacks/CallBackManager.h @@ -5,6 +5,12 @@ #include #include +#include +#include +#include +#include +#include + #include "../Events/EventManager.h" #include "../GbxRemote/GbxRemote.h" #include "../GbxRemote/Message/GbxMessage.h" @@ -27,10 +33,11 @@ class CallBackManager * * \param serverPtr Pointer to the GbxRemote. * \param eventManagerPtr Pointer to the EventManager. + * \param databasePtr Pointer to the database connection. * \param playerList Pointer to playerlist. * \param mapList Pointer to maplist. */ - CallBackManager(GbxRemote* serverPtr, EventManager* eventManagerPtr, std::map* playerList, std::map* mapList); + CallBackManager(GbxRemote* serverPtr, EventManager* eventManagerPtr, sql::Connection* databasePtr, std::map* playerList, std::map* mapList); /*! * \brief Handles callback (updates lists, calls plugin functions). @@ -162,6 +169,7 @@ class CallBackManager private: GbxRemote* server; /**< \brief Contains the serverconnection. */ EventManager* events; /**< \brief Contains the event manager. */ + sql::Connection* database; /**< \brief Contains the database connection. */ std::map* players; /**< \brief Contains the list of players currently on the server. */ std::map* maps; /**< \brief Contains the list of players currently on the server. */ }; diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index 691a59b2..401e5422 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -70,9 +70,6 @@ bool ManiaPP::ConnectToServer() methods->GetChatLines(); - retrievePlayerList(); - retrieveMapList(); - std::cout << "[ ] Enabling CallBacks ... " << '\r' << std::flush; if(methods->EnableCallbacks(true)) { @@ -80,10 +77,13 @@ bool ManiaPP::ConnectToServer() if(ConnectToDatabase()) { + retrievePlayerList(); + retrieveMapList(); + events = new EventManager(); plugins = new PluginManager(logging, methods, players, maps, database); plugins->SetEventManager(events); - callbacks = new CallBackManager(server, events, players, maps); + callbacks = new CallBackManager(server, events, database, players, maps); plugins->LoadPlugins(); plugins->InitializePlugins(); @@ -170,6 +170,7 @@ void ManiaPP::MainLoop() void ManiaPP::retrievePlayerList() { std::cout << "[ ] Retrieving current player list ... " << '\r' << std::flush; + int playerListLimit = 512; int playerListIndex = 0; GbxParameters* params = new GbxParameters(); params->Put(&playerListLimit); @@ -196,10 +197,20 @@ void ManiaPP::retrievePlayerList() server->Query(message); Player newPlayer = Player(player); newPlayer.PlayerDetailed(server->GetResponse()->GetParameters().at(0).GetStruct()); - players->insert(std::pair(newPlayer.Login, newPlayer)); - //Player newPlayer = Player(player); - //players->insert(std::pair(newPlayer.Login, newPlayer)); + if(database != NULL) + { + sql::PreparedStatement* pstmt; + pstmt = database->prepareStatement("SELECT * FROM `players` WHERE `Login` = ?"); + pstmt->setString(1, newPlayer.Login); + sql::ResultSet* result = pstmt->executeQuery(); + if(result->next()) + { + newPlayer.SetId(result->getInt("Id")); + } + } + + players->insert(std::pair(newPlayer.Login, newPlayer)); } } @@ -233,6 +244,19 @@ void ManiaPP::retrieveMapList() { std::map map = mapList.at(mapId).GetStruct(); Map newMap = Map(map); + + if(database != NULL) + { + sql::PreparedStatement* pstmt; + pstmt = database->prepareStatement("SELECT * FROM `maps` WHERE `UId` = ?"); + pstmt->setString(1, newMap.UId); + sql::ResultSet* result = pstmt->executeQuery(); + if(result->next()) + { + newMap.SetId(result->getInt("Id")); + } + } + maps->insert(std::pair(newMap.UId, newMap)); } diff --git a/src/ManiaPP.h b/src/ManiaPP.h index 3f922112..246207c9 100644 --- a/src/ManiaPP.h +++ b/src/ManiaPP.h @@ -76,11 +76,15 @@ class ManiaPP /*! * \brief Gets a list of current players from the server. + * + * \todo Insert player into database if needed. */ void retrievePlayerList(); /*! * \brief Gets a list of current maps from the server. + * + * \todo Insert map into database if needed. */ void retrieveMapList(); }; diff --git a/src/Objects/Map.h b/src/Objects/Map.h index 83028fc0..2b62c3a1 100644 --- a/src/Objects/Map.h +++ b/src/Objects/Map.h @@ -9,6 +9,9 @@ */ struct Map { + // === Mania++ information === + int Id = 0; /**< \brief Map identifier (on database). */ + // === Basic information === std::string UId; /**< \brief Unique map identifier. */ std::string Name; /**< \brief Map name. */ @@ -67,6 +70,16 @@ struct Map } } + /*! + * \brief Set database identifier value. + * + * \param Database ID. + */ + void SetId(int id) + { + Id = id; + } + private: /*! * \brief Sets the basic map information from the struct. diff --git a/src/Objects/Player.h b/src/Objects/Player.h index 6090b5b2..03ac3910 100644 --- a/src/Objects/Player.h +++ b/src/Objects/Player.h @@ -9,6 +9,9 @@ */ struct Player { + // === Mania++ information === + int Id = 0; /**< \brief Player identifier (on database). */ + // === Basic information === int PlayerId; /**< \brief Player identifier (on server). */ int TeamId; /**< \brief Team identifier (on server). */ @@ -65,6 +68,16 @@ struct Player } } + /*! + * \brief Set database identifier value. + * + * \param Database ID. + */ + void SetId(int id) + { + Id = id; + } + private: /*! * \brief Sets the basic player information from the struct. From e3b54bc340bc868af2d68ad803f674263ba1860f Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Mon, 2 Jan 2017 01:20:19 +0100 Subject: [PATCH 05/66] Added MapList, expanded LocalRecord and improved Plugin handling (passing of variables). --- .../HelloGoodbye/src/HelloGoodbyePlugin.cpp | 8 +-- .../LocalRecords/src/LocalRecordsPlugin.cpp | 38 +++++++++-- plugins/LocalRecords/src/LocalRecordsPlugin.h | 7 ++ .../LocalRecords/src/Objects/LocalRecord.h | 35 ++++++++++ src/CallBacks/CallBackManager.cpp | 13 ++-- src/CallBacks/CallBackManager.h | 5 +- src/Database/Database.cpp | 6 +- src/Database/Database.h | 24 ++++++- src/ManiaPP.cpp | 12 ++-- src/ManiaPP.h | 3 +- src/Maps/MapList.cpp | 7 ++ src/Maps/MapList.h | 24 +++++++ src/Methods/Maps/MapInfo.cpp | 41 ++++++++++++ src/Methods/Methods.h | 16 +++++ src/Objects/Map.h | 23 ++++++- src/Objects/Player.h | 2 +- src/Plugins/Plugin.h | 67 +++++-------------- src/Plugins/PluginManager.cpp | 25 ++++--- src/Plugins/PluginManager.h | 11 +-- src/Utils/Logging.h | 5 +- src/Utils/Time.h | 4 ++ 21 files changed, 273 insertions(+), 103 deletions(-) create mode 100644 plugins/LocalRecords/src/Objects/LocalRecord.h create mode 100644 src/Maps/MapList.cpp create mode 100644 src/Maps/MapList.h create mode 100644 src/Methods/Maps/MapInfo.cpp diff --git a/plugins/HelloGoodbye/src/HelloGoodbyePlugin.cpp b/plugins/HelloGoodbye/src/HelloGoodbyePlugin.cpp index cdb01548..f537fde7 100644 --- a/plugins/HelloGoodbye/src/HelloGoodbyePlugin.cpp +++ b/plugins/HelloGoodbye/src/HelloGoodbyePlugin.cpp @@ -11,7 +11,7 @@ HelloGoodbyePlugin::HelloGoodbyePlugin() void HelloGoodbyePlugin::Init() { - std::cout << "[ INFO ] Current amount of maps: " << maps->size() << std::endl; + std::cout << "[ INFO ] Current amount of maps: " << controller->Maps->List.size() << std::endl; } void HelloGoodbyePlugin::OnPlayerConnect(Player player) @@ -21,10 +21,10 @@ void HelloGoodbyePlugin::OnPlayerConnect(Player player) std::stringstream chatMessage; chatMessage << "Player joins: "; chatMessage << player.NickName; - chatMessage << " $s$Ladder: "; + chatMessage << " $s$zLadder: "; chatMessage << player.LadderRanking; - methods->ChatSendServerMessage(chatMessage.str()); + controller->Server->ChatSendServerMessage(chatMessage.str()); } void HelloGoodbyePlugin::OnPlayerDisconnect(Player player) @@ -35,5 +35,5 @@ void HelloGoodbyePlugin::OnPlayerDisconnect(Player player) chatMessage << player.NickName; chatMessage << " $s$zhas left the game."; - methods->ChatSendServerMessage(chatMessage.str()); + controller->Server->ChatSendServerMessage(chatMessage.str()); } diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp index 12f90f03..68dc2449 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp @@ -10,22 +10,50 @@ LocalRecordsPlugin::LocalRecordsPlugin() void LocalRecordsPlugin::Init() { + retrieveRecords(*controller->Maps->Current); + std::cout << "Records for " << controller->Maps->Current->Name << ":" << std::endl; + for(int recordId = 0; recordId < localRecords.size(); recordId++) + { + LocalRecord record = localRecords.at(recordId); + std::cout << "#" << recordId << ": " << record.FormattedTime << " by " << record.Login << std::endl; + } } void LocalRecordsPlugin::OnBeginMap(Map map) { - sql::PreparedStatement* pstmt = database->prepareStatement("SELECT * FROM `records` WHERE `MapId` = ? ORDER BY `Score` ASC LIMIT ?"); + retrieveRecords(map); + + std::cout << "Records for " << map.Name << ":" << std::endl; + for(int recordId = 0; recordId < localRecords.size(); recordId++) + { + LocalRecord record = localRecords.at(recordId); + std::cout << "#" << recordId << ": " << record.FormattedTime << " by " << record.Login << std::endl; + } +} + +void LocalRecordsPlugin::retrieveRecords(Map map) +{ + localRecords = std::vector(); + + sql::PreparedStatement* pstmt = controller->Database->prepareStatement("SELECT * FROM `records` WHERE `MapId` = ? ORDER BY `Score` ASC LIMIT ?"); pstmt->setInt(1, map.Id); pstmt->setInt(2, 100); sql::ResultSet* result = pstmt->executeQuery(); - std::cout << "Records for " << map.Name << ":" << std::endl; - int recordIndex = 1; while(result->next()) { - std::cout << "#" << recordIndex << ": " << Time::FormatTime(result->getInt("Score")) << " by " << result->getInt("PlayerId") << std::endl; - recordIndex++; + LocalRecord localRecord = LocalRecord(result); + + pstmt = controller->Database->prepareStatement("SELECT * FROM `players` WHERE `Id` = ?"); + pstmt->setInt(1, result->getInt("PlayerId")); + sql::ResultSet* playerResult = pstmt->executeQuery(); + playerResult->next(); + + localRecord.Login = playerResult->getString("Login"); + localRecord.NickName = playerResult->getString("NickName"); + + localRecords.push_back(localRecord); } delete pstmt; pstmt = NULL; diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.h b/plugins/LocalRecords/src/LocalRecordsPlugin.h index e35c661c..843d5a7d 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.h +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.h @@ -5,6 +5,8 @@ #include "Plugins/Plugin.h" #include "Utils/Time.h" +#include "Objects/LocalRecord.h" + class LocalRecordsPlugin : public Plugin { public: @@ -12,6 +14,11 @@ class LocalRecordsPlugin : public Plugin void Init(); void OnBeginMap(Map map); + +private: + std::vector localRecords; + + void retrieveRecords(Map map); }; #endif // LOCALRECORDSPLUGIN_H_ diff --git a/plugins/LocalRecords/src/Objects/LocalRecord.h b/plugins/LocalRecords/src/Objects/LocalRecord.h new file mode 100644 index 00000000..6a610e69 --- /dev/null +++ b/plugins/LocalRecords/src/Objects/LocalRecord.h @@ -0,0 +1,35 @@ +#ifndef LOCALRECORD_H_ +#define LOCALRECORD_H_ + +class LocalRecord +{ +public: + int Id; + Map RecordMap; + std::string Login; + std::string NickName; + + int Time; + std::string FormattedTime; + std::vector Checkpoints; + + std::string Date; + + LocalRecord(sql::ResultSet* result) + { + Id = result->getInt("Id"); + Time = result->getInt("Score"); + FormattedTime = Time::FormatTime(Time); + std::stringstream checkpoints(result->getString("Checkpoints")); + while(checkpoints.good()) + { + std::string checkpoint; + getline(checkpoints, checkpoint, ','); + Checkpoints.push_back(atoi(checkpoint.c_str())); + } + + Date = result->getString("Date"); + } +}; + +#endif // LOCALRECORD_H_ diff --git a/src/CallBacks/CallBackManager.cpp b/src/CallBacks/CallBackManager.cpp index b56c221b..ead704bc 100644 --- a/src/CallBacks/CallBackManager.cpp +++ b/src/CallBacks/CallBackManager.cpp @@ -1,6 +1,6 @@ #include "CallBackManager.h" -CallBackManager::CallBackManager(GbxRemote* serverPtr, EventManager* eventManagerPtr, sql::Connection* databasePtr, std::map* playerList, std::map* mapList) +CallBackManager::CallBackManager(GbxRemote* serverPtr, EventManager* eventManagerPtr, sql::Connection* databasePtr, std::map* playerList, MapList* mapList) { server = serverPtr; events = eventManagerPtr; @@ -227,16 +227,17 @@ void CallBackManager::HandleEndMatch(std::vector parameter void CallBackManager::HandleBeginMap(std::vector parameters) { std::map mapStruct = parameters.at(0).GetStruct(); - Map map = maps->at(mapStruct.at("UId").GetString()); - map.MapDetailed(parameters.at(0).GetStruct()); - events->CallBeginMap(map); + Map* map = &(maps->List.at(mapStruct.at("UId").GetString())); + map->MapDetailed(parameters.at(0).GetStruct()); + + maps->SetCurrentMap(mapStruct.at("UId").GetString()); + events->CallBeginMap(*map); } void CallBackManager::HandleEndMap(std::vector parameters) { std::map mapStruct = parameters.at(0).GetStruct(); - Map map = maps->at(mapStruct.at("UId").GetString()); - map.MapDetailed(parameters.at(0).GetStruct()); + Map map = maps->List.at(mapStruct.at("UId").GetString()); events->CallEndMap(map); } diff --git a/src/CallBacks/CallBackManager.h b/src/CallBacks/CallBackManager.h index 9f3c5ce5..d307ec3d 100644 --- a/src/CallBacks/CallBackManager.h +++ b/src/CallBacks/CallBackManager.h @@ -15,6 +15,7 @@ #include "../GbxRemote/GbxRemote.h" #include "../GbxRemote/Message/GbxMessage.h" #include "../GbxRemote/Parameters/GbxParameters.h" +#include "../Maps/MapList.h" #include "../Objects/Map.h" #include "../Objects/Player.h" #include "../Utils/Logging.h" @@ -37,7 +38,7 @@ class CallBackManager * \param playerList Pointer to playerlist. * \param mapList Pointer to maplist. */ - CallBackManager(GbxRemote* serverPtr, EventManager* eventManagerPtr, sql::Connection* databasePtr, std::map* playerList, std::map* mapList); + CallBackManager(GbxRemote* serverPtr, EventManager* eventManagerPtr, sql::Connection* databasePtr, std::map* playerList, MapList* mapList); /*! * \brief Handles callback (updates lists, calls plugin functions). @@ -171,7 +172,7 @@ class CallBackManager EventManager* events; /**< \brief Contains the event manager. */ sql::Connection* database; /**< \brief Contains the database connection. */ std::map* players; /**< \brief Contains the list of players currently on the server. */ - std::map* maps; /**< \brief Contains the list of players currently on the server. */ + MapList* maps; /**< \brief Contains the list of players currently on the server. */ }; #endif // CALLBACKMANAGER_H_ diff --git a/src/Database/Database.cpp b/src/Database/Database.cpp index b9691f9d..a1631187 100644 --- a/src/Database/Database.cpp +++ b/src/Database/Database.cpp @@ -1,9 +1,9 @@ #include "Database.h" -Database::Database(std::string serveraddress, int serverport) +Database::Database(std::string serverAddress, int serverPort) { - address = serveraddress; - port = serverport; + address = serverAddress; + port = serverPort; } sql::Connection* Database::Connect(std::string username, std::string password, std::string database) diff --git a/src/Database/Database.h b/src/Database/Database.h index 254b484b..98769c0a 100644 --- a/src/Database/Database.h +++ b/src/Database/Database.h @@ -10,15 +10,33 @@ #include #include +//* Database +/** + * \brief Handles the connection to the database. + */ class Database { public: - Database(std::string serveraddress, int serverport = 3306); + /*! + * \brief Sets a few settings. + * + * \param serverAddress IP Address of the database server. + * \param serverPort Port of the database server. + */ + Database(std::string serverAddress, int serverPort = 3306); + + /*! + * \brief Connects to the database. + * + * \param username Database user name. + * \param password Database user password. + * \param database Database name. + */ sql::Connection* Connect(std::string username, std::string password, std::string database); private: - std::string address; - int port; + std::string address; /**< \brief IP Address of the database server. */ + int port; /**< \brief Port of the database server. */ }; #endif // DATABASE_H_ diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index 401e5422..e2c28697 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -9,7 +9,7 @@ ManiaPP::ManiaPP() server = new GbxRemote(); methods = new Methods(server); players = new std::map(); - maps = new std::map(); + maps = new MapList(); } ManiaPP::~ManiaPP() @@ -80,8 +80,12 @@ bool ManiaPP::ConnectToServer() retrievePlayerList(); retrieveMapList(); + Map currentMap = methods->GetCurrentMapInfo(); + maps->SetCurrentMap(currentMap.UId); + maps->Current->CopyDetailedMap(currentMap); + events = new EventManager(); - plugins = new PluginManager(logging, methods, players, maps, database); + plugins = new PluginManager(methods, players, maps, database); plugins->SetEventManager(events); callbacks = new CallBackManager(server, events, database, players, maps); @@ -257,10 +261,10 @@ void ManiaPP::retrieveMapList() } } - maps->insert(std::pair(newMap.UId, newMap)); + maps->List.insert(std::pair(newMap.UId, newMap)); } - std::cout << "[ \033[0;32mOK.\033[0;0m ] Retrieved current map list: " << maps->size() << " found." << std::endl; + std::cout << "[ \033[0;32mOK.\033[0;0m ] Retrieved current map list: " << maps->List.size() << " found." << std::endl; } else { diff --git a/src/ManiaPP.h b/src/ManiaPP.h index 246207c9..f0756aab 100644 --- a/src/ManiaPP.h +++ b/src/ManiaPP.h @@ -9,6 +9,7 @@ #include "Database/Database.h" #include "Events/EventManager.h" #include "GbxRemote/GbxRemote.h" +#include "Maps/MapList.h" #include "Methods/Methods.h" #include "Objects/Map.h" #include "Objects/Player.h" @@ -62,7 +63,7 @@ class ManiaPP Logging* logging; /**< \brief Contains functions to log information. */ GbxRemote* server; /**< \brief Contains the connection with the server and its methods. */ std::map* players; /**< \brief Contains the list of players currently on the server. */ - std::map* maps; /**< \brief Contains the list of players currently on the server. */ + MapList* maps; /**< \brief Contains the list of maps on the server. */ EventManager* events; /**< \brief Contains the event manager. */ PluginManager* plugins; /**< \brief Contains the plugin manager. */ diff --git a/src/Maps/MapList.cpp b/src/Maps/MapList.cpp new file mode 100644 index 00000000..6ba0d9bc --- /dev/null +++ b/src/Maps/MapList.cpp @@ -0,0 +1,7 @@ +#include "MapList.h" + +void MapList::SetCurrentMap(std::string uid) +{ + Map* map = &(List.at(uid)); + Current = map; +} diff --git a/src/Maps/MapList.h b/src/Maps/MapList.h new file mode 100644 index 00000000..1aa578da --- /dev/null +++ b/src/Maps/MapList.h @@ -0,0 +1,24 @@ +#ifndef MAPLIST_H_ +#define MAPLIST_H_ + +#include "../Objects/Map.h" + +//* MapList +/** + * \brief Contains the maplist and a pointer to the current map. + */ +class MapList +{ +public: + std::map List; /**< \brief List of all maps on the server. */ + Map* Current = NULL; /**< \brief Pointer to the current map (in the list). */ + + /*! + * \brief Sets the current map to the one belonging to the unique identifier. + * + * \param uid Map identifier (from game/server). + */ + void SetCurrentMap(std::string uid); +}; + +#endif // MAPLIST_H_ diff --git a/src/Methods/Maps/MapInfo.cpp b/src/Methods/Maps/MapInfo.cpp new file mode 100644 index 00000000..bea4ec77 --- /dev/null +++ b/src/Methods/Maps/MapInfo.cpp @@ -0,0 +1,41 @@ +#include "../Methods.h" + +Map Methods::GetCurrentMapInfo() +{ + Map currentMap = Map(); + + GbxMessage* message = new GbxMessage("GetCurrentMapInfo"); + if(server->Query(message)) + { + delete message; message = NULL; + + std::vector responseParams = server->GetResponse()->GetParameters(); + std::map mapStruct = responseParams.at(0).GetStruct(); + + currentMap.MapDetailed(mapStruct); + } + + return currentMap; +} + +Map Methods::GetMapInfo(std::string fileName) +{ + Map mapInfo = Map(); + + GbxParameters* params = new GbxParameters(); + params->Put(&fileName); + + GbxMessage* message = new GbxMessage("GetMapInfo", params); + if(server->Query(message)) + { + delete message; message = NULL; + delete params; params = NULL; + + std::vector responseParams = server->GetResponse()->GetParameters(); + std::map mapStruct = responseParams.at(0).GetStruct(); + + mapInfo.MapDetailed(mapStruct); + } + + return mapInfo; +} diff --git a/src/Methods/Methods.h b/src/Methods/Methods.h index 95a21f89..cc3bedb4 100644 --- a/src/Methods/Methods.h +++ b/src/Methods/Methods.h @@ -2,6 +2,7 @@ #define METHODS_H_ #include "../GbxRemote/GbxRemote.h" +#include "../Objects/Map.h" #include "../Objects/Player.h" #include "Structs.h" @@ -250,6 +251,21 @@ class Methods std::string GetMapsDirectory(); std::string GetSkinsDirectory();*/ + /*! + * \brief Provides a map object for the map currently being played on the server. + * + * This is mainly meant for the start-up of the controller. + * The current map can always be request via the Current property of MapList. + */ + Map GetCurrentMapInfo(); + + /*! + * \brief Provides a map object for the map with the specified file name. + * + * \param fileName File name of the map. + */ + Map GetMapInfo(std::string fileName); + private: GbxRemote* server; /**< \brief Pointer to GbxRemote. */ }; diff --git a/src/Objects/Map.h b/src/Objects/Map.h index 2b62c3a1..c593e80f 100644 --- a/src/Objects/Map.h +++ b/src/Objects/Map.h @@ -65,15 +65,34 @@ struct Map { if(serverStruct.find("Mood") != serverStruct.end()) { - setBasicInfo(serverStruct); setDetailedInfo(serverStruct); } } + /*! + * \brief Copies information from map object. + * + * \param map Map object to be copied into this. + */ + void CopyDetailedMap(Map map) + { + if(!map.Mood.empty()) + { + Mood = map.Mood; + BronzeTime = map.BronzeTime; + SilverTime = map.SilverTime; + AuthorTime = map.AuthorTime; + + LapRace = map.LapRace; + NbLaps = map.NbLaps; + NbCheckpoints = map.NbCheckpoints; + } + } + /*! * \brief Set database identifier value. * - * \param Database ID. + * \param id Database ID. */ void SetId(int id) { diff --git a/src/Objects/Player.h b/src/Objects/Player.h index 03ac3910..857356c3 100644 --- a/src/Objects/Player.h +++ b/src/Objects/Player.h @@ -71,7 +71,7 @@ struct Player /*! * \brief Set database identifier value. * - * \param Database ID. + * \param id Database ID. */ void SetId(int id) { diff --git a/src/Plugins/Plugin.h b/src/Plugins/Plugin.h index 5215d467..be904a4c 100644 --- a/src/Plugins/Plugin.h +++ b/src/Plugins/Plugin.h @@ -16,11 +16,24 @@ #include #include "../Events/Structs.h" +#include "../Maps/MapList.h" #include "../Methods/Methods.h" #include "../Objects/Player.h" #include "../Objects/Map.h" #include "../Utils/Logging.h" +//* Controller +/** + * \brief Struct with all instances needed for plugins. + */ +struct Controller +{ + Methods* Server; /**< \brief Methods instance. */ + std::map* Players; /**< \brief Playerlist instance. */ + MapList* Maps; /**< \brief Maplist instance. */ + sql::Connection* Database; /**< \brief Database instance. */ +}; + //* Plugin /** * \brief Plugin interface, inherited by all plugins. @@ -34,53 +47,13 @@ class Plugin virtual void Init() = 0; /*! - * \brief Sets the logging instance. - * - * \param loggingPtr Pointer to the logging instance. - */ - void SetLogging(Logging* loggingPtr) - { - logging = loggingPtr; - } - - /*! - * \brief Sets the server instance. - * - * \param methodsPtr Pointer to the Methods instance. - */ - void SetMethods(Methods* methodsPtr) - { - methods = methodsPtr; - } - - /*! - * \brief Sets the playerlist instance. - * - * \param playersPtr Pointer to the playerlist instance. - */ - void SetPlayers(std::map* playersPtr) - { - players = playersPtr; - } - - /*! - * \brief Sets the maplist instance. - * - * \param mapsPtr Pointer to the maplist instance. - */ - void SetMaps(std::map* mapsPtr) - { - maps = mapsPtr; - } - - /*! - * \brief Sets the database instance. + * \brief Sets the controller instance. * - * \param databasePtr Pointer to the database instance. + * \param controllerPtr Pointer to the controller instance. */ - void SetDatabase(sql::Connection* databasePtr) + void SetController(Controller* controllerPtr) { - database = databasePtr; + controller = controllerPtr; } std::vector> PlayerConnect; /**< \brief Vector with functions for the PlayerConnect event. */ @@ -105,11 +78,7 @@ class Plugin std::string Author; /**< \brief Plugin author. */ protected: - Logging* logging; /**< \brief Logging instance. */ - Methods* methods; /**< \brief Methods instance. */ - std::map* players; /**< \brief Playerlist instance. */ - std::map* maps; /**< \brief Maplist instance. */ - sql::Connection* database; /**< \brief Database instance. */ + Controller* controller; /**< \brief Struct with needed instances. */ }; #endif // PLUGIN_H_ diff --git a/src/Plugins/PluginManager.cpp b/src/Plugins/PluginManager.cpp index 9ead7e51..a0d168c2 100644 --- a/src/Plugins/PluginManager.cpp +++ b/src/Plugins/PluginManager.cpp @@ -1,15 +1,18 @@ #include "PluginManager.h" -PluginManager::PluginManager(Logging* loggingPtr, Methods* methodsPtr, std::map* playersPtr, std::map* mapsPtr, sql::Connection* databasePtr) +PluginManager::PluginManager(Methods* methodsPtr, + std::map* playersPtr, + MapList* mapsPtr, + sql::Connection* databasePtr) { plugins = std::map(); events = NULL; - logging = loggingPtr; - methods = methodsPtr; - players = playersPtr; - maps = mapsPtr; - database = databasePtr; + controller = new Controller(); + controller->Server = methodsPtr; + controller->Players = playersPtr; + controller->Maps = mapsPtr; + controller->Database = databasePtr; } PluginManager::~PluginManager() @@ -52,7 +55,7 @@ void PluginManager::LoadPlugins(std::string pluginsFolder) std::stringstream message; message << "Could not load '" << pluginId->first << "'..."; error->message = message.str(); - logging->PrintError(error); + Logging::PrintError(error); } else { @@ -67,16 +70,12 @@ void PluginManager::LoadPlugins(std::string pluginsFolder) std::stringstream message; message << "Could not call 'startPlugin'-function for '" << pluginId->first << "'..."; error->message = message.str(); - logging->PrintError(error); + Logging::PrintError(error); } else { Plugin* plugin = startPlugin(); - plugin->SetLogging(logging); - plugin->SetMethods(methods); - plugin->SetPlayers(players); - plugin->SetMaps(maps); - plugin->SetDatabase(database); + plugin->SetController(controller); std::cout << "[ \033[0;32mOK.\033[0;0m ] Loaded plugin '" << pluginId->first << "': v" << plugin->Version << " by " << plugin->Author << "." << std::endl; diff --git a/src/Plugins/PluginManager.h b/src/Plugins/PluginManager.h index 2b1a5618..182b6482 100644 --- a/src/Plugins/PluginManager.h +++ b/src/Plugins/PluginManager.h @@ -35,12 +35,12 @@ class PluginManager /*! * \brief Initializes the pointers the plugins need. * - * \param loggingPtr Current instance of Logging. * \param methodsPtr Current instance of Methods. * \param playersPtr Current instance of the playerlist. * \param mapsPtr Current instance of the maplist. + * \param databasePtr Current instance of the database connection. */ - PluginManager(Logging* loggingPtr, Methods* methodsPtr, std::map* playersPtr, std::map* mapsPtr, sql::Connection* databasePtr); + PluginManager(Methods* methodsPtr, std::map* playersPtr, MapList* mapsPtr, sql::Connection* databasePtr); /*! * \brief Destructor closes all open plugins. @@ -74,12 +74,7 @@ class PluginManager std::map plugins; /**< \brief Vector of loaded plugins. */ EventManager* events; /**< \brief Current instance of the EventManager. */ - - Logging* logging; /**< \brief Current instance of Logging. */ - Methods* methods; /**< \brief Current instance of Methods. */ - std::map* players; /**< \brief Current instance of the playerlist. */ - std::map* maps; /**< \brief Current instance of the maplist. */ - sql::Connection* database; /**< \brief Current instance of the database. */ + Controller* controller; /**< \brief Current instance of the controller. */ }; #endif // PLUGINMANAGER_H_ diff --git a/src/Utils/Logging.h b/src/Utils/Logging.h index d6d944ab..573b4cc4 100644 --- a/src/Utils/Logging.h +++ b/src/Utils/Logging.h @@ -54,7 +54,8 @@ class Logging /*! * \brief Prints error to console. * - * \param error Error structure. + * \param number Error number. + * \param message Error message. */ static void PrintError(int number, std::string message) { @@ -62,7 +63,7 @@ class Logging error->number = number; error->message = message; PrintError(error); - + delete error; error = NULL; } diff --git a/src/Utils/Time.h b/src/Utils/Time.h index 4688f5ad..590be31d 100644 --- a/src/Utils/Time.h +++ b/src/Utils/Time.h @@ -5,6 +5,10 @@ #include #include +//* Time +/** + * \brief Contains utilities to format map times. + */ class Time { public: From b5c7ef717a2e4cfab74266440a57ddf9fdbda211 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Mon, 2 Jan 2017 01:52:14 +0100 Subject: [PATCH 06/66] Fixed few memory issues. Still 'possibly lost' issues in MySQL driver, does not seem to be issue on controller side. --- plugins/LocalRecords/src/LocalRecordsPlugin.cpp | 4 ++++ src/Database/Database.cpp | 17 ++++++++++------- src/Database/Database.h | 12 ++++++++++-- src/ManiaPP.cpp | 17 +++++++++++++++-- src/ManiaPP.h | 1 + 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp index 68dc2449..4c0a164c 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp @@ -43,6 +43,7 @@ void LocalRecordsPlugin::retrieveRecords(Map map) while(result->next()) { + delete pstmt; pstmt = NULL; LocalRecord localRecord = LocalRecord(result); pstmt = controller->Database->prepareStatement("SELECT * FROM `players` WHERE `Id` = ?"); @@ -54,6 +55,9 @@ void LocalRecordsPlugin::retrieveRecords(Map map) localRecord.NickName = playerResult->getString("NickName"); localRecords.push_back(localRecord); + + delete pstmt; pstmt = NULL; + delete playerResult; playerResult = NULL; } delete pstmt; pstmt = NULL; diff --git a/src/Database/Database.cpp b/src/Database/Database.cpp index a1631187..ce609027 100644 --- a/src/Database/Database.cpp +++ b/src/Database/Database.cpp @@ -6,18 +6,21 @@ Database::Database(std::string serverAddress, int serverPort) port = serverPort; } -sql::Connection* Database::Connect(std::string username, std::string password, std::string database) +Database::~Database() { - sql::Driver* driver; - sql::Connection* connection; + Driver->threadEnd(); + Driver = NULL; +} +sql::Connection* Database::Connect(std::string username, std::string password, std::string database) +{ std::stringstream host; host << "tcp://" << address << ":" << port; - driver = get_driver_instance(); - connection = driver->connect(host.str(), username, password); + Driver = get_driver_instance(); + Connection = Driver->connect(host.str(), username, password); - connection->setSchema(database); + Connection->setSchema(database); - return connection; + return Connection; } diff --git a/src/Database/Database.h b/src/Database/Database.h index 98769c0a..3754cd14 100644 --- a/src/Database/Database.h +++ b/src/Database/Database.h @@ -25,6 +25,11 @@ class Database */ Database(std::string serverAddress, int serverPort = 3306); + /*! + * Deconstructor closes driver thread. + */ + ~Database(); + /*! * \brief Connects to the database. * @@ -34,9 +39,12 @@ class Database */ sql::Connection* Connect(std::string username, std::string password, std::string database); + sql::Driver* Driver; /**< \brief Database server driver. */ + sql::Connection* Connection; /**< \brief Database server driver. */ + private: - std::string address; /**< \brief IP Address of the database server. */ - int port; /**< \brief Port of the database server. */ + std::string address; /**< \brief IP Address of the database server. */ + int port; /**< \brief Port of the database server. */ }; #endif // DATABASE_H_ diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index e2c28697..bde7a7f0 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -24,6 +24,10 @@ ManiaPP::~ManiaPP() delete events; events = NULL; delete callbacks; callbacks = NULL; delete methods; methods = NULL; + + database->close(); + delete database; database = NULL; + delete db; db = NULL; } bool ManiaPP::ConnectToServer() @@ -119,8 +123,8 @@ bool ManiaPP::ConnectToDatabase() std::cout << "[ ] Connecting to the database on '" << config->Database->address << ":" << config->Database->port << "' ... " << '\r' << std::flush; try { - Database db = Database(config->Database->address, config->Database->port); - sql::Connection* dbConnection = db.Connect(config->Database->username, config->Database->password, config->Database->database); + db = new Database(config->Database->address, config->Database->port); + sql::Connection* dbConnection = db->Connect(config->Database->username, config->Database->password, config->Database->database); if(dbConnection != NULL) { logging->PrintOKFlush(); @@ -212,9 +216,15 @@ void ManiaPP::retrievePlayerList() { newPlayer.SetId(result->getInt("Id")); } + + delete pstmt; pstmt = NULL; + delete result; result = NULL; } players->insert(std::pair(newPlayer.Login, newPlayer)); + + delete message; message = NULL; + delete params; params = NULL; } } @@ -259,6 +269,9 @@ void ManiaPP::retrieveMapList() { newMap.SetId(result->getInt("Id")); } + + delete pstmt; pstmt = NULL; + delete result; result = NULL; } maps->List.insert(std::pair(newMap.UId, newMap)); diff --git a/src/ManiaPP.h b/src/ManiaPP.h index f0756aab..307391cf 100644 --- a/src/ManiaPP.h +++ b/src/ManiaPP.h @@ -70,6 +70,7 @@ class ManiaPP CallBackManager* callbacks; /**< \brief Contains the callback manager. */ Methods* methods; /**< \brief Server method caller. */ + Database* db = NULL; /**< \brief Contains database connection and driver (needed on destruction). */ sql::Connection* database = NULL; /**< \brief Contains database driver. */ ServerVersion serverVersion; /**< \brief Struct with server version information. */ From 80c28577af9fec6c78c2ae63e5c969f3d1268a1c Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Mon, 2 Jan 2017 02:04:20 +0100 Subject: [PATCH 07/66] Added (more) MapInfo methods. --- src/ManiaPP.cpp | 19 +++-------- src/Methods/Maps/MapInfo.cpp | 65 ++++++++++++++++++++++++++++++++++++ src/Methods/Methods.h | 20 +++++++++++ 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index bde7a7f0..d9990d7c 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -241,23 +241,12 @@ void ManiaPP::retrieveMapList() { std::cout << "[ ] Retrieving current map list ... " << '\r' << std::flush; - int mapListLimit = 2048; int mapListIndex = 0; - GbxParameters* params = new GbxParameters(); - params->Put(&mapListLimit); - params->Put(&mapListIndex); - GbxMessage* getMapList = new GbxMessage("GetMapList", params); - if(server->Query(getMapList)) + std::vector list = methods->GetMapList(2048, 0); + if(list.size() > 0) { - delete getMapList; getMapList = NULL; - delete params; params = NULL; - - std::vector responseParams = server->GetResponse()->GetParameters(); - std::vector mapList = responseParams.at(0).GetArray(); - - for(int mapId = 0; mapId < mapList.size(); mapId++) + for(int mapId = 0; mapId < list.size(); mapId++) { - std::map map = mapList.at(mapId).GetStruct(); - Map newMap = Map(map); + Map newMap = list.at(mapId); if(database != NULL) { diff --git a/src/Methods/Maps/MapInfo.cpp b/src/Methods/Maps/MapInfo.cpp index bea4ec77..1016cdb5 100644 --- a/src/Methods/Maps/MapInfo.cpp +++ b/src/Methods/Maps/MapInfo.cpp @@ -18,6 +18,24 @@ Map Methods::GetCurrentMapInfo() return currentMap; } +Map Methods::GetNextMapInfo() +{ + Map nextMap = Map(); + + GbxMessage* message = new GbxMessage("GetNextMapInfo"); + if(server->Query(message)) + { + delete message; message = NULL; + + std::vector responseParams = server->GetResponse()->GetParameters(); + std::map mapStruct = responseParams.at(0).GetStruct(); + + nextMap.MapDetailed(mapStruct); + } + + return nextMap; +} + Map Methods::GetMapInfo(std::string fileName) { Map mapInfo = Map(); @@ -39,3 +57,50 @@ Map Methods::GetMapInfo(std::string fileName) return mapInfo; } + +bool Methods::CheckMapForCurrentServerParams(std::string fileName) +{ + bool response = false; + + GbxParameters* params = new GbxParameters(); + params->Put(&fileName); + + GbxMessage* message = new GbxMessage("CheckMapForCurrentServerParams", params); + + if(server->Query(message)) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + delete params; params = NULL; + delete message; message = NULL; + return response; +} + +std::vector Methods::GetMapList(int limit, int index) +{ + std::vector list = std::vector(); + + GbxParameters* params = new GbxParameters(); + params->Put(&limit); + params->Put(&index); + GbxMessage* getMapList = new GbxMessage("GetMapList", params); + if(server->Query(getMapList)) + { + delete getMapList; getMapList = NULL; + delete params; params = NULL; + + std::vector responseParams = server->GetResponse()->GetParameters(); + std::vector mapList = responseParams.at(0).GetArray(); + + for(int mapId = 0; mapId < mapList.size(); mapId++) + { + std::map map = mapList.at(mapId).GetStruct(); + Map newMap = Map(map); + list.push_back(newMap); + } + } + + return list; +} diff --git a/src/Methods/Methods.h b/src/Methods/Methods.h index cc3bedb4..08eb19b2 100644 --- a/src/Methods/Methods.h +++ b/src/Methods/Methods.h @@ -251,6 +251,11 @@ class Methods std::string GetMapsDirectory(); std::string GetSkinsDirectory();*/ + /*! + * \brief Provides a map object for the map being played next on the server. + */ + Map GetNextMapInfo(); + /*! * \brief Provides a map object for the map currently being played on the server. * @@ -266,6 +271,21 @@ class Methods */ Map GetMapInfo(std::string fileName); + /*! + * \brief Checks if the map matches the current server settings. + * + * \param fileName File name of the map. + */ + bool CheckMapForCurrentServerParams(std::string fileName); + + /*! + * \brief Gets the maplist from the server. + * + * \param limit Maximum amount of maps to be retrieved. + * \param index Map index on which to start the retrieval. + */ + std::vector GetMapList(int limit, int index); + private: GbxRemote* server; /**< \brief Pointer to GbxRemote. */ }; From b51de008170c874ff9e3d2e67fabfa6425db90cd Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Mon, 2 Jan 2017 02:29:07 +0100 Subject: [PATCH 08/66] Added possibility to make server query without pointers (and their required deletion). --- src/GbxRemote/GbxRemote.cpp | 9 +++- src/GbxRemote/GbxRemote.h | 9 ++++ src/GbxRemote/Message/GbxMessage.cpp | 28 ++++++++++-- src/GbxRemote/Message/GbxMessage.h | 8 ++++ src/GbxRemote/Parameters/GbxParameter.cpp | 8 ++-- src/GbxRemote/Parameters/GbxParameters.h | 10 ++--- src/Methods/Maps/MapInfo.cpp | 36 +++++---------- src/Methods/Server/Chat.cpp | 43 +++++++----------- src/Methods/Server/Global.cpp | 54 ++++++++--------------- src/Methods/Server/Notice.cpp | 26 +++++------ 10 files changed, 116 insertions(+), 115 deletions(-) diff --git a/src/GbxRemote/GbxRemote.cpp b/src/GbxRemote/GbxRemote.cpp index 150c8f92..9bd90526 100644 --- a/src/GbxRemote/GbxRemote.cpp +++ b/src/GbxRemote/GbxRemote.cpp @@ -72,7 +72,7 @@ void GbxRemote::Terminate() connected = false; } -bool GbxRemote::Query(GbxMessage* query) +bool GbxRemote::Query(GbxMessage query) { delete currentError; delete currentResponse; @@ -83,7 +83,7 @@ bool GbxRemote::Query(GbxMessage* query) currentError = new GbxError(); currentResponse = new GbxResponse(); - if(!server.Send(query->GetXml())) + if(!server.Send(query.GetXml())) { currentError->number = -32300; currentError->message = "transport error - connection interrupted!"; @@ -130,6 +130,11 @@ bool GbxRemote::Query(GbxMessage* query) } } +bool GbxRemote::Query(GbxMessage* query) +{ + return Query(*query); +} + bool GbxRemote::ReadCallBacks() { if(!connected) diff --git a/src/GbxRemote/GbxRemote.h b/src/GbxRemote/GbxRemote.h index 85102b94..038f1f90 100644 --- a/src/GbxRemote/GbxRemote.h +++ b/src/GbxRemote/GbxRemote.h @@ -54,6 +54,15 @@ class GbxRemote */ bool Query(GbxMessage* query); + /*! + * \brief Sends a GbxMessage to the server. + * + * Returns whether the query was successfully sent. + * + * \param query Query to be send. + */ + bool Query(GbxMessage query); + /*! * \brief Read callbacks from the server. * diff --git a/src/GbxRemote/Message/GbxMessage.cpp b/src/GbxRemote/Message/GbxMessage.cpp index dd60b7ed..55d1f04e 100644 --- a/src/GbxRemote/Message/GbxMessage.cpp +++ b/src/GbxRemote/Message/GbxMessage.cpp @@ -9,20 +9,40 @@ GbxMessage::GbxMessage(std::string methodName, GbxParameters* parameters) xml += ""; if(parameters != NULL) { - for(int paramId = 0; paramId < parameters->GetParameters()->size(); paramId++) + for(int paramId = 0; paramId < parameters->GetParameters().size(); paramId++) { xml += ""; - GbxParameter* param = new GbxParameter(parameters->GetParameters()->at(paramId)); - xml += param->GetXml(); + GbxParameter param = GbxParameter(parameters->GetParameters().at(paramId)); + xml += param.GetXml(); xml += ""; - delete param; param = NULL; + //delete param; param = NULL; } } xml += ""; xml += ""; } +GbxMessage::GbxMessage(std::string methodName, GbxParameters parameters) + : method(methodName) +{ + xml = ""; + xml += ""; + xml += "" + method + ""; + xml += ""; + for(int paramId = 0; paramId < parameters.GetParameters().size(); paramId++) + { + xml += ""; + GbxParameter param = GbxParameter(parameters.GetParameters().at(paramId)); + xml += param.GetXml(); + xml += ""; + + //delete param; param = NULL; + } + xml += ""; + xml += ""; +} + std::string GbxMessage::GetMethod() { return method; diff --git a/src/GbxRemote/Message/GbxMessage.h b/src/GbxRemote/Message/GbxMessage.h index 7145c081..aec1e5db 100644 --- a/src/GbxRemote/Message/GbxMessage.h +++ b/src/GbxRemote/Message/GbxMessage.h @@ -22,6 +22,14 @@ class GbxMessage */ GbxMessage(std::string methodName, GbxParameters* parameters = NULL); + /*! + * \brief Builds XML message. + * + * \param methodName Server method name. + * \param parameters Params which belong to the method. + */ + GbxMessage(std::string methodName, GbxParameters parameters); + /*! * \brief Returns the method name. */ diff --git a/src/GbxRemote/Parameters/GbxParameter.cpp b/src/GbxRemote/Parameters/GbxParameter.cpp index ba4fa958..27270abf 100644 --- a/src/GbxRemote/Parameters/GbxParameter.cpp +++ b/src/GbxRemote/Parameters/GbxParameter.cpp @@ -63,12 +63,12 @@ void GbxParameter::dereferenceData(void* pointer) data += ""; GbxParameters** paramPtr = static_cast(pointer); GbxParameters* parameters = *paramPtr; - std::vector* gbxParams = parameters->GetParameters(); - for(int paramId = 0; paramId < gbxParams->size(); paramId++) + std::vector gbxParams = parameters->GetParameters(); + for(int paramId = 0; paramId < gbxParams.size(); paramId++) { data += ""; - GbxParameter* param = new GbxParameter(gbxParams->at(paramId)); - data += param->GetXml(); + GbxParameter param = GbxParameter(gbxParams.at(paramId)); + data += param.GetXml(); data += ""; } data += ""; diff --git a/src/GbxRemote/Parameters/GbxParameters.h b/src/GbxRemote/Parameters/GbxParameters.h index 5dadcf5e..316abc38 100644 --- a/src/GbxRemote/Parameters/GbxParameters.h +++ b/src/GbxRemote/Parameters/GbxParameters.h @@ -27,8 +27,8 @@ class GbxParameters */ ~GbxParameters() { - delete parameters; - parameters = NULL; + //delete parameters; + //parameters = NULL; } /*! @@ -40,19 +40,19 @@ class GbxParameters void Put(T* pointer) { GbxParam paramStruct = { pointer, &typeid(T) }; - parameters->push_back(paramStruct); + parameters.push_back(paramStruct); } /*! * \brief Return the current list of parameters. */ - std::vector* GetParameters() + std::vector GetParameters() { return parameters; } private: - std::vector* parameters = new std::vector(); /**< \brief List of parameters. */ + std::vector parameters = std::vector(); /**< \brief List of parameters. */ }; #endif // GBXPARAMETERS_H_ diff --git a/src/Methods/Maps/MapInfo.cpp b/src/Methods/Maps/MapInfo.cpp index 1016cdb5..4239aded 100644 --- a/src/Methods/Maps/MapInfo.cpp +++ b/src/Methods/Maps/MapInfo.cpp @@ -4,11 +4,9 @@ Map Methods::GetCurrentMapInfo() { Map currentMap = Map(); - GbxMessage* message = new GbxMessage("GetCurrentMapInfo"); + GbxMessage message = GbxMessage("GetCurrentMapInfo"); if(server->Query(message)) { - delete message; message = NULL; - std::vector responseParams = server->GetResponse()->GetParameters(); std::map mapStruct = responseParams.at(0).GetStruct(); @@ -22,11 +20,9 @@ Map Methods::GetNextMapInfo() { Map nextMap = Map(); - GbxMessage* message = new GbxMessage("GetNextMapInfo"); + GbxMessage message = GbxMessage("GetNextMapInfo"); if(server->Query(message)) { - delete message; message = NULL; - std::vector responseParams = server->GetResponse()->GetParameters(); std::map mapStruct = responseParams.at(0).GetStruct(); @@ -40,15 +36,12 @@ Map Methods::GetMapInfo(std::string fileName) { Map mapInfo = Map(); - GbxParameters* params = new GbxParameters(); - params->Put(&fileName); + GbxParameters params = GbxParameters(); + params.Put(&fileName); - GbxMessage* message = new GbxMessage("GetMapInfo", params); + GbxMessage message = GbxMessage("GetMapInfo", params); if(server->Query(message)) { - delete message; message = NULL; - delete params; params = NULL; - std::vector responseParams = server->GetResponse()->GetParameters(); std::map mapStruct = responseParams.at(0).GetStruct(); @@ -62,10 +55,10 @@ bool Methods::CheckMapForCurrentServerParams(std::string fileName) { bool response = false; - GbxParameters* params = new GbxParameters(); - params->Put(&fileName); + GbxParameters params = GbxParameters(); + params.Put(&fileName); - GbxMessage* message = new GbxMessage("CheckMapForCurrentServerParams", params); + GbxMessage message = GbxMessage("CheckMapForCurrentServerParams", params); if(server->Query(message)) { @@ -73,8 +66,6 @@ bool Methods::CheckMapForCurrentServerParams(std::string fileName) std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } @@ -82,15 +73,12 @@ std::vector Methods::GetMapList(int limit, int index) { std::vector list = std::vector(); - GbxParameters* params = new GbxParameters(); - params->Put(&limit); - params->Put(&index); - GbxMessage* getMapList = new GbxMessage("GetMapList", params); + GbxParameters params = GbxParameters(); + params.Put(&limit); + params.Put(&index); + GbxMessage getMapList = GbxMessage("GetMapList", params); if(server->Query(getMapList)) { - delete getMapList; getMapList = NULL; - delete params; params = NULL; - std::vector responseParams = server->GetResponse()->GetParameters(); std::vector mapList = responseParams.at(0).GetArray(); diff --git a/src/Methods/Server/Chat.cpp b/src/Methods/Server/Chat.cpp index 281d1dd7..3043933f 100644 --- a/src/Methods/Server/Chat.cpp +++ b/src/Methods/Server/Chat.cpp @@ -6,10 +6,10 @@ bool Methods::ChatSendServerMessage(std::string text) std::string sendText = "»» " + text; - GbxParameters* params = new GbxParameters(); - params->Put(&sendText); + GbxParameters params = GbxParameters(); + params.Put(&sendText); - GbxMessage* message = new GbxMessage("ChatSendServerMessage", params); + GbxMessage message = GbxMessage("ChatSendServerMessage", params); if(server->Query(message)) { @@ -17,8 +17,6 @@ bool Methods::ChatSendServerMessage(std::string text) std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } @@ -28,11 +26,11 @@ bool Methods::ChatSendServerMessageToLogin(std::string text, std::string login) std::string sendText = "» " + text; - GbxParameters* params = new GbxParameters(); - params->Put(&sendText); - params->Put(&login); + GbxParameters params = GbxParameters(); + params.Put(&sendText); + params.Put(&login); - GbxMessage* message = new GbxMessage("ChatSendServerMessageToLogin", params); + GbxMessage message = GbxMessage("ChatSendServerMessageToLogin", params); if(server->Query(message)) { @@ -40,8 +38,6 @@ bool Methods::ChatSendServerMessageToLogin(std::string text, std::string login) std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } @@ -53,7 +49,7 @@ bool Methods::ChatSendServerMessageToPlayer(std::string text, Player player) std::vector Methods::GetChatLines() { std::vector chatLines = std::vector(); - GbxMessage* message = new GbxMessage("GetChatLines"); + GbxMessage message = GbxMessage("GetChatLines"); if(server->Query(message)) { @@ -64,7 +60,6 @@ std::vector Methods::GetChatLines() } } - delete message; message = NULL; return chatLines; } @@ -72,11 +67,11 @@ bool Methods::ChatEnableManualRouting(bool enable, bool autoForward) { bool response = false; - GbxParameters* params = new GbxParameters(); - params->Put(&enable); - params->Put(&autoForward); + GbxParameters params = GbxParameters(); + params.Put(&enable); + params.Put(&autoForward); - GbxMessage* message = new GbxMessage("ChatEnableManualRouting", params); + GbxMessage message = GbxMessage("ChatEnableManualRouting", params); if(server->Query(message)) { @@ -84,8 +79,6 @@ bool Methods::ChatEnableManualRouting(bool enable, bool autoForward) std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } @@ -93,13 +86,13 @@ bool Methods::ChatForwardToLogin(std::string text, std::string sender, std::stri { bool response = false; - GbxParameters* params = new GbxParameters(); - params->Put(&text); - params->Put(&sender); + GbxParameters params = GbxParameters(); + params.Put(&text); + params.Put(&sender); if(!destination.empty()) - params->Put(&destination); + params.Put(&destination); - GbxMessage* message = new GbxMessage("ChatForwardToLogin", params); + GbxMessage message = GbxMessage("ChatForwardToLogin", params); if(server->Query(message)) { @@ -107,7 +100,5 @@ bool Methods::ChatForwardToLogin(std::string text, std::string sender, std::stri std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } diff --git a/src/Methods/Server/Global.cpp b/src/Methods/Server/Global.cpp index 7f0eee15..76aae793 100644 --- a/src/Methods/Server/Global.cpp +++ b/src/Methods/Server/Global.cpp @@ -4,11 +4,11 @@ bool Methods::Authenticate(std::string username, std::string password) { bool response = false; - GbxParameters* params = new GbxParameters(); - params->Put(&username); - params->Put(&password); + GbxParameters params = GbxParameters(); + params.Put(&username); + params.Put(&password); - GbxMessage* message = new GbxMessage("Authenticate", params); + GbxMessage message = GbxMessage("Authenticate", params); if(server->Query(message)) { @@ -16,8 +16,6 @@ bool Methods::Authenticate(std::string username, std::string password) std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } @@ -25,11 +23,11 @@ bool Methods::ChangeAuthPassword(std::string username, std::string password) { bool response = false; - GbxParameters* params = new GbxParameters(); - params->Put(&username); - params->Put(&password); + GbxParameters params = GbxParameters(); + params.Put(&username); + params.Put(&password); - GbxMessage* message = new GbxMessage("ChangeAuthPassword", params); + GbxMessage message = GbxMessage("ChangeAuthPassword", params); if(server->Query(message)) { @@ -37,8 +35,6 @@ bool Methods::ChangeAuthPassword(std::string username, std::string password) std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } @@ -46,10 +42,10 @@ bool Methods::EnableCallbacks(bool enable) { bool response = false; - GbxParameters* params = new GbxParameters(); - params->Put(&enable); + GbxParameters params = GbxParameters(); + params.Put(&enable); - GbxMessage* message = new GbxMessage("EnableCallbacks", params); + GbxMessage message = GbxMessage("EnableCallbacks", params); if(server->Query(message)) { @@ -57,8 +53,6 @@ bool Methods::EnableCallbacks(bool enable) std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } @@ -66,10 +60,10 @@ bool Methods::SetApiVersion(std::string version) { bool response = false; - GbxParameters* params = new GbxParameters(); - params->Put(&version); + GbxParameters params = GbxParameters(); + params.Put(&version); - GbxMessage* message = new GbxMessage("SetApiVersion", params); + GbxMessage message = GbxMessage("SetApiVersion", params); if(server->Query(message)) { @@ -77,15 +71,13 @@ bool Methods::SetApiVersion(std::string version) std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } std::vector Methods::ListMethods() { std::vector methods = std::vector(); - GbxMessage* message = new GbxMessage("system.listMethods"); + GbxMessage message = GbxMessage("system.listMethods"); if(server->Query(message)) { std::vector responseParams = server->GetResponse()->GetParameters(); @@ -97,7 +89,6 @@ std::vector Methods::ListMethods() } } - delete message; message = NULL; return methods; } @@ -105,11 +96,9 @@ ServerVersion Methods::GetVersion() { ServerVersion serverVersion = ServerVersion(); - GbxMessage* message = new GbxMessage("GetVersion"); + GbxMessage message = GbxMessage("GetVersion"); if(server->Query(message)) { - delete message; message = NULL; - std::vector responseParams = server->GetResponse()->GetParameters(); std::map versionStruct = responseParams.at(0).GetStruct(); @@ -127,11 +116,9 @@ SystemInfo Methods::GetSystemInfo() { SystemInfo systemInfo = SystemInfo(); - GbxMessage* message = new GbxMessage("GetSystemInfo"); + GbxMessage message = GbxMessage("GetSystemInfo"); if(server->Query(message)) { - delete message; message = NULL; - std::vector responseParams = server->GetResponse()->GetParameters(); std::map infoStruct = responseParams.at(0).GetStruct(); @@ -154,11 +141,9 @@ ServerStatus Methods::GetStatus() { ServerStatus serverStatus = ServerStatus(); - GbxMessage* message = new GbxMessage("GetStatus"); + GbxMessage message = GbxMessage("GetStatus"); if(server->Query(message)) { - delete message; message = NULL; - std::vector responseParams = server->GetResponse()->GetParameters(); std::map statusStruct = responseParams.at(0).GetStruct(); @@ -173,7 +158,7 @@ bool Methods::QuitGame() { bool response = false; - GbxMessage* message = new GbxMessage("QuitGame"); + GbxMessage message = GbxMessage("QuitGame"); if(server->Query(message)) { @@ -181,6 +166,5 @@ bool Methods::QuitGame() std::istringstream(responseParams.at(0).GetString()) >> response; } - delete message; message = NULL; return response; } diff --git a/src/Methods/Server/Notice.cpp b/src/Methods/Server/Notice.cpp index b051474c..701b1d09 100644 --- a/src/Methods/Server/Notice.cpp +++ b/src/Methods/Server/Notice.cpp @@ -4,12 +4,12 @@ bool Methods::SendNotice(std::string text, std::string avatarLogin, int variant) { bool response = false; - GbxParameters* params = new GbxParameters(); - params->Put(&text); - params->Put(&avatarLogin); - params->Put(&variant); + GbxParameters params = GbxParameters(); + params.Put(&text); + params.Put(&avatarLogin); + params.Put(&variant); - GbxMessage* message = new GbxMessage("SendNotice", params); + GbxMessage message = GbxMessage("SendNotice", params); if(server->Query(message)) { @@ -17,8 +17,6 @@ bool Methods::SendNotice(std::string text, std::string avatarLogin, int variant) std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } @@ -26,13 +24,13 @@ bool Methods::SendNoticeToLogin(std::string login, std::string text, std::string { bool response = false; - GbxParameters* params = new GbxParameters(); - params->Put(&login); - params->Put(&text); - params->Put(&avatarLogin); - params->Put(&variant); + GbxParameters params = GbxParameters(); + params.Put(&login); + params.Put(&text); + params.Put(&avatarLogin); + params.Put(&variant); - GbxMessage* message = new GbxMessage("SendNoticeToLogin", params); + GbxMessage message = GbxMessage("SendNoticeToLogin", params); if(server->Query(message)) { @@ -40,7 +38,5 @@ bool Methods::SendNoticeToLogin(std::string login, std::string text, std::string std::istringstream(responseParams.at(0).GetString()) >> response; } - delete params; params = NULL; - delete message; message = NULL; return response; } From 68dae5640dc7d4517f50a4100e89a99b6bbc311b Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Mon, 2 Jan 2017 02:35:22 +0100 Subject: [PATCH 09/66] Added possibility to make server query without pointers (and their required deletion) (2). --- src/CallBacks/CallBackManager.cpp | 11 ++++------ src/GbxRemote/GbxRemote.cpp | 5 ----- src/GbxRemote/GbxRemote.h | 9 -------- src/GbxRemote/Message/GbxMessage.cpp | 25 ----------------------- src/GbxRemote/Message/GbxMessage.h | 10 +-------- src/GbxRemote/Parameters/GbxParameter.cpp | 10 ++++----- src/ManiaPP.cpp | 20 +++++++----------- 7 files changed, 17 insertions(+), 73 deletions(-) diff --git a/src/CallBacks/CallBackManager.cpp b/src/CallBacks/CallBackManager.cpp index ead704bc..a138f243 100644 --- a/src/CallBacks/CallBackManager.cpp +++ b/src/CallBacks/CallBackManager.cpp @@ -91,14 +91,14 @@ void CallBackManager::HandleCallBack(std::string methodName, std::vector parameters) { - GbxParameters* params = new GbxParameters(); + GbxParameters params = GbxParameters(); std::string login = parameters.at(0).GetString(); - params->Put(&login); + params.Put(&login); - GbxMessage* message = new GbxMessage("GetPlayerInfo", params); + GbxMessage message = GbxMessage("GetPlayerInfo", params); server->Query(message); Player newPlayer = Player(server->GetResponse()->GetParameters().at(0).GetStruct()); - message = new GbxMessage("GetDetailedPlayerInfo", params); + message = GbxMessage("GetDetailedPlayerInfo", params); newPlayer.PlayerDetailed(server->GetResponse()->GetParameters().at(0).GetStruct()); if(database != NULL) @@ -118,9 +118,6 @@ void CallBackManager::HandlePlayerConnect(std::vector para std::cout << "Player connected: " << newPlayer.Login << " (# players: " << players->size() << ")" << std::endl; events->CallPlayerConnect(newPlayer); - - delete params; params = NULL; - delete message; message = NULL; } void CallBackManager::HandlePlayerDisconnect(std::vector parameters) diff --git a/src/GbxRemote/GbxRemote.cpp b/src/GbxRemote/GbxRemote.cpp index 9bd90526..4e4f67ae 100644 --- a/src/GbxRemote/GbxRemote.cpp +++ b/src/GbxRemote/GbxRemote.cpp @@ -130,11 +130,6 @@ bool GbxRemote::Query(GbxMessage query) } } -bool GbxRemote::Query(GbxMessage* query) -{ - return Query(*query); -} - bool GbxRemote::ReadCallBacks() { if(!connected) diff --git a/src/GbxRemote/GbxRemote.h b/src/GbxRemote/GbxRemote.h index 038f1f90..079b9352 100644 --- a/src/GbxRemote/GbxRemote.h +++ b/src/GbxRemote/GbxRemote.h @@ -45,15 +45,6 @@ class GbxRemote */ void Terminate(); - /*! - * \brief Sends a GbxMessage to the server. - * - * Returns whether the query was successfully sent. - * - * \param query Query to be send. - */ - bool Query(GbxMessage* query); - /*! * \brief Sends a GbxMessage to the server. * diff --git a/src/GbxRemote/Message/GbxMessage.cpp b/src/GbxRemote/Message/GbxMessage.cpp index 55d1f04e..58e11b01 100644 --- a/src/GbxRemote/Message/GbxMessage.cpp +++ b/src/GbxRemote/Message/GbxMessage.cpp @@ -1,28 +1,5 @@ #include "GbxMessage.h" -GbxMessage::GbxMessage(std::string methodName, GbxParameters* parameters) - : method(methodName) -{ - xml = ""; - xml += ""; - xml += "" + method + ""; - xml += ""; - if(parameters != NULL) - { - for(int paramId = 0; paramId < parameters->GetParameters().size(); paramId++) - { - xml += ""; - GbxParameter param = GbxParameter(parameters->GetParameters().at(paramId)); - xml += param.GetXml(); - xml += ""; - - //delete param; param = NULL; - } - } - xml += ""; - xml += ""; -} - GbxMessage::GbxMessage(std::string methodName, GbxParameters parameters) : method(methodName) { @@ -36,8 +13,6 @@ GbxMessage::GbxMessage(std::string methodName, GbxParameters parameters) GbxParameter param = GbxParameter(parameters.GetParameters().at(paramId)); xml += param.GetXml(); xml += ""; - - //delete param; param = NULL; } xml += ""; xml += ""; diff --git a/src/GbxRemote/Message/GbxMessage.h b/src/GbxRemote/Message/GbxMessage.h index aec1e5db..035c74c3 100644 --- a/src/GbxRemote/Message/GbxMessage.h +++ b/src/GbxRemote/Message/GbxMessage.h @@ -20,15 +20,7 @@ class GbxMessage * \param methodName Server method name. * \param parameters Params which belong to the method. */ - GbxMessage(std::string methodName, GbxParameters* parameters = NULL); - - /*! - * \brief Builds XML message. - * - * \param methodName Server method name. - * \param parameters Params which belong to the method. - */ - GbxMessage(std::string methodName, GbxParameters parameters); + GbxMessage(std::string methodName, GbxParameters parameters = GbxParameters()); /*! * \brief Returns the method name. diff --git a/src/GbxRemote/Parameters/GbxParameter.cpp b/src/GbxRemote/Parameters/GbxParameter.cpp index 27270abf..830fcef7 100644 --- a/src/GbxRemote/Parameters/GbxParameter.cpp +++ b/src/GbxRemote/Parameters/GbxParameter.cpp @@ -22,7 +22,7 @@ void GbxParameter::determineType(const std::type_info* param) char* realParamName = abi::__cxa_demangle(param->name(), 0, 0, &status); type = std::string(realParamName); - if(type.find("GbxParameters*") != std::string::npos) + if(type.find("GbxParameters") != std::string::npos) { xmlType = "array"; } @@ -58,12 +58,12 @@ void GbxParameter::determineType(const std::type_info* param) void GbxParameter::dereferenceData(void* pointer) { - if(type.find("GbxParameters*") != std::string::npos) + if(type.find("GbxParameters") != std::string::npos) { data += ""; - GbxParameters** paramPtr = static_cast(pointer); - GbxParameters* parameters = *paramPtr; - std::vector gbxParams = parameters->GetParameters(); + GbxParameters* paramPtr = static_cast(pointer); + GbxParameters parameters = *paramPtr; + std::vector gbxParams = parameters.GetParameters(); for(int paramId = 0; paramId < gbxParams.size(); paramId++) { data += ""; diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index d9990d7c..d4b7ddaa 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -180,15 +180,12 @@ void ManiaPP::retrievePlayerList() std::cout << "[ ] Retrieving current player list ... " << '\r' << std::flush; int playerListLimit = 512; int playerListIndex = 0; - GbxParameters* params = new GbxParameters(); - params->Put(&playerListLimit); - params->Put(&playerListIndex); - GbxMessage* getPlayerList = new GbxMessage("GetPlayerList", params); + GbxParameters params = GbxParameters(); + params.Put(&playerListLimit); + params.Put(&playerListIndex); + GbxMessage getPlayerList = GbxMessage("GetPlayerList", params); if(server->Query(getPlayerList)) { - delete getPlayerList; getPlayerList = NULL; - delete params; params = NULL; - std::vector responseParams = server->GetResponse()->GetParameters(); std::vector playerList = responseParams.at(0).GetArray(); @@ -197,11 +194,11 @@ void ManiaPP::retrievePlayerList() std::map player = playerList.at(playerId).GetStruct(); if(player.find("Login")->second.GetString() != systemInfo.ServerLogin) { - GbxParameters* params = new GbxParameters(); + GbxParameters params = GbxParameters(); std::string login = player.find("Login")->second.GetString(); - params->Put(&(login)); + params.Put(&(login)); - GbxMessage* message = new GbxMessage("GetDetailedPlayerInfo", params); + GbxMessage message = GbxMessage("GetDetailedPlayerInfo", params); server->Query(message); Player newPlayer = Player(player); newPlayer.PlayerDetailed(server->GetResponse()->GetParameters().at(0).GetStruct()); @@ -222,9 +219,6 @@ void ManiaPP::retrievePlayerList() } players->insert(std::pair(newPlayer.Login, newPlayer)); - - delete message; message = NULL; - delete params; params = NULL; } } From 2c6fea76345d21a51ccfd71b22b42483d01a4536 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Mon, 2 Jan 2017 02:40:31 +0100 Subject: [PATCH 10/66] Small plugin Init() changes. --- plugins/HelloGoodbye/src/HelloGoodbyePlugin.cpp | 2 +- plugins/LocalRecords/src/LocalRecordsPlugin.cpp | 16 ++-------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/plugins/HelloGoodbye/src/HelloGoodbyePlugin.cpp b/plugins/HelloGoodbye/src/HelloGoodbyePlugin.cpp index f537fde7..ebf048f9 100644 --- a/plugins/HelloGoodbye/src/HelloGoodbyePlugin.cpp +++ b/plugins/HelloGoodbye/src/HelloGoodbyePlugin.cpp @@ -11,7 +11,7 @@ HelloGoodbyePlugin::HelloGoodbyePlugin() void HelloGoodbyePlugin::Init() { - std::cout << "[ INFO ] Current amount of maps: " << controller->Maps->List.size() << std::endl; + } void HelloGoodbyePlugin::OnPlayerConnect(Player player) diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp index 4c0a164c..a6c10e0b 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp @@ -11,25 +11,13 @@ LocalRecordsPlugin::LocalRecordsPlugin() void LocalRecordsPlugin::Init() { retrieveRecords(*controller->Maps->Current); - - std::cout << "Records for " << controller->Maps->Current->Name << ":" << std::endl; - for(int recordId = 0; recordId < localRecords.size(); recordId++) - { - LocalRecord record = localRecords.at(recordId); - std::cout << "#" << recordId << ": " << record.FormattedTime << " by " << record.Login << std::endl; - } + std::cout << "[ INFO ] " << localRecords.size() << " records found for " << controller->Maps->Current->Name << "." << std::endl; } void LocalRecordsPlugin::OnBeginMap(Map map) { retrieveRecords(map); - - std::cout << "Records for " << map.Name << ":" << std::endl; - for(int recordId = 0; recordId < localRecords.size(); recordId++) - { - LocalRecord record = localRecords.at(recordId); - std::cout << "#" << recordId << ": " << record.FormattedTime << " by " << record.Login << std::endl; - } + std::cout << "[ INFO ] " << localRecords.size() << " records found for " << map.Name << "." << std::endl; } void LocalRecordsPlugin::retrieveRecords(Map map) From 92cb85cd3772060c38bf77ab94429a99cf27c23b Mon Sep 17 00:00:00 2001 From: Max Klaversma Date: Mon, 2 Jan 2017 13:15:19 +0100 Subject: [PATCH 11/66] Updated aims/goals in README. --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e28fa959..e6f2ff90 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,8 @@ Mania++ is (currently) not compatible with Windows systems and the ```./install. ## Aims and working points ## * [Be comparable or better than standard PhpRemote](https://themaximum.github.io/mania-pp/comparison.html) -* Create more usable objects (f.e. `Record`?) -* Expand usable objects (`Player` and `Map`) * Working plugin system - * Plugin interface - * Callback handling - * Access to server, database and ManiaLink -* Database support + * Access to ManiaLink * ManiaLink support ## Achieved goals ## @@ -41,6 +36,11 @@ Mania++ is (currently) not compatible with Windows systems and the ```./install. * De-XMLify responses and callbacks * Create usable objects (f.e. `Player` and `Map`) * Working configuration system (YAML) +* Database support +* Working plugin system + * Plugin interface + * Callback handling + * Access to server and database ## Installing for the first time ## * ```./install.sh``` From 87936ee01d7200ba889c94fd9f4c22f2888b351f Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Mon, 2 Jan 2017 16:12:37 +0100 Subject: [PATCH 12/66] Added more Methods-implementations (Map actions/ManiaLink). --- src/ManiaPP.cpp | 3 +- src/Methods/Maps/MapActions.cpp | 129 +++++++++++++++++++++++++++++++ src/Methods/Maps/MapInfo.cpp | 17 ++-- src/Methods/Methods.cpp | 3 +- src/Methods/Methods.h | 103 +++++++++++++++++++++--- src/Methods/Server/Chat.cpp | 19 ++--- src/Methods/Server/Global.cpp | 33 +++----- src/Methods/Server/ManiaLink.cpp | 92 ++++++++++++++++++++++ src/Methods/Server/Notice.cpp | 8 +- src/Methods/Structs.h | 10 +++ 10 files changed, 352 insertions(+), 65 deletions(-) create mode 100644 src/Methods/Maps/MapActions.cpp create mode 100644 src/Methods/Server/ManiaLink.cpp diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index d4b7ddaa..d04a03e0 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -7,9 +7,10 @@ ManiaPP::ManiaPP() config = new Config("config.yaml"); logging = new Logging(); server = new GbxRemote(); - methods = new Methods(server); players = new std::map(); maps = new MapList(); + + methods = new Methods(server, players); } ManiaPP::~ManiaPP() diff --git a/src/Methods/Maps/MapActions.cpp b/src/Methods/Maps/MapActions.cpp new file mode 100644 index 00000000..f3d861db --- /dev/null +++ b/src/Methods/Maps/MapActions.cpp @@ -0,0 +1,129 @@ +#include "../Methods.h" + +bool Methods::AddMap(std::string fileName) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&fileName); + + if(server->Query(GbxMessage("AddMap", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +int Methods::AddMapList(std::vector fileNames) +{ + int response = 0; + + GbxParameters params = GbxParameters(); + params.Put(&fileNames); + + if(server->Query(GbxMessage("AddMapList", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + response = atoi(responseParams.at(0).GetString().c_str()); + } + + return response; +} + +bool Methods::RemoveMap(std::string fileName) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&fileName); + + if(server->Query(GbxMessage("RemoveMap", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +int Methods::RemoveMapList(std::vector fileNames) +{ + int response = 0; + + GbxParameters params = GbxParameters(); + params.Put(&fileNames); + + if(server->Query(GbxMessage("RemoveMapList", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + response = atoi(responseParams.at(0).GetString().c_str()); + } + + return response; +} + +bool Methods::InsertMap(std::string fileName) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&fileName); + + if(server->Query(GbxMessage("InsertMap", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +int Methods::InsertMapList(std::vector fileNames) +{ + int response = 0; + + GbxParameters params = GbxParameters(); + params.Put(&fileNames); + + if(server->Query(GbxMessage("InsertMapList", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + response = atoi(responseParams.at(0).GetString().c_str()); + } + + return response; +} + +bool Methods::ChooseNextMap(std::string fileName) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&fileName); + + if(server->Query(GbxMessage("ChooseNextMap", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +int Methods::ChooseNextMapList(std::vector fileNames) +{ + int response = 0; + + GbxParameters params = GbxParameters(); + params.Put(&fileNames); + + if(server->Query(GbxMessage("ChooseNextMapList", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + response = atoi(responseParams.at(0).GetString().c_str()); + } + + return response; +} diff --git a/src/Methods/Maps/MapInfo.cpp b/src/Methods/Maps/MapInfo.cpp index 4239aded..feceece9 100644 --- a/src/Methods/Maps/MapInfo.cpp +++ b/src/Methods/Maps/MapInfo.cpp @@ -4,8 +4,7 @@ Map Methods::GetCurrentMapInfo() { Map currentMap = Map(); - GbxMessage message = GbxMessage("GetCurrentMapInfo"); - if(server->Query(message)) + if(server->Query(GbxMessage("GetCurrentMapInfo"))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::map mapStruct = responseParams.at(0).GetStruct(); @@ -20,8 +19,7 @@ Map Methods::GetNextMapInfo() { Map nextMap = Map(); - GbxMessage message = GbxMessage("GetNextMapInfo"); - if(server->Query(message)) + if(server->Query(GbxMessage("GetNextMapInfo"))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::map mapStruct = responseParams.at(0).GetStruct(); @@ -39,8 +37,7 @@ Map Methods::GetMapInfo(std::string fileName) GbxParameters params = GbxParameters(); params.Put(&fileName); - GbxMessage message = GbxMessage("GetMapInfo", params); - if(server->Query(message)) + if(server->Query(GbxMessage("GetMapInfo", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::map mapStruct = responseParams.at(0).GetStruct(); @@ -58,9 +55,7 @@ bool Methods::CheckMapForCurrentServerParams(std::string fileName) GbxParameters params = GbxParameters(); params.Put(&fileName); - GbxMessage message = GbxMessage("CheckMapForCurrentServerParams", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("CheckMapForCurrentServerParams", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; @@ -76,8 +71,8 @@ std::vector Methods::GetMapList(int limit, int index) GbxParameters params = GbxParameters(); params.Put(&limit); params.Put(&index); - GbxMessage getMapList = GbxMessage("GetMapList", params); - if(server->Query(getMapList)) + + if(server->Query(GbxMessage("GetMapList", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::vector mapList = responseParams.at(0).GetArray(); diff --git a/src/Methods/Methods.cpp b/src/Methods/Methods.cpp index b25c0cc7..eb70072a 100644 --- a/src/Methods/Methods.cpp +++ b/src/Methods/Methods.cpp @@ -1,6 +1,7 @@ #include "Methods.h" -Methods::Methods(GbxRemote* serverPtr) +Methods::Methods(GbxRemote* serverPtr, std::map* playerListPtr) { server = serverPtr; + playerList = playerListPtr; } diff --git a/src/Methods/Methods.h b/src/Methods/Methods.h index 08eb19b2..c8513e48 100644 --- a/src/Methods/Methods.h +++ b/src/Methods/Methods.h @@ -18,7 +18,7 @@ class Methods * * \param serverPtr Pointer to server connection. */ - Methods(GbxRemote* serverPtr); + Methods(GbxRemote* serverPtr, std::map* playerListPtr); /*! * \brief Authenticates with the server. Returns whether authentication was successful. @@ -160,15 +160,43 @@ class Methods // bool SetCallVoteRatiosEx(bool replaceAll, array[struct{Command, Param, Ratio}]); // array[struct{Command, Param, Ratio}] GetCallVoteRatiosEx(); - /*bool SendDisplayManialinkPage(std::string page, int timeout, bool hideOnClick); + /*! + * \brief Displays ManiaLink page to all players. + * + * \param page XML page to be displayed. + * \param timeout Timeout to autohide (0 = permanent). + * \param hideOnClick Hide the page if a page option is clicked. + */ + bool SendDisplayManialinkPage(std::string page, int timeout, bool hideOnClick); + + /*! + * \brief Displays ManiaLink page to a player. + * + * \param login Player login. + * \param page XML page to be displayed. + * \param timeout Timeout to autohide (0 = permanent). + * \param hideOnClick Hide the page if a page option is clicked. + */ bool SendDisplayManialinkPageToLogin(std::string login, std::string page, int timeout, bool hideOnClick); + /*! + * \brief Hides all ManiaLink pages for all players. + */ bool SendHideManialinkPage(); + + /*! + * \brief Hides all ManiaLink pages for a player. + * + * \param login Player login. + */ bool SendHideManialinkPageToLogin(std::string login); - // array[struct{Login,PlayerId,Result}] GetManialinkPageAnswers(); + /*! + * \brief Returns the latest results from the current ManiaLink page. + */ + std::vector GetManialinkPageAnswers(); - bool SendOpenLinkToLogin(std::string login, std::string link, int linkType); + /*bool SendOpenLinkToLogin(std::string login, std::string link, int linkType); bool Kick(std::string login, std::string message = ""); bool Ban(std::string login, std::string message = ""); @@ -267,27 +295,84 @@ class Methods /*! * \brief Provides a map object for the map with the specified file name. * - * \param fileName File name of the map. + * \param fileName File name of the map. */ Map GetMapInfo(std::string fileName); /*! * \brief Checks if the map matches the current server settings. * - * \param fileName File name of the map. + * \param fileName File name of the map. */ bool CheckMapForCurrentServerParams(std::string fileName); /*! * \brief Gets the maplist from the server. * - * \param limit Maximum amount of maps to be retrieved. - * \param index Map index on which to start the retrieval. + * \param limit Maximum amount of maps to be retrieved. + * \param index Map index on which to start the retrieval. */ std::vector GetMapList(int limit, int index); + /*! + * \brief Add the map with the specified filename at the end of the current selection. + * + * \param fileName Filename of the map. + */ + bool AddMap(std::string fileName); + + /*! + * \brief Add the list of maps with the specified filenames at the end of the current selection. + * + * \param fileNames List of map filenames. + */ + int AddMapList(std::vector fileNames); + + /*! + * \brief Remove the map with the specified filename from the current selection. + * + * \param fileName Filename of the map. + */ + bool RemoveMap(std::string fileName); + + /*! + * \brief Remove the list of maps with the specified filenames from the current selection. + * + * \param fileNames List of map filenames. + */ + int RemoveMapList(std::vector fileNames); + + /*! + * \brief Insert the map with the specified filename after the current map. + * + * \param fileName Filename of the map. + */ + bool InsertMap(std::string fileName); + + /*! + * \brief Insert the list of maps with the specified filenames after the current map. + * + * \param fileNames List of map filenames. + */ + int InsertMapList(std::vector fileNames); + + /*! + * \brief Set as next map the one with the specified filename, if it is present in the selection. + * + * \param fileName Filename of the map. + */ + bool ChooseNextMap(std::string fileName); + + /*! + * \brief Set as next maps the list of maps with the specified filenames, if they are present in the selection. + * + * \param fileNames List of map filenames. + */ + int ChooseNextMapList(std::vector fileNames); + private: - GbxRemote* server; /**< \brief Pointer to GbxRemote. */ + GbxRemote* server; /**< \brief Pointer to GbxRemote. */ + std::map* playerList; /**< \brief Pointer to the playerlist. */ }; #endif // METHODS_H_ diff --git a/src/Methods/Server/Chat.cpp b/src/Methods/Server/Chat.cpp index 3043933f..1998337a 100644 --- a/src/Methods/Server/Chat.cpp +++ b/src/Methods/Server/Chat.cpp @@ -9,9 +9,7 @@ bool Methods::ChatSendServerMessage(std::string text) GbxParameters params = GbxParameters(); params.Put(&sendText); - GbxMessage message = GbxMessage("ChatSendServerMessage", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("ChatSendServerMessage", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; @@ -30,9 +28,7 @@ bool Methods::ChatSendServerMessageToLogin(std::string text, std::string login) params.Put(&sendText); params.Put(&login); - GbxMessage message = GbxMessage("ChatSendServerMessageToLogin", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("ChatSendServerMessageToLogin", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; @@ -49,9 +45,8 @@ bool Methods::ChatSendServerMessageToPlayer(std::string text, Player player) std::vector Methods::GetChatLines() { std::vector chatLines = std::vector(); - GbxMessage message = GbxMessage("GetChatLines"); - if(server->Query(message)) + if(server->Query(GbxMessage("GetChatLines"))) { std::vector responseParams = server->GetResponse()->GetParameters().at(0).GetArray(); for(int paramId = 0; paramId < responseParams.size(); paramId++) @@ -71,9 +66,7 @@ bool Methods::ChatEnableManualRouting(bool enable, bool autoForward) params.Put(&enable); params.Put(&autoForward); - GbxMessage message = GbxMessage("ChatEnableManualRouting", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("ChatEnableManualRouting", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; @@ -92,9 +85,7 @@ bool Methods::ChatForwardToLogin(std::string text, std::string sender, std::stri if(!destination.empty()) params.Put(&destination); - GbxMessage message = GbxMessage("ChatForwardToLogin", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("ChatForwardToLogin", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; diff --git a/src/Methods/Server/Global.cpp b/src/Methods/Server/Global.cpp index 76aae793..ab22eda1 100644 --- a/src/Methods/Server/Global.cpp +++ b/src/Methods/Server/Global.cpp @@ -8,9 +8,7 @@ bool Methods::Authenticate(std::string username, std::string password) params.Put(&username); params.Put(&password); - GbxMessage message = GbxMessage("Authenticate", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("Authenticate", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; @@ -27,9 +25,7 @@ bool Methods::ChangeAuthPassword(std::string username, std::string password) params.Put(&username); params.Put(&password); - GbxMessage message = GbxMessage("ChangeAuthPassword", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("ChangeAuthPassword", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; @@ -45,9 +41,7 @@ bool Methods::EnableCallbacks(bool enable) GbxParameters params = GbxParameters(); params.Put(&enable); - GbxMessage message = GbxMessage("EnableCallbacks", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("EnableCallbacks", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; @@ -63,9 +57,7 @@ bool Methods::SetApiVersion(std::string version) GbxParameters params = GbxParameters(); params.Put(&version); - GbxMessage message = GbxMessage("SetApiVersion", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("SetApiVersion", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; @@ -77,8 +69,8 @@ bool Methods::SetApiVersion(std::string version) std::vector Methods::ListMethods() { std::vector methods = std::vector(); - GbxMessage message = GbxMessage("system.listMethods"); - if(server->Query(message)) + + if(server->Query(GbxMessage("system.listMethods"))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::vector methodsArray = responseParams.at(0).GetArray(); @@ -96,8 +88,7 @@ ServerVersion Methods::GetVersion() { ServerVersion serverVersion = ServerVersion(); - GbxMessage message = GbxMessage("GetVersion"); - if(server->Query(message)) + if(server->Query(GbxMessage("GetVersion"))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::map versionStruct = responseParams.at(0).GetStruct(); @@ -116,8 +107,7 @@ SystemInfo Methods::GetSystemInfo() { SystemInfo systemInfo = SystemInfo(); - GbxMessage message = GbxMessage("GetSystemInfo"); - if(server->Query(message)) + if(server->Query(GbxMessage("GetSystemInfo"))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::map infoStruct = responseParams.at(0).GetStruct(); @@ -141,8 +131,7 @@ ServerStatus Methods::GetStatus() { ServerStatus serverStatus = ServerStatus(); - GbxMessage message = GbxMessage("GetStatus"); - if(server->Query(message)) + if(server->Query(GbxMessage("GetStatus"))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::map statusStruct = responseParams.at(0).GetStruct(); @@ -158,9 +147,7 @@ bool Methods::QuitGame() { bool response = false; - GbxMessage message = GbxMessage("QuitGame"); - - if(server->Query(message)) + if(server->Query(GbxMessage("QuitGame"))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; diff --git a/src/Methods/Server/ManiaLink.cpp b/src/Methods/Server/ManiaLink.cpp new file mode 100644 index 00000000..50b3a0c6 --- /dev/null +++ b/src/Methods/Server/ManiaLink.cpp @@ -0,0 +1,92 @@ +#include "../Methods.h" + +bool Methods::SendDisplayManialinkPage(std::string page, int timeout, bool hideOnClick) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&page); + params.Put(&timeout); + params.Put(&hideOnClick); + + if(server->Query(GbxMessage("SendDisplayManialinkPage", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +bool Methods::SendDisplayManialinkPageToLogin(std::string login, std::string page, int timeout, bool hideOnClick) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&login); + params.Put(&page); + params.Put(&timeout); + params.Put(&hideOnClick); + + if(server->Query(GbxMessage("SendDisplayManialinkPageToLogin", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +bool Methods::SendHideManialinkPage() +{ + bool response = false; + + if(server->Query(GbxMessage("SendHideManialinkPage"))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +bool Methods::SendHideManialinkPageToLogin(std::string login) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&login); + + if(server->Query(GbxMessage("SendHideManialinkPageToLogin", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +std::vector Methods::GetManialinkPageAnswers() +{ + std::vector answers = std::vector(); + + if(server->Query(GbxMessage("GetManialinkPageAnswers"))) + { + std::vector responseParams = server->GetResponse()->GetParameters().at(0).GetArray(); + for(int paramId = 0; paramId < responseParams.size(); paramId++) + { + std::map answer = responseParams.at(paramId).GetStruct(); + std::string login = answer.find("Login")->second.GetString(); + Player player = playerList->find(login)->second; + int result = atoi(answer.find("Result")->second.GetString().c_str()); + + ManiaLinkPageAnswer linkAnswer = ManiaLinkPageAnswer(); + linkAnswer.AnsweringPlayer = player; + linkAnswer.Result = result; + + answers.push_back(linkAnswer); + } + } + + return answers; +} diff --git a/src/Methods/Server/Notice.cpp b/src/Methods/Server/Notice.cpp index 701b1d09..21d8fa23 100644 --- a/src/Methods/Server/Notice.cpp +++ b/src/Methods/Server/Notice.cpp @@ -9,9 +9,7 @@ bool Methods::SendNotice(std::string text, std::string avatarLogin, int variant) params.Put(&avatarLogin); params.Put(&variant); - GbxMessage message = GbxMessage("SendNotice", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("SendNotice", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; @@ -30,9 +28,7 @@ bool Methods::SendNoticeToLogin(std::string login, std::string text, std::string params.Put(&avatarLogin); params.Put(&variant); - GbxMessage message = GbxMessage("SendNoticeToLogin", params); - - if(server->Query(message)) + if(server->Query(GbxMessage("SendNoticeToLogin", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; diff --git a/src/Methods/Structs.h b/src/Methods/Structs.h index 4785617f..f1798ddc 100644 --- a/src/Methods/Structs.h +++ b/src/Methods/Structs.h @@ -42,4 +42,14 @@ struct ServerStatus std::string Name; /**< \brief Current status name. */ }; +//* ManiaLinkPageAnswer +/** + * \brief Struct with a ManiaLink page answer. + */ +struct ManiaLinkPageAnswer +{ + Player AnsweringPlayer; /**< \brief Player answering. */ + int Result; /**< \brief Answering result. */ +}; + #endif // METHODS_STRUCTS_H_ From 89ed06110f75c6dd525383f219c0f630d6abb133 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Mon, 2 Jan 2017 17:25:58 +0100 Subject: [PATCH 13/66] Added CallVote-methods (SetCallVoteRatiosEx needs struct-sending support in GbxRemote). --- src/GbxRemote/Parameters/GbxParameter.h | 2 + src/GbxRemote/Parameters/GbxParameters.h | 9 - src/ManiaPP.cpp | 13 ++ src/Methods/Methods.h | 109 ++++++++++-- src/Methods/Server/CallVote.cpp | 212 +++++++++++++++++++++++ src/Methods/Server/Global.cpp | 17 ++ src/Methods/Server/Notice.cpp | 18 ++ src/Methods/Structs.h | 42 +++++ 8 files changed, 398 insertions(+), 24 deletions(-) create mode 100644 src/Methods/Server/CallVote.cpp diff --git a/src/GbxRemote/Parameters/GbxParameter.h b/src/GbxRemote/Parameters/GbxParameter.h index df837e08..e5578c82 100644 --- a/src/GbxRemote/Parameters/GbxParameter.h +++ b/src/GbxRemote/Parameters/GbxParameter.h @@ -10,6 +10,8 @@ //* GbxParameter /** * \brief XML-fies the parameter for communication with the server. + * + * \todo Handle structs. */ class GbxParameter { diff --git a/src/GbxRemote/Parameters/GbxParameters.h b/src/GbxRemote/Parameters/GbxParameters.h index 316abc38..9e76e82b 100644 --- a/src/GbxRemote/Parameters/GbxParameters.h +++ b/src/GbxRemote/Parameters/GbxParameters.h @@ -22,15 +22,6 @@ struct GbxParam class GbxParameters { public: - /*! - * \brief Deletes + nullifies parameters pointer. - */ - ~GbxParameters() - { - //delete parameters; - //parameters = NULL; - } - /*! * \brief Add parameter to the list. * diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index d04a03e0..3ad2d67f 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -99,6 +99,19 @@ bool ManiaPP::ConnectToServer() PrintServerInfo(); + std::vector callVoteRatios = std::vector(); + ExtendedCallVoteRatio callVoteRatio = ExtendedCallVoteRatio(); + callVoteRatio.Command = "CommandOne"; + callVoteRatio.Param = "ParamA"; + callVoteRatio.Ratio = 0.5; + callVoteRatios.push_back(callVoteRatio); + callVoteRatio = ExtendedCallVoteRatio(); + callVoteRatio.Command = "CommandTwo"; + callVoteRatio.Param = "ParamB"; + callVoteRatio.Ratio = 0.85; + callVoteRatios.push_back(callVoteRatio); + methods->SetCallVoteRatiosEx(true, callVoteRatios); + return true; } else diff --git a/src/Methods/Methods.h b/src/Methods/Methods.h index c8513e48..00e043ca 100644 --- a/src/Methods/Methods.h +++ b/src/Methods/Methods.h @@ -71,6 +71,14 @@ class Methods */ SystemInfo GetSystemInfo(); + /*! + * \brief Sets the server connection rates. + * + * \param downloadRate New download rate. + * \param uploadRate New upload rate. + */ + bool SetConnectionRates(int downloadRate, int uploadRate); + /*! * \brief Returns server status. * @@ -147,18 +155,92 @@ class Methods */ bool SendNoticeToLogin(std::string login, std::string text, std::string avatarLogin = "", int variant = 0); - // bool CallVote(std::string xmlRequest); - // bool CallVoteEx(std::string xmlRequest, double ratio, int timeout, int whoVotes); - // bool CancelVote(); - // struct{CallerLogin, CmdName, CmdParam} GetCurrentCallVote(); - // bool SetCallVoteTimeOut(int timeout); - // array[CurrentValue, NextValue] GetCallVoteTimeOut(); - // bool SetCallVoteRatio(double ratio); - // double GetCallVoteRatio(); - // bool SetCallVoteRatios(array[ratios]); - // array[ratios] GetCallVoteRatios(); - // bool SetCallVoteRatiosEx(bool replaceAll, array[struct{Command, Param, Ratio}]); - // array[struct{Command, Param, Ratio}] GetCallVoteRatiosEx(); + /*! + * \brief Send link to player login. + * + * \param login Player login. + * \param link Link address. + * \param linkType Link type (0 = external, 1 = ManiaLink). + */ + bool SendOpenLinkToLogin(std::string login, std::string link, int linkType); + + /*! + * \brief Start callvote. + * + * \param xmlRequest Callvote request. + */ + bool CallVote(std::string xmlRequest); + + /*! + * \brief Start callvote. + * + * \param xmlRequest Callvote request. + * \param ratio Passing ratio (between 0 and 1, -1 = default). + * \param timeout Timeout for the callvote (0 = default, 1 = indefinite). + * \param whoVotes Who votes for the callvote (0 = active players, 1 = all players, 2 = everyone, including spectators). + */ + bool CallVoteEx(std::string xmlRequest, double ratio, int timeout, int whoVotes); + + /*! + * \brief Cancels the current callvote. + */ + bool CancelVote(); + + /*! + * \brief Returns the current callvote. + */ + CurrentCallVote GetCurrentCallVote(); + + /*! + * \brief Sets the default callvote timeout. + * + * \param timeout Default timeout. + */ + bool SetCallVoteTimeOut(int timeout); + + /*! + * \brief Gets the default callvote timeout. + */ + CurrentNextValue GetCallVoteTimeOut(); + + /*! + * \brief Sets the default callvote ratio. + * + * \param ratio Default timeout. + */ + bool SetCallVoteRatio(double ratio); + + /*! + * \brief Gets the default callvote ratio. + */ + double GetCallVoteRatio(); + + /*! + * \brief Sets the callvote ratios. + * + * \param ratios List of callvote ratios. + */ + bool SetCallVoteRatios(std::vector ratios); + + /*! + * \brief Gets the callvote ratios. + */ + std::vector GetCallVoteRatios(); + + /*! + * \brief Sets the callvote ratios. + * + * \param replaceAll Replace all current callvote ratios? + * \param ratios List of callvote ratios. + * + * \todo Implement when GbxParameters supports structs. + */ + bool SetCallVoteRatiosEx(bool replaceAll, std::vector ratios); + + /*! + * \brief Gets the callvote ratios. + */ + std::vector GetCallVoteRatiosEx(); /*! * \brief Displays ManiaLink page to all players. @@ -235,9 +317,6 @@ class Methods // struct{State, StateName, TransactionId} GetBillState(int billId); int GetServerPlanets(); - //struct{?} GetSystemInfo(); - bool SetConnectionRates(int download, int upload); - //array[struct{Name, Value}] GetServerTags(); bool SetServerTag(std::string name, std::string value); bool UnsetServerTag(std::string name); diff --git a/src/Methods/Server/CallVote.cpp b/src/Methods/Server/CallVote.cpp new file mode 100644 index 00000000..fd98475f --- /dev/null +++ b/src/Methods/Server/CallVote.cpp @@ -0,0 +1,212 @@ +#include "../Methods.h" + +bool Methods::CallVote(std::string xmlRequest) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&xmlRequest); + + if(server->Query(GbxMessage("CallVote", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +bool Methods::CallVoteEx(std::string xmlRequest, double ratio, int timeout, int whoVotes) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&xmlRequest); + params.Put(&ratio); + params.Put(&timeout); + params.Put(&whoVotes); + + if(server->Query(GbxMessage("CallVoteEx", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +bool Methods::CancelVote() +{ + bool response = false; + + if(server->Query(GbxMessage("CancelVote"))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +CurrentCallVote Methods::GetCurrentCallVote() +{ + CurrentCallVote response = CurrentCallVote(); + + if(server->Query(GbxMessage("GetCurrentCallVote"))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::map currentStruct = responseParams.at(0).GetStruct(); + response.CallerLogin = currentStruct.find("CallerLogin")->second.GetString(); + response.CmdName = currentStruct.find("CmdName")->second.GetString(); + response.CmdParam = currentStruct.find("CmdParam")->second.GetString(); + } + + return response; +} + +bool Methods::SetCallVoteTimeOut(int timeout) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&timeout); + + if(server->Query(GbxMessage("SetCallVoteTimeOut", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +CurrentNextValue Methods::GetCallVoteTimeOut() +{ + CurrentNextValue response = CurrentNextValue(); + + if(server->Query(GbxMessage("GetCallVoteTimeOut"))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::map currentStruct = responseParams.at(0).GetStruct(); + response.CurrentValue = atoi(currentStruct.find("CurrentValue")->second.GetString().c_str()); + response.NextValue = atoi(currentStruct.find("NextValue")->second.GetString().c_str()); + } + + return response; +} + +bool Methods::SetCallVoteRatio(double ratio) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&ratio); + + if(server->Query(GbxMessage("SetCallVoteRatio", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +double Methods::GetCallVoteRatio() +{ + double response; + + if(server->Query(GbxMessage("GetCallVoteRatio"))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + response = atof(responseParams.at(0).GetString().c_str()); + } + + return response; +} + +bool Methods::SetCallVoteRatios(std::vector ratios) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&ratios); + + if(server->Query(GbxMessage("SetCallVoteRatios", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + +std::vector Methods::GetCallVoteRatios() +{ + std::vector response = std::vector(); + + if(server->Query(GbxMessage("GetCallVoteRatios"))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::vector responseArray = responseParams.at(0).GetArray(); + + for(int responseId = 0; responseId < responseArray.size(); responseId++) + { + std::map ratioStruct = responseArray.at(responseId).GetStruct(); + + CallVoteRatio ratio = CallVoteRatio(); + ratio.Command = ratioStruct.find("Command")->second.GetString(); + ratio.Ratio = atof(ratioStruct.find("Ratio")->second.GetString().c_str()); + response.push_back(ratio); + } + } + + return response; +} + +bool Methods::SetCallVoteRatiosEx(bool replaceAll, std::vector ratios) +{ + return false; + + /*bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&replaceAll); + //params.Put(&ratios); + + std::vectorQuery(GbxMessage("SetCallVoteRatiosEx", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + }* + + return response;*/ +} + +std::vector Methods::GetCallVoteRatiosEx() +{ + std::vector response = std::vector(); + + if(server->Query(GbxMessage("GetCallVoteRatiosEx"))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::vector responseArray = responseParams.at(0).GetArray(); + + for(int responseId = 0; responseId < responseArray.size(); responseId++) + { + std::map ratioStruct = responseArray.at(responseId).GetStruct(); + + ExtendedCallVoteRatio ratio = ExtendedCallVoteRatio(); + ratio.Command = ratioStruct.find("Command")->second.GetString(); + ratio.Param = ratioStruct.find("Param")->second.GetString(); + ratio.Ratio = atof(ratioStruct.find("Ratio")->second.GetString().c_str()); + response.push_back(ratio); + } + } + + return response; +} diff --git a/src/Methods/Server/Global.cpp b/src/Methods/Server/Global.cpp index ab22eda1..8f9ec655 100644 --- a/src/Methods/Server/Global.cpp +++ b/src/Methods/Server/Global.cpp @@ -127,6 +127,23 @@ SystemInfo Methods::GetSystemInfo() return systemInfo; } +bool Methods::SetConnectionRates(int downloadRate, int uploadRate) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&downloadRate); + params.Put(&uploadRate); + + if(server->Query(GbxMessage("SetConnectionRates", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} + ServerStatus Methods::GetStatus() { ServerStatus serverStatus = ServerStatus(); diff --git a/src/Methods/Server/Notice.cpp b/src/Methods/Server/Notice.cpp index 21d8fa23..624fea30 100644 --- a/src/Methods/Server/Notice.cpp +++ b/src/Methods/Server/Notice.cpp @@ -36,3 +36,21 @@ bool Methods::SendNoticeToLogin(std::string login, std::string text, std::string return response; } + +bool Methods::SendOpenLinkToLogin(std::string login, std::string link, int linkType) +{ + bool response = false; + + GbxParameters params = GbxParameters(); + params.Put(&login); + params.Put(&link); + params.Put(&linkType); + + if(server->Query(GbxMessage("SendOpenLinkToLogin", params))) + { + std::vector responseParams = server->GetResponse()->GetParameters(); + std::istringstream(responseParams.at(0).GetString()) >> response; + } + + return response; +} diff --git a/src/Methods/Structs.h b/src/Methods/Structs.h index f1798ddc..531caebd 100644 --- a/src/Methods/Structs.h +++ b/src/Methods/Structs.h @@ -52,4 +52,46 @@ struct ManiaLinkPageAnswer int Result; /**< \brief Answering result. */ }; +//* CurrentCallVote +/** + * \brief Struct with a current callvote. + */ +struct CurrentCallVote +{ + std::string CallerLogin; /**< \brief Player login who created the vote. */ + std::string CmdName; /**< \brief Name of the callvote. */ + std::string CmdParam; /**< \brief Callvote parameter. */ +}; + +//* CallVoteRatio +/** + * \brief Struct with a callvote ratio. + */ +struct CallVoteRatio +{ + std::string Command; /**< \brief Command for the ratio. */ + double Ratio; /**< \brief Vote passing ratio (between 0 and 1, -1 for disable). */ +}; + +//* CallVote +/** + * \brief Struct with a callvote. + */ +struct ExtendedCallVoteRatio +{ + std::string Command; /**< \brief Name of the callvote. */ + std::string Param; /**< \brief Parameter of the callvote. */ + double Ratio; /**< \brief Vote passing ratio (between 0 and 1, -1 for disable). */ +}; + +//* CurrentNextValue +/** + * \brief Struct with a current and next value. + */ +struct CurrentNextValue +{ + int CurrentValue; /**< \brief Current value of the setting. */ + int NextValue; /**< \brief Next value of the setting. */ +}; + #endif // METHODS_STRUCTS_H_ From 18974662679476daead6982da7c33dd8ab016b76 Mon Sep 17 00:00:00 2001 From: Max Klaversma Date: Tue, 3 Jan 2017 01:35:58 +0100 Subject: [PATCH 14/66] README: Mania++ works on "Bash on Ubuntu on Windows". --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e6f2ff90..265995b0 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,9 @@ Mania++ and its sources are available under the [GNU General Public License v3]( * Ubuntu 16.04.1 LTS (Linux 4.4.0-31), 32-bit with gcc 5.4.0 (testing) * Ubuntu 14.04.5 LTS (Linux 4.4.0-51), 64-bit with gcc 4.8.4 (Travis) * Debian 8.6 (Linux 3.16.0-4), 64-bit with gcc 4.9.2 (production) +* "Bash on Ubuntu 14.04.5 LTS on Windows 10" (Linux 4.4.0-51), 64-bit with gcc 4.8.4 (development) -Mania++ is (currently) not compatible with Windows systems and the ```./install.sh```-script requires a Debian-like system to function (with ```apt-get```). +The ```./install.sh```-script requires a Debian-like system to function (with ```apt-get```). ## Requirements ## * C++11 From eee2442c42f4906787e846936ae4b41e7288cf05 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Tue, 3 Jan 2017 13:17:52 +0100 Subject: [PATCH 15/66] Only delete/null SQL connection if it isn't already NULL. --- src/ManiaPP.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index 3ad2d67f..c33bcc5f 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -26,8 +26,11 @@ ManiaPP::~ManiaPP() delete callbacks; callbacks = NULL; delete methods; methods = NULL; - database->close(); - delete database; database = NULL; + if(database != NULL) + { + database->close(); + delete database; database = NULL; + } delete db; db = NULL; } From b1ce4af2c56ff4a4307eb64d4be900a8016255c2 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Tue, 3 Jan 2017 15:53:42 +0100 Subject: [PATCH 16/66] Added (crappy) support for sending structs when calling methods. --- src/GbxRemote/Parameters/GbxParameter.cpp | 21 ++++++++++++++-- src/GbxRemote/Parameters/GbxParameters.h | 30 +++++++++++++++++++++++ src/ManiaPP.cpp | 2 +- src/Methods/Server/CallVote.cpp | 28 ++++++++++++--------- 4 files changed, 67 insertions(+), 14 deletions(-) diff --git a/src/GbxRemote/Parameters/GbxParameter.cpp b/src/GbxRemote/Parameters/GbxParameter.cpp index 830fcef7..3969c8dd 100644 --- a/src/GbxRemote/Parameters/GbxParameter.cpp +++ b/src/GbxRemote/Parameters/GbxParameter.cpp @@ -22,7 +22,11 @@ void GbxParameter::determineType(const std::type_info* param) char* realParamName = abi::__cxa_demangle(param->name(), 0, 0, &status); type = std::string(realParamName); - if(type.find("GbxParameters") != std::string::npos) + if(type.find("GbxStructParameters") != std::string::npos) + { + xmlType = "struct"; + } + else if(type.find("GbxParameters") != std::string::npos) { xmlType = "array"; } @@ -58,12 +62,25 @@ void GbxParameter::determineType(const std::type_info* param) void GbxParameter::dereferenceData(void* pointer) { - if(type.find("GbxParameters") != std::string::npos) + if(type.find("GbxStructParameters") != std::string::npos) + { + GbxStructParameters* paramPtr = static_cast(pointer); + GbxStructParameters parameters = *paramPtr; + std::vector gbxParams = parameters.GetParameters(); + + for(int paramId = 0; paramId < gbxParams.size(); paramId++) + { + if(paramId > 0) data += ""; + data += gbxParams.at(paramId); + } + } + else if(type.find("GbxParameters") != std::string::npos) { data += ""; GbxParameters* paramPtr = static_cast(pointer); GbxParameters parameters = *paramPtr; std::vector gbxParams = parameters.GetParameters(); + for(int paramId = 0; paramId < gbxParams.size(); paramId++) { data += ""; diff --git a/src/GbxRemote/Parameters/GbxParameters.h b/src/GbxRemote/Parameters/GbxParameters.h index 9e76e82b..0b852ac4 100644 --- a/src/GbxRemote/Parameters/GbxParameters.h +++ b/src/GbxRemote/Parameters/GbxParameters.h @@ -3,6 +3,7 @@ #include #include +#include #include //* GbxParam @@ -15,6 +16,35 @@ struct GbxParam const std::type_info* typeinfo; /**< \brief Type information of the parameter. */ }; +//* GbxStructParameters +/** + * \brief List of struct parameters. + */ +class GbxStructParameters +{ +public: + /*! + * \brief Add parameter to the list. + * + * \param text Parameter XML. + */ + void Put(std::string text) + { + parameters.push_back(text); + } + + /*! + * \brief Return the current list of parameters. + */ + std::vector GetParameters() + { + return parameters; + } + +private: + std::vector parameters = std::vector(); /**< \brief List of parameters. */ +}; + //* GbxParameters /** * \brief List of parameters. diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index c33bcc5f..3390a862 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -113,7 +113,7 @@ bool ManiaPP::ConnectToServer() callVoteRatio.Param = "ParamB"; callVoteRatio.Ratio = 0.85; callVoteRatios.push_back(callVoteRatio); - methods->SetCallVoteRatiosEx(true, callVoteRatios); + std::cout << "Test struct response: " << methods->SetCallVoteRatiosEx(true, callVoteRatios) << std::endl; return true; } diff --git a/src/Methods/Server/CallVote.cpp b/src/Methods/Server/CallVote.cpp index fd98475f..b6edcf6a 100644 --- a/src/Methods/Server/CallVote.cpp +++ b/src/Methods/Server/CallVote.cpp @@ -165,26 +165,32 @@ std::vector Methods::GetCallVoteRatios() bool Methods::SetCallVoteRatiosEx(bool replaceAll, std::vector ratios) { - return false; - - /*bool response = false; + bool response = false; GbxParameters params = GbxParameters(); params.Put(&replaceAll); - //params.Put(&ratios); - std::vectorCommand" << ratio.Command << ""; + stringStruct << "Param" << ratio.Param << ""; + stringStruct << "Ratio" << ratio.Ratio << ""; + structRatios.Put(stringStruct.str()); + } + structArray.Put(&structRatios); + params.Put(&structArray); - /*if(server->Query(GbxMessage("SetCallVoteRatiosEx", params))) + if(server->Query(GbxMessage("SetCallVoteRatiosEx", params))) { std::vector responseParams = server->GetResponse()->GetParameters(); std::istringstream(responseParams.at(0).GetString()) >> response; - }* + } - return response;*/ + return response; } std::vector Methods::GetCallVoteRatiosEx() From a15ada3df64766e0f0f359b874d6562ac1d33ec6 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Tue, 3 Jan 2017 17:43:33 +0100 Subject: [PATCH 17/66] Plugins can now use ManiaLinks (example: LocalRecords), to be prettified. --- plugins/LocalRecords/CMakeLists.txt | 1 + .../LocalRecords/src/LocalRecordsPlugin.cpp | 5 + plugins/LocalRecords/src/LocalRecordsPlugin.h | 2 + .../LocalRecords/src/Objects/LocalRecord.h | 4 + .../src/Widget/LocalRecordsWidget.cpp | 73 ++++++++++++++ .../src/Widget/LocalRecordsWidget.h | 97 +++++++++++++++++++ src/ManiaPP.cpp | 15 +-- src/Methods/Methods.cpp | 5 + src/Methods/Methods.h | 6 +- src/Methods/Server/ManiaLink.cpp | 3 +- src/Plugins/PluginManager.cpp | 2 +- src/Utils/Text.h | 39 ++++++++ 12 files changed, 235 insertions(+), 17 deletions(-) create mode 100644 plugins/LocalRecords/src/Widget/LocalRecordsWidget.cpp create mode 100644 plugins/LocalRecords/src/Widget/LocalRecordsWidget.h create mode 100644 src/Utils/Text.h diff --git a/plugins/LocalRecords/CMakeLists.txt b/plugins/LocalRecords/CMakeLists.txt index edabdde3..0e0bc815 100644 --- a/plugins/LocalRecords/CMakeLists.txt +++ b/plugins/LocalRecords/CMakeLists.txt @@ -4,6 +4,7 @@ project (LocalRecordsPlugin) add_definitions(-std=c++11) add_definitions(-Wno-deprecated) add_definitions(-Wl,--export-dynamic) +add_definitions(-rdynamic) set (LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}) link_directories(${PROJECT_SOURCE_DIR}/../../lib/yaml/build) diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp index a6c10e0b..3f175ad1 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp @@ -12,12 +12,17 @@ void LocalRecordsPlugin::Init() { retrieveRecords(*controller->Maps->Current); std::cout << "[ INFO ] " << localRecords.size() << " records found for " << controller->Maps->Current->Name << "." << std::endl; + + widget = LocalRecordsWidget(controller->Server, &localRecords); + widget.DisplayToAll(controller->Players); } void LocalRecordsPlugin::OnBeginMap(Map map) { retrieveRecords(map); std::cout << "[ INFO ] " << localRecords.size() << " records found for " << map.Name << "." << std::endl; + + widget.DisplayToAll(controller->Players); } void LocalRecordsPlugin::retrieveRecords(Map map) diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.h b/plugins/LocalRecords/src/LocalRecordsPlugin.h index 843d5a7d..1f49bb77 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.h +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.h @@ -6,6 +6,7 @@ #include "Utils/Time.h" #include "Objects/LocalRecord.h" +#include "Widget/LocalRecordsWidget.h" class LocalRecordsPlugin : public Plugin { @@ -17,6 +18,7 @@ class LocalRecordsPlugin : public Plugin private: std::vector localRecords; + LocalRecordsWidget widget; void retrieveRecords(Map map); }; diff --git a/plugins/LocalRecords/src/Objects/LocalRecord.h b/plugins/LocalRecords/src/Objects/LocalRecord.h index 6a610e69..2f297b7b 100644 --- a/plugins/LocalRecords/src/Objects/LocalRecord.h +++ b/plugins/LocalRecords/src/Objects/LocalRecord.h @@ -1,6 +1,10 @@ #ifndef LOCALRECORD_H_ #define LOCALRECORD_H_ +#include + +#include "Utils/Time.h" + class LocalRecord { public: diff --git a/plugins/LocalRecords/src/Widget/LocalRecordsWidget.cpp b/plugins/LocalRecords/src/Widget/LocalRecordsWidget.cpp new file mode 100644 index 00000000..f57ef3bc --- /dev/null +++ b/plugins/LocalRecords/src/Widget/LocalRecordsWidget.cpp @@ -0,0 +1,73 @@ +#include "LocalRecordsWidget.h" + +LocalRecordsWidget::LocalRecordsWidget() +{ + +} + +LocalRecordsWidget::LocalRecordsWidget(Methods* serverMethods, std::vector* localRecords) +{ + server = serverMethods; + records = localRecords; +} + +void LocalRecordsWidget::DisplayToAll(std::map* players) +{ + for(std::map::iterator player = players->begin(); player != players->end(); ++player) + { + DisplayToPlayer(player->second); + } +} + +void LocalRecordsWidget::DisplayToPlayer(Player player) +{ + std::stringstream widget; + widget << ""; + widget << ""; + widget << " "; + widget << " "; + + if(!server->SendDisplayManialinkPageToLogin(player.Login, widget.str(), 0, false)) + { + Logging::PrintError(server->GetCurrentError()); + } +} diff --git a/plugins/LocalRecords/src/Widget/LocalRecordsWidget.h b/plugins/LocalRecords/src/Widget/LocalRecordsWidget.h new file mode 100644 index 00000000..b3ed0828 --- /dev/null +++ b/plugins/LocalRecords/src/Widget/LocalRecordsWidget.h @@ -0,0 +1,97 @@ +#ifndef LOCALRECORDSWIDGET_H_ +#define LOCALRECORDSWIDGET_H_ + +#include +#include +#include + +#include "Methods/Methods.h" +#include "../Objects/LocalRecord.h" +#include "Objects/Player.h" +#include "Utils/Logging.h" + +class LocalRecordsWidget +{ +public: + LocalRecordsWidget(); + LocalRecordsWidget(Methods* serverMethods, std::vector* localRecords); + void DisplayToAll(std::map* players); + void DisplayToPlayer(Player player); + +private: + Methods* server; + std::vector* records; + + std::string manialinkId = "LocalRecords"; + std::string actionId = "OpenLocalRecords"; + std::string title = "Local Records"; + + int widgetEntries = 16; + int widgetTopCount = 3; + double widgetWidth = 15.5; + double widgetHeight = ((1.8 * widgetEntries) + 3.2); + double columnHeight = (widgetHeight - 3.1); + double backgroundWidth = (widgetWidth - 0.2); + double backgroundHeight = (widgetHeight - 0.2); + double borderWidth = (widgetWidth + 0.4); + double borderHeight = (widgetHeight + 0.6); + double columnNameWidth = (widgetWidth - 6.45); + + double widgetX = 49.2; + double widgetY = 28.2; + + double left_IconX = 0.6; + double left_IconY = 0; + double left_TitleX = 3.2; + double left_TitleY = -0.65; + std::string left_TitleHalign = "left"; + double left_ImageOpenX = -0.3; + std::string left_ImageOpen = "http://static.undef.name/ingame/records-eyepiece/edge-open-ld-dark.png"; + + double right_IconX = 12.5; + double right_IconY = 0; + double right_TitleX = 12.4; + double right_TitleY = -0.65; + std::string right_TitleHalign = "right"; + double right_ImageOpenX = 12.2; + std::string right_ImageOpen = "http://static.undef.name/ingame/records-eyepiece/edge-open-rd-dark.png"; + + std::string backgroundColor = "3342"; + std::string backgroundFocus = "09F6"; + std::string backgroundRank = "06F5"; + std::string backgroundScore = "09F3"; + std::string backgroundName = "09F1"; + + std::string backgroundStyle = "Bgs1"; + std::string backgroundSubstyle = "BgTitleGlow"; + std::string borderStyle = "Bgs1"; + std::string borderSubstyle = "BgTitleShadow"; + + double imageOpenX = (widgetX < 0) ? (right_ImageOpenX + (widgetWidth - 15.5)) : left_ImageOpenX; + double imageOpenY = -(widgetHeight - 3.18); + std::string imageOpen = (widgetX < 0) ? right_ImageOpen : left_ImageOpen; + + double titleBackgroundWidth = (widgetWidth - 0.8); + std::string titleStyle = "BgsPlayerCard"; + std::string titleSubstyle = "BgRacePlayerName"; + double titleX = (widgetX < 0) ? (right_TitleX + (widgetWidth - 15.5)) : left_TitleX; + double titleY = (widgetX < 0) ? right_TitleY : left_TitleY; + std::string titleHalign = (widgetX < 0) ? right_TitleHalign : left_TitleHalign; + + double iconX = (widgetX < 0) ? (right_IconX + (widgetWidth - 15.5)) : left_IconX; + double iconY = (widgetX < 0) ? right_IconY : left_IconY; + std::string iconStyle = "BgRaceScore2"; + std::string iconSubstyle = "LadderRank"; + + std::string textColor = "FFFF"; + + double topWidth = (widgetWidth - 0.8); + double topHeight = ((widgetTopCount * 1.8) + 0.2); + std::string topStyle = "BgsPlayerCard"; + std::string topSubstyle = "BgCardSystem"; + + double playerIconBoxX = (widgetX < 0) ? widgetWidth : -2; + double playerIconX = (widgetX < 0) ? (widgetWidth + 0.2) : -1.8; +}; + +#endif // LOCALRECORDSWIDGET_H_ diff --git a/src/ManiaPP.cpp b/src/ManiaPP.cpp index 3390a862..0c1a4133 100644 --- a/src/ManiaPP.cpp +++ b/src/ManiaPP.cpp @@ -76,7 +76,7 @@ bool ManiaPP::ConnectToServer() std::cout << "[ \033[0;32mOK.\033[0;0m ] Retrieved system info, server login: '" << systemInfo.ServerLogin << "'." << std::endl; - methods->GetChatLines(); + methods->SendHideManialinkPage(); std::cout << "[ ] Enabling CallBacks ... " << '\r' << std::flush; if(methods->EnableCallbacks(true)) @@ -102,19 +102,6 @@ bool ManiaPP::ConnectToServer() PrintServerInfo(); - std::vector callVoteRatios = std::vector(); - ExtendedCallVoteRatio callVoteRatio = ExtendedCallVoteRatio(); - callVoteRatio.Command = "CommandOne"; - callVoteRatio.Param = "ParamA"; - callVoteRatio.Ratio = 0.5; - callVoteRatios.push_back(callVoteRatio); - callVoteRatio = ExtendedCallVoteRatio(); - callVoteRatio.Command = "CommandTwo"; - callVoteRatio.Param = "ParamB"; - callVoteRatio.Ratio = 0.85; - callVoteRatios.push_back(callVoteRatio); - std::cout << "Test struct response: " << methods->SetCallVoteRatiosEx(true, callVoteRatios) << std::endl; - return true; } else diff --git a/src/Methods/Methods.cpp b/src/Methods/Methods.cpp index eb70072a..5e9840f4 100644 --- a/src/Methods/Methods.cpp +++ b/src/Methods/Methods.cpp @@ -5,3 +5,8 @@ Methods::Methods(GbxRemote* serverPtr, std::map* playerList server = serverPtr; playerList = playerListPtr; } + +GbxError* Methods::GetCurrentError() +{ + return server->GetCurrentError(); +} diff --git a/src/Methods/Methods.h b/src/Methods/Methods.h index 00e043ca..78a3c009 100644 --- a/src/Methods/Methods.h +++ b/src/Methods/Methods.h @@ -4,6 +4,7 @@ #include "../GbxRemote/GbxRemote.h" #include "../Objects/Map.h" #include "../Objects/Player.h" +#include "../Utils/Text.h" #include "Structs.h" //* Methods @@ -16,10 +17,13 @@ class Methods /*! * \brief Constructor with link to server connection. * - * \param serverPtr Pointer to server connection. + * \param serverPtr Pointer to server connection. + * \param playerListPtr Pointer to the playerlist. */ Methods(GbxRemote* serverPtr, std::map* playerListPtr); + GbxError* GetCurrentError(); + /*! * \brief Authenticates with the server. Returns whether authentication was successful. * diff --git a/src/Methods/Server/ManiaLink.cpp b/src/Methods/Server/ManiaLink.cpp index 50b3a0c6..fe507b8c 100644 --- a/src/Methods/Server/ManiaLink.cpp +++ b/src/Methods/Server/ManiaLink.cpp @@ -21,10 +21,11 @@ bool Methods::SendDisplayManialinkPage(std::string page, int timeout, bool hideO bool Methods::SendDisplayManialinkPageToLogin(std::string login, std::string page, int timeout, bool hideOnClick) { bool response = false; + std::string escapedPage = Text::EscapeXML(page); GbxParameters params = GbxParameters(); params.Put(&login); - params.Put(&page); + params.Put(&escapedPage); params.Put(&timeout); params.Put(&hideOnClick); diff --git a/src/Plugins/PluginManager.cpp b/src/Plugins/PluginManager.cpp index a0d168c2..91cf3c40 100644 --- a/src/Plugins/PluginManager.cpp +++ b/src/Plugins/PluginManager.cpp @@ -53,7 +53,7 @@ void PluginManager::LoadPlugins(std::string pluginsFolder) GbxError* error = new GbxError(); error->number = -201; std::stringstream message; - message << "Could not load '" << pluginId->first << "'..."; + message << "Could not load '" << pluginId->first << "': " << dlerror() << "..."; error->message = message.str(); Logging::PrintError(error); } diff --git a/src/Utils/Text.h b/src/Utils/Text.h new file mode 100644 index 00000000..dadedaec --- /dev/null +++ b/src/Utils/Text.h @@ -0,0 +1,39 @@ +#ifndef TEXT_H_ +#define TEXT_H_ + +#include +#include + +//* Text +/** + * \brief Contains utilities to format text. + */ +class Text +{ +public: + /*! + * \brief Escapes XML signs, to send XML strings. + * + * \param src Source XML to be escaped. + */ + static std::string EscapeXML(const std::string& src) + { + std::stringstream dst; + for(char ch : src) + { + switch (ch) + { + case '&': dst << "&"; break; + case '\'': dst << "'"; break; + case '"': dst << """; break; + case '<': dst << "<"; break; + case '>': dst << ">"; break; + default: dst << ch; break; + } + } + + return dst.str(); + } +}; + +#endif // TEXT_H_ From f6903aee5205e473cd762855ab4df9a071581271 Mon Sep 17 00:00:00 2001 From: Max Klaversma Date: Tue, 3 Jan 2017 17:46:50 +0100 Subject: [PATCH 18/66] Added ManiaLink information in README. --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 265995b0..ed31dac2 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ The ```./install.sh```-script requires a Debian-like system to function (with `` ## Aims and working points ## * [Be comparable or better than standard PhpRemote](https://themaximum.github.io/mania-pp/comparison.html) * Working plugin system - * Access to ManiaLink -* ManiaLink support + * Receive settings from configuration file +* Create ManiaLink handler (of some sorts) ## Achieved goals ## * Working GbxRemote module @@ -38,10 +38,11 @@ The ```./install.sh```-script requires a Debian-like system to function (with `` * Create usable objects (f.e. `Player` and `Map`) * Working configuration system (YAML) * Database support +* ManiaLink support * Working plugin system * Plugin interface * Callback handling - * Access to server and database + * Access to server, database, playerlist, maplist and ManiaLink ## Installing for the first time ## * ```./install.sh``` From 84c77f58367a20a4ef6ae4bc499203d2f1cba243 Mon Sep 17 00:00:00 2001 From: TheMaximum Date: Tue, 3 Jan 2017 17:58:16 +0100 Subject: [PATCH 19/66] LocalRecords plugin now listens to widget action. --- plugins/LocalRecords/src/LocalRecordsPlugin.cpp | 9 +++++++++ plugins/LocalRecords/src/LocalRecordsPlugin.h | 1 + plugins/LocalRecords/src/Widget/LocalRecordsWidget.cpp | 2 +- plugins/LocalRecords/src/Widget/LocalRecordsWidget.h | 3 ++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp index 3f175ad1..337610c7 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.cpp +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.cpp @@ -6,6 +6,7 @@ LocalRecordsPlugin::LocalRecordsPlugin() Author = "TheM"; BeginMap.push_back([this](Map map) { OnBeginMap(map); }); + PlayerManialinkPageAnswer.push_back([this](Player player, std::string answer, std::vector entries) { OnPlayerManialinkPageAnswer(player, answer, entries); }); } void LocalRecordsPlugin::Init() @@ -25,6 +26,14 @@ void LocalRecordsPlugin::OnBeginMap(Map map) widget.DisplayToAll(controller->Players); } +void LocalRecordsPlugin::OnPlayerManialinkPageAnswer(Player player, std::string answer, std::vector entries) +{ + if(answer.find(widget.ActionId) != std::string::npos) + { + std::cout << "Player '" << player.Login << "' has clicked the LocalRecords widget (" << answer << ")!" << std::endl; + } +} + void LocalRecordsPlugin::retrieveRecords(Map map) { localRecords = std::vector(); diff --git a/plugins/LocalRecords/src/LocalRecordsPlugin.h b/plugins/LocalRecords/src/LocalRecordsPlugin.h index 1f49bb77..b256dd37 100644 --- a/plugins/LocalRecords/src/LocalRecordsPlugin.h +++ b/plugins/LocalRecords/src/LocalRecordsPlugin.h @@ -15,6 +15,7 @@ class LocalRecordsPlugin : public Plugin void Init(); void OnBeginMap(Map map); + void OnPlayerManialinkPageAnswer(Player player, std::string answer, std::vector entries); private: std::vector localRecords; diff --git a/plugins/LocalRecords/src/Widget/LocalRecordsWidget.cpp b/plugins/LocalRecords/src/Widget/LocalRecordsWidget.cpp index f57ef3bc..4b35a530 100644 --- a/plugins/LocalRecords/src/Widget/LocalRecordsWidget.cpp +++ b/plugins/LocalRecords/src/Widget/LocalRecordsWidget.cpp @@ -25,7 +25,7 @@ void LocalRecordsWidget::DisplayToPlayer(Player player) widget << ""; widget << ""; widget << " "; - widget << "