eqemu-server/common/repositories/base/base_faction_association_repository.h
Michael Cook (mackal) 2b4e555eae
[Feature] Faction Association (#2408)
* Add faction logging category

Probably should use this for more things

* Add FactionAssociation struct

This is simply just a struct that contains an array of faction ids and
multiplier. This can hold a maximum of 10 entries (Seru hit is 8, so 2
extra) this can be raised if need be.

* Add database changes and other data point changes

This is all the database changes and loading changes

Included is an optional SQL that will be used as a starting point, there
is likely errors or typos, but we will fix those as they are discovered.

* Add Client::RewardFaction function

This just takes the faction ID and the magnitude of the primary faction
hit and calculates the rest.

The minimum change will be either 1 or -1. We stop processing after we
see an ID of 0 and assume there will be no later entries.

The primary faction ID will always receive a hit even if there is no
faction association entries

* Add users of RewardFaction to NPC death, tasks, and QuestRewards

This will only use the new system if the magnitude is set, otherwise we
will just use the old system still

* Add quest system calls and lua QuestReward support

* Add #factionassociation command

This just calls RewardFaction, mostly useful for debugging
2022-09-03 10:57:55 -04:00

524 lines
12 KiB
C++

/**
* DO NOT MODIFY THIS FILE
*
* This repository was automatically generated and is NOT to be modified directly.
* Any repository modifications are meant to be made to the repository extending the base.
* Any modifications to base repositories are to be made by the generator only
*
* @generator ./utils/scripts/generators/repository-generator.pl
* @docs https://eqemu.gitbook.io/server/in-development/developer-area/repositories
*/
#ifndef EQEMU_BASE_FACTION_ASSOCIATION_REPOSITORY_H
#define EQEMU_BASE_FACTION_ASSOCIATION_REPOSITORY_H
#include "../../database.h"
#include "../../strings.h"
#include <ctime>
class BaseFactionAssociationRepository {
public:
struct FactionAssociation {
int32_t id;
int32_t id_1;
float mod_1;
int32_t id_2;
float mod_2;
int32_t id_3;
float mod_3;
int32_t id_4;
float mod_4;
int32_t id_5;
float mod_5;
int32_t id_6;
float mod_6;
int32_t id_7;
float mod_7;
int32_t id_8;
float mod_8;
int32_t id_9;
float mod_9;
int32_t id_10;
float mod_10;
};
static std::string PrimaryKey()
{
return std::string("id");
}
static std::vector<std::string> Columns()
{
return {
"id",
"id_1",
"mod_1",
"id_2",
"mod_2",
"id_3",
"mod_3",
"id_4",
"mod_4",
"id_5",
"mod_5",
"id_6",
"mod_6",
"id_7",
"mod_7",
"id_8",
"mod_8",
"id_9",
"mod_9",
"id_10",
"mod_10",
};
}
static std::vector<std::string> SelectColumns()
{
return {
"id",
"id_1",
"mod_1",
"id_2",
"mod_2",
"id_3",
"mod_3",
"id_4",
"mod_4",
"id_5",
"mod_5",
"id_6",
"mod_6",
"id_7",
"mod_7",
"id_8",
"mod_8",
"id_9",
"mod_9",
"id_10",
"mod_10",
};
}
static std::string ColumnsRaw()
{
return std::string(Strings::Implode(", ", Columns()));
}
static std::string SelectColumnsRaw()
{
return std::string(Strings::Implode(", ", SelectColumns()));
}
static std::string TableName()
{
return std::string("faction_association");
}
static std::string BaseSelect()
{
return fmt::format(
"SELECT {} FROM {}",
SelectColumnsRaw(),
TableName()
);
}
static std::string BaseInsert()
{
return fmt::format(
"INSERT INTO {} ({}) ",
TableName(),
ColumnsRaw()
);
}
static FactionAssociation NewEntity()
{
FactionAssociation e{};
e.id = 0;
e.id_1 = 0;
e.mod_1 = 0;
e.id_2 = 0;
e.mod_2 = 0;
e.id_3 = 0;
e.mod_3 = 0;
e.id_4 = 0;
e.mod_4 = 0;
e.id_5 = 0;
e.mod_5 = 0;
e.id_6 = 0;
e.mod_6 = 0;
e.id_7 = 0;
e.mod_7 = 0;
e.id_8 = 0;
e.mod_8 = 0;
e.id_9 = 0;
e.mod_9 = 0;
e.id_10 = 0;
e.mod_10 = 0;
return e;
}
static FactionAssociation GetFactionAssociation(
const std::vector<FactionAssociation> &faction_associations,
int faction_association_id
)
{
for (auto &faction_association : faction_associations) {
if (faction_association.id == faction_association_id) {
return faction_association;
}
}
return NewEntity();
}
static FactionAssociation FindOne(
Database& db,
int faction_association_id
)
{
auto results = db.QueryDatabase(
fmt::format(
"{} WHERE id = {} LIMIT 1",
BaseSelect(),
faction_association_id
)
);
auto row = results.begin();
if (results.RowCount() == 1) {
FactionAssociation e{};
e.id = static_cast<int32_t>(atoi(row[0]));
e.id_1 = static_cast<int32_t>(atoi(row[1]));
e.mod_1 = strtof(row[2], nullptr);
e.id_2 = static_cast<int32_t>(atoi(row[3]));
e.mod_2 = strtof(row[4], nullptr);
e.id_3 = static_cast<int32_t>(atoi(row[5]));
e.mod_3 = strtof(row[6], nullptr);
e.id_4 = static_cast<int32_t>(atoi(row[7]));
e.mod_4 = strtof(row[8], nullptr);
e.id_5 = static_cast<int32_t>(atoi(row[9]));
e.mod_5 = strtof(row[10], nullptr);
e.id_6 = static_cast<int32_t>(atoi(row[11]));
e.mod_6 = strtof(row[12], nullptr);
e.id_7 = static_cast<int32_t>(atoi(row[13]));
e.mod_7 = strtof(row[14], nullptr);
e.id_8 = static_cast<int32_t>(atoi(row[15]));
e.mod_8 = strtof(row[16], nullptr);
e.id_9 = static_cast<int32_t>(atoi(row[17]));
e.mod_9 = strtof(row[18], nullptr);
e.id_10 = static_cast<int32_t>(atoi(row[19]));
e.mod_10 = strtof(row[20], nullptr);
return e;
}
return NewEntity();
}
static int DeleteOne(
Database& db,
int faction_association_id
)
{
auto results = db.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {} = {}",
TableName(),
PrimaryKey(),
faction_association_id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int UpdateOne(
Database& db,
const FactionAssociation &e
)
{
std::vector<std::string> v;
auto columns = Columns();
v.push_back(columns[0] + " = " + std::to_string(e.id));
v.push_back(columns[1] + " = " + std::to_string(e.id_1));
v.push_back(columns[2] + " = " + std::to_string(e.mod_1));
v.push_back(columns[3] + " = " + std::to_string(e.id_2));
v.push_back(columns[4] + " = " + std::to_string(e.mod_2));
v.push_back(columns[5] + " = " + std::to_string(e.id_3));
v.push_back(columns[6] + " = " + std::to_string(e.mod_3));
v.push_back(columns[7] + " = " + std::to_string(e.id_4));
v.push_back(columns[8] + " = " + std::to_string(e.mod_4));
v.push_back(columns[9] + " = " + std::to_string(e.id_5));
v.push_back(columns[10] + " = " + std::to_string(e.mod_5));
v.push_back(columns[11] + " = " + std::to_string(e.id_6));
v.push_back(columns[12] + " = " + std::to_string(e.mod_6));
v.push_back(columns[13] + " = " + std::to_string(e.id_7));
v.push_back(columns[14] + " = " + std::to_string(e.mod_7));
v.push_back(columns[15] + " = " + std::to_string(e.id_8));
v.push_back(columns[16] + " = " + std::to_string(e.mod_8));
v.push_back(columns[17] + " = " + std::to_string(e.id_9));
v.push_back(columns[18] + " = " + std::to_string(e.mod_9));
v.push_back(columns[19] + " = " + std::to_string(e.id_10));
v.push_back(columns[20] + " = " + std::to_string(e.mod_10));
auto results = db.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
Strings::Implode(", ", v),
PrimaryKey(),
e.id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static FactionAssociation InsertOne(
Database& db,
FactionAssociation e
)
{
std::vector<std::string> v;
v.push_back(std::to_string(e.id));
v.push_back(std::to_string(e.id_1));
v.push_back(std::to_string(e.mod_1));
v.push_back(std::to_string(e.id_2));
v.push_back(std::to_string(e.mod_2));
v.push_back(std::to_string(e.id_3));
v.push_back(std::to_string(e.mod_3));
v.push_back(std::to_string(e.id_4));
v.push_back(std::to_string(e.mod_4));
v.push_back(std::to_string(e.id_5));
v.push_back(std::to_string(e.mod_5));
v.push_back(std::to_string(e.id_6));
v.push_back(std::to_string(e.mod_6));
v.push_back(std::to_string(e.id_7));
v.push_back(std::to_string(e.mod_7));
v.push_back(std::to_string(e.id_8));
v.push_back(std::to_string(e.mod_8));
v.push_back(std::to_string(e.id_9));
v.push_back(std::to_string(e.mod_9));
v.push_back(std::to_string(e.id_10));
v.push_back(std::to_string(e.mod_10));
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
Strings::Implode(",", v)
)
);
if (results.Success()) {
e.id = results.LastInsertedID();
return e;
}
e = NewEntity();
return e;
}
static int InsertMany(
Database& db,
const std::vector<FactionAssociation> &entries
)
{
std::vector<std::string> insert_chunks;
for (auto &e: entries) {
std::vector<std::string> v;
v.push_back(std::to_string(e.id));
v.push_back(std::to_string(e.id_1));
v.push_back(std::to_string(e.mod_1));
v.push_back(std::to_string(e.id_2));
v.push_back(std::to_string(e.mod_2));
v.push_back(std::to_string(e.id_3));
v.push_back(std::to_string(e.mod_3));
v.push_back(std::to_string(e.id_4));
v.push_back(std::to_string(e.mod_4));
v.push_back(std::to_string(e.id_5));
v.push_back(std::to_string(e.mod_5));
v.push_back(std::to_string(e.id_6));
v.push_back(std::to_string(e.mod_6));
v.push_back(std::to_string(e.id_7));
v.push_back(std::to_string(e.mod_7));
v.push_back(std::to_string(e.id_8));
v.push_back(std::to_string(e.mod_8));
v.push_back(std::to_string(e.id_9));
v.push_back(std::to_string(e.mod_9));
v.push_back(std::to_string(e.id_10));
v.push_back(std::to_string(e.mod_10));
insert_chunks.push_back("(" + Strings::Implode(",", v) + ")");
}
std::vector<std::string> v;
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES {}",
BaseInsert(),
Strings::Implode(",", insert_chunks)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static std::vector<FactionAssociation> All(Database& db)
{
std::vector<FactionAssociation> all_entries;
auto results = db.QueryDatabase(
fmt::format(
"{}",
BaseSelect()
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
FactionAssociation e{};
e.id = static_cast<int32_t>(atoi(row[0]));
e.id_1 = static_cast<int32_t>(atoi(row[1]));
e.mod_1 = strtof(row[2], nullptr);
e.id_2 = static_cast<int32_t>(atoi(row[3]));
e.mod_2 = strtof(row[4], nullptr);
e.id_3 = static_cast<int32_t>(atoi(row[5]));
e.mod_3 = strtof(row[6], nullptr);
e.id_4 = static_cast<int32_t>(atoi(row[7]));
e.mod_4 = strtof(row[8], nullptr);
e.id_5 = static_cast<int32_t>(atoi(row[9]));
e.mod_5 = strtof(row[10], nullptr);
e.id_6 = static_cast<int32_t>(atoi(row[11]));
e.mod_6 = strtof(row[12], nullptr);
e.id_7 = static_cast<int32_t>(atoi(row[13]));
e.mod_7 = strtof(row[14], nullptr);
e.id_8 = static_cast<int32_t>(atoi(row[15]));
e.mod_8 = strtof(row[16], nullptr);
e.id_9 = static_cast<int32_t>(atoi(row[17]));
e.mod_9 = strtof(row[18], nullptr);
e.id_10 = static_cast<int32_t>(atoi(row[19]));
e.mod_10 = strtof(row[20], nullptr);
all_entries.push_back(e);
}
return all_entries;
}
static std::vector<FactionAssociation> GetWhere(Database& db, const std::string &where_filter)
{
std::vector<FactionAssociation> all_entries;
auto results = db.QueryDatabase(
fmt::format(
"{} WHERE {}",
BaseSelect(),
where_filter
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
FactionAssociation e{};
e.id = static_cast<int32_t>(atoi(row[0]));
e.id_1 = static_cast<int32_t>(atoi(row[1]));
e.mod_1 = strtof(row[2], nullptr);
e.id_2 = static_cast<int32_t>(atoi(row[3]));
e.mod_2 = strtof(row[4], nullptr);
e.id_3 = static_cast<int32_t>(atoi(row[5]));
e.mod_3 = strtof(row[6], nullptr);
e.id_4 = static_cast<int32_t>(atoi(row[7]));
e.mod_4 = strtof(row[8], nullptr);
e.id_5 = static_cast<int32_t>(atoi(row[9]));
e.mod_5 = strtof(row[10], nullptr);
e.id_6 = static_cast<int32_t>(atoi(row[11]));
e.mod_6 = strtof(row[12], nullptr);
e.id_7 = static_cast<int32_t>(atoi(row[13]));
e.mod_7 = strtof(row[14], nullptr);
e.id_8 = static_cast<int32_t>(atoi(row[15]));
e.mod_8 = strtof(row[16], nullptr);
e.id_9 = static_cast<int32_t>(atoi(row[17]));
e.mod_9 = strtof(row[18], nullptr);
e.id_10 = static_cast<int32_t>(atoi(row[19]));
e.mod_10 = strtof(row[20], nullptr);
all_entries.push_back(e);
}
return all_entries;
}
static int DeleteWhere(Database& db, const std::string &where_filter)
{
auto results = db.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {}",
TableName(),
where_filter
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int Truncate(Database& db)
{
auto results = db.QueryDatabase(
fmt::format(
"TRUNCATE TABLE {}",
TableName()
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int64 GetMaxId(Database& db)
{
auto results = db.QueryDatabase(
fmt::format(
"SELECT COALESCE(MAX({}), 0) FROM {}",
PrimaryKey(),
TableName()
)
);
return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0);
}
static int64 Count(Database& db, const std::string &where_filter = "")
{
auto results = db.QueryDatabase(
fmt::format(
"SELECT COUNT(*) FROM {} {}",
TableName(),
(where_filter.empty() ? "" : "WHERE " + where_filter)
)
);
return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0);
}
};
#endif //EQEMU_BASE_FACTION_ASSOCIATION_REPOSITORY_H