[Character] Convert Delete/Load/Save of Character Spells to Repositories (#3842)

* [Character] Convert Delete/Load/Save of Character Spells to Repositories

- Converts `DeleteCharacterSpell`, `LoadCharacterSpellBook`, and `SaveCharacterSpell` to repositories.

* Update zonedb.cpp
This commit is contained in:
Alex King 2024-01-07 00:12:40 -05:00 committed by GitHub
parent bc4bebb4a9
commit eb5eb0ca30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 112 additions and 36 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_SPELLS_REPOSITORY_H #ifndef EQEMU_BASE_CHARACTER_SPELLS_REPOSITORY_H
@ -16,6 +16,7 @@
#include "../../strings.h" #include "../../strings.h"
#include <ctime> #include <ctime>
class BaseCharacterSpellsRepository { class BaseCharacterSpellsRepository {
public: public:
struct CharacterSpells { struct CharacterSpells {
@ -112,8 +113,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_spells_id character_spells_id
) )
); );
@ -337,6 +339,66 @@ 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 CharacterSpells &e
)
{
std::vector<std::string> v;
v.push_back(std::to_string(e.id));
v.push_back(std::to_string(e.slot_id));
v.push_back(std::to_string(e.spell_id));
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<CharacterSpells> &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.slot_id));
v.push_back(std::to_string(e.spell_id));
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_SPELLS_REPOSITORY_H #endif //EQEMU_BASE_CHARACTER_SPELLS_REPOSITORY_H

View File

@ -5771,7 +5771,7 @@ void Client::Handle_OP_DeleteSpell(const EQApplicationPacket *app)
if (m_pp.spell_book[dss->spell_slot] != SPELLBOOK_UNKNOWN) { if (m_pp.spell_book[dss->spell_slot] != SPELLBOOK_UNKNOWN) {
m_pp.spell_book[dss->spell_slot] = SPELLBOOK_UNKNOWN; m_pp.spell_book[dss->spell_slot] = SPELLBOOK_UNKNOWN;
database.DeleteCharacterSpell(CharacterID(), m_pp.spell_book[dss->spell_slot], dss->spell_slot); database.DeleteCharacterSpell(CharacterID(), dss->spell_slot);
dss->success = 1; dss->success = 1;
} }
else else
@ -14550,10 +14550,10 @@ void Client::Handle_OP_SwapSpell(const EQApplicationPacket *app)
/* Save Spell Swaps */ /* Save Spell Swaps */
if (!database.SaveCharacterSpell(CharacterID(), m_pp.spell_book[swapspell->from_slot], swapspell->from_slot)) { if (!database.SaveCharacterSpell(CharacterID(), m_pp.spell_book[swapspell->from_slot], swapspell->from_slot)) {
database.DeleteCharacterSpell(CharacterID(), m_pp.spell_book[swapspell->from_slot], swapspell->from_slot); database.DeleteCharacterSpell(CharacterID(), swapspell->from_slot);
} }
if (!database.SaveCharacterSpell(CharacterID(), swapspelltemp, swapspell->to_slot)) { if (!database.SaveCharacterSpell(CharacterID(), swapspelltemp, swapspell->to_slot)) {
database.DeleteCharacterSpell(CharacterID(), swapspelltemp, swapspell->to_slot); database.DeleteCharacterSpell(CharacterID(), swapspell->to_slot);
} }
QueuePacket(app); QueuePacket(app);

View File

