[Character] Convert Load/Save of Character Skills to Repositories (#3843)

* [Character] Convert Load/Save of Character Skills to Repositories

- Converts `LoadCharacterSkills` and `SaveCharacterSkill` to repositories.

* Update zonedb.cpp
This commit is contained in:
Alex King 2024-01-07 00:13:39 -05:00 committed by GitHub
parent eb5eb0ca30
commit 9d48cbcd29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 23 deletions

View File

@ -6,7 +6,7 @@
* 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
* @docs https://docs.eqemu.io/developer/repositories
*/
#ifndef EQEMU_BASE_CHARACTER_SKILLS_REPOSITORY_H
@ -16,6 +16,7 @@
#include "../../strings.h"
#include <ctime>
class BaseCharacterSkillsRepository {
public:
struct CharacterSkills {
@ -112,8 +113,9 @@ public:
{
auto results = db.QueryDatabase(
fmt::format(
"{} WHERE id = {} LIMIT 1",
"{} WHERE {} = {} LIMIT 1",
BaseSelect(),
PrimaryKey(),
character_skills_id
)
);
@ -337,6 +339,66 @@ public:
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 CharacterSkills &e
)
{
std::vector<std::string> v;
v.push_back(std::to_string(e.id));
v.push_back(std::to_string(e.skill_id));
v.push_back(std::to_string(e.value));
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<CharacterSkills> &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.skill_id));
v.push_back(std::to_string(e.value));
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_SKILLS_REPOSITORY_H

View File

@ -28,6 +28,7 @@
#include "../common/repositories/character_material_repository.h"
#include "../common/repositories/character_memmed_spells_repository.h"
#include "../common/repositories/character_spells_repository.h"
#include "../common/repositories/character_skills_repository.h"
#include <ctime>
#include <iostream>
@ -782,24 +783,24 @@ bool ZoneDatabase::LoadCharacterDisciplines(uint32 character_id, PlayerProfile_S
return true;
}
bool ZoneDatabase::LoadCharacterSkills(uint32 character_id, PlayerProfile_Struct* pp){
std::string query = StringFormat(
"SELECT "
"skill_id, "
"`value` "
"FROM "
"`character_skills` "
"WHERE `id` = %u ORDER BY `skill_id`", character_id);
auto results = database.QueryDatabase(query);
int i = 0;
/* Initialize Skill */
for (i = 0; i < MAX_PP_SKILL; ++i)
pp->skills[i] = 0;
bool ZoneDatabase::LoadCharacterSkills(uint32 character_id, PlayerProfile_Struct* pp)
{
const auto& l = CharacterSkillsRepository::GetWhere(
*this,
fmt::format(
"`id` = {} ORDER BY `skill_id",
character_id
)
);
for (auto& row = results.begin(); row != results.end(); ++row) {
i = Strings::ToInt(row[0]);
if (i < MAX_PP_SKILL)
pp->skills[i] = Strings::ToInt(row[1]);
for (int i = 0; i < MAX_PP_SKILL; ++i) { // Initialize Skills
pp->skills[i] = 0;
}
for (const auto& e : l) {
if (e.skill_id < MAX_PP_SKILL) {
pp->skills[e.skill_id] = e.value;
}
}
return true;
@ -1003,10 +1004,16 @@ bool ZoneDatabase::SaveCharacterMaterialColor(uint32 character_id, uint8 slot_id
);
}
bool ZoneDatabase::SaveCharacterSkill(uint32 character_id, uint32 skill_id, uint32 value){
std::string query = StringFormat("REPLACE INTO `character_skills` (id, skill_id, value) VALUES (%u, %u, %u)", character_id, skill_id, value); auto results = QueryDatabase(query);
LogDebug("ZoneDatabase::SaveCharacterSkill for character ID: [{}], skill_id:[{}] value:[{}] done", character_id, skill_id, value);
return true;
bool ZoneDatabase::SaveCharacterSkill(uint32 character_id, uint32 skill_id, uint32 value)
{
return CharacterSkillsRepository::ReplaceOne(
*this,
CharacterSkillsRepository::CharacterSkills{
.id = character_id,
.skill_id = static_cast<uint16_t>(skill_id),
.value = static_cast<uint16_t>(value)
}
);
}
bool ZoneDatabase::SaveCharacterDisc(uint32 character_id, uint32 slot_id, uint32 disc_id){