diff --git a/common/database.cpp b/common/database.cpp index 5f8b11cb0..58896f0d1 100644 --- a/common/database.cpp +++ b/common/database.cpp @@ -2303,7 +2303,7 @@ bool Database::CheckDatabaseConversions() { } } } - QueryDatabase(StringFormat("ALTER TABLE `character_corpses` DROP COLUMN `data`")); + QueryDatabase(StringFormat("ALTER TABLE `character_corpses` DROP COLUMN `data`")); } diff --git a/common/dbcore.cpp b/common/dbcore.cpp index 8e609dae8..84d2528f0 100644 --- a/common/dbcore.cpp +++ b/common/dbcore.cpp @@ -155,95 +155,6 @@ MySQLRequestResult DBcore::QueryDatabase(const char* query, uint32 querylen, boo return requestResult; } -bool DBcore::RunQuery(const char* query, uint32 querylen, char* errbuf, MYSQL_RES** result, uint32* affected_rows, uint32* last_insert_id, uint32* errnum, bool retry) { - if (errnum) - *errnum = 0; - if (errbuf) - errbuf[0] = 0; - bool ret = false; - LockMutex lock(&MDatabase); - if (pStatus != Connected) - Open(); - - if (mysql_real_query(&mysql, query, querylen)) { - if (mysql_errno(&mysql) == CR_SERVER_GONE_ERROR) - pStatus = Error; - if (mysql_errno(&mysql) == CR_SERVER_LOST || mysql_errno(&mysql) == CR_SERVER_GONE_ERROR) { - if (retry) { - std::cout << "Database Error: Lost connection, attempting to recover...." << std::endl; - ret = RunQuery(query, querylen, errbuf, result, affected_rows, last_insert_id, errnum, false); - if (ret) - std::cout << "Reconnection to database successful." << std::endl; - } - else { - pStatus = Error; - if (errnum) - *errnum = mysql_errno(&mysql); - if (errbuf) - snprintf(errbuf, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(&mysql), mysql_error(&mysql)); - std::cout << "DB Query Error #" << mysql_errno(&mysql) << ": " << mysql_error(&mysql) << std::endl; - ret = false; - } - } - else { - if (errnum) - *errnum = mysql_errno(&mysql); - if (errbuf) - snprintf(errbuf, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(&mysql), mysql_error(&mysql)); -#ifdef _EQDEBUG - std::cout << "DB Query Error #" << mysql_errno(&mysql) << ": " << mysql_error(&mysql) << std::endl; -#endif - ret = false; - } - } - else { - if (result && mysql_field_count(&mysql)) { - *result = mysql_store_result(&mysql); -#ifdef _EQDEBUG - DBMemLeak::Alloc(*result, query); -#endif - } - else if (result) - *result = 0; - if (affected_rows) - *affected_rows = mysql_affected_rows(&mysql); - if (last_insert_id) - *last_insert_id = (uint32)mysql_insert_id(&mysql); - if (result) { - if (*result) { - ret = true; - } - else { -#ifdef _EQDEBUG - std::cout << "DB Query Error: No Result" << std::endl; -#endif - if (errnum) - *errnum = UINT_MAX; - if (errbuf) - strcpy(errbuf, "DBcore::RunQuery: No Result"); - ret = false; - } - } - else { - ret = true; - } - } -#if DEBUG_MYSQL_QUERIES >= 1 - if (ret) { - std::cout << "query successful"; - if (result && (*result)) - std::cout << ", " << (int) mysql_num_rows(*result) << " rows returned"; - if (affected_rows) - std::cout << ", " << (*affected_rows) << " rows affected"; - std::cout<< std::endl; - } - else { - std::cout << "QUERY: query FAILED" << std::endl; - } -#endif - return ret; -} - void DBcore::TransactionBegin() { QueryDatabase("START TRANSACTION"); } diff --git a/common/dbcore.h b/common/dbcore.h index 9dd5aef5e..0cdceb61b 100644 --- a/common/dbcore.h +++ b/common/dbcore.h @@ -23,7 +23,6 @@ public: DBcore(); ~DBcore(); eStatus GetStatus() { return pStatus; } - bool RunQuery(const char* query, uint32 querylen, char* errbuf = 0, MYSQL_RES** result = 0, uint32* affected_rows = 0, uint32* last_insert_id = 0, uint32* errnum = 0, bool retry = true); MySQLRequestResult QueryDatabase(const char* query, uint32 querylen, bool retryOnFailureOnce = true); MySQLRequestResult QueryDatabase(std::string query, bool retryOnFailureOnce = true); void TransactionBegin(); diff --git a/zone/corpse.cpp b/zone/corpse.cpp index a95b279ea..8be978350 100644 --- a/zone/corpse.cpp +++ b/zone/corpse.cpp @@ -344,8 +344,7 @@ Corpse::Corpse(Client* client, int32 in_rezexp) : Mob ( iter_queue it; for(it=client->GetInv().cursor_begin(),i=8001; it!=client->GetInv().cursor_end(); ++it,i++) { item = *it; - if((item && (!client->IsBecomeNPC())) || (item && client->IsBecomeNPC() && !item->GetItem()->NoRent)) - { + if((item && (!client->IsBecomeNPC())) || (item && client->IsBecomeNPC() && !item->GetItem()->NoRent)) { std::list slot_list = MoveItemToCorpse(client, item, i); removed_list.merge(slot_list); cursor = true; @@ -370,7 +369,7 @@ Corpse::Corpse(Client* client, int32 in_rezexp) : Mob ( ++iter; } ss << ")"; - database.RunQuery(ss.str().c_str(), ss.str().length()); + database.QueryDatabase(ss.str().c_str()); } if(cursor) { // all cursor items should be on corpse (client < SoF or RespawnFromHover = false) @@ -1395,44 +1394,23 @@ void Corpse::AddLooter(Mob* who) { } } -void Corpse::LoadPlayerCorpseDecayTime(uint32 dbid){ - if(!dbid) +void Corpse::LoadPlayerCorpseDecayTime(uint32 corpse_db_id){ + if(!corpse_db_id) return; - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - MYSQL_RES *result; - MYSQL_ROW row; - if (database.RunQuery(query, MakeAnyLenString(&query, "SELECT (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time_of_death)) FROM character_corpses WHERE id=%d and not time_of_death=0", dbid), errbuf, &result)) { - safe_delete_array(query); - while ((row = mysql_fetch_row(result))) { - if(atoi(row[0]) > 0 && RuleI(Character, CorpseDecayTimeMS) > (atoi(row[0]) * 1000)) { - corpse_decay_timer.SetTimer(RuleI(Character, CorpseDecayTimeMS) - (atoi(row[0]) * 1000)); - /* - if(RuleI(Character, CorpseResTimeMS) > (atoi(row[0]) * 1000)) { - corpse_res_timer.SetTimer(RuleI(Character, CorpseResTimeMS) - (atoi(row[0]) * 1000)); - } - else { - corpse_res_timer.Disable(); - can_rez = false; - } - */ - } - else { - corpse_decay_timer.SetTimer(2000); - //corpse_res_timer.SetTimer(300000); - } - if(atoi(row[0]) > 0 && RuleI(Zone, GraveyardTimeMS) > (atoi(row[0]) * 1000)) { - corpse_graveyard_timer.SetTimer(RuleI(Zone, GraveyardTimeMS) - (atoi(row[0]) * 1000)); - } - else { - corpse_graveyard_timer.SetTimer(3000); - } - } - mysql_free_result(result); + uint32 active_corpse_decay_timer = database.GetCharacterCorpseDecayTimer(corpse_db_id); + if (active_corpse_decay_timer > 0 && RuleI(Character, CorpseDecayTimeMS) > (active_corpse_decay_timer * 1000)) { + corpse_decay_timer.SetTimer(RuleI(Character, CorpseDecayTimeMS) - (active_corpse_decay_timer * 1000)); + } + else { + corpse_decay_timer.SetTimer(2000); + } + if (active_corpse_decay_timer > 0 && RuleI(Zone, GraveyardTimeMS) > (active_corpse_decay_timer * 1000)) { + corpse_graveyard_timer.SetTimer(RuleI(Zone, GraveyardTimeMS) - (active_corpse_decay_timer * 1000)); + } + else { + corpse_graveyard_timer.SetTimer(3000); } - else - safe_delete_array(query); } /* diff --git a/zone/zonedb.cpp b/zone/zonedb.cpp index a1b7779a8..aa05208df 100644 --- a/zone/zonedb.cpp +++ b/zone/zonedb.cpp @@ -3367,6 +3367,16 @@ uint32 ZoneDatabase::SendCharacterCorpseToGraveyard(uint32 dbid, uint32 zone_id, return dbid; } +uint32 ZoneDatabase::GetCharacterCorpseDecayTimer(uint32 corpse_db_id){ + std::string query = StringFormat("SELECT(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time_of_death)) FROM `character_corpses` WHERE `id` = %d AND NOT `time_of_death` = 0", corpse_db_id); + auto results = QueryDatabase(query); + auto row = results.begin(); + if (results.Success() && results.RowsAffected() != 0){ + return atoll(row[0]); + } + return 0; +} + uint32 ZoneDatabase::UpdateCharacterCorpse(uint32 db_id, uint32 char_id, const char* char_name, uint32 zone_id, uint16 instance_id, PlayerCorpse_Struct* dbpc, float x, float y, float z, float heading, bool is_rezzed) { std::string query = StringFormat("UPDATE `character_corpses` SET \n" "`charname` = '%s',\n" diff --git a/zone/zonedb.h b/zone/zonedb.h index 53f3496cd..33554aac3 100644 --- a/zone/zonedb.h +++ b/zone/zonedb.h @@ -311,6 +311,7 @@ public: bool UnburyCharacterCorpse(uint32 dbid, uint32 new_zoneid, uint16 dest_instanceid, float new_x, float new_y, float new_z, float new_heading); bool LoadCharacterCorpses(uint32 iZoneID, uint16 iInstanceID); bool DeleteGraveyard(uint32 zone_id, uint32 graveyard_id); + uint32 GetCharacterCorpseDecayTimer(uint32 corpse_db_id); uint32 GetCharacterBuriedCorpseCount(uint32 char_id); uint32 SendCharacterCorpseToGraveyard(uint32 dbid, uint32 zoneid, uint16 instanceid, float x, float y, float z, float heading); uint32 CreateGraveyardRecord(uint32 graveyard_zoneid, float graveyard_x, float graveyard_y, float graveyard_z, float graveyard_heading);