GetNPCSpells converted to QueryDatabase

This commit is contained in:
Arthur Ice 2014-08-18 16:34:27 -07:00
parent 87efd22394
commit 52344bfe24

View File

@ -2627,7 +2627,7 @@ void NPC::AISpellsList(Client *c)
DBnpcspells_Struct* ZoneDatabase::GetNPCSpells(uint32 iDBSpellsID) { DBnpcspells_Struct* ZoneDatabase::GetNPCSpells(uint32 iDBSpellsID) {
if (iDBSpellsID == 0) if (iDBSpellsID == 0)
return 0; return nullptr;
if (!npc_spells_cache) { if (!npc_spells_cache) {
npc_spells_maxid = GetMaxNPCSpellsID(); npc_spells_maxid = GetMaxNPCSpellsID();
@ -2640,22 +2640,31 @@ DBnpcspells_Struct* ZoneDatabase::GetNPCSpells(uint32 iDBSpellsID) {
} }
if (iDBSpellsID > npc_spells_maxid) if (iDBSpellsID > npc_spells_maxid)
return 0; return nullptr;
if (npc_spells_cache[iDBSpellsID]) { // it's in the cache, easy =) if (npc_spells_cache[iDBSpellsID]) { // it's in the cache, easy =)
return npc_spells_cache[iDBSpellsID]; return npc_spells_cache[iDBSpellsID];
} }
else if (!npc_spells_loadtried[iDBSpellsID]) { // no reason to ask the DB again if we have failed once already else if (!npc_spells_loadtried[iDBSpellsID]) { // no reason to ask the DB again if we have failed once already
npc_spells_loadtried[iDBSpellsID] = true; npc_spells_loadtried[iDBSpellsID] = true;
char errbuf[MYSQL_ERRMSG_SIZE];
char *query = 0;
MYSQL_RES *result;
MYSQL_ROW row;
if (RunQuery(query, MakeAnyLenString(&query, "SELECT id, parent_list, attack_proc, proc_chance, range_proc, rproc_chance, defensive_proc, dproc_chance, fail_recast, engaged_no_sp_recast_min, engaged_no_sp_recast_max, engaged_b_self_chance, engaged_b_other_chance, engaged_d_chance, pursue_no_sp_recast_min, pursue_no_sp_recast_max, pursue_d_chance, idle_no_sp_recast_min, idle_no_sp_recast_max, idle_b_chance from npc_spells where id=%d", iDBSpellsID), errbuf, &result)) { std::string query = StringFormat("SELECT id, parent_list, attack_proc, proc_chance, "
safe_delete_array(query); "range_proc, rproc_chance, defensive_proc, dproc_chance, "
if (mysql_num_rows(result) == 1) { "fail_recast, engaged_no_sp_recast_min, engaged_no_sp_recast_max, "
row = mysql_fetch_row(result); "engaged_b_self_chance, engaged_b_other_chance, engaged_d_chance, "
"pursue_no_sp_recast_min, pursue_no_sp_recast_max, "
"pursue_d_chance, idle_no_sp_recast_min, idle_no_sp_recast_max, "
"idle_b_chance FROM npc_spells WHERE id=%d", iDBSpellsID);
auto results = QueryDatabase(query);
if (!results.Success()) {
std::cerr << "Error in AddNPCSpells query1 '" << query << "' " << results.ErrorMessage() << std::endl;
return nullptr;
}
if (results.RowCount() != 1)
return nullptr;
auto row = results.begin();
uint32 tmpparent_list = atoi(row[1]); uint32 tmpparent_list = atoi(row[1]);
uint16 tmpattack_proc = atoi(row[2]); uint16 tmpattack_proc = atoi(row[2]);
uint8 tmpproc_chance = atoi(row[3]); uint8 tmpproc_chance = atoi(row[3]);
@ -2675,10 +2684,20 @@ DBnpcspells_Struct* ZoneDatabase::GetNPCSpells(uint32 iDBSpellsID) {
uint32 tmpidle_no_sp_recast_min = atoi(row[17]); uint32 tmpidle_no_sp_recast_min = atoi(row[17]);
uint32 tmpidle_no_sp_recast_max = atoi(row[18]); uint32 tmpidle_no_sp_recast_max = atoi(row[18]);
uint8 tmpidle_b_chance = atoi(row[19]); uint8 tmpidle_b_chance = atoi(row[19]);
mysql_free_result(result);
if (RunQuery(query, MakeAnyLenString(&query, "SELECT spellid, type, minlevel, maxlevel, manacost, recast_delay, priority, resist_adjust from npc_spells_entries where npc_spells_id=%d ORDER BY minlevel", iDBSpellsID), errbuf, &result)) { query = StringFormat("SELECT spellid, type, minlevel, maxlevel, "
safe_delete_array(query); "manacost, recast_delay, priority, resist_adjust "
uint32 tmpSize = sizeof(DBnpcspells_Struct) + (sizeof(DBnpcspells_entries_Struct) * mysql_num_rows(result)); "FROM npc_spells_entries "
"WHERE npc_spells_id=%d ORDER BY minlevel", iDBSpellsID);
results = QueryDatabase(query);
if (!results.Success())
{
std::cerr << "Error in AddNPCSpells query1 '" << query << "' " << results.ErrorMessage() << std::endl;
return nullptr;
}
uint32 tmpSize = sizeof(DBnpcspells_Struct) + (sizeof(DBnpcspells_entries_Struct) * results.RowCount());
npc_spells_cache[iDBSpellsID] = (DBnpcspells_Struct*) new uchar[tmpSize]; npc_spells_cache[iDBSpellsID] = (DBnpcspells_Struct*) new uchar[tmpSize];
memset(npc_spells_cache[iDBSpellsID], 0, tmpSize); memset(npc_spells_cache[iDBSpellsID], 0, tmpSize);
npc_spells_cache[iDBSpellsID]->parent_list = tmpparent_list; npc_spells_cache[iDBSpellsID]->parent_list = tmpparent_list;
@ -2700,52 +2719,30 @@ DBnpcspells_Struct* ZoneDatabase::GetNPCSpells(uint32 iDBSpellsID) {
npc_spells_cache[iDBSpellsID]->idle_no_sp_recast_min = tmpidle_no_sp_recast_min; npc_spells_cache[iDBSpellsID]->idle_no_sp_recast_min = tmpidle_no_sp_recast_min;
npc_spells_cache[iDBSpellsID]->idle_no_sp_recast_max = tmpidle_no_sp_recast_max; npc_spells_cache[iDBSpellsID]->idle_no_sp_recast_max = tmpidle_no_sp_recast_max;
npc_spells_cache[iDBSpellsID]->idle_beneficial_chance = tmpidle_b_chance; npc_spells_cache[iDBSpellsID]->idle_beneficial_chance = tmpidle_b_chance;
npc_spells_cache[iDBSpellsID]->numentries = mysql_num_rows(result); npc_spells_cache[iDBSpellsID]->numentries = results.RowCount();
int j = 0;
while ((row = mysql_fetch_row(result))) { int entryIndex = 0;
for (row = results.begin(); row != results.end(); ++row, ++entryIndex)
{
int spell_id = atoi(row[0]); int spell_id = atoi(row[0]);
npc_spells_cache[iDBSpellsID]->entries[j].spellid = spell_id; npc_spells_cache[iDBSpellsID]->entries[entryIndex].spellid = spell_id;
npc_spells_cache[iDBSpellsID]->entries[j].type = atoi(row[1]); npc_spells_cache[iDBSpellsID]->entries[entryIndex].type = atoi(row[1]);
npc_spells_cache[iDBSpellsID]->entries[j].minlevel = atoi(row[2]); npc_spells_cache[iDBSpellsID]->entries[entryIndex].minlevel = atoi(row[2]);
npc_spells_cache[iDBSpellsID]->entries[j].maxlevel = atoi(row[3]); npc_spells_cache[iDBSpellsID]->entries[entryIndex].maxlevel = atoi(row[3]);
npc_spells_cache[iDBSpellsID]->entries[j].manacost = atoi(row[4]); npc_spells_cache[iDBSpellsID]->entries[entryIndex].manacost = atoi(row[4]);
npc_spells_cache[iDBSpellsID]->entries[j].recast_delay = atoi(row[5]); npc_spells_cache[iDBSpellsID]->entries[entryIndex].recast_delay = atoi(row[5]);
npc_spells_cache[iDBSpellsID]->entries[j].priority = atoi(row[6]); npc_spells_cache[iDBSpellsID]->entries[entryIndex].priority = atoi(row[6]);
if(row[7]) if(row[7])
{ npc_spells_cache[iDBSpellsID]->entries[entryIndex].resist_adjust = atoi(row[7]);
npc_spells_cache[iDBSpellsID]->entries[j].resist_adjust = atoi(row[7]); else if(IsValidSpell(spell_id))
} npc_spells_cache[iDBSpellsID]->entries[entryIndex].resist_adjust = spells[spell_id].ResistDiff;
else
{
if(IsValidSpell(spell_id))
{
npc_spells_cache[iDBSpellsID]->entries[j].resist_adjust = spells[spell_id].ResistDiff;
}
}
j++;
}
mysql_free_result(result);
return npc_spells_cache[iDBSpellsID];
}
else {
std::cerr << "Error in AddNPCSpells query1 '" << query << "' " << errbuf << std::endl;
safe_delete_array(query);
return 0;
}
}
else {
mysql_free_result(result);
}
}
else {
std::cerr << "Error in AddNPCSpells query1 '" << query << "' " << errbuf << std::endl;
safe_delete_array(query);
return 0;
} }
return 0; return npc_spells_cache[iDBSpellsID];
} }
return 0;
return nullptr;
} }
uint32 ZoneDatabase::GetMaxNPCSpellsID() { uint32 ZoneDatabase::GetMaxNPCSpellsID() {