diff --git a/common/repositories/base/base_character_currency_repository.h b/common/repositories/base/base_character_currency_repository.h index 5e83e4697..63744d506 100644 --- a/common/repositories/base/base_character_currency_repository.h +++ b/common/repositories/base/base_character_currency_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_CURRENCY_REPOSITORY_H @@ -16,6 +16,7 @@ #include "../../strings.h" #include + class BaseCharacterCurrencyRepository { public: struct CharacterCurrency { @@ -168,8 +169,9 @@ public: { auto results = db.QueryDatabase( fmt::format( - "{} WHERE id = {} LIMIT 1", + "{} WHERE {} = {} LIMIT 1", BaseSelect(), + PrimaryKey(), character_currency_id ) ); @@ -478,6 +480,94 @@ 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 CharacterCurrency &e + ) + { + std::vector v; + + v.push_back(std::to_string(e.id)); + v.push_back(std::to_string(e.platinum)); + v.push_back(std::to_string(e.gold)); + v.push_back(std::to_string(e.silver)); + v.push_back(std::to_string(e.copper)); + v.push_back(std::to_string(e.platinum_bank)); + v.push_back(std::to_string(e.gold_bank)); + v.push_back(std::to_string(e.silver_bank)); + v.push_back(std::to_string(e.copper_bank)); + v.push_back(std::to_string(e.platinum_cursor)); + v.push_back(std::to_string(e.gold_cursor)); + v.push_back(std::to_string(e.silver_cursor)); + v.push_back(std::to_string(e.copper_cursor)); + v.push_back(std::to_string(e.radiant_crystals)); + v.push_back(std::to_string(e.career_radiant_crystals)); + v.push_back(std::to_string(e.ebon_crystals)); + v.push_back(std::to_string(e.career_ebon_crystals)); + + 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.platinum)); + v.push_back(std::to_string(e.gold)); + v.push_back(std::to_string(e.silver)); + v.push_back(std::to_string(e.copper)); + v.push_back(std::to_string(e.platinum_bank)); + v.push_back(std::to_string(e.gold_bank)); + v.push_back(std::to_string(e.silver_bank)); + v.push_back(std::to_string(e.copper_bank)); + v.push_back(std::to_string(e.platinum_cursor)); + v.push_back(std::to_string(e.gold_cursor)); + v.push_back(std::to_string(e.silver_cursor)); + v.push_back(std::to_string(e.copper_cursor)); + v.push_back(std::to_string(e.radiant_crystals)); + v.push_back(std::to_string(e.career_radiant_crystals)); + v.push_back(std::to_string(e.ebon_crystals)); + v.push_back(std::to_string(e.career_ebon_crystals)); + + 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_CURRENCY_REPOSITORY_H diff --git a/zone/zonedb.cpp b/zone/zonedb.cpp index f367a288f..2070803b8 100755 --- a/zone/zonedb.cpp +++ b/zone/zonedb.cpp @@ -31,6 +31,7 @@ #include "../common/repositories/character_skills_repository.h" #include "../common/repositories/character_potionbelt_repository.h" #include "../common/repositories/character_bandolier_repository.h" +#include "../common/repositories/character_currency_repository.h" #include #include @@ -808,47 +809,30 @@ bool ZoneDatabase::LoadCharacterSkills(uint32 character_id, PlayerProfile_Struct return true; } -bool ZoneDatabase::LoadCharacterCurrency(uint32 character_id, PlayerProfile_Struct* pp){ - std::string query = StringFormat( - "SELECT " - "platinum, " - "gold, " - "silver, " - "copper, " - "platinum_bank, " - "gold_bank, " - "silver_bank, " - "copper_bank, " - "platinum_cursor, " - "gold_cursor, " - "silver_cursor, " - "copper_cursor, " - "radiant_crystals, " - "career_radiant_crystals," - "ebon_crystals, " - "career_ebon_crystals " - "FROM " - "character_currency " - "WHERE `id` = %i ", character_id); - auto results = database.QueryDatabase(query); - for (auto& row = results.begin(); row != results.end(); ++row) { - pp->platinum = Strings::ToInt(row[0]); - pp->gold = Strings::ToInt(row[1]); - pp->silver = Strings::ToInt(row[2]); - pp->copper = Strings::ToInt(row[3]); - pp->platinum_bank = Strings::ToInt(row[4]); - pp->gold_bank = Strings::ToInt(row[5]); - pp->silver_bank = Strings::ToInt(row[6]); - pp->copper_bank = Strings::ToInt(row[7]); - pp->platinum_cursor = Strings::ToInt(row[8]); - pp->gold_cursor = Strings::ToInt(row[9]); - pp->silver_cursor = Strings::ToInt(row[10]); - pp->copper_cursor = Strings::ToInt(row[11]); - pp->currentRadCrystals = Strings::ToInt(row[12]); - pp->careerRadCrystals = Strings::ToInt(row[13]); - pp->currentEbonCrystals = Strings::ToInt(row[14]); - pp->careerEbonCrystals = Strings::ToInt(row[15]); +bool ZoneDatabase::LoadCharacterCurrency(uint32 character_id, PlayerProfile_Struct* pp) +{ + const auto& e = CharacterCurrencyRepository::FindOne(*this, character_id); + if (!e.id) { + return false; } + + pp->platinum = e.platinum; + pp->platinum_bank = e.platinum_bank; + pp->platinum_cursor = e.platinum_cursor; + pp->gold = e.gold; + pp->gold_bank = e.gold_bank; + pp->gold_cursor = e.gold_cursor; + pp->silver = e.silver; + pp->silver_bank = e.silver_bank; + pp->silver_cursor = e.silver_cursor; + pp->copper = e.copper; + pp->copper_bank = e.copper_bank; + pp->copper_cursor = e.copper_cursor; + pp->currentRadCrystals = e.radiant_crystals; + pp->careerRadCrystals = e.career_radiant_crystals; + pp->currentEbonCrystals = e.ebon_crystals; + pp->careerEbonCrystals = e.career_ebon_crystals; + return true; } @@ -1258,45 +1242,34 @@ bool ZoneDatabase::SaveCharacterData( return true; } -bool ZoneDatabase::SaveCharacterCurrency(uint32 character_id, PlayerProfile_Struct* pp){ - if (pp->copper < 0) { pp->copper = 0; } - if (pp->silver < 0) { pp->silver = 0; } - if (pp->gold < 0) { pp->gold = 0; } - if (pp->platinum < 0) { pp->platinum = 0; } - if (pp->copper_bank < 0) { pp->copper_bank = 0; } - if (pp->silver_bank < 0) { pp->silver_bank = 0; } - if (pp->gold_bank < 0) { pp->gold_bank = 0; } - if (pp->platinum_bank < 0) { pp->platinum_bank = 0; } - if (pp->platinum_cursor < 0) { pp->platinum_cursor = 0; } - if (pp->gold_cursor < 0) { pp->gold_cursor = 0; } - if (pp->silver_cursor < 0) { pp->silver_cursor = 0; } - if (pp->copper_cursor < 0) { pp->copper_cursor = 0; } - std::string query = StringFormat( - "REPLACE INTO `character_currency` (id, platinum, gold, silver, copper," - "platinum_bank, gold_bank, silver_bank, copper_bank," - "platinum_cursor, gold_cursor, silver_cursor, copper_cursor, " - "radiant_crystals, career_radiant_crystals, ebon_crystals, career_ebon_crystals)" - "VALUES (%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u)", - character_id, - pp->platinum, - pp->gold, - pp->silver, - pp->copper, - pp->platinum_bank, - pp->gold_bank, - pp->silver_bank, - pp->copper_bank, - pp->platinum_cursor, - pp->gold_cursor, - pp->silver_cursor, - pp->copper_cursor, - pp->currentRadCrystals, - pp->careerRadCrystals, - pp->currentEbonCrystals, - pp->careerEbonCrystals); - auto results = database.QueryDatabase(query); - LogDebug("Saving Currency for character ID: [{}], done", character_id); - return true; +bool ZoneDatabase::SaveCharacterCurrency(uint32 character_id, PlayerProfile_Struct* pp) +{ + ZeroPlayerProfileCurrency(pp); + + auto e = CharacterCurrencyRepository::NewEntity(); + + return CharacterCurrencyRepository::ReplaceOne( + *this, + CharacterCurrencyRepository::CharacterCurrency{ + .id = character_id, + .platinum = static_cast(pp->platinum), + .gold = static_cast(pp->gold), + .silver = static_cast(pp->silver), + .copper = static_cast(pp->copper), + .platinum_bank = static_cast(pp->platinum_bank), + .gold_bank = static_cast(pp->gold_bank), + .silver_bank = static_cast(pp->silver_bank), + .copper_bank = static_cast(pp->copper_bank), + .platinum_cursor = static_cast(pp->platinum_cursor), + .gold_cursor = static_cast(pp->gold_cursor), + .silver_cursor = static_cast(pp->silver_cursor), + .copper_cursor = static_cast(pp->copper_cursor), + .radiant_crystals = pp->currentRadCrystals, + .career_radiant_crystals = pp->careerRadCrystals, + .ebon_crystals = pp->currentEbonCrystals, + .career_ebon_crystals = pp->careerEbonCrystals + } + ); } bool ZoneDatabase::SaveCharacterAA(uint32 character_id, uint32 aa_id, uint32 current_level, uint32 charges){ @@ -4522,3 +4495,54 @@ void ZoneDatabase::SaveCharacterBinds(Client *c) CharacterBindRepository::InsertMany(database, binds); } } + +void ZoneDatabase::ZeroPlayerProfileCurrency(PlayerProfile_Struct* pp) +{ + if (pp->copper < 0) { + pp->copper = 0; + } + + if (pp->silver < 0) { + pp->silver = 0; + } + + if (pp->gold < 0) { + pp->gold = 0; + } + + if (pp->platinum < 0) { + pp->platinum = 0; + } + + if (pp->copper_bank < 0) { + pp->copper_bank = 0; + } + + if (pp->silver_bank < 0) { + pp->silver_bank = 0; + } + + if (pp->gold_bank < 0) { + pp->gold_bank = 0; + } + + if (pp->platinum_bank < 0) { + pp->platinum_bank = 0; + } + + if (pp->platinum_cursor < 0) { + pp->platinum_cursor = 0; + } + + if (pp->gold_cursor < 0) { + pp->gold_cursor = 0; + } + + if (pp->silver_cursor < 0) { + pp->silver_cursor = 0; + } + + if (pp->copper_cursor < 0) { + pp->copper_cursor = 0; + } +} diff --git a/zone/zonedb.h b/zone/zonedb.h index 56ca0c8f5..17b326132 100644 --- a/zone/zonedb.h +++ b/zone/zonedb.h @@ -457,6 +457,8 @@ public: bool SaveCharacterSkill(uint32 character_id, uint32 skill_id, uint32 value); bool SaveCharacterSpell(uint32 character_id, uint32 spell_id, uint32 slot_id); + void ZeroPlayerProfileCurrency(PlayerProfile_Struct* pp); + double GetAAEXPModifier(uint32 character_id, uint32 zone_id, int16 instance_version = -1) const; double GetEXPModifier(uint32 character_id, uint32 zone_id, int16 instance_version = -1) const; void SetAAEXPModifier(uint32 character_id, uint32 zone_id, double aa_modifier, int16 instance_version = -1);