[Character] Convert Load/Save of Character Potion Belt to Repositories (#3844)

* [Character] Convert Load/Save of Character Potion Belt to Repositories

- Converts `LoadCharacterPotionBelt` and `SaveCharacterPotionBelt` to repositories.

* Update zonedb.cpp

* Update zonedb.cpp
This commit is contained in:
Alex King 2024-01-07 00:15:58 -05:00 committed by GitHub
parent 9d48cbcd29
commit f8de9b9167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 22 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_POTIONBELT_REPOSITORY_H #ifndef EQEMU_BASE_CHARACTER_POTIONBELT_REPOSITORY_H
@ -16,6 +16,7 @@
#include "../../strings.h" #include "../../strings.h"
#include <ctime> #include <ctime>
class BaseCharacterPotionbeltRepository { class BaseCharacterPotionbeltRepository {
public: public:
struct CharacterPotionbelt { struct CharacterPotionbelt {
@ -116,8 +117,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_potionbelt_id character_potionbelt_id
) )
); );
@ -348,6 +350,68 @@ 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 CharacterPotionbelt &e
)
{
std::vector<std::string> v;
v.push_back(std::to_string(e.id));
v.push_back(std::to_string(e.potion_id));
v.push_back(std::to_string(e.item_id));
v.push_back(std::to_string(e.icon));
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<CharacterPotionbelt> &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.potion_id));
v.push_back(std::to_string(e.item_id));
v.push_back(std::to_string(e.icon));
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_POTIONBELT_REPOSITORY_H #endif //EQEMU_BASE_CHARACTER_POTIONBELT_REPOSITORY_H

View File

@ -1268,7 +1268,7 @@ void Client::Handle_Connect_OP_ZoneEntry(const EQApplicationPacket *app)
database.LoadCharacterBandolier(cid, &m_pp); /* Load Character Bandolier */ database.LoadCharacterBandolier(cid, &m_pp); /* Load Character Bandolier */
database.LoadCharacterBindPoint(cid, &m_pp); /* Load Character Bind */ database.LoadCharacterBindPoint(cid, &m_pp); /* Load Character Bind */
database.LoadCharacterMaterialColor(cid, &m_pp); /* Load Character Material */ database.LoadCharacterMaterialColor(cid, &m_pp); /* Load Character Material */
database.LoadCharacterPotions(cid, &m_pp); /* Load Character Potion Belt */ database.LoadCharacterPotionBelt(cid, &m_pp); /* Load Character Potion Belt */
database.LoadCharacterCurrency(cid, &m_pp); /* Load Character Currency into PP */ database.LoadCharacterCurrency(cid, &m_pp); /* Load Character Currency into PP */
database.LoadCharacterData(cid, &m_pp, &m_epp); /* Load Character Data from DB into PP as well as E_PP */ database.LoadCharacterData(cid, &m_pp, &m_epp); /* Load Character Data from DB into PP as well as E_PP */
database.LoadCharacterSkills(cid, &m_pp); /* Load Character Skills */ database.LoadCharacterSkills(cid, &m_pp); /* Load Character Skills */

View File

