mirror of
https://github.com/HikikoMarmy/Champions-Reborn-Server.git
synced 2026-04-05 00:49:48 -03:00
Reorganized and cleaned up the solution.
This commit is contained in:
92
Include/Database/Database.hpp
Normal file
92
Include/Database/Database.hpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
|
||||
#include <sqlite3.h>
|
||||
#include "Transaction.hpp"
|
||||
#include "Game/RealmCharacter.hpp"
|
||||
#include "Game/RealmCharacterMetaKV.hpp"
|
||||
|
||||
enum class QueryID {
|
||||
CreateAccount,
|
||||
VerifyAccount,
|
||||
LoadAccount,
|
||||
|
||||
LoadCharacterSlots,
|
||||
CreateNewCharacter,
|
||||
SaveCharacter,
|
||||
LoadCharacter,
|
||||
|
||||
SaveFriend,
|
||||
RemoveFriend,
|
||||
LoadFriendList,
|
||||
|
||||
SaveIgnore,
|
||||
RemoveIgnore,
|
||||
LoadIgnoreList,
|
||||
};
|
||||
|
||||
class Database {
|
||||
private:
|
||||
static inline std::unique_ptr<Database> m_instance;
|
||||
static inline std::mutex m_mutex;
|
||||
|
||||
sqlite3 *m_db = nullptr;
|
||||
std::unordered_map< QueryID, sqlite3_stmt * > m_statements;
|
||||
|
||||
public:
|
||||
static Database &Get()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock( m_mutex );
|
||||
if( !m_instance )
|
||||
m_instance.reset( new Database() );
|
||||
return *m_instance;
|
||||
}
|
||||
|
||||
Database( const Database & ) = delete;
|
||||
Database &operator=( const Database & ) = delete;
|
||||
|
||||
Database();
|
||||
~Database();
|
||||
|
||||
public:
|
||||
int64_t CreateNewAccount( const std::string &username,
|
||||
const std::string &password,
|
||||
const std::string &email_address,
|
||||
const std::string &date_of_birth,
|
||||
const std::string &chat_handle );
|
||||
|
||||
std::tuple< bool, int64_t, std::wstring > VerifyAccount( const std::wstring &username, const std::wstring &password );
|
||||
|
||||
uint32_t CreateNewCharacter( const int64_t account_id,
|
||||
const CharacterSlotData meta,
|
||||
const std::vector< uint8_t > &blob );
|
||||
|
||||
bool SaveCharacter( const int64_t account_id,
|
||||
const int32_t character_id,
|
||||
const CharacterSlotData meta,
|
||||
const std::vector< uint8_t > &blob );
|
||||
|
||||
std::map< uint32_t, CharacterSlotData > LoadCharacterSlots( const int64_t account_id );
|
||||
|
||||
sptr_realm_character LoadCharacterData( const int64_t account_id, const int32_t character_id );
|
||||
|
||||
bool SaveFriend( const int64_t account_id, const std::wstring &friend_handle );
|
||||
bool RemoveFriend( const int64_t account_id, const std::wstring &friend_handle );
|
||||
std::vector< std::wstring > LoadFriends( const int64_t account_id );
|
||||
|
||||
bool SaveIgnore( const int64_t account_id, const std::wstring &ignore_handle );
|
||||
bool RemoveIgnore( const int64_t account_id, const std::wstring &ignore_handle );
|
||||
std::vector< std::wstring > LoadIgnores( const int64_t account_id );
|
||||
|
||||
private:
|
||||
void CreateTables();
|
||||
void PrepareStatements();
|
||||
void FinalizeStatements();
|
||||
void Execute( const char *sql );
|
||||
};
|
||||
Reference in New Issue
Block a user