Skip to content

Commit

Permalink
[Character] Convert Load/Save of Character Potion Belt to Repositories (
Browse files Browse the repository at this point in the history
#3844)

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

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

* Update zonedb.cpp

* Update zonedb.cpp
  • Loading branch information
Kinglykrab committed Jan 7, 2024
1 parent 9d48cbc commit f8de9b9
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
68 changes: 66 additions & 2 deletions common/repositories/base/base_character_potionbelt_repository.h
Expand Up @@ -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_POTIONBELT_REPOSITORY_H
Expand All @@ -16,6 +16,7 @@
#include "../../strings.h"
#include <ctime>


class BaseCharacterPotionbeltRepository {
public:
struct CharacterPotionbelt {
Expand Down Expand Up @@ -116,8 +117,9 @@ class BaseCharacterPotionbeltRepository {
{
auto results = db.QueryDatabase(
fmt::format(
"{} WHERE id = {} LIMIT 1",
"{} WHERE {} = {} LIMIT 1",
BaseSelect(),
PrimaryKey(),
character_potionbelt_id
)
);
Expand Down Expand Up @@ -348,6 +350,68 @@ class BaseCharacterPotionbeltRepository {
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
2 changes: 1 addition & 1 deletion zone/client_packet.cpp
Expand Up @@ -1268,7 +1268,7 @@ void Client::Handle_Connect_OP_ZoneEntry(const EQApplicationPacket *app)
database.LoadCharacterBandolier(cid, &m_pp); /* Load Character Bandolier */
database.LoadCharacterBindPoint(cid, &m_pp); /* Load Character Bind */
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.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 */
Expand Down
49 changes: 31 additions & 18 deletions zone/zonedb.cpp
Expand Up @@ -29,6 +29,7 @@
#include "../common/repositories/character_memmed_spells_repository.h"
#include "../common/repositories/character_spells_repository.h"
#include "../common/repositories/character_skills_repository.h"
#include "../common/repositories/character_potionbelt_repository.h"

#include <ctime>
#include <iostream>
Expand Down Expand Up @@ -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 =
StringFormat("SELECT `potion_id`, `item_id`, `icon` FROM `character_potionbelt` WHERE `id` = %u LIMIT %u",
character_id, EQ::profile::POTION_BELT_SIZE);
auto results = database.QueryDatabase(query);
int i = 0;
for (i = 0; i < EQ::profile::POTION_BELT_SIZE; i++) {
const auto& l = CharacterPotionbeltRepository::GetWhere(
database,
fmt::format(
"`id` = {} LIMIT {}",
character_id,
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].ID = 0;
pp->potionbelt.Items[i].ID = 0;
pp->potionbelt.Items[i].Name[0] = '\0';
}

for (auto& row = results.begin(); row != results.end(); ++row) {
i = Strings::ToInt(row[0]);
const EQ::ItemData *item_data = database.GetItem(Strings::ToInt(row[1]));
if (!item_data)
for (const auto& e : l) {
const auto* item_data = database.GetItem(e.item_id);
if (!item_data) {
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;
Expand Down Expand Up @@ -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)
{
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);
auto results = QueryDatabase(query);
return true;
return CharacterPotionbeltRepository::ReplaceOne(
*this,
CharacterPotionbeltRepository::CharacterPotionbelt{
.id = character_id,
.potion_id = potion_id,
.item_id = item_id,
.icon = icon
}
);
}

bool ZoneDatabase::SaveCharacterLeadershipAbilities(uint32 character_id, PlayerProfile_Struct* pp)
Expand Down
2 changes: 1 addition & 1 deletion zone/zonedb.h
Expand Up @@ -440,7 +440,7 @@ class ZoneDatabase : public SharedDatabase {
bool LoadCharacterLeadershipAbilities(uint32 character_id, PlayerProfile_Struct* pp);
bool LoadCharacterMaterialColor(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 LoadCharacterSpellBook(uint32 character_id, PlayerProfile_Struct* pp);

Expand Down

0 comments on commit f8de9b9

Please sign in to comment.