Skip to content

Commit

Permalink
initial implementation of MongoDB interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNen committed Jan 8, 2024
1 parent 26dce37 commit e92baa0
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 13 deletions.
66 changes: 59 additions & 7 deletions include/Core/Database.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,73 @@
#pragma once

#include <string>

#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include "API/Types/AccountId.hpp"

template <typename T>
concept MongoSupportedType = std::is_fundamental_v<T> || std::is_same_v<T, std::string> || std::is_same_v<T, std::wstring> ||
std::is_same_v<T, std::wstring_view> || std::is_same_v<T, std::string_view>;

class ClientList;
class FLHook;

class Database
{
friend ClientList;
friend FLHook;

Database(const std::string& uri);
~Database();
Database();
~Database() = default;
Database(const Database&&) = delete;

mongocxx::instance instance;
mongocxx::collection accounts;
mongocxx::client client;
mongocxx::database database;

void ResetDatabase();


public:
template <typename T>
requires MongoSupportedType<T>
void AddValueToCharacter(std::string character, std::pair<std::string, T> val);

template <typename T>
requires MongoSupportedType<T>
void AddValueToAccount(AccountId account, std::pair<std::string, T> val);

void RemoveValueFromCharacter(std::string character, std::string value);
void RemoveValueFromAccount(AccountId account, std::string value);
};

template <typename T>
requires MongoSupportedType<T>
void Database::AddValueToCharacter(std::string character, std::pair<std::string, T> val)
{
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;

auto searchDoc = make_document(kvp("character_name", character));
auto updateDoc = make_document(kvp("$set", make_document(kvp(val.first, val.second))));

accounts.update_one(searchDoc.view(), updateDoc.view());
}

template <typename T>
requires MongoSupportedType<T>
void Database::AddValueToAccount(AccountId account, std::pair<std::string, T> val)
{
const auto cAcc = account.GetValue();
auto key = StringUtils::wstos(cAcc->accId);

using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;

auto searchDoc = make_document(kvp("_id", key));
auto updateDoc = make_document(kvp("$set", make_document(kvp(val.first, val.second))));

accounts.update_one(searchDoc.view(), updateDoc.view());
}
3 changes: 3 additions & 0 deletions include/Core/FLHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Logger;
class TempBanManager;
class FlPacket;
class PersonalityHelper;
class Database;

class FLHook final
{
Expand Down Expand Up @@ -95,6 +96,7 @@ class FLHook final

StartupCache* startupCache;
ClientList* clientList;
Database* database;
InfocardManager* infocardManager;
TempBanManager* tempbanManager;
PersonalityHelper* personalityHelper;
Expand Down Expand Up @@ -159,6 +161,7 @@ class FLHook final
static bool GetShipInspect(uint& ship, IObjInspectImpl*& inspect, uint& dunno) { return getShipInspect(ship, inspect, dunno); }

static ClientList& Clients() { return *instance->clientList; }
static Database& GetDatabase() { return *instance->database; }
static InfocardManager& GetInfocardManager() { return *instance->infocardManager; }
static TempBanManager& GetTempBanManager() { return *instance->tempbanManager; }
static LastHitInformation GetLastHitInformation() { return { nonGunHitsBase, lastHitPts, dmgToClient, dmgToSpaceId }; }
Expand Down
10 changes: 9 additions & 1 deletion include/Defs/FLHookConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ struct DLL FLHookConfig final : Singleton<FLHookConfig>
Serialize(Callsign, allowedFormations, disableRandomisedFormations, disableUsingAffiliationForCallsign);
};

struct DatabaseConfig final
{
std::string uri;

Serialize(DatabaseConfig, uri);
};

Debug debug;
General general;
AutoKicks autoKicks;
Expand All @@ -215,6 +222,7 @@ struct DLL FLHookConfig final : Singleton<FLHookConfig>
Bans bans;
ChatConfig chatConfig;
Callsign callsign;
DatabaseConfig databaseConfig;

Serialize(FLHookConfig, debug, general, autoKicks, plugins, messageQueue, userCommands, bans, chatConfig, callsign);
Serialize(FLHookConfig, debug, general, autoKicks, plugins, messageQueue, userCommands, bans, chatConfig, callsign, databaseConfig);
};
45 changes: 40 additions & 5 deletions source/Core/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,58 @@
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_array;
using bsoncxx::builder::basic::make_document;
using bsoncxx::builder::stream::document;

Database::Database(const std::string& uri)
//TODO: MongoDB/Mongocxx and wstring support. Mainly it doesn't support wstring as values and look into it.
Database::Database()
{
try
{
const auto mongoURI = mongocxx::uri{ uri };
const auto mongoURI = mongocxx::uri{ FLHookConfig::i()->databaseConfig.uri };
mongocxx::options::client clientOptions;
//TODO: Why does this server_api object not exist? It is on both the spike and documentation.
const auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1 };


client = { mongoURI, clientOptions };
database = client["flhook"];
accounts = database["accounts"];
}

catch (std::exception& err)
{
Logger::Log(LogLevel::Err, StringUtils::stows(std::string(err.what())));

}

}

void Database::ResetDatabase()
{
accounts.drop();
}

void Database::RemoveValueFromCharacter(std::string character, std::string value)
{
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;

auto searchDoc = make_document(kvp("character_name", character));
auto updateDoc = make_document(kvp("$unset", make_document(kvp(value, 0))));

accounts.update_one(searchDoc.view(), updateDoc.view());

}

void Database::RemoveValueFromAccount(AccountId account, std::string value)
{
const auto cAcc = account.GetValue();
auto key = StringUtils::wstos(cAcc->accId);

using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;

auto searchDoc = make_document(kvp("_id", key));
auto updateDoc = make_document(kvp("$unset", make_document(kvp(value, 0))));

accounts.update_one(searchDoc.view(), updateDoc.view());


}
5 changes: 5 additions & 0 deletions source/FLHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Core/FLHook.hpp"

#include "API/FLHook/ClientList.hpp"
#include "Core/Database.hpp"
#include "API/FLHook/InfocardManager.hpp"
#include "API/FLHook/PersonalityHelper.hpp"
#include "API/InternalApi.hpp"
Expand Down Expand Up @@ -79,12 +80,16 @@ FLHook::FLHook()
// Load our settings before anything that might need access to debug mode
LoadSettings();


// Replace the global exception filter with our own so we can write exception dumps
// ExceptionHandler::SetupExceptionHandling();

// Setup needed debug tools
DebugTools::Init();

//Initialize the Database before everything as other systems rely on it
database = new Database;

// Init our message service, this is a blocking call and some plugins might want to setup their own queues,
// so we want to make sure the service is up at startup time
if (FLHookConfig::i()->messageQueue.enableQueues)
Expand Down

0 comments on commit e92baa0

Please sign in to comment.