1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 06:27:45 +00:00

Old db class removed entirely from REST server. Also some cleanups

This commit is contained in:
Mordred Admin 2017-10-26 12:16:34 +02:00
parent f6354736da
commit ff859cebfb
3 changed files with 68 additions and 96 deletions

View file

@ -1,7 +1,6 @@
#include "PlayerMinimal.h" #include "PlayerMinimal.h"
#include <Server_Common/Util/Util.h> #include <Server_Common/Util/Util.h>
#include <Server_Common/Database/Database.h>
#include <Server_Common/Common.h> #include <Server_Common/Common.h>
#include <Server_Common/Exd/ExdData.h> #include <Server_Common/Exd/ExdData.h>
@ -12,7 +11,6 @@
#include "src/libraries/sapphire/mysqlConnector/MySqlConnector.h" #include "src/libraries/sapphire/mysqlConnector/MySqlConnector.h"
extern Core::Db::Database g_database;
extern Core::Db::DbWorkerPool< Core::Db::CharaDbConnection > g_charaDb; extern Core::Db::DbWorkerPool< Core::Db::CharaDbConnection > g_charaDb;
extern Core::Data::ExdData g_exdData; extern Core::Data::ExdData g_exdData;

View file

@ -2,7 +2,6 @@
#include <src/servers/Server_Common/Crypt/base64.h> #include <src/servers/Server_Common/Crypt/base64.h>
#include "Session.h" #include "Session.h"
#include "PlayerMinimal.h" #include "PlayerMinimal.h"
#include <src/servers/Server_Common/Database/Database.h>
#include <time.h> #include <time.h>
#define BOOST_SPIRIT_THREADSAFE #define BOOST_SPIRIT_THREADSAFE
@ -11,9 +10,15 @@
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <Server_Common/Database/DbLoader.h>
#include <Server_Common/Database/CharaDbConnection.h>
#include <Server_Common/Database/DbWorkerPool.h>
#include <Server_Common/Database/PreparedStatement.h>
#include "src/libraries/sapphire/mysqlConnector/MySqlConnector.h"
#include <boost/make_shared.hpp> #include <boost/make_shared.hpp>
extern Core::Db::Database g_database; extern Core::Db::DbWorkerPool< Core::Db::CharaDbConnection > g_charaDb;
Core::Network::SapphireAPI::SapphireAPI() 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 + "';"; 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 // check if a user with that name / password exists
auto pQR = g_database.query( query ); auto pQR = g_charaDb.query( query );
// found? // found?
if( !pQR ) if( !pQR->next() )
return false; return false;
// user found, proceed // user found, proceed
int32_t accountId = pQR->fetch()[0].get< uint32_t >(); uint32_t accountId = pQR->getUInt( 1 );
// session id string generation // session id string generation
srand( ( uint32_t )time( NULL ) + 42 ); 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 ) bool Core::Network::SapphireAPI::createAccount( const std::string& username, const std::string& pass, std::string& sId )
{ {
// get account from login name // get account from login name
auto pQR = g_database.query( "SELECT account_id FROM accounts WHERE account_name = '" + username + "';" ); auto pQR = g_charaDb.query( "SELECT account_id FROM accounts WHERE account_name = '" + username + "';" );
// found?
// if account was found if( !pQR->next() )
if( pQR )
return false; return false;
// we are clear and can create a new account // we are clear and can create a new account
// get the next free account id // get the next free account id
pQR = g_database.query( "SELECT MAX(account_id) FROM accounts;" ); pQR = g_charaDb.query( "SELECT MAX(account_id) FROM accounts;" );
int32_t accountId = pQR->fetch()[0].get< uint32_t >() + 1; if( !pQR->next() )
return false;
uint32_t accountId = pQR->getUInt( 1 ) + 1;
// store the account to the db // store the account to the db
g_database.execute( "INSERT INTO accounts (account_Id, account_name, account_pass, account_created) VALUE( " + g_charaDb.execute( "INSERT INTO accounts (account_Id, account_name, account_pass, account_created) VALUE( " +
std::to_string( accountId ) + ", '" + std::to_string( accountId ) + ", '" +
username + "', '" + username + "', '" +
pass + "', " + pass + "', " +
std::to_string( time( nullptr ) ) + ");"); std::to_string( time( nullptr ) ) + ");");
if( !login( username, pass, sId ) ) 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 ); lookPart = lookPart.substr( 0, pos + 1 );
} }
std::vector<int32_t> tmpVector; std::vector< int32_t > tmpVector;
std::vector<int32_t> tmpVector2; std::vector< int32_t > tmpVector2;
BOOST_FOREACH( boost::property_tree::ptree::value_type &v, pt.get_child( "content" ) ) 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() ) if( !v.second.data().empty() )
tmpVector2.push_back( std::stoi( v.second.data() ) ); tmpVector2.push_back( std::stoi( v.second.data() ) );
} }
std::vector<int32_t>::iterator it = tmpVector.begin(); std::vector< int32_t >::iterator it = tmpVector.begin();
for( int32_t i = 0; it != tmpVector.end(); ++it, i++ ) for( int32_t i = 0; it != tmpVector.end(); ++it, i++ )
{ {
newPlayer.setLook( i, *it ); newPlayer.setLook( i, *it );
@ -190,56 +196,48 @@ void Core::Network::SapphireAPI::deleteCharacter( std::string name, uint32_t acc
int32_t id = deletePlayer.getId(); int32_t id = deletePlayer.getId();
g_database.execute( "DELETE FROM charainfo WHERE CharacterId LIKE '" + std::to_string( id ) + "';" ); g_charaDb.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_charaDb.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_charaDb.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_charaDb.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_charaDb.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_charaDb.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_charaDb.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_charaDb.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_charaDb.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_charaDb.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 charaquest WHERE CharacterId LIKE '" + std::to_string( id ) + "';" );
} }
std::vector<Core::PlayerMinimal> Core::Network::SapphireAPI::getCharList( uint32_t accountId ) std::vector< Core::PlayerMinimal > Core::Network::SapphireAPI::getCharList( uint32_t accountId )
{ {
std::vector<Core::PlayerMinimal> charList; std::vector< Core::PlayerMinimal > charList;
boost::shared_ptr<Core::Db::QueryResult> 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 ) while( pQR->next() )
{
// no chars found for the account
return charList;
}
do
{ {
Core::PlayerMinimal player; Core::PlayerMinimal player;
Core::Db::Field *field = pQR->fetch(); uint32_t charId = pQR->getUInt( 1 );
uint32_t charId = field[0].get< uint32_t >();
player.load( charId ); player.load( charId );
charList.push_back( player ); charList.push_back( player );
}
} while( pQR->nextRow() );
return charList; return charList;
} }
bool Core::Network::SapphireAPI::checkNameTaken( std::string name ) 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; return false;
else else
return true; return true;
@ -247,20 +245,16 @@ bool Core::Network::SapphireAPI::checkNameTaken( std::string name )
uint32_t Core::Network::SapphireAPI::getNextCharId() uint32_t Core::Network::SapphireAPI::getNextCharId()
{ {
int32_t charId = 0; uint32_t charId = 0;
boost::shared_ptr<Core::Db::QueryResult> 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; return 0x00200001;
}
charId = pQR->fetch()[0].get< uint32_t >() + 1; charId = pQR->getUInt( 1 ) + 1;
if( charId < 0x00200001 ) if( charId < 0x00200001 )
{
return 0x00200001; return 0x00200001;
}
return charId; return charId;
} }
@ -269,19 +263,14 @@ uint64_t Core::Network::SapphireAPI::getNextContentId()
{ {
uint64_t contentId = 0; uint64_t contentId = 0;
boost::shared_ptr<Core::Db::QueryResult> 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; return 0x0040000001000001;
}
contentId = pQR->fetch()[0].getUInt64() + 1; contentId = pQR->getUInt64( 1 ) + 1;
if( contentId < 0x0040000001000001 ) if( contentId < 0x0040000001000001 )
{
return 0x0040000001000001; return 0x0040000001000001;
}
return contentId; return contentId;
} }

