From c1e984dfc1369a53df8bc931e9684d659622cc57 Mon Sep 17 00:00:00 2001 From: Alex King <89047260+Kinglykrab@users.noreply.github.com> Date: Sun, 7 Jan 2024 00:03:54 -0500 Subject: [PATCH] [Character] Convert Delete/Load/Save of Character Material to Repositories (#3846) * [Character] Convert Delete/Load/Save of Character Material to Repositories - Convert `DeleteCharacterMaterialColor`, `LoadCharacterMaterialColor`, and `SaveCharacterMaterialColor` to repositories. * Cleanup --- .../base/base_character_material_repository.h | 74 ++++++++++++++++++- zone/client.cpp | 2 +- zone/zonedb.cpp | 58 +++++++++------ zone/zonedb.h | 4 +- 4 files changed, 112 insertions(+), 26 deletions(-) diff --git a/common/repositories/base/base_character_material_repository.h b/common/repositories/base/base_character_material_repository.h index 80506693c..fa1f55425 100644 --- a/common/repositories/base/base_character_material_repository.h +++ b/common/repositories/base/base_character_material_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_MATERIAL_REPOSITORY_H @@ -16,6 +16,7 @@ #include "../../strings.h" #include + class BaseCharacterMaterialRepository { public: struct CharacterMaterial { @@ -128,8 +129,9 @@ public: { auto results = db.QueryDatabase( fmt::format( - "{} WHERE id = {} LIMIT 1", + "{} WHERE {} = {} LIMIT 1", BaseSelect(), + PrimaryKey(), character_material_id ) ); @@ -377,6 +379,74 @@ 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 CharacterMaterial &e + ) + { + std::vector v; + + v.push_back(std::to_string(e.id)); + v.push_back(std::to_string(e.slot)); + v.push_back(std::to_string(e.blue)); + v.push_back(std::to_string(e.green)); + v.push_back(std::to_string(e.red)); + v.push_back(std::to_string(e.use_tint)); + v.push_back(std::to_string(e.color)); + + 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.slot)); + v.push_back(std::to_string(e.blue)); + v.push_back(std::to_string(e.green)); + v.push_back(std::to_string(e.red)); + v.push_back(std::to_string(e.use_tint)); + v.push_back(std::to_string(e.color)); + + 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_MATERIAL_REPOSITORY_H diff --git a/zone/client.cpp b/zone/client.cpp index 44c80afc7..be4971738 100644 --- a/zone/client.cpp +++ b/zone/client.cpp @@ -11261,7 +11261,7 @@ void Client::Undye() SendWearChange(slot); } - database.DeleteCharacterDye(CharacterID()); + database.DeleteCharacterMaterialColor(CharacterID()); } void Client::SetTrackingID(uint32 entity_id) diff --git a/zone/zonedb.cpp b/zone/zonedb.cpp index 55a83df31..598b32ce0 100755 --- a/zone/zonedb.cpp +++ b/zone/zonedb.cpp @@ -24,6 +24,7 @@ #include "../common/repositories/criteria/content_filter_criteria.h" #include "../common/repositories/spawn2_disabled_repository.h" #include "../common/repositories/character_leadership_abilities_repository.h" +#include "../common/repositories/character_material_repository.h" #include #include @@ -940,17 +941,23 @@ bool ZoneDatabase::LoadCharacterCurrency(uint32 character_id, PlayerProfile_Stru return true; } -bool ZoneDatabase::LoadCharacterMaterialColor(uint32 character_id, PlayerProfile_Struct* pp){ - std::string query = StringFormat("SELECT slot, blue, green, red, use_tint, color FROM `character_material` WHERE `id` = %u LIMIT 9", character_id); - auto results = database.QueryDatabase(query); int i = 0; int r = 0; - for (auto& row = results.begin(); row != results.end(); ++row) { - r = 0; - i = Strings::ToInt(row[r]); /* Slot */ r++; - pp->item_tint.Slot[i].Blue = Strings::ToInt(row[r]); r++; - pp->item_tint.Slot[i].Green = Strings::ToInt(row[r]); r++; - pp->item_tint.Slot[i].Red = Strings::ToInt(row[r]); r++; - pp->item_tint.Slot[i].UseTint = Strings::ToInt(row[r]); +bool ZoneDatabase::LoadCharacterMaterialColor(uint32 character_id, PlayerProfile_Struct* pp) +{ + const auto& l = CharacterMaterialRepository::GetWhere( + database, + fmt::format( + "`id` = {} LIMIT 9", + character_id + ) + ); + + for (const auto& e : l) { + pp->item_tint.Slot[e.slot].Blue = e.blue; + pp->item_tint.Slot[e.slot].Green = e.green; + pp->item_tint.Slot[e.slot].Red = e.red; + pp->item_tint.Slot[e.slot].UseTint = e.use_tint; } + return true; } @@ -1068,14 +1075,24 @@ bool ZoneDatabase::SaveCharacterLanguage(uint32 character_id, uint32 lang_id, ui return true; } -bool ZoneDatabase::SaveCharacterMaterialColor(uint32 character_id, uint32 slot_id, uint32 color){ - uint8 red = (color & 0x00FF0000) >> 16; - uint8 green = (color & 0x0000FF00) >> 8; - uint8 blue = (color & 0x000000FF); +bool ZoneDatabase::SaveCharacterMaterialColor(uint32 character_id, uint8 slot_id, uint32 color) +{ + const uint8 red = (color & 0x00FF0000) >> 16; + const uint8 green = (color & 0x0000FF00) >> 8; + const uint8 blue = (color & 0x000000FF); - std::string query = StringFormat("REPLACE INTO `character_material` (id, slot, red, green, blue, color, use_tint) VALUES (%u, %u, %u, %u, %u, %u, 255)", character_id, slot_id, red, green, blue, color); auto results = QueryDatabase(query); - LogDebug("ZoneDatabase::SaveCharacterMaterialColor for character ID: [{}], slot_id: [{}] color: [{}] done", character_id, slot_id, color); - return true; + return CharacterMaterialRepository::ReplaceOne( + *this, + CharacterMaterialRepository::CharacterMaterial{ + .id = character_id, + .slot = slot_id, + .blue = blue, + .green = green, + .red = red, + .use_tint = UINT8_MAX, + .color = color + } + ); } bool ZoneDatabase::SaveCharacterSkill(uint32 character_id, uint32 skill_id, uint32 value){ @@ -1569,10 +1586,9 @@ bool ZoneDatabase::DeleteCharacterAAs(uint32 character_id){ return true; } -bool ZoneDatabase::DeleteCharacterDye(uint32 character_id){ - std::string query = StringFormat("DELETE FROM `character_material` WHERE `id` = %u", character_id); - QueryDatabase(query); - return true; +bool ZoneDatabase::DeleteCharacterMaterialColor(uint32 character_id) +{ + return CharacterMaterialRepository::DeleteOne(*this, character_id); } bool ZoneDatabase::DeleteCharacterMemorizedSpell(uint32 character_id, uint32 spell_id, uint32 slot_id){ diff --git a/zone/zonedb.h b/zone/zonedb.h index d065e5ba5..5ea41e2dd 100644 --- a/zone/zonedb.h +++ b/zone/zonedb.h @@ -425,7 +425,7 @@ public: bool DeleteCharacterAAs(uint32 character_id); bool DeleteCharacterBandolier(uint32 character_id, uint32 band_id); bool DeleteCharacterDisc(uint32 character_id, uint32 slot_id); - bool DeleteCharacterDye(uint32 character_id); + bool DeleteCharacterMaterialColor(uint32 character_id); bool DeleteCharacterLeadershipAbilities(uint32 character_id); bool DeleteCharacterMemorizedSpell(uint32 character_id, uint32 spell_id, uint32 slot_id); bool DeleteCharacterSpell(uint32 character_id, uint32 spell_id, uint32 slot_id); @@ -451,7 +451,7 @@ public: bool SaveCharacterDisc(uint32 character_id, uint32 slot_id, uint32 disc_id); bool SaveCharacterLanguage(uint32 character_id, uint32 lang_id, uint32 value); bool SaveCharacterLeadershipAbilities(uint32 character_id, PlayerProfile_Struct* pp); - bool SaveCharacterMaterialColor(uint32 character_id, uint32 slot_id, uint32 color); + bool SaveCharacterMaterialColor(uint32 character_id, uint8 slot_id, uint32 color); bool SaveCharacterMemorizedSpell(uint32 character_id, uint32 spell_id, uint32 slot_id); bool SaveCharacterPotionBelt(uint32 character_id, uint8 potion_id, uint32 item_id, uint32 icon); bool SaveCharacterSkill(uint32 character_id, uint32 skill_id, uint32 value);