EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Development::Bots (https://www.eqemulator.org/forums/forumdisplay.php?f=676)
-   -   Fix for #bot create when same named PC exists (https://www.eqemulator.org/forums/showthread.php?t=36506)

zippzipp 02-16-2013 06:39 PM

Fix for #bot create when same named PC exists
 
Hello all. I found that when you create a bot that has the same name as an existing player it bugs both the player and the bot out like crazy. I added a check against the character_ table in the database to make sure a player does not already exist with the target name.

Code:

Index: bot.cpp
===================================================================
--- bot.cpp        (revision 2506)
+++ bot.cpp        (working copy)
@@ -2370,6 +2370,26 @@
                                Result = true;
 
                        mysql_free_result(DatasetResult);
+
+
+
+                        if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM character_ WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult)) {
+                                *errorMessage = std::string(TempErrorMessageBuffer);
+                        }
+                        else {
+                                uint32 ExistingNameCount = 0;
+
+                                while(DataRow = mysql_fetch_row(DatasetResult)) {
+                                        ExistingNameCount = atoi(DataRow[0]);
+                                        break;
+                                }
+
+                                if(ExistingNameCount == 0)
+                                        Result = true;
+
+                                mysql_free_result(DatasetResult);
+
+                        }
                }
 
                safe_delete(Query);


NatedogEZ 02-16-2013 11:34 PM

Nice! This really needed to be fixed

Zamthos 02-19-2013 04:19 AM

So I added your fix, but instead of crashing one zone now, it crashes the entire zone.exe, could you explain how this is supposed to be used? It's currently causing more detriment than anything.

Edit: Code below.

Code:

bool Bot::IsBotNameAvailable(std::string* errorMessage) {
        bool Result = false;

        if(this->GetCleanName()) {
                char* Query = 0;
                char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE];
                MYSQL_RES* DatasetResult;
                MYSQL_ROW DataRow;

                if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM vwBotCharacterMobs WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult)) {
                        *errorMessage = std::string(TempErrorMessageBuffer);
                }
                else {
                        uint32 ExistingNameCount = 0;

                        while(DataRow = mysql_fetch_row(DatasetResult)) {
                                ExistingNameCount = atoi(DataRow[0]);
                                break;
                        }

                        if(ExistingNameCount == 0)
                                Result = true;

                        mysql_free_result(DatasetResult);
               
                        if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM character_ WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult))
                        {
                                *errorMessage = std::string(TempErrorMessageBuffer);
                        }
                        else
                        {
                                uint32 ExistingNameCount = 0;

                                while(DataRow = mysql_fetch_row(DatasetResult))
                                {
                                        ExistingNameCount = atoi(DataRow[0]);
                                        break;
                                }

                                if(ExistingNameCount == 0)
                                        Result = true;

                                mysql_free_result(DatasetResult);

                        }

                }

                safe_delete(Query);
        }

        return Result;
}


NatedogEZ 02-19-2013 04:33 AM

Doesn't look like this fix works? It seems to still crash

NatedogEZ 02-19-2013 05:18 AM

Your check seems to first check the Bot names... if there is no bot name... it sets

Result = True


Then it checks players .. if the player DOES exist it doesn't set the result back to false.



Code:

bool Bot::IsBotNameAvailable(std::string* errorMessage) {
        bool Result = false;
        bool ResultBot = false;
        bool ResultPlayer = false;

        if(this->GetCleanName()) {
                char* Query = 0;
                char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE];
                MYSQL_RES* DatasetResult;
                MYSQL_ROW DataRow;

                if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM vwBotCharacterMobs WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult)) {
                        *errorMessage = std::string(TempErrorMessageBuffer);
                }
                else {
                        uint32 ExistingNameCount = 0;

                        while(DataRow = mysql_fetch_row(DatasetResult)) {
                                ExistingNameCount = atoi(DataRow[0]);
                                break;
                        }

                        if(ExistingNameCount == 0)
                                ResultBot = true;

                        mysql_free_result(DatasetResult);
                }
               
        safe_delete(Query);

                        if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM character_ WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult)) {
                                *errorMessage = std::string(TempErrorMessageBuffer);
                        }
                        else {
                                uint32 ExistingNameCountPlayer = 0;

                                while(DataRow = mysql_fetch_row(DatasetResult)) {
                                        ExistingNameCountPlayer = atoi(DataRow[0]);
                                        break;
                                }

                                if(ExistingNameCountPlayer == 0)
                                        ResultPlayer = true;

                                mysql_free_result(DatasetResult);

                        }

        safe_delete(Query);
               
                if(ResultPlayer == true && ResultBot == true) {
                        Result = true;
                }
        }

        return Result;
}


I do not have a bots compile to test this on... but this might be a fix? Not 100% on that one just yet

c0ncrete 02-19-2013 06:24 AM