View file

@ -8,7 +8,6 @@
#include <src/servers/Server_Common/Logging/Logger.h> #include <src/servers/Server_Common/Logging/Logger.h>
#include <src/servers/Server_Common/Config/XMLConfig.h> #include <src/servers/Server_Common/Config/XMLConfig.h>
#include <src/servers/Server_Common/Database/Database.h>
#include <src/servers/Server_Common/Network/Connection.h> #include <src/servers/Server_Common/Network/Connection.h>
#include <src/servers/Server_Common/Network/Hive.h> #include <src/servers/Server_Common/Network/Hive.h>
@ -36,7 +35,6 @@
Core::Logger g_log; Core::Logger g_log;
Core::Db::Database g_database;
Core::Db::DbWorkerPool< Core::Db::CharaDbConnection > g_charaDb; Core::Db::DbWorkerPool< Core::Db::CharaDbConnection > g_charaDb;
Core::Data::ExdData g_exdData; Core::Data::ExdData g_exdData;
Core::Network::SapphireAPI g_sapphireAPI; Core::Network::SapphireAPI g_sapphireAPI;
@ -44,29 +42,31 @@ Core::Network::SapphireAPI g_sapphireAPI;
using namespace std; using namespace std;
using namespace boost::property_tree; using namespace boost::property_tree;
typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer; typedef SimpleWeb::Server< SimpleWeb::HTTP > HttpServer;
typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient; typedef SimpleWeb::Client< SimpleWeb::HTTP > HttpClient;
//Added for the default_resource example //Added for the default_resource example
void default_resource_send( const HttpServer &server, const shared_ptr<HttpServer::Response> &response, void default_resource_send( const HttpServer &server, const shared_ptr< HttpServer::Response > &response,
const shared_ptr<ifstream> &ifs ); const shared_ptr< ifstream > &ifs );
auto m_pConfig = boost::make_shared<Core::XMLConfig>(); auto m_pConfig = boost::make_shared< Core::XMLConfig >();
HttpServer server; HttpServer server;
std::string configPath("config/settings_rest.xml"); std::string configPath( "config/settings_rest.xml" );
void reloadConfig() void reloadConfig()
{ {
m_pConfig = boost::make_shared<Core::XMLConfig>(); m_pConfig = boost::make_shared< Core::XMLConfig >();
if (!m_pConfig->loadConfig(configPath)) if( !m_pConfig->loadConfig( configPath ) )
throw "Error loading config "; throw "Error loading config ";
} }
void print_request_info( shared_ptr<HttpServer::Request> request ) { void print_request_info( shared_ptr< HttpServer::Request > request )
{
g_log.info( "Request from " + request->remote_endpoint_address + " (" + request->path + ")" ); g_log.info( "Request from " + request->remote_endpoint_address + " (" + request->path + ")" );
} }
bool loadSettings( int32_t argc, char* argv[] ) bool loadSettings( int32_t argc, char* argv[] )
{ {
g_log.info( "Loading config " + configPath ); g_log.info( "Loading config " + configPath );
@ -162,29 +162,14 @@ bool loadSettings( int32_t argc, char* argv[] )
if( !loader.initDbs() ) if( !loader.initDbs() )
return false; return false;
Core::Db::DatabaseParams params; server.config.port = static_cast< uint16_t >( std::stoul( m_pConfig->getValue< std::string >( "Settings.General.HttpPort", "80" ) ) );
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< unsigned short >( std::stoul( m_pConfig->getValue<std::string>( "Settings.General.HttpPort", "80" ) ) ); g_log.info( "Database: Connected to " + info.host + ":" + std::to_string( info.port ) );
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));
return true; return true;
} }
int main(int argc, char* argv[]) int main( int argc, char* argv[] )
{ {
g_log.setLogPath( "log\\SapphireAPI" ); g_log.setLogPath( "log\\SapphireAPI" );
g_log.init(); g_log.init();