diff --git a/src/servers/Server_REST/PlayerMinimal.cpp b/src/servers/Server_REST/PlayerMinimal.cpp index d6a34295..f80d1d86 100644 --- a/src/servers/Server_REST/PlayerMinimal.cpp +++ b/src/servers/Server_REST/PlayerMinimal.cpp @@ -1,7 +1,6 @@ #include "PlayerMinimal.h" #include -#include #include #include @@ -12,7 +11,6 @@ #include "src/libraries/sapphire/mysqlConnector/MySqlConnector.h" -extern Core::Db::Database g_database; extern Core::Db::DbWorkerPool< Core::Db::CharaDbConnection > g_charaDb; extern Core::Data::ExdData g_exdData; diff --git a/src/servers/Server_REST/SapphireAPI.cpp b/src/servers/Server_REST/SapphireAPI.cpp index ba5fdd03..f4957847 100644 --- a/src/servers/Server_REST/SapphireAPI.cpp +++ b/src/servers/Server_REST/SapphireAPI.cpp @@ -2,7 +2,6 @@ #include #include "Session.h" #include "PlayerMinimal.h" -#include #include #define BOOST_SPIRIT_THREADSAFE @@ -11,9 +10,15 @@ #include #include #include +#include +#include +#include +#include + +#include "src/libraries/sapphire/mysqlConnector/MySqlConnector.h" #include -extern Core::Db::Database g_database; +extern Core::Db::DbWorkerPool< Core::Db::CharaDbConnection > g_charaDb; Core::Network::SapphireAPI::SapphireAPI() { @@ -30,13 +35,13 @@ bool Core::Network::SapphireAPI::login( const std::string& username, const std:: std::string query = "SELECT account_id FROM accounts WHERE account_name = '" + username + "' AND account_pass = '" + pass + "';"; // check if a user with that name / password exists - auto pQR = g_database.query( query ); + auto pQR = g_charaDb.query( query ); // found? - if( !pQR ) + if( !pQR->next() ) return false; // user found, proceed - int32_t accountId = pQR->fetch()[0].get< uint32_t >(); + uint32_t accountId = pQR->getUInt( 1 ); // session id string generation srand( ( uint32_t )time( NULL ) + 42 ); @@ -87,23 +92,24 @@ bool Core::Network::SapphireAPI::insertSession( const uint32_t& accountId, std:: bool Core::Network::SapphireAPI::createAccount( const std::string& username, const std::string& pass, std::string& sId ) { // get account from login name - auto pQR = g_database.query( "SELECT account_id FROM accounts WHERE account_name = '" + username + "';" ); - - // if account was found - if( pQR ) + auto pQR = g_charaDb.query( "SELECT account_id FROM accounts WHERE account_name = '" + username + "';" ); + // found? + if( !pQR->next() ) return false; // we are clear and can create a new account // get the next free account id - pQR = g_database.query( "SELECT MAX(account_id) FROM accounts;" ); - int32_t accountId = pQR->fetch()[0].get< uint32_t >() + 1; + pQR = g_charaDb.query( "SELECT MAX(account_id) FROM accounts;" ); + if( !pQR->next() ) + return false; + uint32_t accountId = pQR->getUInt( 1 ) + 1; // store the account to the db - g_database.execute( "INSERT INTO accounts (account_Id, account_name, account_pass, account_created) VALUE( " + - std::to_string( accountId ) + ", '" + - username + "', '" + - pass + "', " + - std::to_string( time( nullptr ) ) + ");"); + g_charaDb.execute( "INSERT INTO accounts (account_Id, account_name, account_pass, account_created) VALUE( " + + std::to_string( accountId ) + ", '" + + username + "', '" + + pass + "', " + + std::to_string( time( nullptr ) ) + ");"); if( !login( username, pass, sId ) ) @@ -138,8 +144,8 @@ int Core::Network::SapphireAPI::createCharacter( const int& accountId, const std lookPart = lookPart.substr( 0, pos + 1 ); } - std::vector tmpVector; - std::vector tmpVector2; + std::vector< int32_t > tmpVector; + std::vector< int32_t > tmpVector2; BOOST_FOREACH( boost::property_tree::ptree::value_type &v, pt.get_child( "content" ) ) { @@ -153,7 +159,7 @@ int Core::Network::SapphireAPI::createCharacter( const int& accountId, const std if( !v.second.data().empty() ) tmpVector2.push_back( std::stoi( v.second.data() ) ); } - std::vector::iterator it = tmpVector.begin(); + std::vector< int32_t >::iterator it = tmpVector.begin(); for( int32_t i = 0; it != tmpVector.end(); ++it, i++ ) { newPlayer.setLook( i, *it ); @@ -190,56 +196,48 @@ void Core::Network::SapphireAPI::deleteCharacter( std::string name, uint32_t acc int32_t id = deletePlayer.getId(); - g_database.execute( "DELETE FROM charainfo WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM characlass WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM charaglobalitem WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM charainfoblacklist WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM charainfofriendlist WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM charainfolinkshell WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM charainfosearch WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM charaitemcrystal WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM charaiteminventory WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM charaitemgearset WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); - g_database.execute( "DELETE FROM charaquest WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charainfo WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM characlass WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charaglobalitem WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charainfoblacklist WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charainfofriendlist WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charainfolinkshell WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charainfosearch WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charaitemcrystal WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charaiteminventory WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charaitemgearset WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); + g_charaDb.execute( "DELETE FROM charaquest WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); } -std::vector Core::Network::SapphireAPI::getCharList( uint32_t accountId ) +std::vector< Core::PlayerMinimal > Core::Network::SapphireAPI::getCharList( uint32_t accountId ) { - std::vector charList; + std::vector< Core::PlayerMinimal > charList; - boost::shared_ptr pQR = g_database.query( "SELECT CharacterId, ContentId FROM charainfo WHERE AccountId = " + std::to_string( accountId ) + ";" ); + auto pQR = g_charaDb.query( "SELECT CharacterId, ContentId FROM charainfo WHERE AccountId = " + std::to_string( accountId ) + ";" ); - if( !pQR ) - { - // no chars found for the account - return charList; - } - - do + while( pQR->next() ) { Core::PlayerMinimal player; - Core::Db::Field *field = pQR->fetch(); - - uint32_t charId = field[0].get< uint32_t >(); + uint32_t charId = pQR->getUInt( 1 ); player.load( charId ); charList.push_back( player ); - - } while( pQR->nextRow() ); - + } return charList; } bool Core::Network::SapphireAPI::checkNameTaken( std::string name ) { - std::string query = "SELECT * FROM charainfo WHERE Name = '" + g_database.escapeString( name ) + "';"; + + g_charaDb.escapeString( name ); + std::string query = "SELECT * FROM charainfo WHERE Name = '" + name + "';"; - auto pQR = g_database.query( query ); + auto pQR = g_charaDb.query( query ); - if( !pQR ) + if( !pQR->next() ) return false; else return true; @@ -247,20 +245,16 @@ bool Core::Network::SapphireAPI::checkNameTaken( std::string name ) uint32_t Core::Network::SapphireAPI::getNextCharId() { - int32_t charId = 0; + uint32_t charId = 0; - boost::shared_ptr pQR = g_database.query( "SELECT MAX(CharacterId) FROM charainfo" ); + auto pQR = g_charaDb.query( "SELECT MAX(CharacterId) FROM charainfo" ); - if( !pQR ) - { + if( !pQR->next() ) return 0x00200001; - } - charId = pQR->fetch()[0].get< uint32_t >() + 1; + charId = pQR->getUInt( 1 ) + 1; if( charId < 0x00200001 ) - { return 0x00200001; - } return charId; } @@ -269,19 +263,14 @@ uint64_t Core::Network::SapphireAPI::getNextContentId() { uint64_t contentId = 0; - boost::shared_ptr pQR = g_database.query( "SELECT MAX(ContentId) FROM charainfo" ); + auto pQR = g_charaDb.query( "SELECT MAX(ContentId) FROM charainfo" ); - if( !pQR ) - { + if( !pQR->next() ) return 0x0040000001000001; - } - contentId = pQR->fetch()[0].getUInt64() + 1; + contentId = pQR->getUInt64( 1 ) + 1; if( contentId < 0x0040000001000001 ) - { return 0x0040000001000001; - } - return contentId; } diff --git a/src/servers/Server_REST/main.cpp b/src/servers/Server_REST/main.cpp index e03a32d1..288228d5 100644 --- a/src/servers/Server_REST/main.cpp +++ b/src/servers/Server_REST/main.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -36,7 +35,6 @@ Core::Logger g_log; -Core::Db::Database g_database; Core::Db::DbWorkerPool< Core::Db::CharaDbConnection > g_charaDb; Core::Data::ExdData g_exdData; Core::Network::SapphireAPI g_sapphireAPI; @@ -44,29 +42,31 @@ Core::Network::SapphireAPI g_sapphireAPI; using namespace std; using namespace boost::property_tree; -typedef SimpleWeb::Server HttpServer; -typedef SimpleWeb::Client HttpClient; +typedef SimpleWeb::Server< SimpleWeb::HTTP > HttpServer; +typedef SimpleWeb::Client< SimpleWeb::HTTP > HttpClient; //Added for the default_resource example -void default_resource_send( const HttpServer &server, const shared_ptr &response, - const shared_ptr &ifs ); +void default_resource_send( const HttpServer &server, const shared_ptr< HttpServer::Response > &response, + const shared_ptr< ifstream > &ifs ); -auto m_pConfig = boost::make_shared(); +auto m_pConfig = boost::make_shared< Core::XMLConfig >(); HttpServer server; -std::string configPath("config/settings_rest.xml"); +std::string configPath( "config/settings_rest.xml" ); void reloadConfig() { - m_pConfig = boost::make_shared(); + m_pConfig = boost::make_shared< Core::XMLConfig >(); - if (!m_pConfig->loadConfig(configPath)) + if( !m_pConfig->loadConfig( configPath ) ) throw "Error loading config "; } -void print_request_info( shared_ptr request ) { +void print_request_info( shared_ptr< HttpServer::Request > request ) +{ g_log.info( "Request from " + request->remote_endpoint_address + " (" + request->path + ")" ); } + bool loadSettings( int32_t argc, char* argv[] ) { g_log.info( "Loading config " + configPath ); @@ -162,29 +162,14 @@ bool loadSettings( int32_t argc, char* argv[] ) if( !loader.initDbs() ) return false; - Core::Db::DatabaseParams params; - params.bufferSize = 16384; - params.connectionCount = 3; - params.databaseName = m_pConfig->getValue< std::string >( "Settings.General.Mysql.Database", "sapphire" ); - params.hostname = m_pConfig->getValue< std::string >( "Settings.General.Mysql.Host", "127.0.0.1" ); - params.password = m_pConfig->getValue< std::string >( "Settings.General.Mysql.Pass", "" ); - params.port = m_pConfig->getValue< uint16_t >( "Settings.General.Mysql.Port", 3306 ); - params.username = m_pConfig->getValue< std::string >( "Settings.General.Mysql.Username", "root" ); + server.config.port = static_cast< uint16_t >( std::stoul( m_pConfig->getValue< std::string >( "Settings.General.HttpPort", "80" ) ) ); - server.config.port = static_cast< unsigned short >( std::stoul( m_pConfig->getValue( "Settings.General.HttpPort", "80" ) ) ); - - if( !g_database.initialize( params ) ) - { - std::this_thread::sleep_for( std::chrono::milliseconds( 5000 ) ); - return false; - } - - g_log.info("Database: Connected to " + params.hostname + ":" + std::to_string(params.port)); + g_log.info( "Database: Connected to " + info.host + ":" + std::to_string( info.port ) ); return true; } -int main(int argc, char* argv[]) +int main( int argc, char* argv[] ) { g_log.setLogPath( "log\\SapphireAPI" ); g_log.init();