[Character] Convert Load/Save of Character Bind to Repositories (#3851)

# Notes
- Convert `LoadCharacterBindPoint` to repositories.
- `SaveCharacterBinds` was already using repositories, cleanup logic.

# Images
## Load

## Save
This commit is contained in:
Alex King 2024-01-07 00:46:24 -05:00 committed by GitHub
parent 2dd0e51936
commit c0769a9c29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 44 deletions

View File

@ -6,7 +6,7 @@
* Any modifications to base repositories are to be made by the generator only * Any modifications to base repositories are to be made by the generator only
* *
* @generator ./utils/scripts/generators/repository-generator.pl * @generator ./utils/scripts/generators/repository-generator.pl
* @docs https://eqemu.gitbook.io/server/in-development/developer-area/repositories * @docs https://docs.eqemu.io/developer/repositories
*/ */
#ifndef EQEMU_BASE_CHARACTER_BIND_REPOSITORY_H #ifndef EQEMU_BASE_CHARACTER_BIND_REPOSITORY_H
@ -16,6 +16,7 @@
#include "../../strings.h" #include "../../strings.h"
#include <ctime> #include <ctime>
class BaseCharacterBindRepository { class BaseCharacterBindRepository {
public: public:
struct CharacterBind { struct CharacterBind {
@ -132,8 +133,9 @@ public:
{ {
auto results = db.QueryDatabase( auto results = db.QueryDatabase(
fmt::format( fmt::format(
"{} WHERE id = {} LIMIT 1", "{} WHERE {} = {} LIMIT 1",
BaseSelect(), BaseSelect(),
PrimaryKey(),
character_bind_id character_bind_id
) )
); );
@ -387,6 +389,76 @@ public:
return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0); return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0);
} }
static std::string BaseReplace()
{
return fmt::format(
"REPLACE INTO {} ({}) ",
TableName(),
ColumnsRaw()
);
}
static int ReplaceOne(
Database& db,
const CharacterBind &e
)
{
std::vector<std::string> v;
v.push_back(std::to_string(e.id));
v.push_back(std::to_string(e.slot));
v.push_back(std::to_string(e.zone_id));
v.push_back(std::to_string(e.instance_id));
v.push_back(std::to_string(e.x));
v.push_back(std::to_string(e.y));
v.push_back(std::to_string(e.z));
v.push_back(std::to_string(e.heading));
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseReplace(),
Strings::Implode(",", v)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int ReplaceMany(
Database& db,
const std::vector<CharacterBind> &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.slot));
v.push_back(std::to_string(e.zone_id));
v.push_back(std::to_string(e.instance_id));
v.push_back(std::to_string(e.x));
v.push_back(std::to_string(e.y));
v.push_back(std::to_string(e.z));
v.push_back(std::to_string(e.heading));
insert_chunks.push_back("(" + Strings::Implode(",", v) + ")");
}
std::vector<std::string> v;
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES {}",
BaseReplace(),
Strings::Implode(",", insert_chunks)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
}; };
#endif //EQEMU_BASE_CHARACTER_BIND_REPOSITORY_H #endif //EQEMU_BASE_CHARACTER_BIND_REPOSITORY_H

View File

@ -959,25 +959,29 @@ bool ZoneDatabase::LoadCharacterPotionBelt(uint32 character_id, PlayerProfile_St
bool ZoneDatabase::LoadCharacterBindPoint(uint32 character_id, PlayerProfile_Struct *pp) bool ZoneDatabase::LoadCharacterBindPoint(uint32 character_id, PlayerProfile_Struct *pp)
{ {
std::string query = StringFormat("SELECT `slot`, `zone_id`, `instance_id`, `x`, `y`, `z`, `heading` FROM " const auto& l = CharacterBindRepository::GetWhere(
"`character_bind` WHERE `id` = %u LIMIT 5", database,
character_id); fmt::format(
auto results = database.QueryDatabase(query); "`id` = {} LIMIT 5",
character_id
)
);
if (!results.RowCount()) // SHIT -- this actually isn't good if (l.empty()) {
return true; return true;
}
for (auto& row = results.begin(); row != results.end(); ++row) { for (const auto& e : l) {
int index = Strings::ToInt(row[0]); if (!EQ::ValueWithin(e.slot, 0, 4)) {
if (index < 0 || index > 4)
continue; continue;
}
pp->binds[index].zone_id = Strings::ToInt(row[1]); pp->binds[e.slot].zone_id = e.zone_id;
pp->binds[index].instance_id = Strings::ToInt(row[2]); pp->binds[e.slot].instance_id = e.instance_id;
pp->binds[index].x = Strings::ToFloat(row[3]); pp->binds[e.slot].x = e.x;
pp->binds[index].y = Strings::ToFloat(row[4]); pp->binds[e.slot].y = e.y;
pp->binds[index].z = Strings::ToFloat(row[5]); pp->binds[e.slot].z = e.z;
pp->binds[index].heading = Strings::ToFloat(row[6]); pp->binds[e.slot].heading = e.heading;
} }
return true; return true;
@ -4462,48 +4466,40 @@ void ZoneDatabase::UpdateGMStatus(uint32 accID, int newStatus)
void ZoneDatabase::SaveCharacterBinds(Client *c) void ZoneDatabase::SaveCharacterBinds(Client *c)
{ {
// bulk save character binds std::vector<CharacterBindRepository::CharacterBind> v;
std::vector<CharacterBindRepository::CharacterBind> binds = {};
CharacterBindRepository::CharacterBind bind = {};
// count character binds auto e = CharacterBindRepository::NewEntity();
int bind_count = 0;
for (auto & b : c->GetPP().binds) { uint32 bind_count = 0;
for (const auto &b : c->GetPP().binds) {
if (b.zone_id) { if (b.zone_id) {
bind_count++; bind_count++;
} }
} }
// allocate memory for binds v.reserve(bind_count);
binds.reserve(bind_count);
// copy binds to vector int slot_id = 0;
int i = 0;
for (auto &b: c->GetPP().binds) { for (const auto &b : c->GetPP().binds) {
if (b.zone_id) { if (b.zone_id) {
// copy bind data e.id = c->CharacterID();
bind.id = c->CharacterID(); e.zone_id = b.zone_id;
bind.zone_id = b.zone_id; e.instance_id = b.instance_id;
bind.instance_id = b.instance_id; e.x = b.x;
bind.x = b.x; e.y = b.y;
bind.y = b.y; e.z = b.z;
bind.z = b.z; e.heading = b.heading;
bind.heading = b.heading; e.slot = slot_id;
bind.slot = i;
// add bind to vector v.emplace_back(e);
binds.emplace_back(bind);
i++; slot_id++;
} }
} }
// save binds
if (bind_count > 0) { if (bind_count > 0) {
// delete old binds CharacterBindRepository::ReplaceMany(database, v);
CharacterBindRepository::DeleteWhere(database, fmt::format("id = {}", c->CharacterID()));
// save new binds
CharacterBindRepository::InsertMany(database, binds);
} }
} }