Skip to content

Commit

Permalink
Convert a lot of db calls in LoadChar
Browse files Browse the repository at this point in the history
  • Loading branch information
zach2good committed Dec 22, 2023
1 parent 15d2f54 commit b2a79f3
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 497 deletions.
2 changes: 1 addition & 1 deletion src/common/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace db
}
else if constexpr (std::is_same_v<std::decay_t<T>, std::string>)
{
stmt->setString(counter, first);
stmt->setString(counter, first.c_str());
}
else if constexpr (std::is_same_v<std::decay_t<T>, bool>)
{
Expand Down
74 changes: 36 additions & 38 deletions src/map/monstrosity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void monstrosity::ReadMonstrosityData(CCharEntity* PChar)
auto data = std::make_unique<MonstrosityData_t>();

// clang-format off
auto ret = _sql->Query("SELECT "
auto rset = db::preparedStmt("SELECT "
"charid, "
"current_monstrosity_id, "
"current_monstrosity_species, "
Expand All @@ -176,47 +176,43 @@ void monstrosity::ReadMonstrosityData(CCharEntity* PChar)
"entry_zone_id, "
"entry_mjob, "
"entry_sjob "
"FROM char_monstrosity WHERE charid = %d LIMIT 1;",
"FROM char_monstrosity WHERE charid = (?) LIMIT 1;",
PChar->id);
// clang-format on

if (ret != SQL_ERROR && _sql->NumRows() != 0)
if (rset && rset->rowsCount() && rset->next())
{
while (_sql->NextRow() == SQL_SUCCESS)
{
// charid: 0
data->MonstrosityId = static_cast<uint16>(_sql->GetUIntData(1));
data->Species = static_cast<uint16>(_sql->GetUIntData(2));
data->Look = gMonstrositySpeciesMap[data->Species].look;

data->NamePrefix1 = static_cast<uint8>(_sql->GetUIntData(3));
data->NamePrefix2 = static_cast<uint8>(_sql->GetUIntData(4));
data->CurrentExp = static_cast<uint32>(_sql->GetUIntData(5));

_sql->GetBlobData(6, &data->EquippedInstincts);
_sql->GetBlobData(7, &data->levels);
_sql->GetBlobData(8, &data->instincts);
_sql->GetBlobData(9, &data->variants);

data->Belligerency = static_cast<bool>(_sql->GetUIntData(10));

data->EntryPos.x = _sql->GetFloatData(11);
data->EntryPos.y = _sql->GetFloatData(12);
data->EntryPos.z = _sql->GetFloatData(13);
data->EntryPos.rotation = static_cast<uint8>(_sql->GetUIntData(14));
data->EntryZoneId = static_cast<uint16>(_sql->GetUIntData(15));
data->EntryMainJob = static_cast<uint8>(_sql->GetUIntData(16));
data->EntrySubJob = static_cast<uint8>(_sql->GetUIntData(17));

// Build additional data from lookups
data->MainJob = gMonstrositySpeciesMap[data->Species].mjob;
data->SubJob = gMonstrositySpeciesMap[data->Species].sjob;
data->Size = gMonstrositySpeciesMap[data->Species].size;

// TODO:
auto level = data->levels[data->MonstrosityId];
std::ignore = level;
}
data->MonstrosityId = static_cast<uint16>(rset->getUInt("current_monstrosity_id"));
data->Species = static_cast<uint16>(rset->getUInt("current_monstrosity_species"));
data->Look = gMonstrositySpeciesMap[data->Species].look;

data->NamePrefix1 = static_cast<uint8>(rset->getUInt("current_monstrosity_name_prefix_1"));
data->NamePrefix2 = static_cast<uint8>(rset->getUInt("current_monstrosity_name_prefix_2"));
data->CurrentExp = static_cast<uint32>(rset->getUInt("current_exp"));

db::extractBlob(rset, "equip", data->EquippedInstincts);
db::extractBlob(rset, "levels", data->levels);
db::extractBlob(rset, "instincts", data->instincts);
db::extractBlob(rset, "variants", data->variants);

data->Belligerency = static_cast<bool>(rset->getUInt("belligerency"));

data->EntryPos.x = rset->getFloat("entry_x");
data->EntryPos.y = rset->getFloat("entry_y");
data->EntryPos.z = rset->getFloat("entry_z");
data->EntryPos.rotation = static_cast<uint8>(rset->getUInt("entry_rot"));
data->EntryZoneId = static_cast<uint16>(rset->getUInt("entry_zone_id"));
data->EntryMainJob = static_cast<uint8>(rset->getUInt("entry_mjob"));
data->EntrySubJob = static_cast<uint8>(rset->getUInt("entry_sjob"));

// Build additional data from lookups
data->MainJob = gMonstrositySpeciesMap[data->Species].mjob;
data->SubJob = gMonstrositySpeciesMap[data->Species].sjob;
data->Size = gMonstrositySpeciesMap[data->Species].size;

// TODO:
auto level = data->levels[data->MonstrosityId];
std::ignore = level;
}

PChar->m_PMonstrosity = std::move(data);
Expand Down Expand Up @@ -277,6 +273,8 @@ void monstrosity::WriteMonstrosityData(CCharEntity* PChar)

void monstrosity::TryPopulateMonstrosityData(CCharEntity* PChar)
{
TracyZoneScoped;

if (settings::get<bool>("main.ENABLE_MONSTROSITY") && PChar->GetMJob() == JOB_MON)
{
// Populates PChar->m_PMonstrosity
Expand Down
28 changes: 18 additions & 10 deletions src/map/status_effect_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,8 @@ void CStatusEffectContainer::SetEffectParams(CStatusEffect* StatusEffect)

void CStatusEffectContainer::LoadStatusEffects()
{
TracyZoneScoped;

if (m_POwner->objtype != TYPE_PC)
{
ShowWarning("Non-PC calling function (%s).", m_POwner->getName());
Expand All @@ -1544,19 +1546,19 @@ void CStatusEffectContainer::LoadStatusEffects()
"flags, "
"timestamp "
"FROM char_effects "
"WHERE charid = %u;";
"WHERE charid = (?);";

int32 ret = _sql->Query(Query, m_POwner->id);
auto rset = db::preparedStmt(Query, m_POwner->id);

std::vector<CStatusEffect*> PEffectList;

if (ret != SQL_ERROR && _sql->NumRows() != 0)
if (rset && rset->rowsCount())
{
while (_sql->NextRow() == SQL_SUCCESS)
while (rset->next())
{
auto flags = _sql->GetUIntData(8);
auto duration = _sql->GetUIntData(4);
auto effectID = (EFFECT)_sql->GetUIntData(0);
auto flags = rset->getUInt("flags");
auto duration = rset->getUInt("duration");
auto effectID = (EFFECT)rset->getUInt("effectid");

if (flags & EFFECTFLAG_OFFLINE_TICK)
{
Expand All @@ -1580,9 +1582,15 @@ void CStatusEffectContainer::LoadStatusEffects()
}
}
CStatusEffect* PStatusEffect =
new CStatusEffect(effectID, (uint16)_sql->GetUIntData(1), (uint16)_sql->GetUIntData(2),
_sql->GetUIntData(3), duration, _sql->GetUIntData(5), (uint16)_sql->GetUIntData(6),
(uint16)_sql->GetUIntData(7), flags);
new CStatusEffect(effectID,
(uint16)rset->getUInt("icon"),
(uint16)rset->getUInt("power"),
(uint16)rset->getUInt("tick"),
duration,
(uint16)rset->getUInt("subid"),
(uint16)rset->getUInt("subpower"),
(uint16)rset->getUInt("tier"),
flags);

PEffectList.emplace_back(PStatusEffect);

Expand Down
16 changes: 7 additions & 9 deletions src/map/utils/blueutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,26 +335,24 @@ namespace blueutils

void LoadSetSpells(CCharEntity* PChar)
{
TracyZoneScoped;

if (PChar->GetMJob() == JOB_BLU || PChar->GetSJob() == JOB_BLU)
{
const char* Query = "SELECT set_blue_spells FROM "
"chars WHERE charid = %u;";

int32 ret = _sql->Query(Query, PChar->id);
"chars WHERE charid = (?);";

if (ret != SQL_ERROR && _sql->NumRows() != 0 && _sql->NextRow() == SQL_SUCCESS)
auto rset = db::preparedStmt(Query, PChar->id);
if (rset && rset->rowsCount() && rset->next())
{
size_t length = 0;
char* blue_spells = nullptr;
_sql->GetData(0, &blue_spells, &length);
memcpy(PChar->m_SetBlueSpells, blue_spells, (length > sizeof(PChar->m_SetBlueSpells) ? sizeof(PChar->m_SetBlueSpells) : length));
db::extractBlob(rset, "set_blue_spells", PChar->m_SetBlueSpells);
}

for (unsigned char& m_SetBlueSpell : PChar->m_SetBlueSpells)
{
if (m_SetBlueSpell != 0)
{
CBlueSpell* PSpell = (CBlueSpell*)spell::GetSpell(static_cast<SpellID>(m_SetBlueSpell + 0x200));

if (PSpell == nullptr)
{
m_SetBlueSpell = 0;
Expand Down
Loading

0 comments on commit b2a79f3

Please sign in to comment.