Merge pull request #242 from addtheice/RunQueryToDatabaseQuery_zone_questmgr

Run query to database query zone questmgr
This commit is contained in:
Alex 2014-09-25 17:26:22 -07:00
commit 763ed7f7a3

View File

@ -1337,81 +1337,69 @@ void QuestManager::setglobal(const char *varname, const char *newvalue, int opti
/* Inserts global variable into quest_globals table */ /* Inserts global variable into quest_globals table */
int QuestManager::InsertQuestGlobal(int charid, int npcid, int zoneid, const char *varname, const char *varvalue, int duration) { int QuestManager::InsertQuestGlobal(int charid, int npcid, int zoneid, const char *varname, const char *varvalue, int duration) {
char *query = 0;
char errbuf[MYSQL_ERRMSG_SIZE];
// Make duration string either "unix_timestamp(now()) + xxx" or "NULL" // Make duration string either "unix_timestamp(now()) + xxx" or "NULL"
std::stringstream duration_ss; std::string durationText = (duration == INT_MAX)? "NULL": StringFormat("unix_timestamp(now()) + %i", duration);
if (duration == INT_MAX) {
duration_ss << "NULL";
}
else {
duration_ss << "unix_timestamp(now()) + " << duration;
}
/* /*
NOTE: this should be escaping the contents of arglist NOTE: this should be escaping the contents of arglist
npcwise a malicious script can arbitrarily alter the DB npcwise a malicious script can arbitrarily alter the DB
*/ */
uint32 last_id = 0;
if (!database.RunQuery(query, MakeAnyLenString(&query,
"REPLACE INTO quest_globals (charid, npcid, zoneid, name, value, expdate)"
"VALUES (%i, %i, %i, '%s', '%s', %s)",
charid, npcid, zoneid, varname, varvalue, duration_ss.str().c_str()
), errbuf, nullptr, nullptr, &last_id))
{
std::cerr << "setglobal error inserting " << varname << " : " << errbuf << std::endl;
}
safe_delete_array(query);
if(zone) { std::string query = StringFormat("REPLACE INTO quest_globals "
/* Delete existing qglobal data and update zone processes */ "(charid, npcid, zoneid, name, value, expdate)"
ServerPacket* pack = new ServerPacket(ServerOP_QGlobalDelete, sizeof(ServerQGlobalDelete_Struct)); "VALUES (%i, %i, %i, '%s', '%s', %s)",
ServerQGlobalDelete_Struct *qgd = (ServerQGlobalDelete_Struct*)pack->pBuffer; charid, npcid, zoneid, varname, varvalue, durationText.c_str());
qgd->npc_id = npcid; auto results = database.QueryDatabase(query);
qgd->char_id = charid; if (!results.Success())
qgd->zone_id = zoneid; std::cerr << "setglobal error inserting " << varname << " : " << results.ErrorMessage() << std::endl;
qgd->from_zone_id = zone->GetZoneID();
qgd->from_instance_id = zone->GetInstanceID();
strcpy(qgd->name, varname);
entity_list.DeleteQGlobal(std::string((char*)qgd->name), qgd->npc_id, qgd->char_id, qgd->zone_id); if(!zone)
zone->DeleteQGlobal(std::string((char*)qgd->name), qgd->npc_id, qgd->char_id, qgd->zone_id); return 0;
worldserver.SendPacket(pack); /* Delete existing qglobal data and update zone processes */
safe_delete(pack); ServerPacket* pack = new ServerPacket(ServerOP_QGlobalDelete, sizeof(ServerQGlobalDelete_Struct));
ServerQGlobalDelete_Struct *qgd = (ServerQGlobalDelete_Struct*)pack->pBuffer;
qgd->npc_id = npcid;
qgd->char_id = charid;
qgd->zone_id = zoneid;
qgd->from_zone_id = zone->GetZoneID();
qgd->from_instance_id = zone->GetInstanceID();
strcpy(qgd->name, varname);
/* Create new qglobal data and update zone processes */ entity_list.DeleteQGlobal(std::string((char*)qgd->name), qgd->npc_id, qgd->char_id, qgd->zone_id);
pack = new ServerPacket(ServerOP_QGlobalUpdate, sizeof(ServerQGlobalUpdate_Struct)); zone->DeleteQGlobal(std::string((char*)qgd->name), qgd->npc_id, qgd->char_id, qgd->zone_id);
ServerQGlobalUpdate_Struct *qgu = (ServerQGlobalUpdate_Struct*)pack->pBuffer;
qgu->npc_id = npcid;
qgu->char_id = charid;
qgu->zone_id = zoneid;
if(duration == INT_MAX) {
qgu->expdate = 0xFFFFFFFF;
}
else {
qgu->expdate = Timer::GetTimeSeconds() + duration;
}
strcpy((char*)qgu->name, varname);
strn0cpy((char*)qgu->value, varvalue, 128);
qgu->id = last_id;
qgu->from_zone_id = zone->GetZoneID();
qgu->from_instance_id = zone->GetInstanceID();
QGlobal temp; worldserver.SendPacket(pack);
temp.npc_id = npcid; safe_delete(pack);
temp.char_id = charid;
temp.zone_id = zoneid;
temp.expdate = qgu->expdate;
temp.name.assign(qgu->name);
temp.value.assign(qgu->value);
entity_list.UpdateQGlobal(qgu->id, temp);
zone->UpdateQGlobal(qgu->id, temp);
worldserver.SendPacket(pack); /* Create new qglobal data and update zone processes */
safe_delete(pack); pack = new ServerPacket(ServerOP_QGlobalUpdate, sizeof(ServerQGlobalUpdate_Struct));
} ServerQGlobalUpdate_Struct *qgu = (ServerQGlobalUpdate_Struct*)pack->pBuffer;
qgu->npc_id = npcid;
qgu->char_id = charid;
qgu->zone_id = zoneid;
qgu->expdate = (duration == INT_MAX)? 0xFFFFFFFF: Timer::GetTimeSeconds() + duration;
strcpy((char*)qgu->name, varname);
strn0cpy((char*)qgu->value, varvalue, 128);
qgu->id = results.LastInsertedID();
qgu->from_zone_id = zone->GetZoneID();
qgu->from_instance_id = zone->GetInstanceID();
QGlobal temp;
temp.npc_id = npcid;
temp.char_id = charid;
temp.zone_id = zoneid;
temp.expdate = qgu->expdate;
temp.name.assign(qgu->name);
temp.value.assign(qgu->value);
entity_list.UpdateQGlobal(qgu->id, temp);
zone->UpdateQGlobal(qgu->id, temp);
worldserver.SendPacket(pack);
safe_delete(pack);
return 0; return 0;
} }
@ -1422,22 +1410,14 @@ void QuestManager::targlobal(const char *varname, const char *value, const char
void QuestManager::delglobal(const char *varname) { void QuestManager::delglobal(const char *varname) {
QuestManagerCurrentQuestVars(); QuestManagerCurrentQuestVars();
char errbuf[MYSQL_ERRMSG_SIZE];
char *query = 0;
int qgZoneid=zone->GetZoneID(); int qgZoneid=zone->GetZoneID();
int qgCharid=0; int qgCharid=0;
int qgNpcid=owner->GetNPCTypeID(); int qgNpcid=owner->GetNPCTypeID();
if (initiator && initiator->IsClient()) // some events like waypoint and spawn don't have a player involved if (initiator && initiator->IsClient()) // some events like waypoint and spawn don't have a player involved
{
qgCharid=initiator->CharacterID(); qgCharid=initiator->CharacterID();
} else
else {
qgCharid=-qgNpcid; // make char id negative npc id as a fudge qgCharid=-qgNpcid; // make char id negative npc id as a fudge
}
/* QS: PlayerLogQGlobalUpdate */ /* QS: PlayerLogQGlobalUpdate */
if (RuleB(QueryServ, PlayerLogQGlobalUpdate) && qgCharid && qgCharid > 0 && initiator && initiator->IsClient()){ if (RuleB(QueryServ, PlayerLogQGlobalUpdate) && qgCharid && qgCharid > 0 && initiator && initiator->IsClient()){
@ -1445,31 +1425,32 @@ void QuestManager::delglobal(const char *varname) {
QServ->PlayerLogEvent(Player_Log_QGlobal_Update, qgCharid, event_desc); QServ->PlayerLogEvent(Player_Log_QGlobal_Update, qgCharid, event_desc);
} }
if (!database.RunQuery(query, std::string query = StringFormat("DELETE FROM quest_globals "
MakeAnyLenString(&query, "WHERE name = '%s' "
"DELETE FROM quest_globals WHERE name='%s'" "&& (npcid=0 || npcid=%i) "
" && (npcid=0 || npcid=%i) && (charid=0 || charid=%i) && (zoneid=%i || zoneid=0)", "&& (charid=0 || charid=%i) "
varname,qgNpcid,qgCharid,qgZoneid),errbuf)) "&& (zoneid=%i || zoneid=0)",
{ varname, qgNpcid, qgCharid, qgZoneid);
std::cerr << "delglobal error deleting " << varname << " : " << errbuf << std::endl; auto results = database.QueryDatabase(query);
} if (!results.Success())
safe_delete_array(query); std::cerr << "delglobal error deleting " << varname << " : " << results.ErrorMessage() << std::endl;
if(zone) { if(!zone)
ServerPacket* pack = new ServerPacket(ServerOP_QGlobalDelete, sizeof(ServerQGlobalDelete_Struct)); return;
ServerQGlobalDelete_Struct *qgu = (ServerQGlobalDelete_Struct*)pack->pBuffer;
qgu->npc_id = qgNpcid; ServerPacket* pack = new ServerPacket(ServerOP_QGlobalDelete, sizeof(ServerQGlobalDelete_Struct));
qgu->char_id = qgCharid; ServerQGlobalDelete_Struct *qgu = (ServerQGlobalDelete_Struct*)pack->pBuffer;
qgu->zone_id = qgZoneid;
strcpy(qgu->name, varname);
entity_list.DeleteQGlobal(std::string((char*)qgu->name), qgu->npc_id, qgu->char_id, qgu->zone_id); qgu->npc_id = qgNpcid;
zone->DeleteQGlobal(std::string((char*)qgu->name), qgu->npc_id, qgu->char_id, qgu->zone_id); qgu->char_id = qgCharid;
qgu->zone_id = qgZoneid;
strcpy(qgu->name, varname);
worldserver.SendPacket(pack); entity_list.DeleteQGlobal(std::string((char*)qgu->name), qgu->npc_id, qgu->char_id, qgu->zone_id);
safe_delete(pack); zone->DeleteQGlobal(std::string((char*)qgu->name), qgu->npc_id, qgu->char_id, qgu->zone_id);
}
worldserver.SendPacket(pack);
safe_delete(pack);
} }
// Converts duration string to duration value (in seconds) // Converts duration string to duration value (in seconds)
@ -1690,11 +1671,6 @@ void QuestManager::showgrid(int grid) {
if(initiator == nullptr) if(initiator == nullptr)
return; return;
char errbuf[MYSQL_ERRMSG_SIZE];
char *query = 0;
MYSQL_RES *result;
MYSQL_ROW row;
FindPerson_Point pt; FindPerson_Point pt;
std::vector<FindPerson_Point> pts; std::vector<FindPerson_Point> pts;
@ -1704,22 +1680,25 @@ void QuestManager::showgrid(int grid) {
pts.push_back(pt); pts.push_back(pt);
// Retrieve all waypoints for this grid // Retrieve all waypoints for this grid
if(database.RunQuery(query,MakeAnyLenString(&query,"SELECT `x`,`y`,`z` FROM grid_entries WHERE `gridid`=%i AND `zoneid`=%i ORDER BY `number`",grid,zone->GetZoneID()),errbuf,&result)) { std::string query = StringFormat("SELECT `x`,`y`,`z` FROM grid_entries "
while((row = mysql_fetch_row(result))) { "WHERE `gridid` = %i AND `zoneid` = %i "
pt.x = atof(row[0]); "ORDER BY `number`", grid, zone->GetZoneID());
pt.y = atof(row[1]); auto results = database.QueryDatabase(query);
pt.z = atof(row[2]); if (!results.Success()) {
pts.push_back(pt); LogFile->write(EQEMuLog::Quest, "Error loading grid %d for showgrid(): %s", grid, results.ErrorMessage().c_str());
}
mysql_free_result(result);
initiator->SendPathPacket(pts);
}
else // DB query error!
{
LogFile->write(EQEMuLog::Quest, "Error loading grid %d for showgrid(): %s", grid, errbuf);
return; return;
} }
safe_delete_array(query);
for(auto row = results.begin(); row != results.end(); ++row) {
pt.x = atof(row[0]);
pt.y = atof(row[1]);
pt.z = atof(row[2]);
pts.push_back(pt);
}
initiator->SendPathPacket(pts);
} }
//change the value of a spawn condition //change the value of a spawn condition
@ -2295,19 +2274,19 @@ bool QuestManager::istaskappropriate(int task) {
} }
void QuestManager::clearspawntimers() { void QuestManager::clearspawntimers() {
if(zone) { if(!zone)
//TODO: Dec 19, 2008, replace with code updated for current spawn timers. return;
LinkedListIterator<Spawn2*> iterator(zone->spawn2_list);
iterator.Reset(); //TODO: Dec 19, 2008, replace with code updated for current spawn timers.
while (iterator.MoreElements()) LinkedListIterator<Spawn2*> iterator(zone->spawn2_list);
{ iterator.Reset();
char errbuf[MYSQL_ERRMSG_SIZE]; while (iterator.MoreElements()) {
char *query = 0; std::string query = StringFormat("DELETE FROM respawn_times "
database.RunQuery(query, MakeAnyLenString(&query, "DELETE FROM respawn_times WHERE id=%lu AND " "WHERE id = %lu AND instance_id = %lu",
"instance_id=%lu",(unsigned long)iterator.GetData()->GetID(), (unsigned long)zone->GetInstanceID()), errbuf); (unsigned long)iterator.GetData()->GetID(),
safe_delete_array(query); (unsigned long)zone->GetInstanceID());
iterator.Advance(); auto results = database.QueryDatabase(query);
} iterator.Advance();
} }
} }
@ -2684,11 +2663,6 @@ void QuestManager::FlagInstanceByRaidLeader(uint32 zone, int16 version)
const char* QuestManager::saylink(char* Phrase, bool silent, const char* LinkName) { const char* QuestManager::saylink(char* Phrase, bool silent, const char* LinkName) {
QuestManagerCurrentQuestVars(); QuestManagerCurrentQuestVars();
const char *ERR_MYSQLERROR = "Error in saylink phrase queries";
char errbuf[MYSQL_ERRMSG_SIZE];
char *query = 0;
MYSQL_RES *result;
MYSQL_ROW row;
int sayid = 0; int sayid = 0;
int sz = strlen(Phrase); int sz = strlen(Phrase);
@ -2696,42 +2670,26 @@ const char* QuestManager::saylink(char* Phrase, bool silent, const char* LinkNam
database.DoEscapeString(escaped_string, Phrase, sz); database.DoEscapeString(escaped_string, Phrase, sz);
// Query for an existing phrase and id in the saylink table // Query for an existing phrase and id in the saylink table
if(database.RunQuery(query,MakeAnyLenString(&query,"SELECT `id` FROM `saylink` WHERE `phrase` = '%s'", escaped_string),errbuf,&result)) std::string query = StringFormat("SELECT `id` FROM `saylink` WHERE `phrase` = '%s'", escaped_string);
auto results = database.QueryDatabase(query);
if(results.Success())
{ {
if (mysql_num_rows(result) >= 1) if (results.RowCount() >= 1)
{ for (auto row = results.begin();row != results.end(); ++row)
while((row = mysql_fetch_row(result)))
{
sayid = atoi(row[0]); sayid = atoi(row[0]);
}
mysql_free_result(result);
}
else // Add a new saylink entry to the database and query it again for the new sayid number else // Add a new saylink entry to the database and query it again for the new sayid number
{ {
safe_delete_array(query); query = StringFormat("INSERT INTO `saylink` (`phrase`) VALUES ('%s')", escaped_string);
results = database.QueryDatabase(query);
database.RunQuery(query,MakeAnyLenString(&query,"INSERT INTO `saylink` (`phrase`) VALUES ('%s')", escaped_string),errbuf); if(!results.Success())
safe_delete_array(query); LogFile->write(EQEMuLog::Error, "Error in saylink phrase queries", results.ErrorMessage().c_str());
else if (results.RowCount() >= 1)
if(database.RunQuery(query,MakeAnyLenString(&query,"SELECT `id` FROM saylink WHERE `phrase` = '%s'", escaped_string),errbuf,&result)) for(auto row = results.begin(); row != results.end(); ++row)
{
if (mysql_num_rows(result) >= 1)
{
while((row = mysql_fetch_row(result)))
{
sayid = atoi(row[0]); sayid = atoi(row[0]);
}
mysql_free_result(result);
}
}
else
{
LogFile->write(EQEMuLog::Error, ERR_MYSQLERROR, errbuf);
}
safe_delete_array(query);
} }
} }
safe_delete_array(query);
safe_delete_array(escaped_string); safe_delete_array(escaped_string);
if(silent) if(silent)
@ -2739,27 +2697,21 @@ const char* QuestManager::saylink(char* Phrase, bool silent, const char* LinkNam
else else
sayid = sayid + 500000; sayid = sayid + 500000;
//Create the say link as an item link hash //Create the say link as an item link hash
char linktext[250]; char linktext[250];
if(initiator) if(initiator)
{ {
if (initiator->GetClientVersion() >= EQClientRoF) if (initiator->GetClientVersion() >= EQClientRoF)
{
sprintf(linktext,"%c%06X%s%s%c",0x12,sayid,"0000000000000000000000000000000000000000000000000",LinkName,0x12); sprintf(linktext,"%c%06X%s%s%c",0x12,sayid,"0000000000000000000000000000000000000000000000000",LinkName,0x12);
}
else if (initiator->GetClientVersion() >= EQClientSoF) else if (initiator->GetClientVersion() >= EQClientSoF)
{
sprintf(linktext,"%c%06X%s%s%c",0x12,sayid,"00000000000000000000000000000000000000000000",LinkName,0x12); sprintf(linktext,"%c%06X%s%s%c",0x12,sayid,"00000000000000000000000000000000000000000000",LinkName,0x12);
}
else else
{
sprintf(linktext,"%c%06X%s%s%c",0x12,sayid,"000000000000000000000000000000000000000",LinkName,0x12); sprintf(linktext,"%c%06X%s%s%c",0x12,sayid,"000000000000000000000000000000000000000",LinkName,0x12);
}
} }
else { // If no initiator, create an RoF saylink, since older clients handle RoF ones better than RoF handles older ones. else // If no initiator, create an RoF saylink, since older clients handle RoF ones better than RoF handles older ones.
sprintf(linktext,"%c%06X%s%s%c",0x12,sayid,"0000000000000000000000000000000000000000000000000",LinkName,0x12); sprintf(linktext,"%c%06X%s%s%c",0x12,sayid,"0000000000000000000000000000000000000000000000000",LinkName,0x12);
}
strcpy(Phrase,linktext); strcpy(Phrase,linktext);
return Phrase; return Phrase;