Core/World: improved GetGlobalPlayer* methods

This commit is contained in:
ShinDarth
2016-08-29 00:15:37 +02:00
parent 9ac33ca092
commit 0f30731133
4 changed files with 85 additions and 38 deletions

View File

@@ -17,6 +17,7 @@ void CharacterDatabaseConnection::DoPrepareStatements()
PrepareStatement(CHAR_DEL_NONEXISTENT_GUILD_BANK_ITEM, "DELETE FROM guild_bank_item WHERE guildid = ? AND TabId = ? AND SlotId = ?", CONNECTION_ASYNC);
PrepareStatement(CHAR_DEL_EXPIRED_BANS, "UPDATE character_banned SET active = 0 WHERE unbandate <= UNIX_TIMESTAMP() AND unbandate <> bandate", CONNECTION_ASYNC);
PrepareStatement(CHAR_SEL_DATA_BY_NAME, "SELECT guid, account, name, gender, race, class, level FROM characters WHERE deleteDate IS NULL AND name = ?", CONNECTION_BOTH);
PrepareStatement(CHAR_SEL_DATA_BY_GUID, "SELECT guid, account, name, gender, race, class, level FROM characters WHERE deleteDate IS NULL AND guid = ?", CONNECTION_BOTH);
PrepareStatement(CHAR_SEL_CHECK_NAME, "SELECT 1 FROM characters WHERE name = ?", CONNECTION_BOTH);
PrepareStatement(CHAR_SEL_CHECK_GUID, "SELECT 1 FROM characters WHERE guid = ?", CONNECTION_SYNCH);
PrepareStatement(CHAR_SEL_SUM_CHARS, "SELECT COUNT(guid) FROM characters WHERE account = ?", CONNECTION_BOTH);

View File

@@ -37,6 +37,7 @@ enum CharacterDatabaseStatements
CHAR_DEL_NONEXISTENT_GUILD_BANK_ITEM,
CHAR_DEL_EXPIRED_BANS,
CHAR_SEL_DATA_BY_NAME,
CHAR_SEL_DATA_BY_GUID,
CHAR_SEL_CHECK_NAME,
CHAR_SEL_CHECK_GUID,
CHAR_SEL_SUM_CHARS,

View File

@@ -2140,47 +2140,11 @@ void ObjectMgr::RemoveGameobjectFromGrid(uint32 guid, GameObjectData const* data
uint64 ObjectMgr::GetPlayerGUIDByName(std::string const& name) const
{
uint32 guidLow;
// Get data from global storage
if (guidLow = sWorld->GetGlobalPlayerGUID(name))
if (uint32 guidLow = sWorld->GetGlobalPlayerGUID(name))
return MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER);
// Player is not in the global storage, try to get it from the Database
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_DATA_BY_NAME);
stmt->setString(0, name);
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (result)
{
// Player was not in the global storage, but it was found in the database
// Let's add it to the global storage
sLog->outString("Player %s was not found in the global storage, but it was found in the database.", name.c_str());
Field* fields = result->Fetch();
guidLow = fields[0].GetUInt32();
sWorld->AddGlobalPlayerData(
guidLow, /*guid*/
fields[1].GetUInt32(), /*accountId*/
fields[2].GetString(), /*name*/
fields[3].GetUInt8(), /*gender*/
fields[4].GetUInt8(), /*race*/
fields[5].GetUInt8(), /*class*/
fields[6].GetUInt8(), /*level*/
0, /*mail count*/
0 /*guild id*/
);
sLog->outString("Player %s added to the global storage.", name.c_str());
return MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER);
}
// Player not found
// No player found
return 0;
}

View File

@@ -3201,16 +3201,97 @@ void World::DeleteGlobalPlayerData(uint32 guid, std::string const& name)
GlobalPlayerData const* World::GetGlobalPlayerData(uint32 guid) const
{
// Get data from global storage
GlobalPlayerDataMap::const_iterator itr = _globalPlayerDataStore.find(guid);
if (itr != _globalPlayerDataStore.end())
return &itr->second;
// Player is not in the global storage, try to get it from the Database
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_DATA_BY_GUID);
stmt->setUInt32(0, guid);
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (result)
{
// Player was not in the global storage, but it was found in the database
// Let's add it to the global storage
Field* fields = result->Fetch();
std::string name = fields[2].GetString();
sLog->outString("Player %s [GUID: %u] was not found in the global storage, but it was found in the database.", name.c_str(), guid);
sWorld->AddGlobalPlayerData(
fields[0].GetUInt32(), /*guid*/
fields[1].GetUInt32(), /*accountId*/
fields[2].GetString(), /*name*/
fields[3].GetUInt8(), /*gender*/
fields[4].GetUInt8(), /*race*/
fields[5].GetUInt8(), /*class*/
fields[6].GetUInt8(), /*level*/
0, /*mail count*/
0 /*guild id*/
);
itr = _globalPlayerDataStore.find(guid);
if (itr != _globalPlayerDataStore.end())
{
sLog->outString("Player %s [GUID: %u] added to the global storage.", name.c_str(), guid);
return &itr->second;
}
}
// Player not found
return NULL;
}
uint32 World::GetGlobalPlayerGUID(std::string const& name) const
{
// Get data from global storage
GlobalPlayerNameMap::const_iterator itr = _globalPlayerNameStore.find(name);
if (itr != _globalPlayerNameStore.end())
return itr->second;
// Player is not in the global storage, try to get it from the Database
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_DATA_BY_NAME);
stmt->setString(0, name);
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (result)
{
// Player was not in the global storage, but it was found in the database
// Let's add it to the global storage
Field* fields = result->Fetch();
uint32 guidLow = fields[0].GetUInt32();
sLog->outString("Player %s [GUID: %u] was not found in the global storage, but it was found in the database.", name.c_str(), guidLow);
sWorld->AddGlobalPlayerData(
guidLow, /*guid*/
fields[1].GetUInt32(), /*accountId*/
fields[2].GetString(), /*name*/
fields[3].GetUInt8(), /*gender*/
fields[4].GetUInt8(), /*race*/
fields[5].GetUInt8(), /*class*/
fields[6].GetUInt8(), /*level*/
0, /*mail count*/
0 /*guild id*/
);
itr = _globalPlayerNameStore.find(name);
if (itr != _globalPlayerNameStore.end())
{
sLog->outString("Player %s [GUID: %u] added to the global storage.", name.c_str(), guidLow);
return guidLow;
}
}
// Player not found
return 0;
}