diff --git a/common/repositories/base/base_character_potionbelt_repository.h b/common/repositories/base/base_character_potionbelt_repository.h index c31fef642..3b28ac8a9 100644 --- a/common/repositories/base/base_character_potionbelt_repository.h +++ b/common/repositories/base/base_character_potionbelt_repository.h @@ -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 @@ -16,6 +16,7 @@ #include "../../strings.h" #include + class BaseCharacterPotionbeltRepository { public: struct CharacterPotionbelt { @@ -116,8 +117,9 @@ public: { auto results = db.QueryDatabase( fmt::format( - "{} WHERE id = {} LIMIT 1", + "{} WHERE {} = {} LIMIT 1", BaseSelect(), + PrimaryKey(), character_potionbelt_id ) ); @@ -348,6 +350,68 @@ 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 CharacterPotionbelt &e + ) + { + std::vector 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 &entries + ) + { + std::vector insert_chunks; + + for (auto &e: entries) { + std::vector 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 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 diff --git a/zone/client_packet.cpp b/zone/client_packet.cpp index d4daad7f6..329addba9 100644 --- a/zone/client_packet.cpp +++ b/zone/client_packet.cpp @@ -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 */ diff --git a/zone/zonedb.cpp b/zone/zonedb.cpp index c47be4183..2f7bf6821 100755 --- a/zone/zonedb.cpp +++ b/zone/zonedb.cpp @@ -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 #include @@ -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; @@ -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) diff --git a/zone/zonedb.h b/zone/zonedb.h index b586765f0..5c78841a4 100644 --- a/zone/zonedb.h +++ b/zone/zonedb.h @@ -440,7 +440,7 @@ public: 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);