[Bug Fix] Fix Bot Creation Issue (#4235)

# Notes
- Creating bots was failing because were checking for `false` on `Database::CheckUsedName()` in `BotDatabase::QueryNameAvailability`.
- `Database::CheckUsedName()` is now `Database::IsNameUsed()` and checks for both bots and character name usages.
- We were checking for `false` which was always happening when there were no entries for the supplied name, meaning we were never allowed to create a bot.
This commit is contained in:
Alex King 2024-04-02 02:12:08 -04:00 committed by GitHub
parent d7a8fb8691
commit dc48c45421
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 27 additions and 34 deletions

View File

@ -74,6 +74,7 @@
#include "repositories/zone_repository.h" #include "repositories/zone_repository.h"
#include "zone_store.h" #include "zone_store.h"
#include "repositories/merchantlist_temp_repository.h" #include "repositories/merchantlist_temp_repository.h"
#include "repositories/bot_data_repository.h"
extern Client client; extern Client client;
@ -915,15 +916,31 @@ bool Database::UpdateName(const std::string& old_name, const std::string& new_na
return CharacterDataRepository::UpdateOne(*this, e); return CharacterDataRepository::UpdateOne(*this, e);
} }
bool Database::CheckUsedName(const std::string& name) bool Database::IsNameUsed(const std::string& name)
{ {
return !CharacterDataRepository::GetWhere( if (RuleB(Bots, Enabled)) {
const auto& bot_data = BotDataRepository::GetWhere(
*this, *this,
fmt::format( fmt::format(
"`name` = '{}'", "`name` = '{}'",
Strings::Escape(name) Strings::Escape(name)
) )
).empty(); );
if (!bot_data.empty()) {
return true;
}
}
const auto& character_data = CharacterDataRepository::GetWhere(
*this,
fmt::format(
"`name` = '{}'",
Strings::Escape(name)
)
);
return !character_data.empty();
} }
uint32 Database::GetServerType() uint32 Database::GetServerType()

View File

@ -115,7 +115,7 @@ public:
bool CheckBannedIPs(const std::string& login_ip); //Check incoming connection against banned IP table. bool CheckBannedIPs(const std::string& login_ip); //Check incoming connection against banned IP table.
bool CheckGMIPs(const std::string& login_ip, uint32 account_id); bool CheckGMIPs(const std::string& login_ip, uint32 account_id);
bool CheckNameFilter(const std::string& name, bool surname = false); bool CheckNameFilter(const std::string& name, bool surname = false);
bool CheckUsedName(const std::string& name); bool IsNameUsed(const std::string& name);
uint32 GetAccountIDByChar(const std::string& name, uint32* character_id = 0); uint32 GetAccountIDByChar(const std::string& name, uint32* character_id = 0);
uint32 GetAccountIDByChar(uint32 character_id); uint32 GetAccountIDByChar(uint32 character_id);

View File

@ -184,35 +184,11 @@ bool BotDatabase::QueryNameAvailablity(const std::string& bot_name, bool& availa
if ( if (
bot_name.empty() || bot_name.empty() ||
bot_name.size() > 60 || bot_name.size() > 60 ||
!database.CheckUsedName(bot_name) database.IsNameUsed(bot_name)
) { ) {
return false; return false;
} }
const auto& bot_data = BotDataRepository::GetWhere(
database,
fmt::format(
"`name` LIKE '{}' LIMIT 1",
bot_name
)
);
if (!bot_data.empty()) {
return false;
}
const auto& character_data = CharacterDataRepository::GetWhere(
database,
fmt::format(
"`name` LIKE '{}' LIMIT 1",
bot_name
)
);
if (!character_data.empty()) {
return false;
}
available_flag = true; available_flag = true;
return true; return true;

View File

@ -2228,8 +2228,8 @@ void Client::ChangeLastName(std::string last_name) {
bool Client::ChangeFirstName(const char* in_firstname, const char* gmname) bool Client::ChangeFirstName(const char* in_firstname, const char* gmname)
{ {
// check duplicate name // check duplicate name
bool usedname = database.CheckUsedName((const char*) in_firstname); bool used_name = database.IsNameUsed((const char*) in_firstname);
if (!usedname) { if (used_name) {
return false; return false;
} }

View File

@ -6875,7 +6875,7 @@ void Client::Handle_OP_GMNameChange(const EQApplicationPacket *app)
Client *c = entity_list.GetClientByName(gmn->oldname); Client *c = entity_list.GetClientByName(gmn->oldname);
LogInfo("GM([{}]) changeing players name. Old:[{}] New:[{}]", GetName(), gmn->oldname, gmn->newname); LogInfo("GM([{}]) changeing players name. Old:[{}] New:[{}]", GetName(), gmn->oldname, gmn->newname);
const bool used_name = database.CheckUsedName(gmn->newname); const bool used_name = database.IsNameUsed(gmn->newname);
if (!c) { if (!c) {
Message(Chat::Red, fmt::format("{} not found for name change. Operation failed!", gmn->oldname).c_str()); Message(Chat::Red, fmt::format("{} not found for name change. Operation failed!", gmn->oldname).c_str());
return; return;
@ -6886,7 +6886,7 @@ void Client::Handle_OP_GMNameChange(const EQApplicationPacket *app)
return; return;
} }
if (!used_name) { if (used_name) {
Message(Chat::Red, fmt::format("{} is already in use. Operation failed!", gmn->newname).c_str()); Message(Chat::Red, fmt::format("{} is already in use. Operation failed!", gmn->newname).c_str());
return; return;
} }

View File

@ -4203,7 +4203,7 @@ const char* Merc::GetRandomName(){
//name must begin with an upper-case letter. //name must begin with an upper-case letter.
valid = false; valid = false;
} }
else if (database.CheckUsedName(rndname)) { else if (!database.IsNameUsed(rndname)) {
valid = true; valid = true;
} }
else { else {