mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-10-10 09:28:06 +00:00
WIP
no crashes
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <cstdint>
|
||||
|
||||
// CPPLinq
|
||||
#ifdef _WIN32
|
||||
|
@@ -21,12 +21,13 @@
|
||||
#include "ICharInfo.h"
|
||||
#include "IAccounts.h"
|
||||
#include "IActivityLog.h"
|
||||
|
||||
#include "IGuilds.h"
|
||||
#include "IGuildMembers.h"
|
||||
namespace sql {
|
||||
class Statement;
|
||||
class PreparedStatement;
|
||||
};
|
||||
|
||||
#define _DEBUG
|
||||
#ifdef _DEBUG
|
||||
# define DLU_SQL_TRY_CATCH_RETHROW(x) do { try { x; } catch (sql::SQLException& ex) { LOG("SQL Error: %s", ex.what()); throw; } } while(0)
|
||||
#else
|
||||
@@ -38,7 +39,7 @@ class GameDatabase :
|
||||
public IMail, public ICommandLog, public IPlayerCheatDetections, public IBugReports,
|
||||
public IPropertyContents, public IProperty, public IPetNames, public ICharXml,
|
||||
public IMigrationHistory, public IUgc, public IFriends, public ICharInfo,
|
||||
public IAccounts, public IActivityLog {
|
||||
public IAccounts, public IActivityLog, public IGuilds, public IGuildMembers {
|
||||
public:
|
||||
virtual ~GameDatabase() = default;
|
||||
// TODO: These should be made private.
|
||||
|
20
dDatabase/GameDatabase/ITables/IGuildMembers.h
Normal file
20
dDatabase/GameDatabase/ITables/IGuildMembers.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef __IGUILDMEMBERS__H__
|
||||
#define __IGUILDMEMBERS__H__
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
|
||||
class IGuildMembers {
|
||||
public:
|
||||
struct GuildMember {
|
||||
uint32_t id;
|
||||
uint16_t rank;
|
||||
};
|
||||
virtual void InsertGuildMember(const uint32_t guild_id, const uint32_t member_id, const uint16_t rank = 4) = 0;
|
||||
virtual void DeleteGuildMember(const uint32_t member_id) = 0;
|
||||
virtual uint32_t GetMembersGuild(const uint32_t member_id) = 0;
|
||||
virtual std::vector<GuildMember> GetGuildMembers(const uint32_t guild_id) = 0;
|
||||
virtual bool CheckIsInGuild(const uint32_t guild_id, const uint32_t character_id) = 0;
|
||||
};
|
||||
|
||||
#endif //!__IGUILDMEMBERS__H__
|
25
dDatabase/GameDatabase/ITables/IGuilds.h
Normal file
25
dDatabase/GameDatabase/ITables/IGuilds.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef __IGUILDS__H__
|
||||
#define __IGUILDS__H__
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
class IGuilds {
|
||||
public:
|
||||
struct Guild {
|
||||
uint32_t id;
|
||||
uint32_t owner_id;
|
||||
std::string name;
|
||||
std::string motd;
|
||||
int64_t reputation;
|
||||
};
|
||||
virtual std::optional<Guild> CreateGuild(const std::string_view name, const int32_t owner_id, const uint64_t reputation) = 0;
|
||||
virtual std::optional<Guild> GetGuild(const uint32_t guild_id) = 0;
|
||||
virtual std::optional<Guild> GetGuildByName(const std::string_view name) = 0;
|
||||
virtual bool CheckGuildNameExists(const std::string_view name) = 0;
|
||||
virtual void SetMOTD(const uint32_t guild_id, const std::string_view motd) = 0;
|
||||
virtual void DeleteGuild(const uint32_t guild_id) = 0;
|
||||
};
|
||||
|
||||
#endif //!__IGUILDS__H__
|
@@ -103,6 +103,17 @@ public:
|
||||
std::optional<uint32_t> GetDonationTotal(const uint32_t activityId) override;
|
||||
std::optional<bool> IsPlaykeyActive(const int32_t playkeyId) override;
|
||||
std::vector<IUgc::Model> GetUgcModels(const LWOOBJID& propertyId) override;
|
||||
std::optional<IGuilds::Guild> CreateGuild(const std::string_view name, const int32_t owner_id, const uint64_t reputation) override;
|
||||
std::optional<IGuilds::Guild> GetGuild(const uint32_t guild_id) override;
|
||||
std::optional<IGuilds::Guild> GetGuildByName(const std::string_view name) override;
|
||||
bool CheckGuildNameExists(const std::string_view name) override;
|
||||
void SetMOTD(const uint32_t guild_id, const std::string_view motd) override;
|
||||
void DeleteGuild(const uint32_t guild_id) override;
|
||||
void InsertGuildMember(const uint32_t guild_id, const uint32_t member_id, const uint16_t rank) override;
|
||||
void DeleteGuildMember(const uint32_t member_id) override;
|
||||
uint32_t GetMembersGuild(const uint32_t member_id) override;
|
||||
std::vector<GuildMember> GetGuildMembers(const uint32_t guild_id) override;
|
||||
bool CheckIsInGuild(const uint32_t guild_id, const uint32_t character_id) override;
|
||||
private:
|
||||
|
||||
// Generic query functions that can be used for any query.
|
||||
|
@@ -6,6 +6,8 @@ set(DDATABASES_DATABASES_MYSQL_TABLES_SOURCES
|
||||
"CharXml.cpp"
|
||||
"CommandLog.cpp"
|
||||
"Friends.cpp"
|
||||
"Guilds.cpp"
|
||||
"GuildMembers.cpp"
|
||||
"Leaderboard.cpp"
|
||||
"Mail.cpp"
|
||||
"MigrationHistory.cpp"
|
||||
|
35
dDatabase/GameDatabase/MySQL/Tables/GuildMembers.cpp
Normal file
35
dDatabase/GameDatabase/MySQL/Tables/GuildMembers.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "MySQLDatabase.h"
|
||||
|
||||
void MySQLDatabase::InsertGuildMember(const uint32_t guild_id, const uint32_t member_id, const uint16_t rank){
|
||||
ExecuteInsert("INSERT INTO guild_members (guild_id, character_id, rank) VALUES (?, ?, ?);", guild_id, member_id, rank);
|
||||
}
|
||||
|
||||
void MySQLDatabase::DeleteGuildMember(const uint32_t member_id){
|
||||
ExecuteDelete("DELETE FROM guild_members WHERE character_id = ?;", member_id);
|
||||
}
|
||||
|
||||
uint32_t MySQLDatabase::GetMembersGuild(const uint32_t member_id){
|
||||
auto res = ExecuteSelect("SELECT * FROM guild_members WHERE character_id = ?;", member_id);
|
||||
if (!res->next()) return 0;
|
||||
return res->getUInt("guild_id");
|
||||
}
|
||||
|
||||
std::vector<IGuildMembers::GuildMember> MySQLDatabase::GetGuildMembers(const uint32_t guild_id){
|
||||
auto res = ExecuteSelect("SELECT * FROM guild_members WHERE guild_id = ?;", guild_id);
|
||||
std::vector<GuildMember> toReturn;
|
||||
toReturn.reserve(res->rowsCount());
|
||||
|
||||
while (res->next()) {
|
||||
GuildMember member;
|
||||
member.id = res->getUInt("character_id");
|
||||
member.rank = res->getUInt("rank");
|
||||
toReturn.push_back(member);
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
bool MySQLDatabase::CheckIsInGuild(const uint32_t guild_id, const uint32_t character_id) {
|
||||
auto res = ExecuteSelect("SELECT * FROM guild_members WHERE guild_id = ? AND character_id = ?;", guild_id, character_id);
|
||||
if (res->next()) return true;
|
||||
return false;
|
||||
}
|
46
dDatabase/GameDatabase/MySQL/Tables/Guilds.cpp
Normal file
46
dDatabase/GameDatabase/MySQL/Tables/Guilds.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "MySQLDatabase.h"
|
||||
|
||||
std::optional<IGuilds::Guild> MySQLDatabase::CreateGuild(const std::string_view name, const int32_t owner_id, const uint64_t reputation) {
|
||||
ExecuteInsert("INSERT INTO guilds (name, owner_id, reputation) VALUES (?, ?, ?);", name, owner_id, reputation);
|
||||
return GetGuildByName(name);
|
||||
}
|
||||
|
||||
std::optional<IGuilds::Guild> MySQLDatabase::GetGuild(const uint32_t guild_id) {
|
||||
auto return_res = ExecuteSelect("SELECT * from guilds where id = ?", guild_id);
|
||||
if (!return_res->next()) return std::nullopt;
|
||||
|
||||
IGuilds::Guild toReturn;
|
||||
toReturn.id = return_res->getInt64("id");
|
||||
toReturn.owner_id = return_res->getUInt("owner_id");
|
||||
toReturn.name = return_res->getString("name").c_str();
|
||||
toReturn.motd = return_res->getString("motd").c_str();
|
||||
toReturn.reputation = return_res->getUInt64("reputation");
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
std::optional<IGuilds::Guild> MySQLDatabase::GetGuildByName(const std::string_view name) {
|
||||
auto return_res = ExecuteSelect("SELECT * from guilds where name = ?", name);
|
||||
if (!return_res->next()) return std::nullopt;
|
||||
|
||||
IGuilds::Guild toReturn;
|
||||
toReturn.id = return_res->getUInt("id");
|
||||
toReturn.owner_id = return_res->getUInt("owner_id");
|
||||
toReturn.name = return_res->getString("name").c_str();
|
||||
toReturn.motd = return_res->getString("motd").c_str();
|
||||
toReturn.reputation = return_res->getUInt64("reputation");
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
bool MySQLDatabase::CheckGuildNameExists(const std::string_view name) {
|
||||
auto res = ExecuteSelect("SELECT * FROM guilds WHERE name = ?;", name);
|
||||
return res->next();
|
||||
}
|
||||
void MySQLDatabase::SetMOTD(const uint32_t guild_id, const std::string_view motd) {
|
||||
auto res = ExecuteUpdate("Update guilds SET motd = ? WHERE id = ?;", motd, guild_id);
|
||||
if (res != 1) throw res;
|
||||
}
|
||||
|
||||
void MySQLDatabase::DeleteGuild(const uint32_t guild_id) {
|
||||
ExecuteDelete("DELETE FROM guilds where id = ?;", guild_id);
|
||||
}
|
Reference in New Issue
Block a user