there is no reason to use COUNT, or LIKE since there is no wildcard being used, because you're only concerned about a single, exact match. you really don't need to return anything but a 1, since you're not doing anything with the ids. it wouldn't hurt to put LIMIT 1 on the end of the query either.

you can do the whole thing in a single query like so.

"SELECT 1 FROM character_ AS c, vwBotCharacterMobs AS b WHERE '%s' IN (c.name, b.name) LIMIT 1;"

zippzipp 02-19-2013 11:09 AM

I think there was a slight issue with my diff. There was also a bug related to the return value. I have fixed the code and just posted the entire IsBotNameAvailable function above.

As for why I used LIKE: I was just using the same method used that was already there to check against the vwBotCharacterMobs table. Just trying to keep the code consistant, and it was really easy to just copy and paste what was there. You are quite right c0ncrete what you posted is much cleaner. I will look at cleaning the code up a bit when I get some time.


Code:

bool Bot::IsBotNameAvailable(std::string* errorMessage) {
        bool Result1 = false;
        bool Result2 = false;

        if(this->GetCleanName()) {
                char* Query = 0;
                char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE];
                MYSQL_RES* DatasetResult;
                MYSQL_ROW DataRow;

                if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM vwBotCharacterMobs WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult)) {
                        *errorMessage = std::string(TempErrorMessageBuffer);
                }
                else {
                        uint32 ExistingNameCount = 0;

                        while(DataRow = mysql_fetch_row(DatasetResult)) {
                                ExistingNameCount = atoi(DataRow[0]);
                                break;
                        }

                        if(ExistingNameCount == 0)
                                Result1 = true;

                        mysql_free_result(DatasetResult);



                        if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM character_ WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult)) {
                                *errorMessage = std::string(TempErrorMessageBuffer);
                        }
                        else {
                                uint32 ExistingNameCount = 0;

                                while(DataRow = mysql_fetch_row(DatasetResult)) {
                                        ExistingNameCount = atoi(DataRow[0]);
                                        break;
                                }

                                if(ExistingNameCount == 0)
                                        Result2 = true;

                                mysql_free_result(DatasetResult);

                        }
                }
                safe_delete(Query);
        }

        if(Result1 && Result2)
                return true;
        else
                return false;
}


Zamthos 02-19-2013 04:37 PM

Is this fix working? Have you tried it on your server/development server?

zippzipp 02-19-2013 10:14 PM

Not yet. I will verify it tomorrow. I have not had a chance yet.

Zamthos 02-19-2013 10:25 PM

All right, the first fix you gave out actually crashed the entire zone.exe rather than fixing anything. D:

wolfwalkereci 02-19-2013 10:27 PM

Thanks zip, shame the other bot servers that already had this fixed never shared how they fixed it. It's been a known issue for some time.

Zamthos 02-19-2013 10:33 PM

This fix works, 100% confirmed, Wolfwalkereci?

zippzipp 02-20-2013 10:41 AM

Zam

I just confirmed it. Replace your existing Bot::IsBotNameAvailable function with this one. Sorry about the issue before. The problem before was a bug in the code and also the diff i posted. I wish I could update the code in the main post but wont let me.

Code:

bool Bot::IsBotNameAvailable(std::string* errorMessage) {
        bool Result1 = false;
        bool Result2 = false;

        if(this->GetCleanName()) {
                char* Query = 0;
                char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE];
                MYSQL_RES* DatasetResult;
                MYSQL_ROW DataRow;

                if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM vwBotCharacterMobs WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult)) {
                        *errorMessage = std::string(TempErrorMessageBuffer);
                }
                else {
                        uint32 ExistingNameCount = 0;

                        while(DataRow = mysql_fetch_row(DatasetResult)) {
                                ExistingNameCount = atoi(DataRow[0]);
                                break;
                        }

                        if(ExistingNameCount == 0)
                                Result1 = true;

                        mysql_free_result(DatasetResult);



                        if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM character_ WHERE name LIKE '%s'", this->GetCleanName()), TempErrorMessageBuffer, &DatasetResult)) {
                                *errorMessage = std::string(TempErrorMessageBuffer);
                        }
                        else {
                                uint32 ExistingNameCount = 0;

                                while(DataRow = mysql_fetch_row(DatasetResult)) {
                                        ExistingNameCount = atoi(DataRow[0]);
                                        break;
                                }

                                if(ExistingNameCount == 0)
                                        Result2 = true;

                                mysql_free_result(DatasetResult);

                        }
                }
                safe_delete(Query);
        }

        if(Result1 && Result2)
                return true;
        else
                return false;
}


Zamthos 02-20-2013 05:30 PM

Good, thanks ZippZipp!

zippzipp 02-22-2013 05:37 PM

