From 118c2a9db97731ecb7603917991b77d287b36ec3 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 13:49:04 -0700 Subject: [PATCH 01/45] LoadAAs converted to QueryDatabase --- zone/bot.cpp | 224 ++++++++++++++++++++++++--------------------------- 1 file changed, 107 insertions(+), 117 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index e782e6db8..3032f5af5 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -1466,85 +1466,75 @@ void Bot::GenerateAABonuses(StatBonuses* newbon) { } void Bot::LoadAAs() { - std::string errorMessage; - char* Query = 0; - int length = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; - int maxAAExpansion = RuleI(Bots, BotAAExpansion); //get expansion to get AAs up to botAAs.clear(); //start fresh + std::string query; + if(GetClass() == BERSERKER) - length = MakeAnyLenString(&Query, "SELECT skill_id FROM altadv_vars WHERE berserker = 1 AND class_type > 1 AND class_type <= %i AND aa_expansion <= %i ORDER BY skill_id;", GetLevel(), maxAAExpansion); + query = StringFormat("SELECT skill_id FROM altadv_vars WHERE berserker = 1 AND class_type > 1 AND class_type <= %i AND aa_expansion <= %i ORDER BY skill_id;", GetLevel(), maxAAExpansion); else - length = MakeAnyLenString(&Query, "SELECT skill_id FROM altadv_vars WHERE ((classes & ( 1 << %i )) >> %i) = 1 AND class_type > 1 AND class_type <= %i AND aa_expansion <= %i ORDER BY skill_id;", GetClass(), GetClass(), GetLevel(), maxAAExpansion); + query = StringFormat("SELECT skill_id FROM altadv_vars WHERE ((classes & ( 1 << %i )) >> %i) = 1 AND class_type > 1 AND class_type <= %i AND aa_expansion <= %i ORDER BY skill_id;", GetClass(), GetClass(), GetLevel(), maxAAExpansion); - if(!database.RunQuery(Query, length, TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - int totalAAs = database.CountAAs(); + auto results = database.QueryDatabase(query); - while(DataRow = mysql_fetch_row(DatasetResult)) { - uint32 skill_id = 0; - skill_id = atoi(DataRow[0]); - - if(skill_id > 0 && skill_id < totalAAs) { - SendAA_Struct *sendAA = zone->FindAA(skill_id); - - if(sendAA) { - for(int i=0; imax_level; i++) { - //Get AA info & add to list - uint32 aaid = sendAA->id + i; - uint8 total_levels = 0; - uint8 req_level; - std::map::iterator RequiredLevel = AARequiredLevelAndCost.find(aaid); - - //Get level required for AA - if(RequiredLevel != AARequiredLevelAndCost.end()) - req_level = RequiredLevel->second.Level; - else - req_level = (sendAA->class_type + i * sendAA->level_inc); - - if(req_level > GetLevel()) - break; - - //Bot is high enough level for AA - std::map::iterator foundAA = botAAs.find(aaid); - - // AA is not already in list - if(foundAA == botAAs.end()) { - if(sendAA->id == aaid) { - BotAA newAA; - - newAA.total_levels = 0; - newAA.aa_id = aaid; - newAA.req_level = req_level; - newAA.total_levels += 1; - - botAAs[aaid] = newAA; //add to list - } - else { - //update master AA record with number of levels a bot has in AA, based on level. - botAAs[sendAA->id].total_levels+=1; - } - } - } - } - } - } - - mysql_free_result(DatasetResult); - } - - safe_delete(Query); - Query = 0; - - if(!errorMessage.empty()) { + if(!results.Success()) { LogFile->write(EQEMuLog::Error, "Error in Bot::LoadAAs()"); + return; } + + int totalAAs = database.CountAAs(); + + for (auto row = results.begin(); row != results.end(); ++row) { + uint32 skill_id = 0; + skill_id = atoi(row[0]); + + if(skill_id <= 0 || skill_id >= totalAAs) + continue; + + SendAA_Struct *sendAA = zone->FindAA(skill_id); + + if(!sendAA) + continue; + + for(int i=0; imax_level; i++) { + //Get AA info & add to list + uint32 aaid = sendAA->id + i; + uint8 total_levels = 0; + uint8 req_level; + std::map::iterator RequiredLevel = AARequiredLevelAndCost.find(aaid); + + //Get level required for AA + if(RequiredLevel != AARequiredLevelAndCost.end()) + req_level = RequiredLevel->second.Level; + else + req_level = (sendAA->class_type + i * sendAA->level_inc); + + if(req_level > GetLevel()) + break; + + //Bot is high enough level for AA + std::map::iterator foundAA = botAAs.find(aaid); + + // AA is already in list + if(foundAA != botAAs.end()) + continue; + + if(sendAA->id == aaid) { + BotAA newAA; + + newAA.total_levels = 0; + newAA.aa_id = aaid; + newAA.req_level = req_level; + newAA.total_levels += 1; + + botAAs[aaid] = newAA; //add to list + } + else //update master AA record with number of levels a bot has in AA, based on level. + botAAs[sendAA->id].total_levels+=1; + } + } + } uint32 Bot::GetAA(uint32 aa_id) { @@ -2485,10 +2475,10 @@ void Bot::SaveBuffs() { CalculateCorruptionCounters(buffs[BuffCount].spellid) > 0 ? buffs[BuffCount].counters : 0, buffs[BuffCount].numhits, buffs[BuffCount].melee_rune, buffs[BuffCount].magic_rune, buffs[BuffCount].dot_rune, - buffs[BuffCount].caston_x, - IsPersistent, + buffs[BuffCount].caston_x, + IsPersistent, buffs[BuffCount].caston_y, - buffs[BuffCount].caston_z, + buffs[BuffCount].caston_z, buffs[BuffCount].ExtraDIChance), TempErrorMessageBuffer)) { errorMessage = std::string(TempErrorMessageBuffer); safe_delete(Query); @@ -3398,7 +3388,7 @@ void Bot::DoMeleeSkillAttackDmg(Mob* other, uint16 weapon_damage, SkillUseTypes if (CanSkillProc && HasSkillProcs()) TrySkillProc(other, skillinuse, ReuseTime); - + if (CanSkillProc && (damage > 0) && HasSkillProcSuccess()) TrySkillProc(other, skillinuse, ReuseTime, true); } @@ -8109,7 +8099,7 @@ void Bot::DoSpecialAttackDamage(Mob *who, SkillUseTypes skill, int32 max_damage, if (HasSkillProcs()) TrySkillProc(who, skill, ReuseTime*1000); - + if (max_damage > 0 && HasSkillProcSuccess()) TrySkillProc(who, skill, ReuseTime*1000, true); @@ -9113,7 +9103,7 @@ void Bot::SetAttackTimer() { } int32 Bot::GetActSpellDamage(uint16 spell_id, int32 value, Mob* target) { - + if (spells[spell_id].targettype == ST_Self) return value; @@ -9125,22 +9115,22 @@ int32 Bot::GetActSpellDamage(uint16 spell_id, int32 value, Mob* target) { // Need to scale HT damage differently after level 40! It no longer scales by the constant value in the spell file. It scales differently, instead of 10 more damage per level, it does 30 more damage per level. So we multiply the level minus 40 times 20 if they are over level 40. if ( (spell_id == SPELL_HARM_TOUCH || spell_id == SPELL_HARM_TOUCH2 || spell_id == SPELL_IMP_HARM_TOUCH ) && GetLevel() > 40) value -= (GetLevel() - 40) * 20; - + //This adds the extra damage from the AA Unholy Touch, 450 per level to the AA Improved Harm TOuch. if (spell_id == SPELL_IMP_HARM_TOUCH) //Improved Harm Touch value -= GetAA(aaUnholyTouch) * 450; //Unholy Touch - + int chance = RuleI(Spells, BaseCritChance); chance += itembonuses.CriticalSpellChance + spellbonuses.CriticalSpellChance + aabonuses.CriticalSpellChance; - + if (chance > 0){ - + int32 ratio = RuleI(Spells, BaseCritRatio); //Critical modifier is applied from spell effects only. Keep at 100 for live like criticals. //Improved Harm Touch is a guaranteed crit if you have at least one level of SCF. if (spell_id == SPELL_IMP_HARM_TOUCH && (GetAA(aaSpellCastingFury) > 0) && (GetAA(aaUnholyTouch) > 0)) chance = 100; - + if (MakeRandomInt(1,100) <= chance){ Critical = true; ratio += itembonuses.SpellCritDmgIncrease + spellbonuses.SpellCritDmgIncrease + aabonuses.SpellCritDmgIncrease; @@ -9153,23 +9143,23 @@ int32 Bot::GetActSpellDamage(uint16 spell_id, int32 value, Mob* target) { } ratio += RuleI(Spells, WizCritRatio); //Default is zero - + if (Critical){ - value = value_BaseEffect*ratio/100; + value = value_BaseEffect*ratio/100; - value += value_BaseEffect*GetBotFocusEffect(BotfocusImprovedDamage, spell_id)/100; + value += value_BaseEffect*GetBotFocusEffect(BotfocusImprovedDamage, spell_id)/100; value += int(value_BaseEffect*GetBotFocusEffect(BotfocusFcDamagePctCrit, spell_id)/100)*ratio/100; if (target) { - value += int(value_BaseEffect*target->GetVulnerability(this, spell_id, 0)/100)*ratio/100; - value -= target->GetFcDamageAmtIncoming(this, spell_id); + value += int(value_BaseEffect*target->GetVulnerability(this, spell_id, 0)/100)*ratio/100; + value -= target->GetFcDamageAmtIncoming(this, spell_id); } - value -= GetBotFocusEffect(BotfocusFcDamageAmtCrit, spell_id)*ratio/100; + value -= GetBotFocusEffect(BotfocusFcDamageAmtCrit, spell_id)*ratio/100; - value -= GetBotFocusEffect(BotfocusFcDamageAmt, spell_id); + value -= GetBotFocusEffect(BotfocusFcDamageAmt, spell_id); if(itembonuses.SpellDmg && spells[spell_id].classes[(GetClass()%16) - 1] >= GetLevel() - 5) value += GetExtraSpellAmt(spell_id, itembonuses.SpellDmg, value)*ratio/100; @@ -9181,28 +9171,28 @@ int32 Bot::GetActSpellDamage(uint16 spell_id, int32 value, Mob* target) { } value = value_BaseEffect; - - value += value_BaseEffect*GetBotFocusEffect(BotfocusImprovedDamage, spell_id)/100; - + + value += value_BaseEffect*GetBotFocusEffect(BotfocusImprovedDamage, spell_id)/100; + value += value_BaseEffect*GetBotFocusEffect(BotfocusFcDamagePctCrit, spell_id)/100; if (target) { value += value_BaseEffect*target->GetVulnerability(this, spell_id, 0)/100; - value -= target->GetFcDamageAmtIncoming(this, spell_id); + value -= target->GetFcDamageAmtIncoming(this, spell_id); } - value -= GetBotFocusEffect(BotfocusFcDamageAmtCrit, spell_id); + value -= GetBotFocusEffect(BotfocusFcDamageAmtCrit, spell_id); + + value -= GetBotFocusEffect(BotfocusFcDamageAmt, spell_id); - value -= GetBotFocusEffect(BotfocusFcDamageAmt, spell_id); - if(itembonuses.SpellDmg && spells[spell_id].classes[(GetClass()%16) - 1] >= GetLevel() - 5) - value += GetExtraSpellAmt(spell_id, itembonuses.SpellDmg, value); + value += GetExtraSpellAmt(spell_id, itembonuses.SpellDmg, value); return value; } int32 Bot::GetActSpellHealing(uint16 spell_id, int32 value, Mob* target) { - + if (target == nullptr) target = this; @@ -9210,37 +9200,37 @@ int32 Bot::GetActSpellHealing(uint16 spell_id, int32 value, Mob* target) { int16 chance = 0; int8 modifier = 1; bool Critical = false; - - value_BaseEffect = value + (value*GetBotFocusEffect(BotfocusFcBaseEffects, spell_id)/100); - + + value_BaseEffect = value + (value*GetBotFocusEffect(BotfocusFcBaseEffects, spell_id)/100); + value = value_BaseEffect; - value += int(value_BaseEffect*GetBotFocusEffect(BotfocusImprovedHeal, spell_id)/100); - + value += int(value_BaseEffect*GetBotFocusEffect(BotfocusImprovedHeal, spell_id)/100); + // Instant Heals if(spells[spell_id].buffduration < 1) { - chance += itembonuses.CriticalHealChance + spellbonuses.CriticalHealChance + aabonuses.CriticalHealChance; + chance += itembonuses.CriticalHealChance + spellbonuses.CriticalHealChance + aabonuses.CriticalHealChance; + + chance += target->GetFocusIncoming(focusFcHealPctCritIncoming, SE_FcHealPctCritIncoming, this, spell_id); - chance += target->GetFocusIncoming(focusFcHealPctCritIncoming, SE_FcHealPctCritIncoming, this, spell_id); - if (spellbonuses.CriticalHealDecay) - chance += GetDecayEffectValue(spell_id, SE_CriticalHealDecay); - + chance += GetDecayEffectValue(spell_id, SE_CriticalHealDecay); + if(chance && (MakeRandomInt(0,99) < chance)) { Critical = true; modifier = 2; //At present time no critical heal amount modifier SPA exists. } - + value *= modifier; - value += GetBotFocusEffect(BotfocusFcHealAmtCrit, spell_id) * modifier; - value += GetBotFocusEffect(BotfocusFcHealAmt, spell_id); - value += target->GetFocusIncoming(focusFcHealAmtIncoming, SE_FcHealAmtIncoming, this, spell_id); + value += GetBotFocusEffect(BotfocusFcHealAmtCrit, spell_id) * modifier; + value += GetBotFocusEffect(BotfocusFcHealAmt, spell_id); + value += target->GetFocusIncoming(focusFcHealAmtIncoming, SE_FcHealAmtIncoming, this, spell_id); if(itembonuses.HealAmt && spells[spell_id].classes[(GetClass()%16) - 1] >= GetLevel() - 5) value += GetExtraSpellAmt(spell_id, itembonuses.HealAmt, value) * modifier; - value += value*target->GetHealRate(spell_id, this)/100; + value += value*target->GetHealRate(spell_id, this)/100; if (Critical) entity_list.MessageClose(this, false, 100, MT_SpellCrits, "%s performs an exceptional heal! (%d)", GetName(), value); @@ -9250,14 +9240,14 @@ int32 Bot::GetActSpellHealing(uint16 spell_id, int32 value, Mob* target) { //Heal over time spells. [Heal Rate and Additional Healing effects do not increase this value] else { - - chance = itembonuses.CriticalHealOverTime + spellbonuses.CriticalHealOverTime + aabonuses.CriticalHealOverTime; - chance += target->GetFocusIncoming(focusFcHealPctCritIncoming, SE_FcHealPctCritIncoming, this, spell_id); - + chance = itembonuses.CriticalHealOverTime + spellbonuses.CriticalHealOverTime + aabonuses.CriticalHealOverTime; + + chance += target->GetFocusIncoming(focusFcHealPctCritIncoming, SE_FcHealPctCritIncoming, this, spell_id); + if (spellbonuses.CriticalRegenDecay) chance += GetDecayEffectValue(spell_id, SE_CriticalRegenDecay); - + if(chance && (MakeRandomInt(0,99) < chance)) return (value * 2); } From 9eb3907d4565ea96654e08d9184ffa86832c6fa4 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 13:59:05 -0700 Subject: [PATCH 02/45] IsBotNameValid converted to QueryDatabase --- zone/bot.cpp | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 3032f5af5..6fd06736f 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2339,35 +2339,29 @@ bool Bot::IsValidName() { } bool Bot::IsBotNameAvailable(std::string* errorMessage) { - bool Result = false; - if(this->GetCleanName()) { - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(!this->GetCleanName()) + return false; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM vwBotCharacterMobs WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult)) { - *errorMessage = std::string(TempErrorMessageBuffer); - } - else { - uint32 ExistingNameCount = 0; + std::string query = StringFormat("SELECT COUNT(id) FROM vwBotCharacterMobs " + "WHERE name LIKE '%s'", this->GetCleanName()); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return false; + } - while(DataRow = mysql_fetch_row(DatasetResult)) { - ExistingNameCount = atoi(DataRow[0]); - break; - } + uint32 existingNameCount = 0; - if(ExistingNameCount == 0) - Result = true; + for (auto row = results.begin(); row != results.end(); ++row) { + existingNameCount = atoi(row[0]); + break; + } - mysql_free_result(DatasetResult); - } + if(existingNameCount != 0) + return false; - safe_delete(Query); - } - - return Result; + return true; } bool Bot::Save() { From 0178f3c9bbfcc9dbffa4eb403a3bce31514da2fd Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 14:16:21 -0700 Subject: [PATCH 03/45] Save converted to QueryDatabase --- zone/bot.cpp | 101 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 37 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 6fd06736f..71139a0f0 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2365,53 +2365,80 @@ bool Bot::IsBotNameAvailable(std::string* errorMessage) { } bool Bot::Save() { - bool Result = false; - std::string errorMessage; - - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - uint32 affectedRows = 0; if(this->GetBotID() == 0) { // New bot record - uint32 TempNewBotID = 0; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "INSERT INTO bots (BotOwnerCharacterID, BotSpellsID, Name, LastName, BotLevel, Race, Class, Gender, Size, Face, LuclinHairStyle, LuclinHairColor, LuclinEyeColor, LuclinEyeColor2, LuclinBeardColor, LuclinBeard, DrakkinHeritage, DrakkinTattoo, DrakkinDetails, HP, Mana, MR, CR, DR, FR, PR, Corrup, AC, STR, STA, DEX, AGI, _INT, WIS, CHA, ATK, LastSpawnDate, TotalPlayTime, LastZoneId) VALUES('%u', '%u', '%s', '%s', '%u', '%i', '%i', '%i', '%f', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', NOW(), 0, %i)", this->_botOwnerCharacterID, this->GetBotSpellID(), this->GetCleanName(), this->lastname, this->GetLevel(), GetRace(), GetClass(), GetGender(), GetSize(), this->GetLuclinFace(), this->GetHairStyle(), GetHairColor(), this->GetEyeColor1(), this->GetEyeColor2(), this->GetBeardColor(), this->GetBeard(), this->GetDrakkinHeritage(), this->GetDrakkinTattoo(), this->GetDrakkinDetails(), GetHP(), GetMana(), GetMR(), GetCR(), GetDR(), GetFR(), GetPR(), GetCorrup(), GetAC(), GetSTR(), GetSTA(), GetDEX(), GetAGI(), GetINT(), GetWIS(), GetCHA(), GetATK(), _lastZoneId), TempErrorMessageBuffer, 0, &affectedRows, &TempNewBotID)) { - errorMessage = std::string(TempErrorMessageBuffer); + std::string query = StringFormat("INSERT INTO bots (BotOwnerCharacterID, BotSpellsID, Name, LastName, " + "BotLevel, Race, Class, Gender, Size, Face, LuclinHairStyle, " + "LuclinHairColor, LuclinEyeColor, LuclinEyeColor2, LuclinBeardColor, " + "LuclinBeard, DrakkinHeritage, DrakkinTattoo, DrakkinDetails, HP, Mana, " + "MR, CR, DR, FR, PR, Corrup, AC, STR, STA, DEX, AGI, _INT, WIS, CHA, ATK, " + "LastSpawnDate, TotalPlayTime, LastZoneId) " + "VALUES('%u', '%u', '%s', '%s', '%u', '%i', '%i', '%i', '%f', '%i', '%i', " + "'%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', " + "'%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', " + "'%i', NOW(), 0, %i)", + this->_botOwnerCharacterID, this->GetBotSpellID(), this->GetCleanName(), + this->lastname, this->GetLevel(), GetRace(), GetClass(), GetGender(), + GetSize(), this->GetLuclinFace(), this->GetHairStyle(), GetHairColor(), + this->GetEyeColor1(), this->GetEyeColor2(), this->GetBeardColor(), + this->GetBeard(), this->GetDrakkinHeritage(), this->GetDrakkinTattoo(), + this->GetDrakkinDetails(), GetHP(), GetMana(), GetMR(), GetCR(), GetDR(), + GetFR(), GetPR(), GetCorrup(), GetAC(), GetSTR(), GetSTA(), GetDEX(), + GetAGI(), GetINT(), GetWIS(), GetCHA(), GetATK(), _lastZoneId); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + auto botOwner = GetBotOwner(); + if (botOwner) + botOwner->Message(13, results.ErrorMessage().c_str()); + return false; } - else { - SetBotID(TempNewBotID); - Result = true; - } - } - else { - // Update existing bot record - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "UPDATE bots SET BotOwnerCharacterID = '%u', BotSpellsID = '%u', Name = '%s', LastName = '%s', BotLevel = '%u', Race = '%i', Class = '%i', Gender = '%i', Size = '%f', Face = '%i', LuclinHairStyle = '%i', LuclinHairColor = '%i', LuclinEyeColor = '%i', LuclinEyeColor2 = '%i', LuclinBeardColor = '%i', LuclinBeard = '%i', DrakkinHeritage = '%i', DrakkinTattoo = '%i', DrakkinDetails = '%i', HP = '%i', Mana = '%i', MR = '%i', CR = '%i', DR = '%i', FR = '%i', PR = '%i', Corrup = '%i', AC = '%i', STR = '%i', STA = '%i', DEX = '%i', AGI = '%i', _INT = '%i', WIS = '%i', CHA = '%i', ATK = '%i', LastSpawnDate = NOW(), TotalPlayTime = '%u', LastZoneId = %i WHERE BotID = '%u'", _botOwnerCharacterID, this->GetBotSpellID(), this->GetCleanName(), this->lastname, this->GetLevel(), _baseRace, this->GetClass(), _baseGender, GetSize(), this->GetLuclinFace(), this->GetHairStyle(), GetHairColor(), this->GetEyeColor1(), this->GetEyeColor2(), this->GetBeardColor(), this->GetBeard(), this->GetDrakkinHeritage(), GetDrakkinTattoo(), GetDrakkinDetails(), GetHP(), GetMana(), _baseMR, _baseCR, _baseDR, _baseFR, _basePR, _baseCorrup, _baseAC, _baseSTR, _baseSTA, _baseDEX, _baseAGI, _baseINT, _baseWIS, _baseCHA, _baseATK, GetTotalPlayTime(), _lastZoneId, GetBotID()), TempErrorMessageBuffer, 0, &affectedRows)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - Result = true; - time(&_startTotalPlayTime); - } - } - safe_delete(Query); - - if(!errorMessage.empty() || (Result && affectedRows != 1)) { - if(GetBotOwner() && !errorMessage.empty()) - GetBotOwner()->Message(13, errorMessage.c_str()); - else if(GetBotOwner()) - GetBotOwner()->Message(13, std::string("Unable to save bot to the database.").c_str()); - - Result = false; - } - else { - SaveBuffs(); + SetBotID(results.LastInsertedID()); + SaveBuffs(); SavePet(); SaveStance(); SaveTimers(); + return true; } - return Result; + + // Update existing bot record + std::string query = StringFormat("UPDATE bots SET BotOwnerCharacterID = '%u', BotSpellsID = '%u', " + "Name = '%s', LastName = '%s', BotLevel = '%u', Race = '%i', " + "Class = '%i', Gender = '%i', Size = '%f', Face = '%i', " + "LuclinHairStyle = '%i', LuclinHairColor = '%i', " + "LuclinEyeColor = '%i', LuclinEyeColor2 = '%i', " + "LuclinBeardColor = '%i', LuclinBeard = '%i', DrakkinHeritage = '%i', " + "DrakkinTattoo = '%i', DrakkinDetails = '%i', HP = '%i', Mana = '%i', " + "MR = '%i', CR = '%i', DR = '%i', FR = '%i', PR = '%i', " + "Corrup = '%i', AC = '%i', STR = '%i', STA = '%i', DEX = '%i', " + "AGI = '%i', _INT = '%i', WIS = '%i', CHA = '%i', ATK = '%i', " + "LastSpawnDate = NOW(), TotalPlayTime = '%u', LastZoneId = %i " + "WHERE BotID = '%u'", + _botOwnerCharacterID, this->GetBotSpellID(), this->GetCleanName(), + this->lastname, this->GetLevel(), _baseRace, this->GetClass(), + _baseGender, GetSize(), this->GetLuclinFace(), this->GetHairStyle(), + GetHairColor(), this->GetEyeColor1(), this->GetEyeColor2(), + this->GetBeardColor(), this->GetBeard(), this->GetDrakkinHeritage(), + GetDrakkinTattoo(), GetDrakkinDetails(), GetHP(), GetMana(), + _baseMR, _baseCR, _baseDR, _baseFR, _basePR, _baseCorrup, _baseAC, + _baseSTR, _baseSTA, _baseDEX, _baseAGI, _baseINT, _baseWIS, _baseCHA, + _baseATK, GetTotalPlayTime(), _lastZoneId, GetBotID()); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + auto botOwner = GetBotOwner(); + if (botOwner) + botOwner->Message(13, results.ErrorMessage().c_str()); + return false; + } + + SaveBuffs(); + SavePet(); + SaveStance(); + SaveTimers(); + + return true; } // Returns the current total play time for the bot From 0574a3db868e0f11e9e97f5999a28a09ad5a2c41 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 14:57:39 -0700 Subject: [PATCH 04/45] SaveBuffs converted to QueryDatabase --- zone/bot.cpp | 86 ++++++++++++++++++---------------------------------- 1 file changed, 30 insertions(+), 56 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 71139a0f0..42eae5db1 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2402,7 +2402,6 @@ bool Bot::Save() { return true; } - // Update existing bot record std::string query = StringFormat("UPDATE bots SET BotOwnerCharacterID = '%u', BotSpellsID = '%u', " "Name = '%s', LastName = '%s', BotLevel = '%u', Race = '%i', " @@ -2459,66 +2458,41 @@ uint32 Bot::GetTotalPlayTime() { } void Bot::SaveBuffs() { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - int BuffCount = 0; - int InsertCount = 0; - uint32 buff_count = GetMaxTotalSlots(); - while(BuffCount < BUFF_COUNT) { - if(buffs[BuffCount].spellid > 0 && buffs[BuffCount].spellid != SPELL_UNKNOWN) { - if(InsertCount == 0) { - // Remove any existing buff saves - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM botbuffs WHERE BotId = %u", GetBotID()), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - safe_delete(Query); - Query = 0; - break; - } - } + // Remove any existing buff saves + std::string query = StringFormat("DELETE FROM botbuffs WHERE BotId = %u", GetBotID()); + auto results = database.QueryDatabase(query); + if(!results.Success()) + return; - int IsPersistent = 0; + for (int buffIndex = 0; buffIndex < BUFF_COUNT; buffIndex++) { + if (buffs[buffIndex].spellid <= 0 || buffs[buffIndex].spellid == SPELL_UNKNOWN) + continue; - if(buffs[BuffCount].persistant_buff) - IsPersistent = 1; - else - IsPersistent = 0; + int isPersistent = buffs[buffIndex].persistant_buff? 1: 0; + query = StringFormat("INSERT INTO botbuffs (BotId, SpellId, CasterLevel, DurationFormula, " + "TicsRemaining, PoisonCounters, DiseaseCounters, CurseCounters, " + "CorruptionCounters, HitCount, MeleeRune, MagicRune, dot_rune, " + "caston_x, Persistent, caston_y, caston_z, ExtraDIChance) " + "VALUES (%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %i, %u, " + "%i, %i, %i);", + GetBotID(), buffs[buffIndex].spellid, buffs[buffIndex].casterlevel, + spells[buffs[buffIndex].spellid].buffdurationformula, + buffs[buffIndex].ticsremaining, + CalculatePoisonCounters(buffs[buffIndex].spellid) > 0 ? buffs[buffIndex].counters : 0, + CalculateDiseaseCounters(buffs[buffIndex].spellid) > 0 ? buffs[buffIndex].counters : 0, + CalculateCurseCounters(buffs[buffIndex].spellid) > 0 ? buffs[buffIndex].counters : 0, + CalculateCorruptionCounters(buffs[buffIndex].spellid) > 0 ? buffs[buffIndex].counters : 0, + buffs[buffIndex].numhits, buffs[buffIndex].melee_rune, + buffs[buffIndex].magic_rune, buffs[buffIndex].dot_rune, + buffs[buffIndex].caston_x, isPersistent, buffs[buffIndex].caston_y, + buffs[buffIndex].caston_z, buffs[buffIndex].ExtraDIChance); + auto results = database.QueryDatabase(query); + if(!results.Success()) + return; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "INSERT INTO botbuffs (BotId, SpellId, CasterLevel, DurationFormula, " - "TicsRemaining, PoisonCounters, DiseaseCounters, CurseCounters, CorruptionCounters, HitCount, MeleeRune, MagicRune, " - "dot_rune, caston_x, Persistent, caston_y, caston_z, ExtraDIChance) VALUES (%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %i, %u, %i, %i, %i);", - GetBotID(), buffs[BuffCount].spellid, buffs[BuffCount].casterlevel, spells[buffs[BuffCount].spellid].buffdurationformula, - buffs[BuffCount].ticsremaining, - CalculatePoisonCounters(buffs[BuffCount].spellid) > 0 ? buffs[BuffCount].counters : 0, - CalculateDiseaseCounters(buffs[BuffCount].spellid) > 0 ? buffs[BuffCount].counters : 0, - CalculateCurseCounters(buffs[BuffCount].spellid) > 0 ? buffs[BuffCount].counters : 0, - CalculateCorruptionCounters(buffs[BuffCount].spellid) > 0 ? buffs[BuffCount].counters : 0, - buffs[BuffCount].numhits, buffs[BuffCount].melee_rune, buffs[BuffCount].magic_rune, - buffs[BuffCount].dot_rune, - buffs[BuffCount].caston_x, - IsPersistent, - buffs[BuffCount].caston_y, - buffs[BuffCount].caston_z, - buffs[BuffCount].ExtraDIChance), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - safe_delete(Query); - Query = 0; - break; - } - else { - safe_delete(Query); - Query = 0; - InsertCount++; - } - } + } - BuffCount++; - } - - if(!errorMessage.empty()) { - // TODO: Record this error message to zone error log - } } void Bot::LoadBuffs() { From 87f1f78b67eb395f1bb42bfb84af6b1b4d649454 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:11:33 -0700 Subject: [PATCH 05/45] LoadBuffs converted to QueryDatabase --- zone/bot.cpp | 97 ++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 60 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 42eae5db1..3dd5c8bc4 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2496,77 +2496,54 @@ void Bot::SaveBuffs() { } void Bot::LoadBuffs() { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; - bool BuffsLoaded = false; + std::string query = StringFormat("SELECT SpellId, CasterLevel, DurationFormula, TicsRemaining, " + "PoisonCounters, DiseaseCounters, CurseCounters, CorruptionCounters, " + "HitCount, MeleeRune, MagicRune, dot_rune, caston_x, Persistent, " + "caston_y, caston_z, ExtraDIChance FROM botbuffs WHERE BotId = %u", + GetBotID()); + auto results = database.QueryDatabase(query); + if(!results.Success()) + return; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT SpellId, CasterLevel, DurationFormula, TicsRemaining, PoisonCounters, DiseaseCounters, CurseCounters, CorruptionCounters, HitCount, MeleeRune, MagicRune, dot_rune, caston_x, Persistent, caston_y, caston_z, ExtraDIChance FROM botbuffs WHERE BotId = %u", GetBotID()), TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - int BuffCount = 0; + int buffCount = 0; - while(DataRow = mysql_fetch_row(DatasetResult)) { - if(BuffCount == BUFF_COUNT) - break; + for (auto row = results.begin(); row != results.end(); ++row) { + if(buffCount == BUFF_COUNT) + break; - buffs[BuffCount].spellid = atoi(DataRow[0]); - buffs[BuffCount].casterlevel = atoi(DataRow[1]); - buffs[BuffCount].ticsremaining = atoi(DataRow[3]); + buffs[buffCount].spellid = atoi(row[0]); + buffs[buffCount].casterlevel = atoi(row[1]); + buffs[buffCount].ticsremaining = atoi(row[3]); - if(CalculatePoisonCounters(buffs[BuffCount].spellid) > 0) { - buffs[BuffCount].counters = atoi(DataRow[4]); - } else if(CalculateDiseaseCounters(buffs[BuffCount].spellid) > 0) { - buffs[BuffCount].counters = atoi(DataRow[5]); - } else if(CalculateCurseCounters(buffs[BuffCount].spellid) > 0) { - buffs[BuffCount].counters = atoi(DataRow[6]); - } else if(CalculateCorruptionCounters(buffs[BuffCount].spellid) > 0) { - buffs[BuffCount].counters = atoi(DataRow[7]); - } - buffs[BuffCount].numhits = atoi(DataRow[8]); - buffs[BuffCount].melee_rune = atoi(DataRow[9]); - buffs[BuffCount].magic_rune = atoi(DataRow[10]); - buffs[BuffCount].dot_rune = atoi(DataRow[11]); - buffs[BuffCount].caston_x = atoi(DataRow[12]); - buffs[BuffCount].casterid = 0; + if(CalculatePoisonCounters(buffs[buffCount].spellid) > 0) + buffs[buffCount].counters = atoi(row[4]); + else if(CalculateDiseaseCounters(buffs[buffCount].spellid) > 0) + buffs[buffCount].counters = atoi(row[5]); + else if(CalculateCurseCounters(buffs[buffCount].spellid) > 0) + buffs[buffCount].counters = atoi(row[6]); + else if(CalculateCorruptionCounters(buffs[buffCount].spellid) > 0) + buffs[buffCount].counters = atoi(row[7]); - bool IsPersistent = false; + buffs[buffCount].numhits = atoi(row[8]); + buffs[buffCount].melee_rune = atoi(row[9]); + buffs[buffCount].magic_rune = atoi(row[10]); + buffs[buffCount].dot_rune = atoi(row[11]); + buffs[buffCount].caston_x = atoi(row[12]); + buffs[buffCount].casterid = 0; - if(atoi(DataRow[13])) - IsPersistent = true; + buffs[buffCount].persistant_buff = atoi(row[13])? true: false; - buffs[BuffCount].caston_y = atoi(DataRow[14]); - buffs[BuffCount].caston_z = atoi(DataRow[15]); - buffs[BuffCount].ExtraDIChance = atoi(DataRow[16]); + buffs[buffCount].caston_y = atoi(row[14]); + buffs[buffCount].caston_z = atoi(row[15]); + buffs[buffCount].ExtraDIChance = atoi(row[16]); - buffs[BuffCount].persistant_buff = IsPersistent; + buffCount++; + } - BuffCount++; - } + query = StringFormat("DELETE FROM botbuffs WHERE BotId = %u", GetBotID()); + results = database.QueryDatabase(query); - mysql_free_result(DatasetResult); - - BuffsLoaded = true; - } - - safe_delete(Query); - Query = 0; - - if(errorMessage.empty() && BuffsLoaded) { - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM botbuffs WHERE BotId = %u", GetBotID()), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - safe_delete(Query); - Query = 0; - } - } - - if(!errorMessage.empty()) { - // TODO: Record this error message to zone error log - } } uint32 Bot::GetPetSaveId() { From 93aa690a6d7cfe0ed84c5d2621520b725de11743 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:14:21 -0700 Subject: [PATCH 06/45] GetPetSaveId converted to QueryDatabase --- zone/bot.cpp | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 3dd5c8bc4..e7339f888 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2547,32 +2547,18 @@ void Bot::LoadBuffs() { } uint32 Bot::GetPetSaveId() { - uint32 Result = 0; - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "select BotPetsId from botpets where BotId = %u;", GetBotID()), TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - while(DataRow = mysql_fetch_row(DatasetResult)) { - Result = atoi(DataRow[0]); - break; - } + std::string query = StringFormat("SELECT BotPetsId FROM botpets WHERE BotId = %u;", GetBotID()); + auto results = database.QueryDatabase(query); + if(!results.Success()) + return 0; - mysql_free_result(DatasetResult); - } + if (results.RowCount() == 0) + return 0; - safe_delete(Query); + auto row = results.begin(); - if(!errorMessage.empty()) { - // TODO: Record this error message to zone error log - } - - return Result; + return atoi(row[0]); } void Bot::LoadPet() { From c8e7d9e005e91d44550f9a546690a139f91ed878 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:19:44 -0700 Subject: [PATCH 07/45] LoadPetStats converted to QueryDatabase --- zone/bot.cpp | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index e7339f888..687019979 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2594,39 +2594,25 @@ void Bot::LoadPet() { } void Bot::LoadPetStats(std::string* petName, uint16* petMana, uint16* petHitPoints, uint32* botPetId, uint32 botPetSaveId) { - if(botPetSaveId > 0) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botPetSaveId == 0) + return; - bool statsLoaded = false; + std::string query = StringFormat("SELECT PetId, Name, Mana, HitPoints " + "FROM botpets WHERE BotPetsId = %u;", + botPetSaveId); + auto results = database.QueryDatabase(query); + if(!results.Success()) + return; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "select PetId, Name, Mana, HitPoints from botpets where BotPetsId = %u;", botPetSaveId), TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - while(DataRow = mysql_fetch_row(DatasetResult)) { - *botPetId = atoi(DataRow[0]); - *petName = std::string(DataRow[1]); - *petMana = atoi(DataRow[2]); - *petHitPoints = atoi(DataRow[3]); - break; - } + if (results.RowCount() == 0) + return; - mysql_free_result(DatasetResult); + auto row = results.begin(); - statsLoaded = true; - } - - safe_delete(Query); - Query = 0; - - if(!errorMessage.empty()) { - // TODO: Record this error message to zone error log - } - } + *botPetId = atoi(row[0]); + *petName = std::string(row[1]); + *petMana = atoi(row[2]); + *petHitPoints = atoi(row[3]); } void Bot::LoadPetBuffs(SpellBuff_Struct* petBuffs, uint32 botPetSaveId) { From a486db5e957b1618eaa40dfe146078a7fa032159 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:28:13 -0700 Subject: [PATCH 08/45] LoadPetBuffs converted to QueryDatabase --- zone/bot.cpp | 58 +++++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 39 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 687019979..c5374bef7 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2616,52 +2616,32 @@ void Bot::LoadPetStats(std::string* petName, uint16* petMana, uint16* petHitPoin } void Bot::LoadPetBuffs(SpellBuff_Struct* petBuffs, uint32 botPetSaveId) { - if(petBuffs && botPetSaveId > 0) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(!petBuffs || botPetSaveId == 0) + return; - bool BuffsLoaded = false; + std::string query = StringFormat("SELECT SpellId, CasterLevel, Duration " + "FROM botpetbuffs WHERE BotPetsId = %u;", botPetSaveId); + auto results = database.QueryDatabase(query); + if(!results.Success()) + return; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT SpellId, CasterLevel, Duration FROM botpetbuffs WHERE BotPetsId = %u;", botPetSaveId), TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - int BuffCount = 0; - while(DataRow = mysql_fetch_row(DatasetResult)) { - if(BuffCount == BUFF_COUNT) - break; + int buffIndex = 0; - petBuffs[BuffCount].spellid = atoi(DataRow[0]); - petBuffs[BuffCount].level = atoi(DataRow[1]); - petBuffs[BuffCount].duration = atoi(DataRow[2]); + for (auto row = results.begin();row != results.end(); ++row) { + if(buffIndex == BUFF_COUNT) + break; - BuffCount++; - } + petBuffs[buffIndex].spellid = atoi(row[0]); + petBuffs[buffIndex].level = atoi(row[1]); + petBuffs[buffIndex].duration = atoi(row[2]); - mysql_free_result(DatasetResult); - - BuffsLoaded = true; - } - - safe_delete(Query); - Query = 0; - - if(errorMessage.empty() && BuffsLoaded) { - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM botpetbuffs WHERE BotPetsId = %u;", botPetSaveId), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - safe_delete(Query); - Query = 0; - } - } - - if(!errorMessage.empty()) { - // TODO: Record this error message to zone error log - } + buffIndex++; } + + query = StringFormat("DELETE FROM botpetbuffs WHERE BotPetsId = %u;", botPetSaveId); + results = database.QueryDatabase(query); + } void Bot::LoadPetItems(uint32* petItems, uint32 botPetSaveId) { From 364a51b11905a518b03ccf42edf881ab50349a8a Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:38:15 -0700 Subject: [PATCH 09/45] LoadPetItems converted to QueryDatabase --- zone/bot.cpp | 53 ++++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index c5374bef7..ce8239e24 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2645,50 +2645,29 @@ void Bot::LoadPetBuffs(SpellBuff_Struct* petBuffs, uint32 botPetSaveId) { } void Bot::LoadPetItems(uint32* petItems, uint32 botPetSaveId) { - if(petItems && botPetSaveId > 0) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(!petItems || botPetSaveId == 0) + return; - bool itemsLoaded = false; + std::string query = StringFormat("SELECT ItemId FROM botpetinventory " + "WHERE BotPetsId = %u;", botPetSaveId); + auto results = database.QueryDatabase(query); + if(!results.Success()) + return; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT ItemId FROM botpetinventory WHERE BotPetsId = %u;", botPetSaveId), TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - int ItemCount = 0; + int itemIndex = 0; - while(DataRow = mysql_fetch_row(DatasetResult)) { - if(ItemCount == EmuConstants::EQUIPMENT_SIZE) - break; + for(auto row = results.begin(); row != results.end(); ++row) { + if(itemIndex == EmuConstants::EQUIPMENT_SIZE) + break; - petItems[ItemCount] = atoi(DataRow[0]); + petItems[itemIndex] = atoi(row[0]); - ItemCount++; - } + itemIndex++; + } - mysql_free_result(DatasetResult); + query = StringFormat("DELETE FROM botpetinventory WHERE BotPetsId = %u;", botPetSaveId); + results = database.QueryDatabase(query); - itemsLoaded = true; - } - - safe_delete(Query); - Query = 0; - - if(errorMessage.empty() && itemsLoaded) { - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM botpetinventory WHERE BotPetsId = %u;", botPetSaveId), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - safe_delete(Query); - Query = 0; - } - } - - if(!errorMessage.empty()) { - // TODO: Record this error message to zone error log - } - } } void Bot::SavePet() { From ab76783f8bef2b0f13341a3a07ce55ac73ecc2e1 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:42:32 -0700 Subject: [PATCH 10/45] SavePetStats converted to QueryDatabase --- zone/bot.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index ce8239e24..a617e57b7 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2707,20 +2707,13 @@ void Bot::SavePet() { } uint32 Bot::SavePetStats(std::string petName, uint16 petMana, uint16 petHitPoints, uint32 botPetId) { - uint32 Result = 0; - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; + std::string query = StringFormat("REPLACE INTO botpets SET PetId = %u, BotId = %u, Name = '%s', " + "Mana = %u, HitPoints = %u;", botPetId, GetBotID(), petName.c_str(), + petMana, petHitPoints); + auto results = database.QueryDatabase(query); - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "REPLACE INTO botpets SET PetId = %u, BotId = %u, Name = '%s', Mana = %u, HitPoints = %u;", botPetId, GetBotID(), petName.c_str(), petMana, petHitPoints), TempErrorMessageBuffer, 0, 0, &Result)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - - safe_delete(Query); - Query = 0; - - return Result; + return 0; } void Bot::SavePetBuffs(SpellBuff_Struct* petBuffs, uint32 botPetSaveId) { From fe753f05df966fc0507fd10f429cfce0e399c4c3 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:47:14 -0700 Subject: [PATCH 11/45] SavePetBuffs converted to QueryDatabase --- zone/bot.cpp | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index a617e57b7..5ceb52464 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2717,33 +2717,28 @@ uint32 Bot::SavePetStats(std::string petName, uint16 petMana, uint16 petHitPoint } void Bot::SavePetBuffs(SpellBuff_Struct* petBuffs, uint32 botPetSaveId) { - if(petBuffs && botPetSaveId > 0) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - int BuffCount = 0; + if(!petBuffs || botPetSaveId == 0) + return; - while(BuffCount < BUFF_COUNT) { - if(petBuffs[BuffCount].spellid > 0 && petBuffs[BuffCount].spellid != SPELL_UNKNOWN) { - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "INSERT INTO botpetbuffs (BotPetsId, SpellId, CasterLevel, Duration) VALUES(%u, %u, %u, %u);", botPetSaveId, petBuffs[BuffCount].spellid, petBuffs[BuffCount].level, petBuffs[BuffCount].duration), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - safe_delete(Query); - Query = 0; - break; - } - else { - safe_delete(Query); - Query = 0; - } - } + int buffIndex = 0; - BuffCount++; - } + while(buffIndex < BUFF_COUNT) { + if(petBuffs[buffIndex].spellid > 0 && petBuffs[buffIndex].spellid != SPELL_UNKNOWN) { + + std::string query = StringFormat("INSERT INTO botpetbuffs " + "(BotPetsId, SpellId, CasterLevel, Duration) " + "VALUES(%u, %u, %u, %u);", + botPetSaveId, petBuffs[buffIndex].spellid, + petBuffs[buffIndex].level, petBuffs[buffIndex].duration); + auto results = database.QueryDatabase(query); + if(!results.Success()) + break; + + } + + buffIndex++; + } - if(!errorMessage.empty()) { - // TODO: Record this error message to zone error log - } - } } void Bot::SavePetItems(uint32* petItems, uint32 botPetSaveId) { From 54b2c50109e15bc3c003afbe3ae41432d2646a15 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:53:32 -0700 Subject: [PATCH 12/45] SavePetItems converted to QueryDatabase --- zone/bot.cpp | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 5ceb52464..719f546b8 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2742,34 +2742,21 @@ void Bot::SavePetBuffs(SpellBuff_Struct* petBuffs, uint32 botPetSaveId) { } void Bot::SavePetItems(uint32* petItems, uint32 botPetSaveId) { - if(petItems && botPetSaveId > 0) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - int ItemCount = 0; + if(!petItems || botPetSaveId == 0) + return; - while (ItemCount < EmuConstants::EQUIPMENT_SIZE) { - if(petItems[ItemCount] > 0) { - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "INSERT INTO botpetinventory (BotPetsId, ItemId) VALUES(%u, %u);", botPetSaveId, petItems[ItemCount]), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - safe_delete(Query); - Query = 0; - break; - } - else { - safe_delete(Query); - Query = 0; - ItemCount++; - } - } + for (int itemIndex = 0;itemIndex < EmuConstants::EQUIPMENT_SIZE; itemIndex++) { + if(petItems[itemIndex] == 0) + continue; - ItemCount++; - } - - if(!errorMessage.empty()) { - // TODO: Record this error message to zone error log - } + std::string query = StringFormat("INSERT INTO botpetinventory " + "(BotPetsId, ItemId) VALUES(%u, %u);", + botPetSaveId, petItems[itemIndex]); + auto results = database.QueryDatabase(query); + if(!results.Success()) + break; } + } void Bot::DeletePetBuffs(uint32 botPetSaveId) { From 510a51e56470a3ed96ae32c70e3e96bf64c139d9 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:55:05 -0700 Subject: [PATCH 13/45] DeletePetBuffs converted to QueryDatabase --- zone/bot.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 719f546b8..778ebc6fb 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2760,18 +2760,12 @@ void Bot::SavePetItems(uint32* petItems, uint32 botPetSaveId) { } void Bot::DeletePetBuffs(uint32 botPetSaveId) { - if(botPetSaveId > 0) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; + if(botPetSaveId == 0) + return; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM botpetbuffs WHERE BotPetsId = %u;", botPetSaveId), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - } + std::string query = StringFormat("DELETE FROM botpetbuffs WHERE BotPetsId = %u;", botPetSaveId); + auto results = database.QueryDatabase(query); - safe_delete(Query); - Query = 0; - } } void Bot::DeletePetItems(uint32 botPetSaveId) { From 04045dfc27d25eda692d2b05defac30d8dacaa32 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 15:56:19 -0700 Subject: [PATCH 14/45] DeletePetItems converted to QueryDatabase --- zone/bot.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 778ebc6fb..774f29b5b 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2769,18 +2769,12 @@ void Bot::DeletePetBuffs(uint32 botPetSaveId) { } void Bot::DeletePetItems(uint32 botPetSaveId) { - if(botPetSaveId > 0) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; + if(botPetSaveId == 0) + return; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM botpetinventory WHERE BotPetsId = %u;", botPetSaveId), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - } + std::string query = StringFormat("DELETE FROM botpetinventory WHERE BotPetsId = %u;", botPetSaveId); + auto results = database.QueryDatabase(query); - safe_delete(Query); - Query = 0; - } } void Bot::DeletePetStats(uint32 botPetSaveId) { From a67255475c67b3c999e60335f0d5f54a33d090c1 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 16:02:02 -0700 Subject: [PATCH 15/45] DeletePetStats converted to QueryDatabase --- zone/bot.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 774f29b5b..185dcedc5 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2778,18 +2778,12 @@ void Bot::DeletePetItems(uint32 botPetSaveId) { } void Bot::DeletePetStats(uint32 botPetSaveId) { - if(botPetSaveId > 0) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; + if(botPetSaveId == 0) + return; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE from botpets where BotPetsId = %u;", botPetSaveId), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - } + std::string query = StringFormat("DELETE from botpets where BotPetsId = %u;", botPetSaveId); + auto results = database.QueryDatabase(query); - safe_delete(Query); - Query = 0; - } } void Bot::LoadStance() { From 14c642a3f786ea00cd8075e49b06080c62665b94 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 16:07:10 -0700 Subject: [PATCH 16/45] LoadStance converted to QueryDatabase --- zone/bot.cpp | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 185dcedc5..443fd5e59 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2787,38 +2787,18 @@ void Bot::DeletePetStats(uint32 botPetSaveId) { } void Bot::LoadStance() { - int Result = 0; - bool loaded = false; - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "select StanceID from botstances where BotID = %u;", GetBotID()), TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - while(DataRow = mysql_fetch_row(DatasetResult)) { - Result = atoi(DataRow[0]); - loaded = true; - break; - } - - mysql_free_result(DatasetResult); - } - - safe_delete(Query); - Query = 0; - - if(!errorMessage.empty()) { + std::string query = StringFormat("SELECT StanceID FROM botstances WHERE BotID = %u;", GetBotID()); + auto results = database.QueryDatabase(query); + if(!results.Success() || results.RowCount() == 0) { LogFile->write(EQEMuLog::Error, "Error in Bot::LoadStance()"); + SetDefaultBotStance(); + return; } - if(loaded) - SetBotStance((BotStanceType)Result); - else - SetDefaultBotStance(); + auto row = results.begin(); + + SetBotStance((BotStanceType)atoi(row[0])); } void Bot::SaveStance() { From 7d8e128b5fd11da2aaecfde60d40cf059496f935 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 16:11:13 -0700 Subject: [PATCH 17/45] SaveStance converted to QueryDatabase --- zone/bot.cpp | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 443fd5e59..f76dd2d75 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2802,25 +2802,15 @@ void Bot::LoadStance() { } void Bot::SaveStance() { - if(_baseBotStance != _botStance) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; + if(_baseBotStance == _botStance) + return; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "REPLACE INTO botstances (BotID, StanceId) VALUES(%u, %u);", GetBotID(), GetBotStance()), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - safe_delete(Query); - Query = 0; - } - else { - safe_delete(Query); - Query = 0; - } + std::string query = StringFormat("REPLACE INTO botstances (BotID, StanceId) " + "VALUES(%u, %u);", GetBotID(), GetBotStance()); + auto results = database.QueryDatabase(query); + if(!results.Success()) + LogFile->write(EQEMuLog::Error, "Error in Bot::SaveStance()"); - if(!errorMessage.empty()) { - LogFile->write(EQEMuLog::Error, "Error in Bot::SaveStance()"); - } - } } void Bot::LoadTimers() { From 89f34246f0bf947e864af27ded810d1836dfe4fe Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 16:16:04 -0700 Subject: [PATCH 18/45] LoadTimers converted to QueryDatabase --- zone/bot.cpp | 54 +++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index f76dd2d75..a343f4584 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2814,39 +2814,33 @@ void Bot::SaveStance() { } void Bot::LoadTimers() { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT IfNull(bt.TimerID, 0) As TimerID, IfNull(bt.Value, 0) As Value, IfNull(MAX(sn.recast_time), 0) AS MaxTimer FROM bottimers bt, spells_new sn WHERE bt.BotID = %u AND sn.EndurTimerIndex = (SELECT case WHEN TimerID > %i THEN TimerID - %i ELSE TimerID END AS TimerID FROM bottimers WHERE TimerID = bt.TimerID AND BotID = bt.BotID ) AND sn.classes%i <= %i;", GetBotID(), DisciplineReuseStart-1, DisciplineReuseStart-1, GetClass(), GetLevel()), TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - int TimerID = 0; - uint32 Value = 0; - uint32 MaxValue = 0; - - while(DataRow = mysql_fetch_row(DatasetResult)) { - TimerID = atoi(DataRow[0]) - 1; - Value = atoi(DataRow[1]); - MaxValue = atoi(DataRow[2]); - - if(TimerID >= 0 && TimerID < MaxTimer && Value < (Timer::GetCurrentTime() + MaxValue)) { - timers[TimerID] = Value; - } - } - - mysql_free_result(DatasetResult); - } - - safe_delete(Query); - Query = 0; - - if(!errorMessage.empty()) { + std::string query = StringFormat("SELECT IfNull(bt.TimerID, 0) As TimerID, IfNull(bt.Value, 0) As Value, " + "IfNull(MAX(sn.recast_time), 0) AS MaxTimer FROM bottimers bt, spells_new sn " + "WHERE bt.BotID = %u AND sn.EndurTimerIndex = " + "(SELECT case WHEN TimerID > %i THEN TimerID - %i ELSE TimerID END AS TimerID " + "FROM bottimers WHERE TimerID = bt.TimerID AND BotID = bt.BotID ) " + "AND sn.classes%i <= %i;", + GetBotID(), DisciplineReuseStart-1, DisciplineReuseStart-1, GetClass(), GetLevel()); + auto results = database.QueryDatabase(query); + if(!results.Success()) { LogFile->write(EQEMuLog::Error, "Error in Bot::LoadTimers()"); + return; } + + int timerID = 0; + uint32 value = 0; + uint32 maxValue = 0; + + for (auto row = results.begin(); row != results.end(); ++row) { + timerID = atoi(row[0]) - 1; + value = atoi(row[1]); + maxValue = atoi(row[2]); + + if(timerID >= 0 && timerID < MaxTimer && value < (Timer::GetCurrentTime() + maxValue)) + timers[timerID] = value; + } + } void Bot::SaveTimers() { From 06d1bd632bb8017dcd051bfd69e337e1307d0253 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 16:22:45 -0700 Subject: [PATCH 19/45] SaveTimers converted to QueryDatabase --- zone/bot.cpp | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index a343f4584..c7f1ef9e2 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -2844,30 +2844,28 @@ void Bot::LoadTimers() { } void Bot::SaveTimers() { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; + bool hadError = false; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM bottimers WHERE BotID = %u;", GetBotID()), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - safe_delete(Query); - Query = 0; + std::string query = StringFormat("DELETE FROM bottimers WHERE BotID = %u;", GetBotID()); + auto results = database.QueryDatabase(query); + if(!results.Success()) + hadError = true; + + for(int timerIndex = 0; timerIndex < MaxTimer; timerIndex++) { + if(timers[timerIndex] <= Timer::GetCurrentTime()) + continue; + + query = StringFormat("REPLACE INTO bottimers (BotID, TimerID, Value) VALUES(%u, %u, %u);", + GetBotID(), timerIndex+1, timers[timerIndex]); + results = database.QueryDatabase(query); + + if(!results.Success()) + hadError = true; } - for(int i = 0; i < MaxTimer; i++) { - if(timers[i] > Timer::GetCurrentTime()) { - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "REPLACE INTO bottimers (BotID, TimerID, Value) VALUES(%u, %u, %u);", GetBotID(), i+1, timers[i]), TempErrorMessageBuffer)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - - safe_delete(Query); - Query = 0; - } - } - - if(!errorMessage.empty()) { + if(hadError) LogFile->write(EQEMuLog::Error, "Error in Bot::SaveTimers()"); - } + } bool Bot::Process() From 515fe8d9e58efa343cbf7f0e7f04392bb56cf48e Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 16:29:47 -0700 Subject: [PATCH 20/45] DeleteBot converted to QueryDatabase --- zone/bot.cpp | 61 +++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index c7f1ef9e2..72aae3089 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4067,44 +4067,41 @@ void Bot::Depop() { } bool Bot::DeleteBot(std::string* errorMessage) { - bool Result = false; - int TempCounter = 0; + bool hadError = false; - if(this->GetBotID() > 0) { - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; + if(this->GetBotID() == 0) + return false; - // TODO: These queries need to be ran together as a transaction.. ie, if one or more fail then they all will fail to commit to the database. + // TODO: These queries need to be ran together as a transaction.. ie, if one or more fail then they all will fail to commit to the database. + std::string query = StringFormat("DELETE FROM botinventory WHERE botid = '%u'", this->GetBotID()); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + hadError = true; + } - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM botinventory WHERE botid = '%u'", this->GetBotID()), TempErrorMessageBuffer)) { - *errorMessage = std::string(TempErrorMessageBuffer); - } - else - TempCounter++; + query = StringFormat("DELETE FROM botbuffs WHERE botid = '%u'", this->GetBotID()); + results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + hadError = true; + } - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM botbuffs WHERE botid = '%u'", this->GetBotID()), TempErrorMessageBuffer)) { - *errorMessage = std::string(TempErrorMessageBuffer); - } - else - TempCounter++; + query = StringFormat("DELETE FROM botstances WHERE BotID = '%u'", this->GetBotID()); + results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + hadError = true; + } - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM botstances WHERE BotID = '%u'", this->GetBotID()), TempErrorMessageBuffer)) { - *errorMessage = std::string(TempErrorMessageBuffer); - } - else - TempCounter++; + query = StringFormat("DELETE FROM bots WHERE BotID = '%u'", this->GetBotID()); + results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + hadError = true; + } - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "DELETE FROM bots WHERE BotID = '%u'", this->GetBotID()), TempErrorMessageBuffer)) { - *errorMessage = std::string(TempErrorMessageBuffer); - } - else - TempCounter++; - - if(TempCounter == 4) - Result = true; - } - - return Result; + return !hadError; } void Bot::Spawn(Client* botCharacterOwner, std::string* errorMessage) { From cf0c773002337401a0b5c631f1291f767eb3ac67 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 16:40:31 -0700 Subject: [PATCH 21/45] SetBotItemInSlot converted to QueryDatabase --- zone/bot.cpp | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 72aae3089..fe9ffa499 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4156,30 +4156,29 @@ void Bot::Spawn(Client* botCharacterOwner, std::string* errorMessage) { // Saves the specified item as an inventory record in the database for this bot. void Bot::SetBotItemInSlot(uint32 slotID, uint32 itemID, const ItemInst* inst, std::string *errorMessage) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; + uint32 augslot[EmuConstants::ITEM_COMMON_SIZE] = { NO_ITEM, NO_ITEM, NO_ITEM, NO_ITEM, NO_ITEM }; - if (this->GetBotID() > 0 && slotID >= EmuConstants::EQUIPMENT_BEGIN && itemID > NO_ITEM) { - if (inst && inst->IsType(ItemClassCommon)) { - for(int i = AUG_BEGIN; i < EmuConstants::ITEM_COMMON_SIZE; ++i) { - ItemInst* auginst = inst->GetItem(i); - augslot[i] = (auginst && auginst->GetItem()) ? auginst->GetItem()->ID : 0; - } - } - if(!database.RunQuery(query, MakeAnyLenString(&query, - "REPLACE INTO botinventory " - " (botid,slotid,itemid,charges,instnodrop,color," - " augslot1,augslot2,augslot3,augslot4,augslot5)" - " VALUES(%lu,%lu,%lu,%lu,%lu,%lu," - " %lu,%lu,%lu,%lu,%lu)", - (unsigned long)this->GetBotID(), (unsigned long)slotID, (unsigned long)itemID, (unsigned long)inst->GetCharges(), (unsigned long)(inst->IsInstNoDrop() ? 1:0),(unsigned long)inst->GetColor(), - (unsigned long)augslot[0],(unsigned long)augslot[1],(unsigned long)augslot[2],(unsigned long)augslot[3],(unsigned long)augslot[4]), errbuf)) { - *errorMessage = std::string(errbuf); - } + if (this->GetBotID() == 0 || slotID < EmuConstants::EQUIPMENT_BEGIN || itemID <= NO_ITEM) + return; - safe_delete_array(query); - } + if (inst && inst->IsType(ItemClassCommon)) { + for(int i = AUG_BEGIN; i < EmuConstants::ITEM_COMMON_SIZE; ++i) { + ItemInst* auginst = inst->GetItem(i); + augslot[i] = (auginst && auginst->GetItem()) ? auginst->GetItem()->ID : 0; + } + } + + std::string query = StringFormat("REPLACE INTO botinventory (botid, slotid, itemid, charges, instnodrop, color, " + "augslot1, augslot2, augslot3, augslot4, augslot5) " + "VALUES(%lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu)", + (unsigned long)this->GetBotID(), (unsigned long)slotID, (unsigned long)itemID, + (unsigned long)inst->GetCharges(), (unsigned long)(inst->IsInstNoDrop()? 1: 0), + (unsigned long)inst->GetColor(), (unsigned long)augslot[0], (unsigned long)augslot[1], + (unsigned long)augslot[2], (unsigned long)augslot[3], (unsigned long)augslot[4]); + auto results = database.QueryDatabase(query); + if(!results.Success()) + *errorMessage = std::string(results.ErrorMessage()); } // Deletes the inventory record for the specified item from the database for this bot. From 53572b4d132383a368ad4f5b5f501157f4cd5792 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 16:43:59 -0700 Subject: [PATCH 22/45] RemoveBotItemBySlot converted to QueryDatabase --- zone/bot.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index fe9ffa499..3d6821617 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4183,16 +4183,18 @@ void Bot::SetBotItemInSlot(uint32 slotID, uint32 itemID, const ItemInst* inst, s // Deletes the inventory record for the specified item from the database for this bot. void Bot::RemoveBotItemBySlot(uint32 slotID, std::string *errorMessage) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - if(this->GetBotID() > 0 && slotID >= 0) { - if(!database.RunQuery(query, MakeAnyLenString(&query, "DELETE FROM botinventory WHERE botid=%i AND slotid=%i", this->GetBotID(), slotID), errbuf)){ - *errorMessage = std::string(errbuf); - } - safe_delete_array(query); - m_inv.DeleteItem(slotID); - } + if(this->GetBotID() == 0) + return; + + std::string query = StringFormat("DELETE FROM botinventory " + "WHERE botid = %i AND slotid = %i", + this->GetBotID(), slotID); + auto results = database.QueryDatabase(query); + if(!results.Success()) + *errorMessage = std::string(results.ErrorMessage()); + + m_inv.DeleteItem(slotID); } // Retrieves all the inventory records from the database for this bot. From b8caa5dc31dd61ad988a7314138d4f2fc9d01aac Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 16:52:18 -0700 Subject: [PATCH 23/45] GetBotItems converted to QueryDatabase --- zone/bot.cpp | 107 +++++++++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 3d6821617..cfdd0c7ff 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4200,65 +4200,62 @@ void Bot::RemoveBotItemBySlot(uint32 slotID, std::string *errorMessage) { // Retrieves all the inventory records from the database for this bot. void Bot::GetBotItems(std::string* errorMessage, Inventory &inv) { - if(this->GetBotID() > 0) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char* query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(this->GetBotID() == 0) + return; - if(database.RunQuery(query, MakeAnyLenString(&query, "SELECT slotid,itemid,charges,color,augslot1,augslot2,augslot3,augslot4,augslot5,instnodrop FROM botinventory WHERE botid=%i order by slotid", this->GetBotID()), errbuf, &DatasetResult)) { - while(DataRow = mysql_fetch_row(DatasetResult)) { - int16 slot_id = atoi(DataRow[0]); - uint32 item_id = atoi(DataRow[1]); - uint16 charges = atoi(DataRow[2]); - uint32 color = atoul(DataRow[3]); - uint32 aug[EmuConstants::ITEM_COMMON_SIZE]; - aug[0] = (uint32)atoul(DataRow[4]); - aug[1] = (uint32)atoul(DataRow[5]); - aug[2] = (uint32)atoul(DataRow[6]); - aug[3] = (uint32)atoul(DataRow[7]); - aug[4] = (uint32)atoul(DataRow[8]); - bool instnodrop = (DataRow[9] && (uint16)atoi(DataRow[9])) ? true : false; + std::string query = StringFormat("SELECT slotid, itemid, charges, color, " + "augslot1, augslot2, augslot3, augslot4, " + "augslot5, instnodrop FROM botinventory " + "WHERE botid = %i ORDER BY slotid", this->GetBotID()); + auto results = database.QueryDatabase(query); + if (!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return; + } - ItemInst* inst = database.CreateItem(item_id, charges, aug[0], aug[1], aug[2], aug[3], aug[4]); - if(inst) { - int16 put_slot_id = INVALID_INDEX; - if(instnodrop || ((slot_id >= EmuConstants::EQUIPMENT_BEGIN) && (slot_id <= EmuConstants::EQUIPMENT_END) && inst->GetItem()->Attuneable)) - inst->SetInstNoDrop(true); - if(color > 0) - inst->SetColor(color); - if(charges==255) - inst->SetCharges(-1); - else - inst->SetCharges(charges); - if((slot_id >= 8000) && (slot_id <= 8999)) { - // do nothing - } - else { - put_slot_id = inv.PutItem(slot_id, *inst); - } - safe_delete(inst); + for (auto row = results.begin(); row != results.end(); ++row) { + int16 slot_id = atoi(row[0]); + uint32 item_id = atoi(row[1]); + uint16 charges = atoi(row[2]); + uint32 color = atoul(row[3]); + uint32 aug[EmuConstants::ITEM_COMMON_SIZE]; + aug[0] = (uint32)atoul(row[4]); + aug[1] = (uint32)atoul(row[5]); + aug[2] = (uint32)atoul(row[6]); + aug[3] = (uint32)atoul(row[7]); + aug[4] = (uint32)atoul(row[8]); + bool instnodrop = (row[9] && (uint16)atoi(row[9])) ? true : false; - // Save ptr to item in inventory - if (put_slot_id == INVALID_INDEX) { - LogFile->write(EQEMuLog::Error, - "Warning: Invalid slot_id for item in inventory: botid=%i, item_id=%i, slot_id=%i", - this->GetBotID(), item_id, slot_id); - } - } - else { - LogFile->write(EQEMuLog::Error, - "Warning: botid %i has an invalid item_id %i in inventory slot %i", - this->GetBotID(), item_id, slot_id); - } - } - mysql_free_result(DatasetResult); - } - else - *errorMessage = std::string(errbuf); + ItemInst* inst = database.CreateItem(item_id, charges, aug[0], aug[1], aug[2], aug[3], aug[4]); + if (!inst) { + LogFile->write(EQEMuLog::Error, "Warning: botid %i has an invalid item_id %i in inventory slot %i", this->GetBotID(), item_id, slot_id); + continue; + } + + int16 put_slot_id = INVALID_INDEX; + + if (instnodrop || ((slot_id >= EmuConstants::EQUIPMENT_BEGIN) && (slot_id <= EmuConstants::EQUIPMENT_END) && inst->GetItem()->Attuneable)) + inst->SetInstNoDrop(true); + + if (color > 0) + inst->SetColor(color); + + if (charges==255) + inst->SetCharges(-1); + else + inst->SetCharges(charges); + + if (slot_id < 8000 || slot_id > 8999) + put_slot_id = inv.PutItem(slot_id, *inst); + + safe_delete(inst); + + // Save ptr to item in inventory + if (put_slot_id == INVALID_INDEX) + LogFile->write(EQEMuLog::Error, "Warning: Invalid slot_id for item in inventory: botid=%i, item_id=%i, slot_id=%i",this->GetBotID(), item_id, slot_id); + + } - safe_delete_array(query); - } } // Returns the inventory record for this bot from the database for the specified equipment slot. From cd1b45f0d6fe9f8c22f456073dccc11027bda2f2 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:04:21 -0700 Subject: [PATCH 24/45] GetBotItemBySlot converted to QueryDatabase --- zone/bot.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index cfdd0c7ff..4cb979997 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4260,27 +4260,21 @@ void Bot::GetBotItems(std::string* errorMessage, Inventory &inv) { // Returns the inventory record for this bot from the database for the specified equipment slot. uint32 Bot::GetBotItemBySlot(uint32 slotID) { - uint32 Result = 0; - if(this->GetBotID() > 0 && slotID >= EmuConstants::EQUIPMENT_BEGIN) { - char* query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(this->GetBotID() == 0 || slotID < EmuConstants::EQUIPMENT_BEGIN) + return 0; - if(database.RunQuery(query, MakeAnyLenString(&query, "SELECT itemid FROM botinventory WHERE botid=%i AND slotid=%i", GetBotID(), slotID), 0, &DatasetResult)) { - if(mysql_num_rows(DatasetResult) == 1) { - DataRow = mysql_fetch_row(DatasetResult); - if(DataRow) - Result = atoi(DataRow[0]); - } + std::string query = StringFormat("SELECT itemid FROM botinventory WHERE botid=%i AND slotid = %i", GetBotID(), slotID); + auto results = database.QueryDatabase(query); + if(results.Success()) + return 0; - mysql_free_result(DatasetResult); - } + if(results.RowCount() != 1) + return 0; - safe_delete_array(query); - } + auto row = results.begin(); - return Result; + return atoi(row[0]); } // Returns the number of inventory records the bot has in the database. From 390dcc9a88f382437ef141bcc8661970b7ffd313 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:07:11 -0700 Subject: [PATCH 25/45] GetBotItemsCount converted to QueryDatabase --- zone/bot.cpp | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 4cb979997..567986005 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4279,30 +4279,22 @@ uint32 Bot::GetBotItemBySlot(uint32 slotID) { // Returns the number of inventory records the bot has in the database. uint32 Bot::GetBotItemsCount(std::string *errorMessage) { - uint32 Result = 0; - if(this->GetBotID() > 0) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char* query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(this->GetBotID() == 0) + return 0; - if(database.RunQuery(query, MakeAnyLenString(&query, "SELECT COUNT(*) FROM botinventory WHERE botid=%i", this->GetBotID()), errbuf, &DatasetResult)) { - if(mysql_num_rows(DatasetResult) == 1) { - DataRow = mysql_fetch_row(DatasetResult); - if(DataRow) - Result = atoi(DataRow[0]); - } + std::string query = StringFormat("SELECT COUNT(*) FROM botinventory WHERE botid = %i", this->GetBotID()); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return 0; + } - mysql_free_result(DatasetResult); - } - else - *errorMessage = std::string(errbuf); + if(results.RowCount() != 1) + return 0; - safe_delete_array(query); - } - - return Result; + auto row = results.begin(); + return atoi(row[0]); } bool Bot::MesmerizeTarget(Mob* target) { From 8312a8cf3b20683de07f40a6c7e558adb4d7e1b6 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:11:06 -0700 Subject: [PATCH 26/45] GetBotIDByBotName converted to QueryDatabase --- zone/bot.cpp | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 567986005..9882a565a 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4487,35 +4487,19 @@ void Bot::FillSpawnStruct(NewSpawn_Struct* ns, Mob* ForWho) { } uint32 Bot::GetBotIDByBotName(std::string botName) { - uint32 Result = 0; + if(botName.empty()) + return 0; - if(!botName.empty()) { - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; - std::string errorMessage; + std::string query = StringFormat("SELECT BotID FROM bots WHERE Name = '%s'", botName.c_str()); + auto results = database.QueryDatabase(query); + if(!results.Success()) + return 0; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT BotID FROM bots WHERE Name = '%s'", botName.c_str()), TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - while(DataRow = mysql_fetch_row(DatasetResult)) { - Result = atoi(DataRow[0]); - break; - } + if (results.RowCount() == 0) + return 0; - mysql_free_result(DatasetResult); - } - - safe_delete_array(Query); - - if(!errorMessage.empty()) { - // TODO: Log this error to zone error log - } - } - - return Result; + auto row = results.begin(); + return atoi(row[0]); } Bot* Bot::LoadBot(uint32 botID, std::string* errorMessage) { From ba612f91c739b8b33f1085d2f942f66dcf6b109a Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:18:50 -0700 Subject: [PATCH 27/45] LoadBot converted to QueryDatabase --- zone/bot.cpp | 55 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 9882a565a..691e69601 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4503,32 +4503,45 @@ uint32 Bot::GetBotIDByBotName(std::string botName) { } Bot* Bot::LoadBot(uint32 botID, std::string* errorMessage) { - Bot* Result = 0; + Bot* loadedBot = nullptr; - if(botID > 0) { - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botID == 0) + return nullptr; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT BotOwnerCharacterID, BotSpellsID, Name, LastName, BotLevel, Race, Class, Gender, Size, Face, LuclinHairStyle, LuclinHairColor, LuclinEyeColor, LuclinEyeColor2, LuclinBeardColor, LuclinBeard, DrakkinHeritage, DrakkinTattoo, DrakkinDetails, HP, Mana, MR, CR, DR, FR, PR, Corrup, AC, STR, STA, DEX, AGI, _INT, WIS, CHA, ATK, BotCreateDate, LastSpawnDate, TotalPlayTime, LastZoneId FROM bots WHERE BotID = '%u'", botID), TempErrorMessageBuffer, &DatasetResult)) { - *errorMessage = std::string(TempErrorMessageBuffer); - } - else { - while(DataRow = mysql_fetch_row(DatasetResult)) { - NPCType DefaultNPCTypeStruct = CreateDefaultNPCTypeStructForBot(std::string(DataRow[2]), std::string(DataRow[3]), atoi(DataRow[4]), atoi(DataRow[5]), atoi(DataRow[6]), atoi(DataRow[7])); - NPCType TempNPCStruct = FillNPCTypeStruct(atoi(DataRow[1]), std::string(DataRow[2]), std::string(DataRow[3]), atoi(DataRow[4]), atoi(DataRow[5]), atoi(DataRow[6]), atoi(DataRow[7]), atof(DataRow[8]), atoi(DataRow[9]), atoi(DataRow[10]), atoi(DataRow[11]), atoi(DataRow[12]), atoi(DataRow[13]), atoi(DataRow[14]), atoi(DataRow[15]), atoi(DataRow[16]), atoi(DataRow[17]), atoi(DataRow[18]), atoi(DataRow[19]), atoi(DataRow[20]), DefaultNPCTypeStruct.MR, DefaultNPCTypeStruct.CR, DefaultNPCTypeStruct.DR, DefaultNPCTypeStruct.FR, DefaultNPCTypeStruct.PR, DefaultNPCTypeStruct.Corrup, DefaultNPCTypeStruct.AC, DefaultNPCTypeStruct.STR, DefaultNPCTypeStruct.STA, DefaultNPCTypeStruct.DEX, DefaultNPCTypeStruct.AGI, DefaultNPCTypeStruct.INT, DefaultNPCTypeStruct.WIS, DefaultNPCTypeStruct.CHA, DefaultNPCTypeStruct.ATK); - Result = new Bot(botID, atoi(DataRow[0]), atoi(DataRow[1]), atof(DataRow[38]), atoi(DataRow[39]), TempNPCStruct); - break; - } + std::string query = StringFormat("SELECT BotOwnerCharacterID, BotSpellsID, Name, LastName, BotLevel, " + "Race, Class, Gender, Size, Face, LuclinHairStyle, LuclinHairColor, " + "LuclinEyeColor, LuclinEyeColor2, LuclinBeardColor, LuclinBeard, " + "DrakkinHeritage, DrakkinTattoo, DrakkinDetails, HP, Mana, MR, CR, " + "DR, FR, PR, Corrup, AC, STR, STA, DEX, AGI, _INT, WIS, CHA, ATK, " + "BotCreateDate, LastSpawnDate, TotalPlayTime, LastZoneId " + "FROM bots WHERE BotID = '%u'", botID); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return nullptr; + } - mysql_free_result(DatasetResult); - } + if (results.RowCount() == 0) + return nullptr; - safe_delete_array(Query); - } + auto row = results.begin(); + NPCType defaultNPCTypeStruct = CreateDefaultNPCTypeStructForBot(std::string(row[2]), std::string(row[3]), + atoi(row[4]), atoi(row[5]), atoi(row[6]), atoi(row[7])); - return Result; + NPCType tempNPCStruct = FillNPCTypeStruct(atoi(row[1]), std::string(row[2]), std::string(row[3]), atoi(row[4]), + atoi(row[5]), atoi(row[6]), atoi(row[7]), atof(row[8]), atoi(row[9]), + atoi(row[10]), atoi(row[11]), atoi(row[12]), atoi(row[13]), atoi(row[14]), + atoi(row[15]), atoi(row[16]), atoi(row[17]), atoi(row[18]), atoi(row[19]), + atoi(row[20]), defaultNPCTypeStruct.MR, defaultNPCTypeStruct.CR, + defaultNPCTypeStruct.DR, defaultNPCTypeStruct.FR, defaultNPCTypeStruct.PR, + defaultNPCTypeStruct.Corrup, defaultNPCTypeStruct.AC, defaultNPCTypeStruct.STR, + defaultNPCTypeStruct.STA, defaultNPCTypeStruct.DEX, defaultNPCTypeStruct.AGI, + defaultNPCTypeStruct.INT, defaultNPCTypeStruct.WIS, defaultNPCTypeStruct.CHA, + defaultNPCTypeStruct.ATK); + + loadedBot = new Bot(botID, atoi(row[0]), atoi(row[1]), atof(row[38]), atoi(row[39]), tempNPCStruct); + + return loadedBot; } std::list Bot::GetGroupedBotsByGroupId(uint32 groupId, std::string* errorMessage) { From d213e3b1060bdc9ebc667dee4bfc1a365c0903e7 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:26:51 -0700 Subject: [PATCH 28/45] GetGroupedBotsByGroupID converted to QueryDatabase --- zone/bot.cpp | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 691e69601..a94f123aa 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4545,31 +4545,24 @@ Bot* Bot::LoadBot(uint32 botID, std::string* errorMessage) { } std::list Bot::GetGroupedBotsByGroupId(uint32 groupId, std::string* errorMessage) { - std::list Result; + std::list groupedBots; - if(groupId > 0) { - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(groupId == 0) + return groupedBots; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "select g.mobid as BotID from vwGroups as g join bots as b on g.mobid = b.BotId and g.mobtype = 'B' where g.groupid = %u", groupId), TempErrorMessageBuffer, &DatasetResult)) { - *errorMessage = std::string(TempErrorMessageBuffer); - } - else { - while(DataRow = mysql_fetch_row(DatasetResult)) { - if(DataRow) { - Result.push_back(atoi(DataRow[0])); - } - } - - mysql_free_result(DatasetResult); - } - - safe_delete_array(Query); + std::string query = StringFormat("SELECT g.mobid AS BotID FROM vwGroups AS g " + "JOIN bots AS b ON g.mobid = b.BotId AND g.mobtype = 'B' " + "WHERE g.groupid = %u", groupId); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return groupedBots; } - return Result; + for (auto row = results.begin(); row != results.end(); ++row) + groupedBots.push_back(atoi(row[0])); + + return groupedBots; } // Load and spawn all zoned bots by bot owner character From 7ae14fffd021df5cee3727cad5f1e628b246025d Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:32:55 -0700 Subject: [PATCH 29/45] GetBotList converted to QueryDatabase --- zone/bot.cpp | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index a94f123aa..29a0ca3c8 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4628,38 +4628,31 @@ bool Bot::GroupHasBot(Group* group) { } std::list Bot::GetBotList(uint32 botOwnerCharacterID, std::string* errorMessage) { - std::list Result; + std::list ownersBots; - if(botOwnerCharacterID > 0) { - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botOwnerCharacterID == 0) + return ownersBots; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT BotID, Name, Class, BotLevel, Race FROM bots WHERE BotOwnerCharacterID = '%u'", botOwnerCharacterID), TempErrorMessageBuffer, &DatasetResult)) { - *errorMessage = std::string(TempErrorMessageBuffer); - } - else { - while(DataRow = mysql_fetch_row(DatasetResult)) { - if(DataRow) { - BotsAvailableList TempAvailableBot; - TempAvailableBot.BotID = atoi(DataRow[0]); - strcpy(TempAvailableBot.BotName, DataRow[1]); - TempAvailableBot.BotClass = atoi(DataRow[2]); - TempAvailableBot.BotLevel = atoi(DataRow[3]); - TempAvailableBot.BotRace = atoi(DataRow[4]); - - Result.push_back(TempAvailableBot); - } - } - - mysql_free_result(DatasetResult); - } - - safe_delete_array(Query); + std::string query = StringFormat("SELECT BotID, Name, Class, BotLevel, Race " + "FROM bots WHERE BotOwnerCharacterID = '%u'", botOwnerCharacterID); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return ownersBots; } - return Result; + for (auto row = results.begin(); row != results.end(); ++row) { + BotsAvailableList availableBot; + availableBot.BotID = atoi(row[0]); + strcpy(availableBot.BotName, row[1]); + availableBot.BotClass = atoi(row[2]); + availableBot.BotLevel = atoi(row[3]); + availableBot.BotRace = atoi(row[4]); + + ownersBots.push_back(availableBot); + } + + return ownersBots; } std::list Bot::ListSpawnedBots(uint32 characterID, std::string* errorMessage) { From 6410f52c9c2c1ca5d91dc8eee94df5be6b8b80c4 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:36:59 -0700 Subject: [PATCH 30/45] ListSpawnedBots converted to QueryDatabase --- zone/bot.cpp | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 29a0ca3c8..2641701a5 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4656,38 +4656,28 @@ std::list Bot::GetBotList(uint32 botOwnerCharacterID, std::st } std::list Bot::ListSpawnedBots(uint32 characterID, std::string* errorMessage) { - std::list Result; - char ErrBuf[MYSQL_ERRMSG_SIZE]; - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + std::list spawnedBots; - if(characterID > 0) { - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT bot_name, zone_name FROM botleader WHERE leaderid=%i", characterID), ErrBuf, &DatasetResult)) { - *errorMessage = std::string(ErrBuf); - } - else { - uint32 RowCount = mysql_num_rows(DatasetResult); + if(characterID == 0) + return spawnedBots; - if(RowCount > 0) { - for(int iCounter = 0; iCounter < RowCount; iCounter++) { - DataRow = mysql_fetch_row(DatasetResult); - SpawnedBotsList TempSpawnedBotsList; - TempSpawnedBotsList.BotLeaderCharID = characterID; - strcpy(TempSpawnedBotsList.BotName, DataRow[0]); - strcpy(TempSpawnedBotsList.ZoneName, DataRow[1]); + std::string query = StringFormat("SELECT bot_name, zone_name FROM botleader WHERE leaderid=%i", characterID); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return spawnedBots; + } - Result.push_back(TempSpawnedBotsList); - } - } + for(auto row = results.begin(); row != results.end(); ++row) { + SpawnedBotsList spawnedBotsList; + spawnedBotsList.BotLeaderCharID = characterID; + strcpy(spawnedBotsList.BotName, row[0]); + strcpy(spawnedBotsList.ZoneName, row[1]); - mysql_free_result(DatasetResult); - } + spawnedBots.push_back(spawnedBotsList); + } - safe_delete_array(Query); - } - - return Result; + return spawnedBots; } void Bot::SaveBotGroup(Group* botGroup, std::string botGroupName, std::string* errorMessage) { From 36325226eb731d2c1da88d539f25f68e1fd95b9c Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:43:18 -0700 Subject: [PATCH 31/45] SaveBotGroup converted to QueryDatabase --- zone/bot.cpp | 56 +++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 2641701a5..9c8dd85ec 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4681,41 +4681,43 @@ std::list Bot::ListSpawnedBots(uint32 characterID, std::string* } void Bot::SaveBotGroup(Group* botGroup, std::string botGroupName, std::string* errorMessage) { - if(botGroup && !botGroupName.empty()) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; + if(!botGroup || botGroupName.empty()) + return; - Mob* tempGroupLeader = botGroup->GetLeader(); + Mob* tempGroupLeader = botGroup->GetLeader(); - if(tempGroupLeader->IsBot()) { - uint32 botGroupId = 0; + if(!tempGroupLeader->IsBot()) + return; - uint32 botGroupLeaderBotId = tempGroupLeader->CastToBot()->GetBotID(); + uint32 botGroupId = 0; + uint32 botGroupLeaderBotId = tempGroupLeader->CastToBot()->GetBotID(); - if(!database.RunQuery(query, MakeAnyLenString(&query, "INSERT into botgroup (BotGroupLeaderBotId, BotGroupName) values (%u, '%s')", botGroupLeaderBotId, botGroupName.c_str()), errbuf, 0, 0, &botGroupId)) { - *errorMessage = std::string(errbuf); - } - else { - if(botGroupId > 0) { - for(int counter = 0; counter < botGroup->GroupCount(); counter++) { - Mob* tempBot = botGroup->members[counter]; + std::string query = StringFormat("INSERT INTO botgroup (BotGroupLeaderBotId, BotGroupName) " + "VALUES (%u, '%s')", botGroupLeaderBotId, botGroupName.c_str()); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return; + } - if(tempBot && tempBot->IsBot()) { - uint32 botGroupMemberBotId = tempBot->CastToBot()->GetBotID(); + if(botGroupId == 0) + return; - safe_delete_array(query); + for(int groupMemberIndex = 0; groupMemberIndex < botGroup->GroupCount(); groupMemberIndex++) { + Mob* tempBot = botGroup->members[groupMemberIndex]; - if(!database.RunQuery(query, MakeAnyLenString(&query, "INSERT into botgroupmembers (BotGroupId, BotId) values (%u, %u)", botGroupId, botGroupMemberBotId), errbuf)) { - *errorMessage = std::string(errbuf); - } - } - } - } - } + if(!tempBot || !tempBot->IsBot()) + continue; + + uint32 botGroupMemberBotId = tempBot->CastToBot()->GetBotID(); + + query = StringFormat("INSERT INTO botgroupmembers (BotGroupId, BotId) " + "VALUES (%u, %u)", botGroupId, botGroupMemberBotId); + results = database.QueryDatabase(query); + if(!results.Success()) + *errorMessage = std::string(results.ErrorMessage()); + } - safe_delete_array(query); - } - } } void Bot::DeleteBotGroup(std::string botGroupName, std::string* errorMessage) { From 96cf3d967f6905ddd0ef97d178486e84a45e1b21 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:47:38 -0700 Subject: [PATCH 32/45] DeleteBotGroup converted to QueryDatabase --- zone/bot.cpp | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 9c8dd85ec..6aa14682b 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4721,27 +4721,26 @@ void Bot::SaveBotGroup(Group* botGroup, std::string botGroupName, std::string* e } void Bot::DeleteBotGroup(std::string botGroupName, std::string* errorMessage) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - if(!botGroupName.empty()) { - uint32 botGroupId = GetBotGroupIdByBotGroupName(botGroupName, errorMessage); + if(botGroupName.empty()) + return; - if(errorMessage->empty() && botGroupId > 0) { - if(!database.RunQuery(query, MakeAnyLenString(&query, "DELETE FROM botgroupmembers WHERE BotGroupId = %u", botGroupId), errbuf)) { - *errorMessage = std::string(errbuf); - } - else { - safe_delete_array(query); + uint32 botGroupId = GetBotGroupIdByBotGroupName(botGroupName, errorMessage); - if(!database.RunQuery(query, MakeAnyLenString(&query, "DELETE FROM botgroup WHERE BotGroupId = %u", botGroupId), errbuf)) { - *errorMessage = std::string(errbuf); - } - } + if(!errorMessage->empty() || botGroupId== 0) + return; - safe_delete_array(query); - } - } + std::string query = StringFormat("DELETE FROM botgroupmembers WHERE BotGroupId = %u", botGroupId); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return; + } + + query = StringFormat("DELETE FROM botgroup WHERE BotGroupId = %u", botGroupId); + results = database.QueryDatabase(query); + if(!results.Success()) + *errorMessage = std::string(results.ErrorMessage()); } std::list Bot::LoadBotGroup(std::string botGroupName, std::string* errorMessage) { From 38d04931bab0593a2c259614595f38defc62fdb3 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:52:06 -0700 Subject: [PATCH 33/45] LoadBotGroup converted to QueryDatabase --- zone/bot.cpp | 50 +++++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 6aa14682b..b33b74b64 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4744,44 +4744,32 @@ void Bot::DeleteBotGroup(std::string botGroupName, std::string* errorMessage) { } std::list Bot::LoadBotGroup(std::string botGroupName, std::string* errorMessage) { - std::list Result; - char ErrBuf[MYSQL_ERRMSG_SIZE]; - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + std::list botGroup; - if(!botGroupName.empty()) { - uint32 botGroupId = GetBotGroupIdByBotGroupName(botGroupName, errorMessage); + if(botGroupName.empty()) + return botGroup; - if(botGroupId > 0) { - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "select BotId from botgroupmembers where BotGroupId = %u", botGroupId), ErrBuf, &DatasetResult)) { - *errorMessage = std::string(ErrBuf); - } - else { - uint32 RowCount = mysql_num_rows(DatasetResult); + uint32 botGroupId = GetBotGroupIdByBotGroupName(botGroupName, errorMessage); - if(RowCount > 0) { - for(int iCounter = 0; iCounter < RowCount; iCounter++) { - DataRow = mysql_fetch_row(DatasetResult); + if(botGroupId == 0) + return botGroup; - if(DataRow) { - BotGroup tempBotGroup; - tempBotGroup.BotGroupID = botGroupId; - tempBotGroup.BotID = atoi(DataRow[0]); + std::string query = StringFormat("SELECT BotId FROM botgroupmembers WHERE BotGroupId = %u", botGroupId); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return botGroup; + } - Result.push_back(tempBotGroup); - } - } - } + for(auto row = results.begin(); row != results.end(); ++row) { + BotGroup tempBotGroup; + tempBotGroup.BotGroupID = botGroupId; + tempBotGroup.BotID = atoi(row[0]); - mysql_free_result(DatasetResult); - } + botGroup.push_back(tempBotGroup); + } - safe_delete_array(Query); - } - } - - return Result; + return botGroup; } std::list Bot::GetBotGroupListByBotOwnerCharacterId(uint32 botOwnerCharacterId, std::string* errorMessage) { From 26569ac51d1007f46f00e4850c0fd831cde4ec79 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 17:57:10 -0700 Subject: [PATCH 34/45] GetBotGroupListByBotOwnerCharacterId converted to QueryDatabase --- zone/bot.cpp | 47 +++++++++++++++++------------------------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index b33b74b64..0225450fa 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4773,41 +4773,28 @@ std::list Bot::LoadBotGroup(std::string botGroupName, std::string* err } std::list Bot::GetBotGroupListByBotOwnerCharacterId(uint32 botOwnerCharacterId, std::string* errorMessage) { - std::list result; + std::list botGroups; - if(botOwnerCharacterId > 0) { - char ErrBuf[MYSQL_ERRMSG_SIZE]; - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botOwnerCharacterId == 0) + return botGroups; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "select BotGroupName, BotGroupLeaderName from vwBotGroups where BotOwnerCharacterId = %u", botOwnerCharacterId), ErrBuf, &DatasetResult)) { - *errorMessage = std::string(ErrBuf); - } - else { - uint32 RowCount = mysql_num_rows(DatasetResult); + std::string query = StringFormat("SELECT BotGroupName, BotGroupLeaderName FROM vwBotGroups " + "WHERE BotOwnerCharacterId = %u", botOwnerCharacterId); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return botGroups; + } - if(RowCount > 0) { - for(int iCounter = 0; iCounter < RowCount; iCounter++) { - DataRow = mysql_fetch_row(DatasetResult); + for(auto row = results.begin(); row != results.end(); ++row) { + BotGroupList botGroupList; + botGroupList.BotGroupName = std::string(row[0]); + botGroupList.BotGroupLeaderName = std::string(row[1]); - if(DataRow) { - BotGroupList botGroupList; - botGroupList.BotGroupName = std::string(DataRow[0]); - botGroupList.BotGroupLeaderName = std::string(DataRow[1]); + botGroups.push_back(botGroupList); + } - result.push_back(botGroupList); - } - } - } - - mysql_free_result(DatasetResult); - } - - safe_delete_array(Query); - } - - return result; + return botGroups; } bool Bot::DoesBotGroupNameExist(std::string botGroupName) { From fe6e289606852342c5789eb7527847d80496b6e0 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:01:32 -0700 Subject: [PATCH 35/45] DoesBotGroupNameExist converted to QueryDatabase --- zone/bot.cpp | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 0225450fa..98507e2bf 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4798,39 +4798,25 @@ std::list Bot::GetBotGroupListByBotOwnerCharacterId(uint32 botOwne } bool Bot::DoesBotGroupNameExist(std::string botGroupName) { - bool result = false; - if(!botGroupName.empty()) { - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botGroupName.empty()) + return false; - if(database.RunQuery(Query, MakeAnyLenString(&Query, "select BotGroupId from vwBotGroups where BotGroupName = '%s'", botGroupName.c_str()), 0, &DatasetResult)) { - uint32 RowCount = mysql_num_rows(DatasetResult); + std::string query = StringFormat("SELECT BotGroupId FROM vwBotGroups " + "WHERE BotGroupName = '%s'", botGroupName.c_str()); + auto results = database.QueryDatabase(query); + if (!results.Success() || results.RowCount() == 0) + return false; - if(RowCount > 0) { - for(int iCounter = 0; iCounter < RowCount; iCounter++) { - DataRow = mysql_fetch_row(DatasetResult); + for(auto row = results.begin(); row != results.end(); ++row) { + uint32 tempBotGroupId = atoi(row[0]); + std::string tempBotGroupName = std::string(row[1]); - if(DataRow) { - uint32 tempBotGroupId = atoi(DataRow[0]); - std::string tempBotGroupName = std::string(DataRow[1]); + if (botGroupName == tempBotGroupName && tempBotGroupId != 0) + return true; + } - if(botGroupName == tempBotGroupName) { - result = tempBotGroupId; - break; - } - } - } - } - - mysql_free_result(DatasetResult); - } - - safe_delete_array(Query); - } - - return result; + return false; } uint32 Bot::CanLoadBotGroup(uint32 botOwnerCharacterId, std::string botGroupName, std::string* errorMessage) { From 52d64d03a693c05fba51baccee4fe03df2b325a9 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:06:05 -0700 Subject: [PATCH 36/45] CanLoadBotGroup converted to QueryDatabase --- zone/bot.cpp | 48 ++++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 98507e2bf..283214355 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4820,43 +4820,31 @@ bool Bot::DoesBotGroupNameExist(std::string botGroupName) { } uint32 Bot::CanLoadBotGroup(uint32 botOwnerCharacterId, std::string botGroupName, std::string* errorMessage) { - uint32 result = 0; - if(botOwnerCharacterId > 0 && !botGroupName.empty()) { - char ErrBuf[MYSQL_ERRMSG_SIZE]; - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botOwnerCharacterId == 0 || botGroupName.empty()) + return 0; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "select BotGroupId, BotGroupName from vwBotGroups where BotOwnerCharacterId = %u", botOwnerCharacterId), ErrBuf, &DatasetResult)) { - *errorMessage = std::string(ErrBuf); - } - else { - uint32 RowCount = mysql_num_rows(DatasetResult); + std::string query = StringFormat("SELECT BotGroupId, BotGroupName FROM vwBotGroups " + "WHERE BotOwnerCharacterId = %u", botOwnerCharacterId); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return 0; + } - if(RowCount > 0) { - for(int iCounter = 0; iCounter < RowCount; iCounter++) { - DataRow = mysql_fetch_row(DatasetResult); + if(results.RowCount() == 0) + return 0; - if(DataRow) { - uint32 tempBotGroupId = atoi(DataRow[0]); - std::string tempBotGroupName = std::string(DataRow[1]); + for(auto row = results.begin(); row != results.end(); ++row) { - if(botGroupName == tempBotGroupName) { - result = tempBotGroupId; - break; - } - } - } - } + uint32 tempBotGroupId = atoi(row[0]); + std::string tempBotGroupName = std::string(row[1]); - mysql_free_result(DatasetResult); - } + if(botGroupName == tempBotGroupName) + return tempBotGroupId; + } - safe_delete_array(Query); - } - - return result; + return 0; } uint32 Bot::GetBotGroupIdByBotGroupName(std::string botGroupName, std::string* errorMessage) { From 6d33a13e23543ecceac70031402013092955f77e Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:09:53 -0700 Subject: [PATCH 37/45] GetBotGroupIdByBotGroupName converted to QueryDatabase --- zone/bot.cpp | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 283214355..8b4d8d072 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4848,38 +4848,23 @@ uint32 Bot::CanLoadBotGroup(uint32 botOwnerCharacterId, std::string botGroupName } uint32 Bot::GetBotGroupIdByBotGroupName(std::string botGroupName, std::string* errorMessage) { - uint32 result = 0; - if(!botGroupName.empty()) { - char ErrBuf[MYSQL_ERRMSG_SIZE]; - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botGroupName.empty()) + return 0; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "select BotGroupId from vwBotGroups where BotGroupName = '%s'", botGroupName.c_str()), ErrBuf, &DatasetResult)) { - *errorMessage = std::string(ErrBuf); - } - else { - uint32 RowCount = mysql_num_rows(DatasetResult); + std::string query = StringFormat("SELECT BotGroupId FROM vwBotGroups " + "WHERE BotGroupName = '%s'", botGroupName.c_str()); + auto results = database.QueryDatabase(query); + if(!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return 0; + } - if(RowCount > 0) { - for(int iCounter = 0; iCounter < RowCount; iCounter++) { - DataRow = mysql_fetch_row(DatasetResult); + if (results.RowCount() == 0) + return 0; - if(DataRow) { - result = atoi(DataRow[0]); - break; - } - } - } - - mysql_free_result(DatasetResult); - } - - safe_delete_array(Query); - } - - return result; + auto row = results.begin(); + return atoi(row[0]); } uint32 Bot::GetBotGroupLeaderIdByBotGroupName(std::string botGroupName) { From 45320fd8ec21a330c87d0e22e4cf40300bf9d8c9 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:13:15 -0700 Subject: [PATCH 38/45] GetBotGroupLeaderIdByBotGroupName converted to QueryDatabase --- zone/bot.cpp | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 8b4d8d072..6817bf540 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4868,34 +4868,17 @@ uint32 Bot::GetBotGroupIdByBotGroupName(std::string botGroupName, std::string* e } uint32 Bot::GetBotGroupLeaderIdByBotGroupName(std::string botGroupName) { - uint32 result = 0; - if(!botGroupName.empty()) { - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botGroupName.empty()) + return 0; - if(database.RunQuery(Query, MakeAnyLenString(&Query, "select BotGroupLeaderBotId from vwBotGroups where BotGroupName = '%s'", botGroupName.c_str()), 0, &DatasetResult)) { - uint32 RowCount = mysql_num_rows(DatasetResult); + std::string query = StringFormat("SELECT BotGroupLeaderBotId FROM vwBotGroups WHERE BotGroupName = '%s'", botGroupName.c_str()); + auto results = database.QueryDatabase(query); + if (!results.Success() || results.RowCount() == 0) + return 0; - if(RowCount > 0) { - for(int iCounter = 0; iCounter < RowCount; iCounter++) { - DataRow = mysql_fetch_row(DatasetResult); - - if(DataRow) { - result = atoi(DataRow[0]); - break; - } - } - } - - mysql_free_result(DatasetResult); - } - - safe_delete_array(Query); - } - - return result; + auto row = results.begin(); + return atoi(row[0]); } uint32 Bot::AllowedBotSpawns(uint32 botOwnerCharacterID, std::string* errorMessage) { From 0ac238d762bbc8989f7ce933effc1de6cd46957c Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:16:22 -0700 Subject: [PATCH 39/45] AllowedBotSpawns converted to QueryDatabase --- zone/bot.cpp | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 6817bf540..b38a74f79 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4882,30 +4882,24 @@ uint32 Bot::GetBotGroupLeaderIdByBotGroupName(std::string botGroupName) { } uint32 Bot::AllowedBotSpawns(uint32 botOwnerCharacterID, std::string* errorMessage) { - uint32 Result = 0; - if(botOwnerCharacterID > 0) { - char ErrBuf[MYSQL_ERRMSG_SIZE]; - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botOwnerCharacterID == 0) + return 0; - if(database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT value FROM quest_globals WHERE name='bot_spawn_limit' and charid=%i", botOwnerCharacterID), ErrBuf, &DatasetResult)) { - if(mysql_num_rows(DatasetResult) == 1) { - DataRow = mysql_fetch_row(DatasetResult); - if(DataRow) - Result = atoi(DataRow[0]); - } + std::string query = StringFormat("SELECT value FROM quest_globals " + "WHERE name = 'bot_spawn_limit' AND charid = %i", + botOwnerCharacterID); + auto results = database.QueryDatabase(query); + if (!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return 0; + } - mysql_free_result(DatasetResult); - } - else - *errorMessage = std::string(ErrBuf); + if (results.RowCount() != 1) + return 0; - safe_delete_array(Query); - } - - return Result; + auto row = results.begin(); + return atoi(row[0]); } uint32 Bot::SpawnedBotCount(uint32 botOwnerCharacterID, std::string* errorMessage) { From 2429980fd5eae59abf4846f348d2d18b9e850c25 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:21:17 -0700 Subject: [PATCH 40/45] CreatedBotCount converted to QueryDatabase --- zone/bot.cpp | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index b38a74f79..f782090d7 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4915,30 +4915,23 @@ uint32 Bot::SpawnedBotCount(uint32 botOwnerCharacterID, std::string* errorMessag } uint32 Bot::CreatedBotCount(uint32 botOwnerCharacterID, std::string* errorMessage) { - uint32 Result = 0; - if(botOwnerCharacterID > 0) { - char ErrBuf[MYSQL_ERRMSG_SIZE]; - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botOwnerCharacterID == 0) + return 0; - if(database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(BotID) FROM bots WHERE BotOwnerCharacterID=%i", botOwnerCharacterID), ErrBuf, &DatasetResult)) { - if(mysql_num_rows(DatasetResult) == 1) { - DataRow = mysql_fetch_row(DatasetResult); - if(DataRow) - Result = atoi(DataRow[0]); - } + std::string query = StringFormat("SELECT COUNT(BotID) FROM bots " + "WHERE BotOwnerCharacterID=%i", botOwnerCharacterID); + auto results = database.QueryDatabase(query); + if (!results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return 0; + } - mysql_free_result(DatasetResult); - } - else - *errorMessage = std::string(ErrBuf); + if (results.RowCount() != 1) + return 0; - safe_delete_array(Query); - } - - return Result; + auto row = results.begin(); + return atoi(row[0]); } uint32 Bot::GetBotOwnerCharacterID(uint32 botID, std::string* errorMessage) { From 081192d29eecc71f9bfe370bab8187f875854122 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:24:30 -0700 Subject: [PATCH 41/45] GetBotOwnerCharacterID converted to QueryDatabase --- zone/bot.cpp | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index f782090d7..ff6c2c0b5 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -4935,29 +4935,22 @@ uint32 Bot::CreatedBotCount(uint32 botOwnerCharacterID, std::string* errorMessag } uint32 Bot::GetBotOwnerCharacterID(uint32 botID, std::string* errorMessage) { - uint32 Result = 0; - if(botID > 0) { - char ErrBuf[MYSQL_ERRMSG_SIZE]; - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + if(botID == 0) + return 0; - if(database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT BotOwnerCharacterID FROM bots WHERE BotID = %u", botID), ErrBuf, &DatasetResult)) { - if(mysql_num_rows(DatasetResult) == 1) { - if(DataRow = mysql_fetch_row(DatasetResult)) - Result = atoi(DataRow[0]); - } + std::string query = StringFormat("SELECT BotOwnerCharacterID FROM bots WHERE BotID = %u", botID); + auto results = database.QueryDatabase(query); + if (results.Success()) { + *errorMessage = std::string(results.ErrorMessage()); + return 0; + } - mysql_free_result(DatasetResult); - } - else - *errorMessage = std::string(ErrBuf); + if (results.RowCount() != 1) + return 0; - safe_delete_array(Query); - } - - return Result; + auto row = results.begin(); + return atoi(row[0]); } void Bot::LevelBotWithClient(Client* client, uint8 level, bool sendlvlapp) { From 2095380ba469473434fab5bf35465340eadb849c Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:31:14 -0700 Subject: [PATCH 42/45] SetBotGuildMembership converted to QueryDatabase --- zone/bot.cpp | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index ff6c2c0b5..c8e93d735 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -8557,28 +8557,19 @@ bool Bot::ProcessGuildRemoval(Client* guildOfficer, std::string botName) { } void Bot::SetBotGuildMembership(uint32 botId, uint32 guildid, uint8 rank) { - if(botId > 0) { - std::string errorMessage; - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; + if(botId == 0) + return; - if(guildid > 0) { - if(!database.RunQuery(query, MakeAnyLenString(&query, "REPLACE INTO botguildmembers SET char_id = %u, guild_id = %u, rank = %u;", botId, guildid, rank), errbuf)) { - errorMessage = std::string(errbuf); - } - } - else { - if(!database.RunQuery(query, MakeAnyLenString(&query, "DELETE FROM botguildmembers WHERE char_id = %u;", botId), errbuf)) { - errorMessage = std::string(errbuf); - } - } + if(guildid > 0) { + std::string query = StringFormat("REPLACE INTO botguildmembers " + "SET char_id = %u, guild_id = %u, rank = %u;", + botId, guildid, rank); + auto results = database.QueryDatabase(query); + return; + } - safe_delete_array(query); - - if(!errorMessage.empty()) { - // TODO: Log this error message to the zone error log - } - } + std::string query = StringFormat("DELETE FROM botguildmembers WHERE char_id = %u;", botId); + auto results = database.QueryDatabase(query); } void Bot::LoadGuildMembership(uint32* guildId, uint8* guildRank, std::string* guildName) { From 049a0bf787e4f98b8a3a31e4a047940ef4a05b01 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:41:21 -0700 Subject: [PATCH 43/45] LoadGuildMembership converted to QueryDatabase --- zone/bot.cpp | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index c8e93d735..3a475c5ce 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -8573,33 +8573,23 @@ void Bot::SetBotGuildMembership(uint32 botId, uint32 guildid, uint8 rank) { } void Bot::LoadGuildMembership(uint32* guildId, uint8* guildRank, std::string* guildName) { - if(guildId && guildRank && guildName) { - std::string errorMessage; - char* Query = 0; - char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE]; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; - if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT gm.guild_id, gm.rank, g.name FROM vwGuildMembers AS gm JOIN guilds AS g ON gm.guild_id = g.id WHERE gm.char_id = %u AND gm.mobtype = 'B';", GetBotID()), TempErrorMessageBuffer, &DatasetResult)) { - errorMessage = std::string(TempErrorMessageBuffer); - } - else { - while(DataRow = mysql_fetch_row(DatasetResult)) { - *guildId = atoi(DataRow[0]); - *guildRank = atoi(DataRow[1]); - *guildName = std::string(DataRow[2]); - break; - } + if(guildId == nullptr || guildRank == nullptr || guildName == nullptr) + return; - mysql_free_result(DatasetResult); - } + std::string query = StringFormat("SELECT gm.guild_id, gm.rank, g.name " + "FROM vwGuildMembers AS gm JOIN guilds AS g " + "ON gm.guild_id = g.id " + "WHERE gm.char_id = %u AND gm.mobtype = 'B';", + GetBotID()); + auto results = database.QueryDatabase(query); + if(!results.Success() || results.RowCount() == 0) + return; - safe_delete(Query); - - if(!errorMessage.empty()) { - // TODO: Record this error message to zone error log - } - } + auto row = results.begin(); + *guildId = atoi(row[0]); + *guildRank = atoi(row[1]); + *guildName = std::string(row[2]); } int32 Bot::CalcMaxMana() { From 73c8d3d09da9b749b793a567fcf6220f91100da6 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:46:41 -0700 Subject: [PATCH 44/45] ProcessBoyCommands converted to QueryDatabase --- zone/bot.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 3a475c5ce..28a82cdc7 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -11349,18 +11349,22 @@ void Bot::ProcessBotCommands(Client *c, const Seperator *sep) { uint32 botid = c->GetTarget()->CastToBot()->GetBotID(); std::string errorMessage; - char* Query = 0; + int setslot = atoi(sep->arg[2]); uint8 red = atoi(sep->arg[3]); uint8 green = atoi(sep->arg[4]); uint8 blue = atoi(sep->arg[5]); uint32 setcolor = (red << 16) | (green << 8) | blue; + std::string query = StringFormat("UPDATE botinventory SET color = %u " + "WHERE slotID = %i AND botID = %u", + setcolor, setslot, botid); + auto results = database.QueryDatabase(query); + if(!results.Success()) + return; - if(database.RunQuery(Query, MakeAnyLenString(&Query, "UPDATE botinventory SET color = %u WHERE slotID = %i AND botID = %u",setcolor, setslot, botid))){ - int slotmaterial = Inventory::CalcMaterialFromSlot(setslot); - c->GetTarget()->CastToBot()->SendWearChange(slotmaterial); - } + int slotmaterial = Inventory::CalcMaterialFromSlot(setslot); + c->GetTarget()->CastToBot()->SendWearChange(slotmaterial); } else { c->Message(15, "You must target a bot you own to do this."); @@ -11368,11 +11372,8 @@ void Bot::ProcessBotCommands(Client *c, const Seperator *sep) { return; } // Help for coloring bot armor - if(!strcasecmp(sep->arg[1], "help") && !strcasecmp(sep->arg[2], "armorcolor") ){ + if(!strcasecmp(sep->arg[1], "help") && !strcasecmp(sep->arg[2], "armorcolor") ){ //read from db - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; c->Message(0, "-----------------#bot armorcolor help-----------------------------"); c->Message(0, "Armor: 17(Chest/Robe), 7(Arms), 9(Bracer), 12(Hands), 18(Legs), 19(Boots), 2(Helm)"); From 95dc0c5fc8c64b551155e5f57a1d383abaebc449 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Wed, 3 Sep 2014 18:49:47 -0700 Subject: [PATCH 45/45] GetEquipmentColor converted to QueryDatabase --- zone/bot.cpp | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/zone/bot.cpp b/zone/bot.cpp index 28a82cdc7..a635fc959 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -16125,27 +16125,20 @@ uint32 Bot::GetEquipmentColor(uint8 material_slot) const { //Bot tints uint32 slotid = 0; - uint32 returncolor = 0; uint32 botid = this->GetBotID(); //Translate code slot # to DB slot # slotid = Inventory::CalcSlotFromMaterial(material_slot); //read from db - char* Query = 0; - MYSQL_RES* DatasetResult; - MYSQL_ROW DataRow; + std::string query = StringFormat("SELECT color FROM botinventory " + "WHERE BotID = %u AND SlotID = %u", botid, slotid); + auto results = database.QueryDatabase(query); + if (!results.Success() || results.RowCount() != 1) + return 0; - if(database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT color FROM botinventory WHERE BotID = %u AND SlotID = %u", botid, slotid), 0, &DatasetResult)) { - if(mysql_num_rows(DatasetResult) == 1) { - DataRow = mysql_fetch_row(DatasetResult); - if(DataRow) - returncolor = atoul(DataRow[0]); - } - mysql_free_result(DatasetResult); - safe_delete_array(Query); - } - return returncolor; + auto row = results.begin(); + return atoul(row[0]); } int Bot::GetRawACNoShield(int &shield_ac)