refactor(Scripts/Commands): Convert cs_character to new system (#8768)

This commit is contained in:
IntelligentQuantum
2021-10-30 15:34:39 +03:30
committed by GitHub
parent e10a3ab6d7
commit 668a86762b
3 changed files with 220 additions and 330 deletions
@@ -148,6 +148,7 @@ void CharacterDatabaseConnection::DoPrepareStatements()
PrepareStatement(CHAR_INS_ACCOUNT_INSTANCE_LOCK_TIMES, "INSERT INTO account_instance_times (accountId, instanceId, releaseTime) VALUES (?, ?, ?)", CONNECTION_ASYNC); PrepareStatement(CHAR_INS_ACCOUNT_INSTANCE_LOCK_TIMES, "INSERT INTO account_instance_times (accountId, instanceId, releaseTime) VALUES (?, ?, ?)", CONNECTION_ASYNC);
PrepareStatement(CHAR_SEL_MATCH_MAKER_RATING, "SELECT matchMakerRating, maxMMR FROM character_arena_stats WHERE guid = ? AND slot = ?", CONNECTION_SYNCH); PrepareStatement(CHAR_SEL_MATCH_MAKER_RATING, "SELECT matchMakerRating, maxMMR FROM character_arena_stats WHERE guid = ? AND slot = ?", CONNECTION_SYNCH);
PrepareStatement(CHAR_SEL_CHARACTER_COUNT, "SELECT account, COUNT(guid) FROM characters WHERE account = ? GROUP BY account", CONNECTION_ASYNC); PrepareStatement(CHAR_SEL_CHARACTER_COUNT, "SELECT account, COUNT(guid) FROM characters WHERE account = ? GROUP BY account", CONNECTION_ASYNC);
PrepareStatement(CHAR_UPD_NAME_BY_GUID, "UPDATE characters SET name = ? WHERE guid = ?", CONNECTION_ASYNC);
PrepareStatement(CHAR_DEL_DECLINED_NAME, "DELETE FROM character_declinedname WHERE guid = ?", CONNECTION_ASYNC); PrepareStatement(CHAR_DEL_DECLINED_NAME, "DELETE FROM character_declinedname WHERE guid = ?", CONNECTION_ASYNC);
// Guild handling // Guild handling
@@ -132,6 +132,7 @@ enum CharacterDatabaseStatements : uint32
CHAR_INS_ACCOUNT_INSTANCE_LOCK_TIMES, CHAR_INS_ACCOUNT_INSTANCE_LOCK_TIMES,
CHAR_SEL_MATCH_MAKER_RATING, CHAR_SEL_MATCH_MAKER_RATING,
CHAR_SEL_CHARACTER_COUNT, CHAR_SEL_CHARACTER_COUNT,
CHAR_UPD_NAME_BY_GUID,
CHAR_DEL_DECLINED_NAME, CHAR_DEL_DECLINED_NAME,
CHAR_INS_GUILD, CHAR_INS_GUILD,
+218 -330
View File
@@ -22,18 +22,19 @@ Comment: All character related commands
Category: commandscripts Category: commandscripts
EndScriptData */ EndScriptData */
#include "ScriptMgr.h"
#include "AccountMgr.h" #include "AccountMgr.h"
#include "Chat.h" #include "Chat.h"
#include "Implementation/CharacterDatabase.h" #include "DatabaseEnv.h"
#include "DBCStores.h"
#include "Log.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h" #include "ObjectMgr.h"
#include "Player.h" #include "Player.h"
#include "PlayerDump.h" #include "PlayerDump.h"
#include "ReputationMgr.h" #include "ReputationMgr.h"
#include "ScriptMgr.h" #include "World.h"
#include "WorldSession.h"
#if AC_COMPILER == AC_COMPILER_GNU
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
using namespace Acore::ChatCommands; using namespace Acore::ChatCommands;
@@ -272,20 +273,21 @@ public:
} }
} }
static bool HandleCharacterTitlesCommand(ChatHandler* handler, char const* args) static bool HandleCharacterTitlesCommand(ChatHandler* handler, Optional<PlayerIdentifier> player)
{ {
if (!*args) if (!player)
return false; player = PlayerIdentifier::FromTargetOrSelf(handler);
Player* target; if (!player || !player->IsConnected())
if (!handler->extractPlayerTarget((char*)args, &target)) {
handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false; return false;
}
if (!target) Player const* target = player->GetConnectedPlayer();
return false;
LocaleConstant loc = handler->GetSessionDbcLocale(); LocaleConstant loc = handler->GetSessionDbcLocale();
char const* targetName = target->GetName().c_str();
char const* knownStr = handler->GetAcoreString(LANG_KNOWN); char const* knownStr = handler->GetAcoreString(LANG_KNOWN);
// Search in CharTitles.dbc // Search in CharTitles.dbc
@@ -295,22 +297,24 @@ public:
if (titleInfo && target->HasTitle(titleInfo)) if (titleInfo && target->HasTitle(titleInfo))
{ {
std::string name = target->getGender() == GENDER_MALE ? titleInfo->nameMale[loc] : titleInfo->nameFemale[loc]; char const* name = target->getGender() == GENDER_MALE ? titleInfo->nameMale[loc] : titleInfo->nameFemale[loc];
if (name.empty()) if (!*name)
name = (target->getGender() == GENDER_MALE ? titleInfo->nameMale[sWorld->GetDefaultDbcLocale()] : titleInfo->nameFemale[sWorld->GetDefaultDbcLocale()]);
if (!*name)
continue; continue;
char const* activeStr = target && target->GetUInt32Value(PLAYER_CHOSEN_TITLE) == titleInfo->bit_index char const* activeStr = "";
? handler->GetAcoreString(LANG_ACTIVE) if (target->GetUInt32Value(PLAYER_CHOSEN_TITLE) == titleInfo->bit_index)
: ""; activeStr = handler->GetAcoreString(LANG_ACTIVE);
char titleNameStr[80]; std::string titleName = Acore::StringFormat(name, player->GetName().c_str());
snprintf(titleNameStr, 80, name.c_str(), targetName);
// send title in "id (idx:idx) - [namedlink locale]" format // send title in "id (idx:idx) - [namedlink locale]" format
if (handler->GetSession()) if (handler->GetSession())
handler->PSendSysMessage(LANG_TITLE_LIST_CHAT, id, titleInfo->bit_index, id, titleNameStr, localeNames[loc], knownStr, activeStr); handler->PSendSysMessage(LANG_TITLE_LIST_CHAT, id, titleInfo->bit_index, id, titleName.c_str(), localeNames[loc], knownStr, activeStr);
else else
handler->PSendSysMessage(LANG_TITLE_LIST_CONSOLE, id, titleInfo->bit_index, name.c_str(), localeNames[loc], knownStr, activeStr); handler->PSendSysMessage(LANG_TITLE_LIST_CONSOLE, id, titleInfo->bit_index, name, localeNames[loc], knownStr, activeStr);
} }
} }
@@ -318,51 +322,99 @@ public:
} }
//rename characters //rename characters
static bool HandleCharacterRenameCommand(ChatHandler* handler, char const* args) static bool HandleCharacterRenameCommand(ChatHandler* handler, Optional<PlayerIdentifier> player, Optional<std::string_view> newNameV)
{ {
char* nameStr = strtok((char*)args, " "); if (!player && newNameV)
char* reserveNameStr = strtok(nullptr, " ");
if (!reserveNameStr && nameStr && atoi(nameStr) == 1)
{
reserveNameStr = nameStr;
nameStr = nullptr;
}
bool reserveName = reserveNameStr != nullptr && atoi(reserveNameStr) == 1;
Player* target;
ObjectGuid targetGuid;
std::string targetName;
if (!handler->extractPlayerTarget(nameStr, &target, &targetGuid, &targetName))
return false; return false;
if (target) if (!player)
{ player = PlayerIdentifier::FromTarget(handler);
// check online security
if (handler->HasLowerSecurity(target))
return false;
handler->PSendSysMessage(LANG_RENAME_PLAYER, handler->GetNameLink(target).c_str()); if (!player)
target->SetAtLoginFlag(AT_LOGIN_RENAME); return false;
if (handler->HasLowerSecurity(nullptr, player->GetGUID()))
return false;
if (newNameV)
{
std::string newName{ *newNameV };
if (!normalizePlayerName(newName))
{
handler->SendSysMessage(LANG_BAD_VALUE);
handler->SetSentErrorMessage(true);
return false;
}
if (ObjectMgr::CheckPlayerName(newName, true) != CHAR_NAME_SUCCESS)
{
handler->SendSysMessage(LANG_BAD_VALUE);
handler->SetSentErrorMessage(true);
return false;
}
if (sObjectMgr->IsReservedName(newName))
{
handler->SendSysMessage(LANG_RESERVED_NAME);
handler->SetSentErrorMessage(true);
return false;
}
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHECK_NAME);
stmt->setString(0, newName);
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (result)
{
handler->PSendSysMessage(LANG_RENAME_PLAYER_ALREADY_EXISTS, newName.c_str());
handler->SetSentErrorMessage(true);
return false;
}
// Remove declined name from db
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_DECLINED_NAME);
stmt->setUInt32(0, player->GetGUID().GetCounter());
CharacterDatabase.Execute(stmt);
if (Player* target = player->GetConnectedPlayer())
{
target->SetName(newName);
if (WorldSession* session = target->GetSession())
session->KickPlayer("HandleCharacterRenameCommand GM Command renaming character");
}
else
{
stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_NAME_BY_GUID);
stmt->setString(0, newName);
stmt->setUInt32(1, player->GetGUID().GetCounter());
CharacterDatabase.Execute(stmt);
}
sWorld->UpdateGlobalNameData(player->GetGUID().GetCounter(), player->GetName().c_str(), newName);
sWorld->UpdateGlobalPlayerData(player->GetGUID().GetCounter(), PLAYER_UPDATE_DATA_NAME, newName);
handler->PSendSysMessage(LANG_RENAME_PLAYER_WITH_NEW_NAME, player->GetName().c_str(), newName.c_str());
} }
else else
{ {
// check offline security if (Player* target = player->GetConnectedPlayer())
if (handler->HasLowerSecurity(nullptr, targetGuid)) {
return false; handler->PSendSysMessage(LANG_RENAME_PLAYER, handler->GetNameLink(target).c_str());
target->SetAtLoginFlag(AT_LOGIN_RENAME);
}
else
{
// check offline security
if (handler->HasLowerSecurity(nullptr, player->GetGUID()))
return false;
std::string oldNameLink = handler->playerLink(targetName); handler->PSendSysMessage(LANG_RENAME_PLAYER_GUID, handler->playerLink(*player).c_str(), player->GetGUID().GetCounter());
handler->PSendSysMessage(LANG_RENAME_PLAYER_GUID, oldNameLink.c_str(), targetGuid.GetCounter());
auto* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_AT_LOGIN_FLAG); CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_AT_LOGIN_FLAG);
stmt->setUInt16(0, uint16(AT_LOGIN_RENAME)); stmt->setUInt16(0, uint16(AT_LOGIN_RENAME));
stmt->setUInt32(1, targetGuid.GetCounter()); stmt->setUInt32(1, player->GetGUID().GetCounter());
CharacterDatabase.Execute(stmt); CharacterDatabase.Execute(stmt);
} }
if (reserveName)
{
sObjectMgr->AddReservedPlayerName(targetName);
} }
return true; return true;
@@ -393,96 +445,90 @@ public:
} }
// customize characters // customize characters
static bool HandleCharacterCustomizeCommand(ChatHandler* handler, char const* args) static bool HandleCharacterCustomizeCommand(ChatHandler* handler, Optional<PlayerIdentifier> player)
{ {
Player* target; if (!player)
ObjectGuid targetGuid; player = PlayerIdentifier::FromTarget(handler);
std::string targetName; if (!player)
if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
return false; return false;
auto* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_AT_LOGIN_FLAG); if (Player* target = player->GetConnectedPlayer())
stmt->setUInt16(0, uint16(AT_LOGIN_CUSTOMIZE));
if (target)
{ {
handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER, handler->GetNameLink(target).c_str()); handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER, handler->GetNameLink(target).c_str());
target->SetAtLoginFlag(AT_LOGIN_CUSTOMIZE); target->SetAtLoginFlag(AT_LOGIN_CUSTOMIZE);
stmt->setUInt32(1, target->GetGUID().GetCounter());
} }
else else
{ {
std::string oldNameLink = handler->playerLink(targetName); handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER_GUID, handler->playerLink(*player).c_str(), player->GetGUID().GetCounter());
stmt->setUInt32(1, targetGuid.GetCounter()); CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_AT_LOGIN_FLAG);
handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER_GUID, oldNameLink.c_str(), targetGuid.GetCounter()); stmt->setUInt16(0, static_cast<uint16>(AT_LOGIN_CUSTOMIZE));
stmt->setUInt32(1, player->GetGUID().GetCounter());
CharacterDatabase.Execute(stmt);
} }
CharacterDatabase.Execute(stmt);
return true; return true;
} }
static bool HandleCharacterChangeFactionCommand(ChatHandler* handler, char const* args) static bool HandleCharacterChangeFactionCommand(ChatHandler* handler, Optional<PlayerIdentifier> player)
{ {
Player* target; if (!player)
ObjectGuid targetGuid; player = PlayerIdentifier::FromTarget(handler);
std::string targetName; if (!player)
if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
return false; return false;
auto* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_AT_LOGIN_FLAG); if (Player* target = player->GetConnectedPlayer())
stmt->setUInt16(0, uint16(AT_LOGIN_CHANGE_FACTION));
if (target)
{ {
handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER, handler->GetNameLink(target).c_str()); handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER, handler->GetNameLink(target).c_str());
target->SetAtLoginFlag(AT_LOGIN_CHANGE_FACTION); target->SetAtLoginFlag(AT_LOGIN_CHANGE_FACTION);
stmt->setUInt32(1, target->GetGUID().GetCounter());
} }
else else
{ {
std::string oldNameLink = handler->playerLink(targetName); handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER_GUID, handler->playerLink(*player).c_str(), player->GetGUID().GetCounter());
handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER_GUID, oldNameLink.c_str(), targetGuid.GetCounter()); CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_AT_LOGIN_FLAG);
stmt->setUInt32(1, targetGuid.GetCounter()); stmt->setUInt16(0, uint16(AT_LOGIN_CHANGE_FACTION));
stmt->setUInt32(1, player->GetGUID().GetCounter());
CharacterDatabase.Execute(stmt);
} }
CharacterDatabase.Execute(stmt);
return true; return true;
} }
static bool HandleCharacterChangeRaceCommand(ChatHandler* handler, char const* args) static bool HandleCharacterChangeRaceCommand(ChatHandler* handler, Optional<PlayerIdentifier> player)
{ {
Player* target; if (!player)
ObjectGuid targetGuid; player = PlayerIdentifier::FromTarget(handler);
std::string targetName; if (!player)
if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
return false; return false;
auto* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_AT_LOGIN_FLAG); if (Player* target = player->GetConnectedPlayer())
stmt->setUInt16(0, uint16(AT_LOGIN_CHANGE_RACE));
if (target)
{ {
// TODO : add text into database
handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER, handler->GetNameLink(target).c_str()); handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER, handler->GetNameLink(target).c_str());
target->SetAtLoginFlag(AT_LOGIN_CHANGE_RACE); target->SetAtLoginFlag(AT_LOGIN_CHANGE_RACE);
stmt->setUInt32(1, target->GetGUID().GetCounter());
} }
else else
{ {
std::string oldNameLink = handler->playerLink(targetName); handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER_GUID, handler->playerLink(*player).c_str(), player->GetGUID().GetCounter());
// TODO : add text into database CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_AT_LOGIN_FLAG);
handler->PSendSysMessage(LANG_CUSTOMIZE_PLAYER_GUID, oldNameLink.c_str(), targetGuid.GetCounter()); stmt->setUInt16(0, uint16(AT_LOGIN_CHANGE_RACE));
stmt->setUInt32(1, targetGuid.GetCounter()); stmt->setUInt32(1, player->GetGUID().GetCounter());
CharacterDatabase.Execute(stmt);
} }
CharacterDatabase.Execute(stmt);
return true; return true;
} }
static bool HandleCharacterReputationCommand(ChatHandler* handler, char const* args) static bool HandleCharacterReputationCommand(ChatHandler* handler, Optional<PlayerIdentifier> player)
{ {
Player* target; if (!player)
if (!handler->extractPlayerTarget((char*)args, &target)) player = PlayerIdentifier::FromTargetOrSelf(handler);
if (!player || !player->IsConnected())
{
handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false; return false;
}
Player const* target = player->GetConnectedPlayer();
LocaleConstant loc = handler->GetSessionDbcLocale(); LocaleConstant loc = handler->GetSessionDbcLocale();
FactionStateList const& targetFSL = target->GetReputationMgr().GetStateList(); FactionStateList const& targetFSL = target->GetReputationMgr().GetStateList();
@@ -531,16 +577,20 @@ public:
* @param args the search string which either contains a player GUID or a part fo the character-name * @param args the search string which either contains a player GUID or a part fo the character-name
*/ */
static bool HandleCharacterDeletedListCommand(ChatHandler* handler, char const* args) static bool HandleCharacterDeletedListCommand(ChatHandler* handler, Optional<std::string_view> needleStr)
{ {
std::string needle;
if (needleStr)
needle.assign(*needleStr);
DeletedInfoList foundList; DeletedInfoList foundList;
if (!GetDeletedCharacterInfoList(foundList, args)) if (!GetDeletedCharacterInfoList(foundList, needle))
return false; return false;
// if no characters have been found, output a warning // if no characters have been found, output a warning
if (foundList.empty()) if (foundList.empty())
{ {
handler->SendSysMessage(LANG_CHARACTER_DELETED_LIST_EMPTY); handler->SendSysMessage(LANG_CHARACTER_DELETED_LIST_EMPTY);
handler->SetSentErrorMessage(true);
return false; return false;
} }
@@ -560,59 +610,52 @@ public:
* *
* @param args the search string which either contains a player GUID or a part of the character-name * @param args the search string which either contains a player GUID or a part of the character-name
*/ */
static bool HandleCharacterDeletedRestoreCommand(ChatHandler* handler, char const* args) static bool HandleCharacterDeletedRestoreCommand(ChatHandler* handler, std::string needle, Optional<std::string_view> newCharName, Optional<AccountIdentifier> newAccount)
{ {
// It is required to submit at least one argument
if (!*args)
return false;
std::string searchString;
std::string newCharName;
uint32 newAccount = 0;
// GCC by some strange reason fail build code without temporary variable
std::istringstream params(args);
params >> searchString >> newCharName >> newAccount;
DeletedInfoList foundList; DeletedInfoList foundList;
if (!GetDeletedCharacterInfoList(foundList, searchString)) if (!GetDeletedCharacterInfoList(foundList, needle))
return false; return false;
if (foundList.empty()) if (foundList.empty())
{ {
handler->SendSysMessage(LANG_CHARACTER_DELETED_LIST_EMPTY); handler->SendSysMessage(LANG_CHARACTER_DELETED_LIST_EMPTY);
handler->SetSentErrorMessage(true);
return false; return false;
} }
handler->SendSysMessage(LANG_CHARACTER_DELETED_RESTORE); handler->SendSysMessage(LANG_CHARACTER_DELETED_RESTORE);
HandleCharacterDeletedListHelper(foundList, handler); HandleCharacterDeletedListHelper(foundList, handler);
if (newCharName.empty()) if (!newCharName)
{ {
// Drop nonexisting account cases // Drop nonexisting account cases
for (DeletedInfoList::iterator itr = foundList.begin(); itr != foundList.end(); ++itr) for (DeletedInfoList::iterator itr = foundList.begin(); itr != foundList.end(); ++itr)
HandleCharacterDeletedRestoreHelper(*itr, handler); HandleCharacterDeletedRestoreHelper(*itr, handler);
return true;
} }
else if (foundList.size() == 1 && normalizePlayerName(newCharName))
if (foundList.size() == 1)
{ {
std::string newName{ *newCharName };
DeletedInfo delInfo = foundList.front(); DeletedInfo delInfo = foundList.front();
// update name // update name
delInfo.name = newCharName; delInfo.name = newName;
// if new account provided update deleted info // if new account provided update deleted info
if (newAccount && newAccount != delInfo.accountId) if (newAccount)
{ {
delInfo.accountId = newAccount; delInfo.accountId = newAccount->GetID();
AccountMgr::GetName(newAccount, delInfo.accountName); delInfo.accountName = newAccount->GetName();
} }
HandleCharacterDeletedRestoreHelper(delInfo, handler); HandleCharacterDeletedRestoreHelper(delInfo, handler);
return true;
} }
else
handler->SendSysMessage(LANG_CHARACTER_DELETED_ERR_RENAME);
return true; handler->SendSysMessage(LANG_CHARACTER_DELETED_ERR_RENAME);
handler->SetSentErrorMessage(true);
return false;
} }
/** /**
@@ -625,19 +668,16 @@ public:
* *
* @param args the search string which either contains a player GUID or a part fo the character-name * @param args the search string which either contains a player GUID or a part fo the character-name
*/ */
static bool HandleCharacterDeletedDeleteCommand(ChatHandler* handler, char const* args) static bool HandleCharacterDeletedDeleteCommand(ChatHandler* handler, std::string needle)
{ {
// It is required to submit at least one argument
if (!*args)
return false;
DeletedInfoList foundList; DeletedInfoList foundList;
if (!GetDeletedCharacterInfoList(foundList, args)) if (!GetDeletedCharacterInfoList(foundList, needle))
return false; return false;
if (foundList.empty()) if (foundList.empty())
{ {
handler->SendSysMessage(LANG_CHARACTER_DELETED_LIST_EMPTY); handler->SendSysMessage(LANG_CHARACTER_DELETED_LIST_EMPTY);
handler->SetSentErrorMessage(true);
return false; return false;
} }
@@ -662,25 +702,16 @@ public:
* *
* @param args the search string which either contains a player GUID or a part of the character-name * @param args the search string which either contains a player GUID or a part of the character-name
*/ */
static bool HandleCharacterDeletedPurgeCommand(ChatHandler* /*handler*/, char const* args) static bool HandleCharacterDeletedPurgeCommand(ChatHandler* /*handler*/, Optional<uint16> days)
{ {
int32 keepDays = sWorld->getIntConfig(CONFIG_CHARDELETE_KEEP_DAYS); int32 keepDays = static_cast<int32>(sWorld->getIntConfig(CONFIG_CHARDELETE_KEEP_DAYS));
char* daysStr = strtok((char*)args, " "); if (days)
if (daysStr) keepDays = static_cast<int32>(*days);
{ else if (keepDays <= 0) // config option value 0 -> disabled and can't be used
if (!isNumeric(daysStr))
return false;
keepDays = atoi(daysStr);
if (keepDays < 0)
return false;
}
// config option value 0 -> disabled and can't be used
else if (keepDays <= 0)
return false; return false;
Player::DeleteOldCharacters(uint32(keepDays)); Player::DeleteOldCharacters(static_cast<uint32>(keepDays));
return true; return true;
} }
@@ -692,46 +723,22 @@ public:
* *
* @param args the search string which either contains a player GUID or a part of the character-name * @param args the search string which either contains a player GUID or a part of the character-name
*/ */
static bool HandleCharacterEraseCommand(ChatHandler* handler, char const* args) static bool HandleCharacterEraseCommand(ChatHandler* handler, PlayerIdentifier player)
{ {
if (!*args)
return false;
char* characterName_str = strtok((char*)args, " ");
if (!characterName_str)
return false;
std::string characterName = characterName_str;
if (!normalizePlayerName(characterName))
return false;
ObjectGuid characterGuid;
uint32 accountId; uint32 accountId;
if (Player* target = player.GetConnectedPlayer())
Player* player = ObjectAccessor::FindPlayerByName(characterName);
if (player)
{ {
characterGuid = player->GetGUID(); accountId = target->GetSession()->GetAccountId();
accountId = player->GetSession()->GetAccountId(); target->GetSession()->KickPlayer("HandleCharacterEraseCommand GM Command deleting character");
player->GetSession()->KickPlayer("HandleCharacterEraseCommand");
} }
else else
{ accountId = sObjectMgr->GetPlayerAccountIdByGUID(player.GetGUID().GetCounter());
characterGuid = sObjectMgr->GetPlayerGUIDByName(characterName);
if (!characterGuid)
{
handler->PSendSysMessage(LANG_NO_PLAYER, characterName.c_str());
handler->SetSentErrorMessage(true);
return false;
}
accountId = sObjectMgr->GetPlayerAccountIdByGUID(characterGuid.GetCounter());
}
std::string accountName; std::string accountName;
AccountMgr::GetName(accountId, accountName); AccountMgr::GetName(accountId, accountName);
Player::DeleteFromDB(characterGuid.GetCounter(), accountId, true, true); Player::DeleteFromDB(player.GetGUID().GetCounter(), accountId, true, true);
handler->PSendSysMessage(LANG_CHARACTER_DELETED, characterName.c_str(), characterGuid.GetCounter(), accountName.c_str(), accountId); handler->PSendSysMessage(LANG_CHARACTER_DELETED, player.GetName().c_str(), player.GetGUID().GetCounter(), accountName.c_str(), accountId);
return true; return true;
} }
@@ -761,53 +768,11 @@ public:
return true; return true;
} }
static bool HandlePDumpLoadCommand(ChatHandler* handler, char const* args) static bool ValidatePDumpTarget(ChatHandler* handler, std::string& name, Optional<std::string_view> characterName, Optional<ObjectGuid::LowType> characterGUID)
{ {
if (!*args) if (characterName)
return false;
char* fileStr = strtok((char*)args, " ");
if (!fileStr)
return false;
char* accountStr = strtok(nullptr, " ");
if (!accountStr)
return false;
std::string accountName = accountStr;
if (!Utf8ToUpperOnlyLatin(accountName))
{ {
handler->PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, accountName.c_str()); name.assign(*characterName);
handler->SetSentErrorMessage(true);
return false;
}
uint32 accountId = AccountMgr::GetId(accountName);
if (!accountId)
{
accountId = atoi(accountStr); // use original string
if (!accountId)
{
handler->PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, accountName.c_str());
handler->SetSentErrorMessage(true);
return false;
}
}
if (!AccountMgr::GetName(accountId, accountName))
{
handler->PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, accountName.c_str());
handler->SetSentErrorMessage(true);
return false;
}
char* guidStr = nullptr;
char* nameStr = strtok(nullptr, " ");
std::string name;
if (nameStr)
{
name = nameStr;
// normalize the name if specified and check if it exists // normalize the name if specified and check if it exists
if (!normalizePlayerName(name)) if (!normalizePlayerName(name))
{ {
@@ -822,45 +787,42 @@ public:
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
} }
guidStr = strtok(nullptr, " ");
} }
ObjectGuid::LowType guid = 0; if (characterGUID)
if (guidStr)
{ {
guid = uint32(atoi(guidStr)); if (sObjectMgr->GetPlayerAccountIdByGUID(*characterGUID))
if (!guid)
{ {
handler->PSendSysMessage(LANG_INVALID_CHARACTER_GUID); handler->PSendSysMessage(LANG_CHARACTER_GUID_IN_USE, *characterGUID);
handler->SetSentErrorMessage(true);
return false;
}
if (sObjectMgr->GetPlayerAccountIdByGUID(guid))
{
handler->PSendSysMessage(LANG_CHARACTER_GUID_IN_USE, guid);
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
} }
} }
switch (PlayerDumpReader().LoadDump(fileStr, accountId, name, guid)) return true;
}
static bool HandlePDumpLoadCommand(ChatHandler* handler, std::string fileName, AccountIdentifier account, Optional<std::string_view> characterName, Optional<ObjectGuid::LowType> characterGUID)
{
std::string name;
if (!ValidatePDumpTarget(handler, name, characterName, characterGUID))
return false;
switch (PlayerDumpReader().LoadDump(fileName, account, name, characterGUID.value_or(0)))
{ {
case DUMP_SUCCESS: case DUMP_SUCCESS:
handler->PSendSysMessage(LANG_COMMAND_IMPORT_SUCCESS); handler->PSendSysMessage(LANG_COMMAND_IMPORT_SUCCESS);
break; break;
case DUMP_FILE_OPEN_ERROR: case DUMP_FILE_OPEN_ERROR:
handler->PSendSysMessage(LANG_FILE_OPEN_FAIL, fileStr); handler->PSendSysMessage(LANG_FILE_OPEN_FAIL, fileName.c_str());
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
case DUMP_FILE_BROKEN: case DUMP_FILE_BROKEN:
handler->PSendSysMessage(LANG_DUMP_BROKEN, fileStr); handler->PSendSysMessage(LANG_DUMP_BROKEN, fileName.c_str());
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
case DUMP_TOO_MANY_CHARS: case DUMP_TOO_MANY_CHARS:
handler->PSendSysMessage(LANG_ACCOUNT_CHARACTER_LIST_FULL, accountName.c_str(), accountId); handler->PSendSysMessage(LANG_ACCOUNT_CHARACTER_LIST_FULL, account.GetName().c_str(), account.GetID());
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
default: default:
@@ -872,79 +834,15 @@ public:
return true; return true;
} }
static bool HandlePDumpWriteCommand(ChatHandler* handler, char const* args) static bool HandlePDumpWriteCommand(ChatHandler* handler, std::string fileName, PlayerIdentifier player)
{ {
if (!*args) switch (PlayerDumpWriter().WriteDump(fileName, player.GetGUID().GetCounter()))
return false;
char* fileStr = strtok((char*)args, " ");
char* playerStr = strtok(nullptr, " ");
if (!fileStr && !playerStr)
{
QueryResult result = CharacterDatabase.PQuery("SELECT guid FROM characters");
if (!result)
return true;
do
{
uint64 _guid = result->Fetch()[0].GetUInt64();
char buff[20];
sprintf(buff, "%u", (uint32)_guid);
switch(PlayerDumpWriter().WriteDump(buff, uint32(_guid)))
{
case DUMP_SUCCESS:
handler->PSendSysMessage(LANG_COMMAND_EXPORT_SUCCESS);
break;
case DUMP_FILE_OPEN_ERROR:
handler->PSendSysMessage(LANG_FILE_OPEN_FAIL, buff);
handler->SetSentErrorMessage(true);
return false;
case DUMP_CHARACTER_DELETED:
handler->PSendSysMessage(LANG_COMMAND_EXPORT_DELETED_CHAR);
handler->SetSentErrorMessage(true);
return false;
default:
handler->PSendSysMessage(LANG_COMMAND_EXPORT_FAILED);
handler->SetSentErrorMessage(true);
return false;
}
} while(result->NextRow());
}
if (!fileStr || !playerStr)
return false;
ObjectGuid guid;
// character name can't start from number
if (isNumeric(playerStr))
guid = ObjectGuid::Create<HighGuid::Player>(atoi(playerStr));
else
{
std::string name = handler->extractPlayerNameFromLink(playerStr);
if (name.empty())
{
handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false;
}
guid = sObjectMgr->GetPlayerGUIDByName(name);
}
if (!sObjectMgr->GetPlayerAccountIdByGUID(guid.GetCounter()))
{
handler->PSendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false;
}
switch (PlayerDumpWriter().WriteDump(fileStr, guid.GetCounter()))
{ {
case DUMP_SUCCESS: case DUMP_SUCCESS:
handler->PSendSysMessage(LANG_COMMAND_EXPORT_SUCCESS); handler->PSendSysMessage(LANG_COMMAND_EXPORT_SUCCESS);
break; break;
case DUMP_FILE_OPEN_ERROR: case DUMP_FILE_OPEN_ERROR:
handler->PSendSysMessage(LANG_FILE_OPEN_FAIL, fileStr); handler->PSendSysMessage(LANG_FILE_OPEN_FAIL, fileName.c_str());
handler->SetSentErrorMessage(true); handler->SetSentErrorMessage(true);
return false; return false;
case DUMP_CHARACTER_DELETED: case DUMP_CHARACTER_DELETED:
@@ -960,30 +858,20 @@ public:
return true; return true;
} }
static bool HandleCharacterCheckBankCommand(ChatHandler* handler, char const* /*args*/) static bool HandleCharacterCheckBankCommand(ChatHandler* handler)
{ {
handler->GetSession()->SendShowBank(handler->GetSession()->GetPlayer()->GetGUID()); handler->GetSession()->SendShowBank(handler->GetSession()->GetPlayer()->GetGUID());
return true; return true;
} }
static bool HandleCharacterCheckBagCommand(ChatHandler* handler, char const* args) static bool HandleCharacterCheckBagCommand(ChatHandler* handler, uint8 BagSlot)
{ {
if (!*args) Player* target = handler->getSelectedPlayerOrSelf();
return false;
char* Slot = strtok((char*)args, " "); if (!target)
if (!Slot)
return false;
Player* player = handler->getSelectedPlayerOrSelf();
if (!player)
return false; return false;
uint8 Counter = 0; uint8 Counter = 0;
uint8 BagSlot = atoi(Slot);
switch (BagSlot) switch (BagSlot)
{ {
case 2: case 2:
@@ -1009,7 +897,7 @@ public:
{ {
for (uint32 i = 23; i < 39; i++) for (uint32 i = 23; i < 39; i++)
{ {
if (Item* item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, i)) if (Item* item = target->GetItemByPos(INVENTORY_SLOT_BAG_0, i))
{ {
Counter++; Counter++;
std::ostringstream ItemString; std::ostringstream ItemString;
@@ -1021,11 +909,11 @@ public:
} }
else else
{ {
if (Bag* bag = player->GetBagByPos(BagSlot)) if (Bag* bag = target->GetBagByPos(BagSlot))
{ {
for (uint32 i = 0; i < bag->GetBagSize(); i++) for (uint32 i = 0; i < bag->GetBagSize(); i++)
{ {
if (Item* item = player->GetItemByPos(BagSlot, i)) if (Item* item = target->GetItemByPos(BagSlot, i))
{ {
Counter++; Counter++;
std::ostringstream ItemString; std::ostringstream ItemString;
@@ -1041,7 +929,7 @@ public:
return true; return true;
} }
static bool HandleCharacterCheckProfessionCommand(ChatHandler* handler, char const*) static bool HandleCharacterCheckProfessionCommand(ChatHandler* handler)
{ {
Player* player = handler->getSelectedPlayerOrSelf(); Player* player = handler->getSelectedPlayerOrSelf();