@ -5707,7 +5707,7 @@ void Client::UnscribeSpell(int slot, bool update_client, bool defer_save)
LogSpells("Spell [{}] erased from spell book slot [{}]", m_pp.spell_book[slot], slot); LogSpells("Spell [{}] erased from spell book slot [{}]", m_pp.spell_book[slot], slot);
if (!defer_save) { if (!defer_save) {
database.DeleteCharacterSpell(CharacterID(), m_pp.spell_book[slot], slot); database.DeleteCharacterSpell(CharacterID(), slot);
} }
if (update_client && slot < EQ::spells::DynamicLookup(ClientVersion(), GetGM())->SpellbookSize) { if (update_client && slot < EQ::spells::DynamicLookup(ClientVersion(), GetGM())->SpellbookSize) {

View File

@ -27,6 +27,7 @@
#include "../common/repositories/character_leadership_abilities_repository.h" #include "../common/repositories/character_leadership_abilities_repository.h"
#include "../common/repositories/character_material_repository.h" #include "../common/repositories/character_material_repository.h"
#include "../common/repositories/character_memmed_spells_repository.h" #include "../common/repositories/character_memmed_spells_repository.h"
#include "../common/repositories/character_spells_repository.h"
#include <ctime> #include <ctime>
#include <iostream> #include <iostream>
@ -682,35 +683,33 @@ bool ZoneDatabase::LoadCharacterMemmedSpells(uint32 character_id, PlayerProfile_
return true; return true;
} }
bool ZoneDatabase::LoadCharacterSpellBook(uint32 character_id, PlayerProfile_Struct* pp){ bool ZoneDatabase::LoadCharacterSpellBook(uint32 character_id, PlayerProfile_Struct* pp)
std::string query = StringFormat( {
"SELECT " const auto& l = CharacterSpellsRepository::GetWhere(
"slot_id, " database,
"`spell_id` " fmt::format(
"FROM " "`id` = {} ORDER BY `slot_id`",
"`character_spells` " character_id
"WHERE `id` = %u ORDER BY `slot_id`", character_id); )
auto results = database.QueryDatabase(query); );
/* Initialize Spells */ memset(pp->spell_book, UINT8_MAX, (sizeof(uint32) * EQ::spells::SPELLBOOK_SIZE));
memset(pp->spell_book, 0xFF, (sizeof(uint32) * EQ::spells::SPELLBOOK_SIZE));
// We have the ability to block loaded spells by max id on a per-client basis.. // We have the ability to block loaded spells by max id on a per-client basis..
// but, we do not have to ability to keep players from using older clients after // but, we do not have to ability to keep players from using older clients after
// they have scribed spells on a newer one that exceeds the older one's limit. // they have scribed spells on a newer one that exceeds the older one's limit.
// Load them all so that server actions are valid..but, nix them in translators. // Load them all so that server actions are valid..but, nix them in translators.
for (auto& row = results.begin(); row != results.end(); ++row) { for (const auto& e : l) {
int idx = Strings::ToInt(row[0]); if (!EQ::ValueWithin(e.slot_id, 0, EQ::spells::SPELLBOOK_SIZE)) {
int id = Strings::ToInt(row[1]);
if (idx < 0 || idx >= EQ::spells::SPELLBOOK_SIZE)
continue;
if (id < 3 || id > SPDAT_RECORDS) // 3 ("Summon Corpse") is the first scribable spell in spells_us.txt
continue; continue;
}
pp->spell_book[idx] = id; if (!IsValidSpell(e.spell_id)) {
continue;
}
pp->spell_book[e.slot_id] = e.spell_id;
} }
return true; return true;
@ -1282,17 +1281,32 @@ bool ZoneDatabase::SaveCharacterMemorizedSpell(uint32 character_id, uint32 spell
); );
} }
bool ZoneDatabase::SaveCharacterSpell(uint32 character_id, uint32 spell_id, uint32 slot_id){ bool ZoneDatabase::SaveCharacterSpell(uint32 character_id, uint32 spell_id, uint32 slot_id)
if (spell_id > SPDAT_RECORDS){ return false; } {
std::string query = StringFormat("REPLACE INTO `character_spells` (id, slot_id, spell_id) VALUES (%u, %u, %u)", character_id, slot_id, spell_id); if (!IsValidSpell(spell_id)) {
QueryDatabase(query); return false;
return true; }
return CharacterSpellsRepository::ReplaceOne(
*this,
CharacterSpellsRepository::CharacterSpells{
.id = character_id,
.slot_id = static_cast<uint16_t>(slot_id),
.spell_id = static_cast<uint16_t>(spell_id)
}
);
} }
bool ZoneDatabase::DeleteCharacterSpell(uint32 character_id, uint32 spell_id, uint32 slot_id){ bool ZoneDatabase::DeleteCharacterSpell(uint32 character_id, uint32 slot_id)
std::string query = StringFormat("DELETE FROM `character_spells` WHERE `slot_id` = %u AND `id` = %u", slot_id, character_id); {
QueryDatabase(query); return CharacterSpellsRepository::DeleteWhere(
return true; *this,
fmt::format(
"`id` = {} AND `slot_id` = {}",
character_id,
slot_id
)
);
} }
bool ZoneDatabase::DeleteCharacterDisc(uint32 character_id, uint32 slot_id){ bool ZoneDatabase::DeleteCharacterDisc(uint32 character_id, uint32 slot_id){

View File

@ -428,7 +428,7 @@ public:
bool DeleteCharacterMaterialColor(uint32 character_id); bool DeleteCharacterMaterialColor(uint32 character_id);
bool DeleteCharacterLeadershipAbilities(uint32 character_id); bool DeleteCharacterLeadershipAbilities(uint32 character_id);
bool DeleteCharacterMemorizedSpell(uint32 character_id, uint32 slot_id); bool DeleteCharacterMemorizedSpell(uint32 character_id, uint32 slot_id);
bool DeleteCharacterSpell(uint32 character_id, uint32 spell_id, uint32 slot_id); bool DeleteCharacterSpell(uint32 character_id, uint32 slot_id);
bool LoadCharacterBandolier(uint32 character_id, PlayerProfile_Struct* pp); bool LoadCharacterBandolier(uint32 character_id, PlayerProfile_Struct* pp);
bool LoadCharacterBindPoint(uint32 character_id, PlayerProfile_Struct* pp); bool LoadCharacterBindPoint(uint32 character_id, PlayerProfile_Struct* pp);