Skip to content

Commit fa2988e

Browse files
committed
seperate bot add and bot addacount so that characters with same name as an account doesn't add all toons from that account but adds the character instead if so desired
1 parent 1ffdf69 commit fa2988e

File tree

1 file changed

+55
-180
lines changed

1 file changed

+55
-180
lines changed

src/PlayerbotMgr.cpp

+55-180
Original file line numberDiff line numberDiff line change
@@ -638,11 +638,22 @@ std::string const PlayerbotHolder::ProcessBotCommand(std::string const cmd, Obje
638638
bool isRandomAccount = sPlayerbotAIConfig->IsInRandomAccountList(botAccount);
639639
bool isMasterAccount = (masterAccountId == botAccount);
640640

641-
if (cmd == "add" || cmd == "login")
641+
if (cmd == "add" || cmd == "addaccount" || cmd == "login")
642642
{
643643
if (ObjectAccessor::FindPlayer(guid))
644644
return "player already logged in";
645645

646+
// For addaccount command, verify it's an account name
647+
if (cmd == "addaccount")
648+
{
649+
uint32 accountId = sCharacterCache->GetCharacterAccountIdByGuid(guid);
650+
if (!accountId)
651+
return "character not found";
652+
653+
if (!sPlayerbotAIConfig->allowAccountBots && accountId != masterAccountId)
654+
return "you can only add bots from your own account";
655+
}
656+
646657
AddPlayerBot(guid, masterAccountId);
647658
return "ok";
648659
}
@@ -816,36 +827,19 @@ std::vector<std::string> PlayerbotHolder::HandlePlayerbotCommand(char const* arg
816827

817828
if (!*args)
818829
{
819-
messages.push_back("usage: bot add character/account NAME or bot remove character/account NAME");
820-
messages.push_back("usage: bot addclass CLASSNAME");
821-
messages.push_back("usage: list/reload/tweak/self");
830+
messages.push_back("usage: list/reload/tweak/self or add/addaccount/init/remove PLAYERNAME\n");
831+
messages.push_back("usage: addclass CLASSNAME");
822832
return messages;
823833
}
824834

825835
char* cmd = strtok((char*)args, " ");
836+
char* charname = strtok(nullptr, " ");
826837
if (!cmd)
827838
{
828-
messages.push_back("usage: bot add character/account NAME or bot remove character/account NAME");
829-
messages.push_back("usage: bot addclass CLASSNAME");
830-
messages.push_back("usage: list/reload/tweak/self");
839+
messages.push_back("usage: list/reload/tweak/self or add/init/remove PLAYERNAME or addclass CLASSNAME");
831840
return messages;
832841
}
833842

834-
// Handle the "bot" command prefix if it exists
835-
bool hasCommandPrefix = false;
836-
if (!strcmp(cmd, "bot"))
837-
{
838-
hasCommandPrefix = true;
839-
cmd = strtok(nullptr, " ");
840-
if (!cmd)
841-
{
842-
messages.push_back("usage: bot add character/account NAME or bot remove character/account NAME");
843-
messages.push_back("usage: bot addclass CLASSNAME");
844-
messages.push_back("usage: list/reload/tweak/self");
845-
return messages;
846-
}
847-
}
848-
849843
if (!strcmp(cmd, "initself"))
850844
{
851845
if (master->GetSession()->GetSecurity() >= SEC_GAMEMASTER)
@@ -1006,10 +1000,8 @@ std::vector<std::string> PlayerbotHolder::HandlePlayerbotCommand(char const* arg
10061000
return messages;
10071001
}
10081002

1009-
// Handle the addclass command to add random bots of specific class
10101003
if (!strcmp(cmd, "addclass"))
10111004
{
1012-
char* charname = strtok(nullptr, " ");
10131005
if (sPlayerbotAIConfig->addClassCommand == 0 && master->GetSession()->GetSecurity() < SEC_GAMEMASTER)
10141006
{
10151007
messages.push_back("You do not have permission to create bot by addclass command");
@@ -1086,151 +1078,20 @@ std::vector<std::string> PlayerbotHolder::HandlePlayerbotCommand(char const* arg
10861078
return messages;
10871079
}
10881080

1089-
// Handle new command format: "add character", "add account", "remove character", "remove account"
1090-
if (!strcmp(cmd, "add") || !strcmp(cmd, "remove"))
1091-
{
1092-
bool isAdd = !strcmp(cmd, "add");
1093-
std::string cmdStr = isAdd ? "add" : "remove";
1094-
1095-
// Get the type (character or account) or wildcard
1096-
char* typeArg = strtok(nullptr, " ");
1097-
if (!typeArg)
1098-
{
1099-
messages.push_back("usage: bot " + cmdStr + " character/account NAME or bot " + cmdStr + " *");
1100-
return messages;
1101-
}
1102-
1103-
// Handle wildcard removal
1104-
if (!strcmp(typeArg, "*") && !isAdd)
1105-
{
1106-
std::unordered_set<std::string> bots;
1107-
for (PlayerBotMap::const_iterator it = GetPlayerBotsBegin(); it != GetPlayerBotsEnd(); ++it)
1108-
{
1109-
if (Player* bot = it->second)
1110-
if (bot->IsInWorld())
1111-
bots.insert(bot->GetName());
1112-
}
1113-
1114-
for (const auto& botName : bots)
1115-
{
1116-
ObjectGuid member = sCharacterCache->GetCharacterGuidByName(botName);
1117-
if (member && master)
1118-
{
1119-
std::string result = ProcessBotCommand(cmdStr, member, master->GetGUID(),
1120-
master->GetSession()->GetSecurity() >= SEC_GAMEMASTER,
1121-
master->GetSession()->GetAccountId(), master->GetGuildId());
1122-
messages.push_back(cmdStr + ": " + botName + " - " + result);
1123-
}
1124-
}
1125-
return messages;
1126-
}
1127-
1128-
std::string typeStr = typeArg;
1129-
bool isAccount = false;
1130-
1131-
if (typeStr == "character" || typeStr == "char")
1132-
{
1133-
isAccount = false;
1134-
}
1135-
else if (typeStr == "account" || typeStr == "acc")
1136-
{
1137-
isAccount = true;
1138-
}
1139-
else
1140-
{
1141-
messages.push_back("Invalid type. Use 'character' or 'account' or '*'");
1142-
return messages;
1143-
}
1144-
1145-
// Get the name
1146-
char* nameArg = strtok(nullptr, " ");
1147-
if (!nameArg)
1148-
{
1149-
messages.push_back("usage: bot " + cmdStr + " " + typeStr + " NAME");
1150-
return messages;
1151-
}
1152-
1153-
std::string nameStr = nameArg;
1154-
std::unordered_set<std::string> bots;
1155-
1156-
if (isAccount)
1157-
{
1158-
// Handle account level operation
1159-
uint32 accountId = GetAccountId(nameStr);
1160-
if (!accountId)
1161-
{
1162-
messages.push_back("Account '" + nameStr + "' not found.");
1163-
return messages;
1164-
}
1165-
1166-
QueryResult results = CharacterDatabase.Query("SELECT name FROM characters WHERE account = {}", accountId);
1167-
if (results)
1168-
{
1169-
do
1170-
{
1171-
Field* fields = results->Fetch();
1172-
std::string const charName = fields[0].Get<std::string>();
1173-
bots.insert(charName);
1174-
} while (results->NextRow());
1175-
}
1176-
else
1177-
{
1178-
messages.push_back("No characters found for account '" + nameStr + "'.");
1179-
return messages;
1180-
}
1181-
}
1182-
else
1183-
{
1184-
// Handle character level operation
1185-
bots.insert(nameStr);
1186-
}
1187-
1188-
for (auto i = bots.begin(); i != bots.end(); ++i)
1189-
{
1190-
std::string const bot = *i;
1191-
1192-
std::ostringstream out;
1193-
out << cmdStr << ": " << bot << " - ";
1194-
1195-
ObjectGuid member = sCharacterCache->GetCharacterGuidByName(bot);
1196-
if (!member)
1197-
{
1198-
out << "character not found";
1199-
}
1200-
else if (master && member != master->GetGUID())
1201-
{
1202-
out << ProcessBotCommand(cmdStr, member, master->GetGUID(),
1203-
master->GetSession()->GetSecurity() >= SEC_GAMEMASTER,
1204-
master->GetSession()->GetAccountId(), master->GetGuildId());
1205-
}
1206-
else if (!master)
1207-
{
1208-
out << ProcessBotCommand(cmdStr, member, ObjectGuid::Empty, true, -1, -1);
1209-
}
1210-
1211-
messages.push_back(out.str());
1212-
}
1213-
1214-
return messages;
1215-
}
1216-
1217-
// Handle legacy command format for backward compatibility
12181081
std::string charnameStr;
1219-
char* charname = strtok(nullptr, " ");
12201082

12211083
if (!charname)
12221084
{
12231085
std::string name;
12241086
bool isPlayer = sCharacterCache->GetCharacterNameByGuid(master->GetTarget(), name);
1087+
// Player* tPlayer = ObjectAccessor::FindConnectedPlayer(master->GetTarget());
12251088
if (isPlayer)
12261089
{
12271090
charnameStr = name;
12281091
}
12291092
else
12301093
{
1231-
messages.push_back("usage: bot add character/account NAME or bot remove character/account NAME");
1232-
messages.push_back("usage: bot addclass CLASSNAME");
1233-
messages.push_back("usage: list/reload/tweak/self");
1094+
messages.push_back("usage: list/reload/tweak/self or add/init/remove PLAYERNAME");
12341095
return messages;
12351096
}
12361097
}
@@ -1280,34 +1141,48 @@ std::vector<std::string> PlayerbotHolder::HandlePlayerbotCommand(char const* arg
12801141
{
12811142
std::string const s = *i;
12821143

1283-
// If * is specified, add all bots to the set
1284-
if (s == "*")
1144+
if (!strcmp(cmd, "addaccount"))
12851145
{
1286-
for (PlayerBotMap::const_iterator it = GetPlayerBotsBegin(); it != GetPlayerBotsEnd(); ++it)
1146+
// When using addaccount, first try to get account ID directly
1147+
uint32 accountId = GetAccountId(s);
1148+
if (!accountId)
12871149
{
1288-
if (Player* bot = it->second)
1289-
if (bot->IsInWorld())
1290-
bots.insert(bot->GetName());
1150+
// If not found, try to get account ID from character name
1151+
ObjectGuid charGuid = sCharacterCache->GetCharacterGuidByName(s);
1152+
if (!charGuid)
1153+
{
1154+
messages.push_back("Neither account nor character '" + s + "' found");
1155+
continue;
1156+
}
1157+
accountId = sCharacterCache->GetCharacterAccountIdByGuid(charGuid);
1158+
if (!accountId)
1159+
{
1160+
messages.push_back("Could not find account for character '" + s + "'");
1161+
continue;
1162+
}
12911163
}
1292-
continue;
1293-
}
12941164

1295-
uint32 accountId = GetAccountId(s);
1296-
if (!accountId)
1297-
{
1298-
bots.insert(s);
1299-
continue;
1165+
QueryResult results = CharacterDatabase.Query("SELECT name FROM characters WHERE account = {}", accountId);
1166+
if (results)
1167+
{
1168+
do
1169+
{
1170+
Field* fields = results->Fetch();
1171+
std::string const charName = fields[0].Get<std::string>();
1172+
bots.insert(charName);
1173+
} while (results->NextRow());
1174+
}
13001175
}
1301-
1302-
QueryResult results = CharacterDatabase.Query("SELECT name FROM characters WHERE account = {}", accountId);
1303-
if (results)
1176+
else
13041177
{
1305-
do
1178+
// For regular add command, only add the specific character
1179+
ObjectGuid charGuid = sCharacterCache->GetCharacterGuidByName(s);
1180+
if (!charGuid)
13061181
{
1307-
Field* fields = results->Fetch();
1308-
std::string const charName = fields[0].Get<std::string>();
1309-
bots.insert(charName);
1310-
} while (results->NextRow());
1182+
messages.push_back("Character '" + s + "' not found");
1183+
continue;
1184+
}
1185+
bots.insert(s);
13111186
}
13121187
}
13131188

@@ -1326,8 +1201,8 @@ std::vector<std::string> PlayerbotHolder::HandlePlayerbotCommand(char const* arg
13261201
else if (master && member != master->GetGUID())
13271202
{
13281203
out << ProcessBotCommand(cmdStr, member, master->GetGUID(),
1329-
master->GetSession()->GetSecurity() >= SEC_GAMEMASTER,
1330-
master->GetSession()->GetAccountId(), master->GetGuildId());
1204+
master->GetSession()->GetSecurity() >= SEC_GAMEMASTER,
1205+
master->GetSession()->GetAccountId(), master->GetGuildId());
13311206
}
13321207
else if (!master)
13331208
{

0 commit comments

Comments
 (0)