@ -29,6 +29,7 @@
#include "../common/repositories/character_memmed_spells_repository.h" #include "../common/repositories/character_memmed_spells_repository.h"
#include "../common/repositories/character_spells_repository.h" #include "../common/repositories/character_spells_repository.h"
#include "../common/repositories/character_skills_repository.h" #include "../common/repositories/character_skills_repository.h"
#include "../common/repositories/character_potionbelt_repository.h"
#include <ctime> #include <ctime>
#include <iostream> #include <iostream>
@ -926,27 +927,33 @@ void ZoneDatabase::LoadCharacterTribute(Client* c){
} }
} }
bool ZoneDatabase::LoadCharacterPotions(uint32 character_id, PlayerProfile_Struct *pp) bool ZoneDatabase::LoadCharacterPotionBelt(uint32 character_id, PlayerProfile_Struct *pp)
{ {
std::string query = const auto& l = CharacterPotionbeltRepository::GetWhere(
StringFormat("SELECT `potion_id`, `item_id`, `icon` FROM `character_potionbelt` WHERE `id` = %u LIMIT %u", database,
character_id, EQ::profile::POTION_BELT_SIZE); fmt::format(
auto results = database.QueryDatabase(query); "`id` = {} LIMIT {}",
int i = 0; character_id,
for (i = 0; i < EQ::profile::POTION_BELT_SIZE; i++) { EQ::profile::POTION_BELT_SIZE
)
);
for (int i = 0; i < EQ::profile::POTION_BELT_SIZE; i++) { // Initialize Potion Belt
pp->potionbelt.Items[i].Icon = 0; pp->potionbelt.Items[i].Icon = 0;
pp->potionbelt.Items[i].ID = 0; pp->potionbelt.Items[i].ID = 0;
pp->potionbelt.Items[i].Name[0] = '\0'; pp->potionbelt.Items[i].Name[0] = '\0';
} }
for (auto& row = results.begin(); row != results.end(); ++row) { for (const auto& e : l) {
i = Strings::ToInt(row[0]); const auto* item_data = database.GetItem(e.item_id);
const EQ::ItemData *item_data = database.GetItem(Strings::ToInt(row[1])); if (!item_data) {
if (!item_data)
continue; continue;
pp->potionbelt.Items[i].ID = item_data->ID; }
pp->potionbelt.Items[i].Icon = Strings::ToInt(row[2]);
strncpy(pp->potionbelt.Items[i].Name, item_data->Name, 64); pp->potionbelt.Items[e.potion_id].ID = item_data->ID;
pp->potionbelt.Items[e.potion_id].Icon = e.icon;
strncpy(pp->potionbelt.Items[e.potion_id].Name, item_data->Name, 64);
} }
return true; return true;
@ -1066,9 +1073,15 @@ bool ZoneDatabase::SaveCharacterBandolier(uint32 character_id, uint8 bandolier_i
bool ZoneDatabase::SaveCharacterPotionBelt(uint32 character_id, uint8 potion_id, uint32 item_id, uint32 icon) bool ZoneDatabase::SaveCharacterPotionBelt(uint32 character_id, uint8 potion_id, uint32 item_id, uint32 icon)
{ {
std::string query = StringFormat("REPLACE INTO `character_potionbelt` (id, potion_id, item_id, icon) VALUES (%u, %u, %u, %u)", character_id, potion_id, item_id, icon); return CharacterPotionbeltRepository::ReplaceOne(
auto results = QueryDatabase(query); *this,
return true; CharacterPotionbeltRepository::CharacterPotionbelt{
.id = character_id,
.potion_id = potion_id,
.item_id = item_id,
.icon = icon
}
);
} }
bool ZoneDatabase::SaveCharacterLeadershipAbilities(uint32 character_id, PlayerProfile_Struct* pp) bool ZoneDatabase::SaveCharacterLeadershipAbilities(uint32 character_id, PlayerProfile_Struct* pp)

View File

@ -440,7 +440,7 @@ public:
bool LoadCharacterLeadershipAbilities(uint32 character_id, PlayerProfile_Struct* pp); bool LoadCharacterLeadershipAbilities(uint32 character_id, PlayerProfile_Struct* pp);
bool LoadCharacterMaterialColor(uint32 character_id, PlayerProfile_Struct* pp); bool LoadCharacterMaterialColor(uint32 character_id, PlayerProfile_Struct* pp);
bool LoadCharacterMemmedSpells(uint32 character_id, PlayerProfile_Struct* pp); bool LoadCharacterMemmedSpells(uint32 character_id, PlayerProfile_Struct* pp);
bool LoadCharacterPotions(uint32 character_id, PlayerProfile_Struct* pp); bool LoadCharacterPotionBelt(uint32 character_id, PlayerProfile_Struct* pp);
bool LoadCharacterSkills(uint32 character_id, PlayerProfile_Struct* pp); bool LoadCharacterSkills(uint32 character_id, PlayerProfile_Struct* pp);
bool LoadCharacterSpellBook(uint32 character_id, PlayerProfile_Struct* pp); bool LoadCharacterSpellBook(uint32 character_id, PlayerProfile_Struct* pp);