diff --git a/world/worlddb.cpp b/world/worlddb.cpp index 97b2a74d5..c82a728eb 100644 --- a/world/worlddb.cpp +++ b/world/worlddb.cpp @@ -35,7 +35,7 @@ extern std::vector character_create_race_class_combos; // solar: the current stuff is at the bottom of this function void WorldDatabase::GetCharSelectInfo(uint32 account_id, CharacterSelect_Struct* cs, uint32 ClientVersion) { Inventory *inv; - uint8 has_home = 0; + uint8 has_home = 0; uint8 has_bind = 0; /* Initialize Variables */ @@ -49,7 +49,7 @@ void WorldDatabase::GetCharSelectInfo(uint32 account_id, CharacterSelect_Struct* /* Get Character Info */ std::string cquery = StringFormat( - "SELECT " + "SELECT " "`id`, " // 0 "name, " // 1 "gender, " // 2 @@ -70,8 +70,8 @@ void WorldDatabase::GetCharSelectInfo(uint32 account_id, CharacterSelect_Struct* "drakkin_tattoo, " // 17 "drakkin_details, " // 18 "zone_id " // 19 - "FROM " - "character_data " + "FROM " + "character_data " "WHERE `account_id` = %i ORDER BY `name` LIMIT 10 ", account_id); auto results = database.QueryDatabase(cquery); int char_num = 0; for (auto row = results.begin(); row != results.end(); ++row) { @@ -108,11 +108,11 @@ void WorldDatabase::GetCharSelectInfo(uint32 account_id, CharacterSelect_Struct* } /* Set Bind Point Data for any character that may possibly be missing it for any reason */ - cquery = StringFormat("SELECT `zone_id`, `instance_id`, `x`, `y`, `z`, `heading`, `is_home` FROM `character_bind` WHERE `id` = %i LIMIT 2", character_id); + cquery = StringFormat("SELECT `zone_id`, `instance_id`, `x`, `y`, `z`, `heading`, `is_home` FROM `character_bind` WHERE `id` = %i LIMIT 2", character_id); auto results_bind = database.QueryDatabase(cquery); has_home = 0; has_bind = 0; for (auto row_b = results_bind.begin(); row_b != results_bind.end(); ++row_b) { if (row_b[6] && atoi(row_b[6]) == 1){ has_home = 1; } - if (row_b[6] && atoi(row_b[6]) == 0){ has_bind = 1; } + if (row_b[6] && atoi(row_b[6]) == 0){ has_bind = 1; } } if (has_home == 0 || has_bind == 0){ @@ -121,34 +121,34 @@ void WorldDatabase::GetCharSelectInfo(uint32 account_id, CharacterSelect_Struct* auto results_bind = database.QueryDatabase(cquery); for (auto row_d = results_bind.begin(); row_d != results_bind.end(); ++row_d) { /* If a bind_id is specified, make them start there */ - if (atoi(row_d[1]) != 0) { + if (atoi(row_d[1]) != 0) { pp.binds[4].zoneId = (uint32)atoi(row_d[1]); GetSafePoints(pp.binds[4].zoneId, 0, &pp.binds[4].x, &pp.binds[4].y, &pp.binds[4].z); } /* Otherwise, use the zone and coordinates given */ - else { + else { pp.binds[4].zoneId = (uint32)atoi(row_d[0]); - float x = atof(row_d[2]); - float y = atof(row_d[3]); - float z = atof(row_d[4]); - if (x == 0 && y == 0 && z == 0){ GetSafePoints(pp.binds[4].zoneId, 0, &x, &y, &z); } + float x = atof(row_d[2]); + float y = atof(row_d[3]); + float z = atof(row_d[4]); + if (x == 0 && y == 0 && z == 0){ GetSafePoints(pp.binds[4].zoneId, 0, &x, &y, &z); } pp.binds[4].x = x; pp.binds[4].y = y; pp.binds[4].z = z; - + } } pp.binds[0] = pp.binds[4]; /* If no home bind set, set it */ if (has_home == 0){ std::string query = StringFormat("REPLACE INTO `character_bind` (id, zone_id, instance_id, x, y, z, heading, is_home)" - " VALUES (%u, %u, %u, %f, %f, %f, %f, %i)", - character_id, pp.binds[4].zoneId, 0, pp.binds[4].x, pp.binds[4].y, pp.binds[4].z, pp.binds[4].heading, 1); + " VALUES (%u, %u, %u, %f, %f, %f, %f, %i)", + character_id, pp.binds[4].zoneId, 0, pp.binds[4].x, pp.binds[4].y, pp.binds[4].z, pp.binds[4].heading, 1); auto results_bset = QueryDatabase(query); ThrowDBError(results_bset.ErrorMessage(), "WorldDatabase::GetCharSelectInfo Set Home Point", query); } /* If no regular bind set, set it */ if (has_bind == 0){ std::string query = StringFormat("REPLACE INTO `character_bind` (id, zone_id, instance_id, x, y, z, heading, is_home)" - " VALUES (%u, %u, %u, %f, %f, %f, %f, %i)", - character_id, pp.binds[0].zoneId, 0, pp.binds[0].x, pp.binds[0].y, pp.binds[0].z, pp.binds[0].heading, 0); + " VALUES (%u, %u, %u, %f, %f, %f, %f, %i)", + character_id, pp.binds[0].zoneId, 0, pp.binds[0].x, pp.binds[0].y, pp.binds[0].z, pp.binds[0].heading, 0); auto results_bset = QueryDatabase(query); ThrowDBError(results_bset.ErrorMessage(), "WorldDatabase::GetCharSelectInfo Set Bind Point", query); } } @@ -166,7 +166,7 @@ void WorldDatabase::GetCharSelectInfo(uint32 account_id, CharacterSelect_Struct* cquery = StringFormat("SELECT slot, red, green, blue, use_tint, color FROM `character_material` WHERE `id` = %u", character_id); auto results_b = database.QueryDatabase(cquery); uint8 slot = 0; for (auto row_b = results_b.begin(); row_b != results_b.end(); ++row_b) { - slot = atoi(row_b[0]); + slot = atoi(row_b[0]); pp.item_tint[slot].rgb.red = atoi(row_b[1]); pp.item_tint[slot].rgb.green = atoi(row_b[2]); pp.item_tint[slot].rgb.blue = atoi(row_b[3]); @@ -187,7 +187,7 @@ void WorldDatabase::GetCharSelectInfo(uint32 account_id, CharacterSelect_Struct* if (pp.item_tint[material].rgb.use_tint){ color = pp.item_tint[material].color; } else{ color = item->GetItem()->Color; } - cs->cs_colors[char_num][material].color = color; + cs->cs_colors[char_num][material].color = color; /* Weapons are handled a bit differently */ if ((material == MaterialPrimary) || (material == MaterialSecondary)) { @@ -227,49 +227,28 @@ int WorldDatabase::MoveCharacterToBind(int CharID, uint8 bindnum) { bool WorldDatabase::GetStartZone(PlayerProfile_Struct* in_pp, CharCreate_Struct* in_cc) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - MYSQL_RES *result; - MYSQL_ROW row = 0; - int rows; - if(!in_pp || !in_cc) return false; in_pp->x = in_pp->y = in_pp->z = in_pp->heading = in_pp->zone_id = 0; in_pp->binds[0].x = in_pp->binds[0].y = in_pp->binds[0].z = in_pp->binds[0].zoneId = 0; - if(!RunQuery(query, MakeAnyLenString(&query, "SELECT x,y,z,heading,zone_id,bind_id FROM start_zones WHERE player_choice=%i AND player_class=%i " - "AND player_deity=%i AND player_race=%i", - in_cc->start_zone, - in_cc->class_, - in_cc->deity, - in_cc->race), errbuf, &result)) - { - LogFile->write(EQEMuLog::Error, "Start zone query failed: %s : %s\n", query, errbuf); - safe_delete_array(query); + std::string query = StringFormat("SELECT x, y, z, heading, zone_id, bind_id " + "FROM start_zones WHERE player_choice = % i " + "AND player_class = %i AND player_deity = %i " + "AND player_race = %i", + in_cc->start_zone, in_cc->class_, in_cc->deity, + in_cc->race); + auto results = QueryDatabase(query); + if(!results.Success()) { + LogFile->write(EQEMuLog::Error, "Start zone query failed: %s : %s\n", query.c_str(), results.ErrorMessage().c_str()); return false; } - LogFile->write(EQEMuLog::Status, "Start zone query: %s\n", query); - safe_delete_array(query); + LogFile->write(EQEMuLog::Status, "Start zone query: %s\n", query.c_str()); - if((rows = mysql_num_rows(result)) > 0) - row = mysql_fetch_row(result); - - if(row) - { - LogFile->write(EQEMuLog::Status, "Found starting location in start_zones"); - in_pp->x = atof(row[0]); - in_pp->y = atof(row[1]); - in_pp->z = atof(row[2]); - in_pp->heading = atof(row[3]); - in_pp->zone_id = atoi(row[4]); - in_pp->binds[0].zoneId = atoi(row[5]); - } - else - { - printf("No start_zones entry in database, using defaults\n"); + if (results.RowCount() == 0) { + printf("No start_zones entry in database, using defaults\n"); switch(in_cc->start_zone) { case 0: @@ -357,6 +336,16 @@ bool WorldDatabase::GetStartZone(PlayerProfile_Struct* in_pp, CharCreate_Struct* break; } } + } + else { + LogFile->write(EQEMuLog::Status, "Found starting location in start_zones"); + auto row = results.begin(); + in_pp->x = atof(row[0]); + in_pp->y = atof(row[1]); + in_pp->z = atof(row[2]); + in_pp->heading = atof(row[3]); + in_pp->zone_id = atoi(row[4]); + in_pp->binds[0].zoneId = atoi(row[5]); } if(in_pp->x == 0 && in_pp->y == 0 && in_pp->z == 0) @@ -364,14 +353,12 @@ bool WorldDatabase::GetStartZone(PlayerProfile_Struct* in_pp, CharCreate_Struct* if(in_pp->binds[0].x == 0 && in_pp->binds[0].y == 0 && in_pp->binds[0].z == 0) database.GetSafePoints(in_pp->binds[0].zoneId, 0, &in_pp->binds[0].x, &in_pp->binds[0].y, &in_pp->binds[0].z); - if(result) - mysql_free_result(result); + return true; } bool WorldDatabase::GetStartZoneSoF(PlayerProfile_Struct* in_pp, CharCreate_Struct* in_cc) { - // SoF doesn't send the player_choice field in character creation, it now sends the real zoneID instead. // // For SoF, search for an entry in start_zones with a matching zone_id, class, race and deity. @@ -379,53 +366,25 @@ bool WorldDatabase::GetStartZoneSoF(PlayerProfile_Struct* in_pp, CharCreate_Stru // For now, if no row matching row is found, send them to Crescent Reach, as that is probably the most likely // reason for no match being found. // - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - MYSQL_RES *result; - MYSQL_ROW row = 0; - int rows; - if(!in_pp || !in_cc) return false; in_pp->x = in_pp->y = in_pp->z = in_pp->heading = in_pp->zone_id = 0; in_pp->binds[0].x = in_pp->binds[0].y = in_pp->binds[0].z = in_pp->binds[0].zoneId = 0; - if(!RunQuery(query, MakeAnyLenString(&query, "SELECT x, y, z, heading, bind_id " - " FROM start_zones " - " WHERE zone_id = %i " - " AND player_class = %i " - " AND player_deity = %i" - " AND player_race = %i", - in_cc->start_zone, - in_cc->class_, - in_cc->deity, - in_cc->race), errbuf, &result)) - { - LogFile->write(EQEMuLog::Status, "SoF Start zone query failed: %s : %s\n", query, errbuf); - safe_delete_array(query); + std::string query = StringFormat("SELECT x, y, z, heading, bind_id FROM start_zones WHERE zone_id = %i " + "AND player_class = %i AND player_deity = %i AND player_race = %i", + in_cc->start_zone, in_cc->class_, in_cc->deity, in_cc->race); + auto results = QueryDatabase(query); + if(!results.Success()) { + LogFile->write(EQEMuLog::Status, "SoF Start zone query failed: %s : %s\n", query.c_str(), results.ErrorMessage().c_str()); return false; } - LogFile->write(EQEMuLog::Status, "SoF Start zone query: %s\n", query); - safe_delete_array(query); + LogFile->write(EQEMuLog::Status, "SoF Start zone query: %s\n", query.c_str()); - if((rows = mysql_num_rows(result)) > 0) - row = mysql_fetch_row(result); - - if(row) - { - LogFile->write(EQEMuLog::Status, "Found starting location in start_zones"); - in_pp->x = atof(row[0]); - in_pp->y = atof(row[1]); - in_pp->z = atof(row[2]); - in_pp->heading = atof(row[3]); - in_pp->zone_id = in_cc->start_zone; - in_pp->binds[0].zoneId = atoi(row[4]); - } - else - { - printf("No start_zones entry in database, using defaults\n"); + if (results.RowCount() == 0) { + printf("No start_zones entry in database, using defaults\n"); if(in_cc->start_zone == RuleI(World, TutorialZoneID)) in_pp->zone_id = in_cc->start_zone; @@ -435,7 +394,16 @@ bool WorldDatabase::GetStartZoneSoF(PlayerProfile_Struct* in_pp, CharCreate_Stru in_pp->z = in_pp->binds[0].z = 0.79; in_pp->zone_id = in_pp->binds[0].zoneId = 394; // Crescent Reach. } - + } + else { + LogFile->write(EQEMuLog::Status, "Found starting location in start_zones"); + auto row = results.begin(); + in_pp->x = atof(row[0]); + in_pp->y = atof(row[1]); + in_pp->z = atof(row[2]); + in_pp->heading = atof(row[3]); + in_pp->zone_id = in_cc->start_zone; + in_pp->binds[0].zoneId = atoi(row[4]); } if(in_pp->x == 0 && in_pp->y == 0 && in_pp->z == 0) @@ -443,39 +411,27 @@ bool WorldDatabase::GetStartZoneSoF(PlayerProfile_Struct* in_pp, CharCreate_Stru if(in_pp->binds[0].x == 0 && in_pp->binds[0].y == 0 && in_pp->binds[0].z == 0) database.GetSafePoints(in_pp->binds[0].zoneId, 0, &in_pp->binds[0].x, &in_pp->binds[0].y, &in_pp->binds[0].z); - if(result) - mysql_free_result(result); + return true; } void WorldDatabase::GetLauncherList(std::vector &rl) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char* query = 0; - MYSQL_RES *result; - MYSQL_ROW row; - rl.clear(); - if (RunQuery(query, MakeAnyLenString(&query, - "SELECT name FROM launcher" ) - , errbuf, &result)) - { - while ((row = mysql_fetch_row(result))) { - rl.push_back(row[0]); - } - mysql_free_result(result); - } - else { - LogFile->write(EQEMuLog::Error, "WorldDatabase::GetLauncherList: %s", errbuf); - } - safe_delete_array(query); + const std::string query = "SELECT name FROM launcher"; + auto results = QueryDatabase(query); + if (!results.Success()) { + LogFile->write(EQEMuLog::Error, "WorldDatabase::GetLauncherList: %s", results.ErrorMessage().c_str()); + return; + } + + for (auto row = results.begin(); row != results.end(); ++row) + rl.push_back(row[0]); + } void WorldDatabase::SetMailKey(int CharID, int IPAddress, int MailKey) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - char MailKeyString[17]; if(RuleB(Chat, EnableMailKeyIPVerification) == true) @@ -483,75 +439,60 @@ void WorldDatabase::SetMailKey(int CharID, int IPAddress, int MailKey) { else sprintf(MailKeyString, "%08X", MailKey); - if (!RunQuery(query, MakeAnyLenString(&query, "UPDATE `character_data` SET mailkey = '%s' WHERE id='%i'", - MailKeyString, CharID), errbuf)) - - LogFile->write(EQEMuLog::Error, "WorldDatabase::SetMailKey(%i, %s) : %s", CharID, MailKeyString, errbuf); - - safe_delete_array(query); + std::string query = StringFormat("UPDATE character_data SET mailkey = '%s' WHERE id = '%i'", + MailKeyString, CharID); + auto results = QueryDatabase(query); + if (!results.Success()) + LogFile->write(EQEMuLog::Error, "WorldDatabase::SetMailKey(%i, %s) : %s", CharID, MailKeyString, results.ErrorMessage().c_str()); } bool WorldDatabase::GetCharacterLevel(const char *name, int &level) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char* query = 0; - MYSQL_RES *result; - MYSQL_ROW row; + std::string query = StringFormat("SELECT level FROM character_data WHERE name = '%s'", name); + auto results = QueryDatabase(query); + if (!results.Success()) { + LogFile->write(EQEMuLog::Error, "WorldDatabase::GetCharacterLevel: %s", results.ErrorMessage().c_str()); + return false; + } - if(RunQuery(query, MakeAnyLenString(&query, "SELECT `level` FROM `character_data` WHERE `name` = '%s'", name), errbuf, &result)) - { - if(row = mysql_fetch_row(result)) - { - level = atoi(row[0]); - mysql_free_result(result); - safe_delete_array(query); - return true; - } - mysql_free_result(result); - } - else - { - LogFile->write(EQEMuLog::Error, "WorldDatabase::GetCharacterLevel: %s", errbuf); - } - safe_delete_array(query); - return false; + if (results.RowCount() == 0) + return false; + + auto row = results.begin(); + level = atoi(row[0]); + + return true; } bool WorldDatabase::LoadCharacterCreateAllocations() { character_create_allocations.clear(); - char errbuf[MYSQL_ERRMSG_SIZE]; - char* query = 0; - MYSQL_RES *result; - MYSQL_ROW row; - if(RunQuery(query, MakeAnyLenString(&query, "SELECT * FROM char_create_point_allocations order by id"), errbuf, &result)) { - safe_delete_array(query); - while(row = mysql_fetch_row(result)) { - RaceClassAllocation allocate; - int r = 0; - allocate.Index = atoi(row[r++]); - allocate.BaseStats[0] = atoi(row[r++]); - allocate.BaseStats[3] = atoi(row[r++]); - allocate.BaseStats[1] = atoi(row[r++]); - allocate.BaseStats[2] = atoi(row[r++]); - allocate.BaseStats[4] = atoi(row[r++]); - allocate.BaseStats[5] = atoi(row[r++]); - allocate.BaseStats[6] = atoi(row[r++]); - allocate.DefaultPointAllocation[0] = atoi(row[r++]); - allocate.DefaultPointAllocation[3] = atoi(row[r++]); - allocate.DefaultPointAllocation[1] = atoi(row[r++]); - allocate.DefaultPointAllocation[2] = atoi(row[r++]); - allocate.DefaultPointAllocation[4] = atoi(row[r++]); - allocate.DefaultPointAllocation[5] = atoi(row[r++]); - allocate.DefaultPointAllocation[6] = atoi(row[r++]); - character_create_allocations.push_back(allocate); - } - mysql_free_result(result); - } else { - safe_delete_array(query); - return false; - } + std::string query = "SELECT * FROM char_create_point_allocations ORDER BY id"; + auto results = QueryDatabase(query); + if (!results.Success()) + return false; + + for (auto row = results.begin(); row != results.end(); ++row) { + RaceClassAllocation allocate; + allocate.Index = atoi(row[0]); + allocate.BaseStats[0] = atoi(row[1]); + allocate.BaseStats[3] = atoi(row[2]); + allocate.BaseStats[1] = atoi(row[3]); + allocate.BaseStats[2] = atoi(row[4]); + allocate.BaseStats[4] = atoi(row[5]); + allocate.BaseStats[5] = atoi(row[6]); + allocate.BaseStats[6] = atoi(row[7]); + allocate.DefaultPointAllocation[0] = atoi(row[8]); + allocate.DefaultPointAllocation[3] = atoi(row[9]); + allocate.DefaultPointAllocation[1] = atoi(row[10]); + allocate.DefaultPointAllocation[2] = atoi(row[11]); + allocate.DefaultPointAllocation[4] = atoi(row[12]); + allocate.DefaultPointAllocation[5] = atoi(row[13]); + allocate.DefaultPointAllocation[6] = atoi(row[14]); + + character_create_allocations.push_back(allocate); + } return true; } @@ -559,27 +500,21 @@ bool WorldDatabase::LoadCharacterCreateAllocations() { bool WorldDatabase::LoadCharacterCreateCombos() { character_create_race_class_combos.clear(); - char errbuf[MYSQL_ERRMSG_SIZE]; - char* query = 0; - MYSQL_RES *result; - MYSQL_ROW row; - if(RunQuery(query, MakeAnyLenString(&query, "select * from char_create_combinations order by race, class, deity, start_zone"), errbuf, &result)) { - safe_delete_array(query); - while(row = mysql_fetch_row(result)) { - RaceClassCombos combo; - int r = 0; - combo.AllocationIndex = atoi(row[r++]); - combo.Race = atoi(row[r++]); - combo.Class = atoi(row[r++]); - combo.Deity = atoi(row[r++]); - combo.Zone = atoi(row[r++]); - combo.ExpansionRequired = atoi(row[r++]); - character_create_race_class_combos.push_back(combo); - } - mysql_free_result(result); - } else { - safe_delete_array(query); - return false; + std::string query = "SELECT * FROM char_create_combinations ORDER BY race, class, deity, start_zone"; + auto results = QueryDatabase(query); + if (!results.Success()) + return false; + + for (auto row = results.begin(); row != results.end(); ++row) { + RaceClassCombos combo; + combo.AllocationIndex = atoi(row[0]); + combo.Race = atoi(row[1]); + combo.Class = atoi(row[2]); + combo.Deity = atoi(row[3]); + combo.Zone = atoi(row[4]); + combo.ExpansionRequired = atoi(row[5]); + + character_create_race_class_combos.push_back(combo); } return true;