So it turns out the issue was seeded much deeper than I had realized. While the fixes posted before fixed issues with creating bots with same name, they did not fix the issue of that player with the same name going Linkdead when another user attempted to create a bot with their name.

The problem was rooted in the fact that the function in bot.cpp:

Bot::IsBotNameAvailable is a non-static class function and a Bot object was needed in order to check if the name was valid. Creating that Bot object was crashing people with the same name. What I did was make the IsBotNameAvailable a static class function and pass it the name to check. I then call the function before the new Bot object is created. This fixes the problem of crashing and prevents people from making bots with the same name as PCs.

Code:

// Bot.h
// modify the IsBotNameAvailable function prototype

// REPLACE THIS:
bool IsBotNameAvailable(std::string* errorMessage);
// WITH THIS:
static bool IsBotNameAvailable(char *botName, std::string* errorMessage);

Code:

// Bot.cpp
// modify the IsBotNameAvailable function to work off without a this pointer.

bool Bot::IsBotNameAvailable(char *botName, std::string* errorMessage) {
        bool Result1 = false;
        bool Result2 = false;

        if(botName !="") {
                char* Query = 0;
                char TempErrorMessageBuffer[MYSQL_ERRMSG_SIZE];
                MYSQL_RES* DatasetResult;
                MYSQL_ROW DataRow;

                if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM vwBotCharacterMobs WHERE name LIKE '%s'", botName), TempErrorMessageBuffer, &DatasetResult)) {
                        *errorMessage = std::string(TempErrorMessageBuffer);
                }
                else {
                        uint32 ExistingNameCount = 0;

                        while(DataRow = mysql_fetch_row(DatasetResult)) {
                                ExistingNameCount = atoi(DataRow[0]);
                                break;
                        }

                        if(ExistingNameCount == 0)
                                Result1 = true;

                        mysql_free_result(DatasetResult);

                        if(!database.RunQuery(Query, MakeAnyLenString(&Query, "SELECT COUNT(id) FROM character_ WHERE name LIKE '%s'", botName), TempErrorMessageBuffer, &DatasetResult)) {
                                *errorMessage = std::string(TempErrorMessageBuffer);
                        } else {
                                uint32 ExistingNameCount = 0;

                                while(DataRow = mysql_fetch_row(DatasetResult)) {
                                        ExistingNameCount = atoi(DataRow[0]);
                                        break;
                                }

                                if(ExistingNameCount == 0)
                                        Result2 = true;

                                mysql_free_result(DatasetResult);

                        }
                }
                safe_delete(Query);
        }

        if(Result1 && Result2)
                return true;
        else
                return false;
}

Modify all references to IsBotNameAvailable to call the static function.

Code:

// bot.cpp
// REPLACE:
NPCType DefaultNPCTypeStruct = CreateDefaultNPCTypeStructForBot(std::string(sep->arg[2]), std::string(), c->GetLevel(), atoi(sep->arg[4]), atoi(sep->arg[3]), gender);
                Bot* NewBot = new Bot(DefaultNPCTypeStruct, c);

// WITH:
if(!IsBotNameAvailable(sep->arg[2],&TempErrorMessage)) {
                        c->Message(0, "The name %s is already being used. Please choose a different name.", sep->arg[2]);
                        return;
                }

                NPCType DefaultNPCTypeStruct = CreateDefaultNPCTypeStructForBot(std::string(sep->arg[2]), std::string(), c->GetLevel(), atoi(sep->arg[4]), atoi(sep->arg[3]), gender);
                Bot* NewBot = new Bot(DefaultNPCTypeStruct, c);


// REMOVE THIS:
if(!NewBot->IsBotNameAvailable(&TempErrorMessage)) {
                        c->Message(0, "The name %s is already being used. Please choose a different name.", NewBot->GetCleanName());
                        return;
                }

ALSO update questmgr.cpp for when a bot is created via quest.
Code:

// questmgr.cpp
// REPLACE:
NPCType DefaultNPCTypeStruct = Bot::CreateDefaultNPCTypeStructForBot(name, lastname, level, race, botclass, gender);
                Bot* NewBot = new Bot(DefaultNPCTypeStruct, initiator);

// WITH:
if(Bot::IsBotNameAvailable((char*)name,&TempErrorMessage)) {
                        initiator->Message(0, "The name %s is already being used. Please choose a different name.", (char*)name);
                        return false;
                }

                NPCType DefaultNPCTypeStruct = Bot::CreateDefaultNPCTypeStructForBot(name, lastname, level, race, botclass, gender);
                Bot* NewBot = new Bot(DefaultNPCTypeStruct, initiator);


// REMOVE THIS:
if(!NewBot->IsBotNameAvailable(&TempErrorMessage)) {
                        initiator->Message(0, "The name %s is already being used. Please choose a different name.", NewBot->GetCleanName());
                        return false;
                }



All times are GMT -4. The time now is 10:21 AM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.