do-while loop help(SQLite / Name checks)
#1

I'm having some problems with my do-while loop..
I dunno how to explain it, but, It should do a loop trough the DB until a "free name" is found, but when a "free name" IS found, nothing happens D:

Code;
pawn Код:
//Gets shown if player fails to login 3x
        case DIALOG_FAIL_LOGIN:
        {
            if( !response ) return SetTimerEx("KickTimer", 50, false, "i", playerid);
            if( response )
            {
                new Query[106], name[24], string[66], character = 1, DBResult:Result;
                format(name, sizeof(name), "%s_%d", GetName(playerid), character);
                format(Query, sizeof(Query), "SELECT * FROM `USERS` WHERE `NAME` = '%s'", name);
                Result = db_query(HDF, Query);
                if(db_num_rows(Result))
                {
                    do
                    {
                        character++;
                        format(name, sizeof(name), "%s_%d", GetName(playerid), character);
                        format(string, sizeof(string), "Skipping name '%s' because is in use.", name), SendClientMessage(playerid, COLOR_NOTES2, string);
                    }
                    while(db_next_row(Result));
                }
                else
                {
                    format(string, sizeof(string), "Found a valid name - '%s' - setting. . .", name), SendClientMessage(playerid, COLOR_NOTES, string);
                    SetPlayerName(playerid, name);

                    SendClientMessage(playerid, COLOR_NOTES, "INFO: You will need to register now to save your stats");
                    PlayerPlaySound(playerid,1057,0.0,0.0,0.0);

                    ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "{64CC66}Registration.", "{FFFFFF}Welcome to the server!\nYour name is detected as {64CC66}unregistered{FFFFFF}.\nYou are required to register to proceed playing.\n{64CC66}Enter your password below to complete the registration.", "Register", "Leave");
                }
                db_free_result(Result);
            }
        }
Yeah, I bet it's because of some brackets/doing stuff outside the do-while, but I'm new to this loop thing so I dunno how it exactly works ;_;
Reply
#2

This is because it is there is nothing to do when loop ends ._ like this --
pawn Код:
case DIALOG_FAIL_LOGIN:
        {
            if( !response ) return SetTimerEx("KickTimer", 50, false, "i", playerid);
            if( response )
            {
                new Query[106], name[24], string[66], character = 1, DBResult:Result;
                format(name, sizeof(name), "%s_%d", GetName(playerid), character);
                format(Query, sizeof(Query), "SELECT * FROM `USERS` WHERE `NAME` = '%s'", name);
                Result = db_query(HDF, Query);
                if(db_num_rows(Result))
                {
                    do
                    {
                        character++;
                        format(name, sizeof(name), "%s_%d", GetName(playerid), character);
                        format(string, sizeof(string), "Skipping name '%s' because is in use.", name), SendClientMessage(playerid, COLOR_NOTES2, string);
                    }
                    while(db_next_row(Result));
                }
               
               
                    format(string, sizeof(string), "Found a valid name - '%s' - setting. . .", name), SendClientMessage(playerid, COLOR_NOTES, string);
                    SetPlayerName(playerid, name);

                    SendClientMessage(playerid, COLOR_NOTES, "INFO: You will need to register now to save your stats");
                    PlayerPlaySound(playerid,1057,0.0,0.0,0.0);

                    ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "{64CC66}Registration.", "{FFFFFF}Welcome to the server!\nYour name is detected as {64CC66}unregistered{FFFFFF}.\nYou are required to register to proceed playing.\n{64CC66}Enter your password below to complete the registration.", "Register", "Leave");
               
                db_free_result(Result);
            }
        }
No need of that else block...
Reply
#3

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
This is because it is there is nothing to do when loop ends ._ like this --
pawn Код:
case DIALOG_FAIL_LOGIN:
        {
            if( !response ) return SetTimerEx("KickTimer", 50, false, "i", playerid);
            if( response )
            {
                new Query[106], name[24], string[66], character = 1, DBResult:Result;
                format(name, sizeof(name), "%s_%d", GetName(playerid), character);
                format(Query, sizeof(Query), "SELECT * FROM `USERS` WHERE `NAME` = '%s'", name);
                Result = db_query(HDF, Query);
                if(db_num_rows(Result))
                {
                    do
                    {
                        character++;
                        format(name, sizeof(name), "%s_%d", GetName(playerid), character);
                        format(string, sizeof(string), "Skipping name '%s' because is in use.", name), SendClientMessage(playerid, COLOR_NOTES2, string);
                    }
                    while(db_next_row(Result));
                }
               
               
                    format(string, sizeof(string), "Found a valid name - '%s' - setting. . .", name), SendClientMessage(playerid, COLOR_NOTES, string);
                    SetPlayerName(playerid, name);

                    SendClientMessage(playerid, COLOR_NOTES, "INFO: You will need to register now to save your stats");
                    PlayerPlaySound(playerid,1057,0.0,0.0,0.0);

                    ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "{64CC66}Registration.", "{FFFFFF}Welcome to the server!\nYour name is detected as {64CC66}unregistered{FFFFFF}.\nYou are required to register to proceed playing.\n{64CC66}Enter your password below to complete the registration.", "Register", "Leave");
               
                db_free_result(Result);
            }
        }
No need of that else block...
[17:33:10] Skipping name 'Kyance_2' because is in use.
[17:33:10] Found a valid name - 'Kyance_2' - setting. . .

:l
Reply
#4

Nvm itll create an infinite loop.
Reply
#5

The problem is this:
The query is running only once.
It's formatted to %s = Name_1
It never re-formats to re-call the query to re-check if the name is available.

EDIT: You probably wont understand what I mean, so I'll explain it with comments.
pawn Код:
case DIALOG_FAIL_LOGIN:
{
    if( !response ) return SetTimerEx("KickTimer", 50, false, "i", playerid);
    if( response )
    {
        new Query[106], name[24], string[66], character = 1, DBResult:Result;
        format(name, sizeof(name), "%s_%d", GetName(playerid), character);
        format(Query, sizeof(Query), "SELECT * FROM `USERS` WHERE `NAME` = '%s'", name);
        // Let's say the query is now this
        // SELECT * FROM `USERS` WHERE `NAME` = 'Example_1'
        Result = db_query(HDF, Query);
        if(db_num_rows(Result)) // If the name exists
        {
            do
            {
                character++;
                format(name, sizeof(name), "%s_%d", GetName(playerid), character);
                // You're adding the new number to the name.
                // But where are you adding it to the query?
                format(string, sizeof(string), "Skipping name '%s' because is in use.", name), SendClientMessage(playerid, COLOR_NOTES2, string);
            }
            while(db_next_row(Result)); // It's always checking for Example_1
        }
        else
        {
            format(string, sizeof(string), "Found a valid name - '%s' - setting. . .", name), SendClientMessage(playerid, COLOR_NOTES, string);
            SetPlayerName(playerid, name);

            SendClientMessage(playerid, COLOR_NOTES, "INFO: You will need to register now to save your stats");
            PlayerPlaySound(playerid,1057,0.0,0.0,0.0);

            ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "{64CC66}Registration.", "{FFFFFF}Welcome to the server!\nYour name is detected as {64CC66}unregistered{FFFFFF}.\nYou are required to register to proceed playing.\n{64CC66}Enter your password below to complete the registration.", "Register", "Leave");
        }
        db_free_result(Result);
    }
}
Reply
#6

pawn Код:
case DIALOG_FAIL_LOGIN:
{
    #define MAX_CHARACTER 32
    if(!response) return 0;
    new query[128],name[24],string[128],DBResult:result;
    for(new character = 0; character < MAX_CHARACTER; character ++)
    {
        format(name,sizeof(name),"%s_%d",GetName(playerid),character);
        format(query,sizeof(query),"SELECT * FROM `USERS` WHERE `NAME` = '%s' LIMIT 1",name);
        result = db_query(database,query);
        if(db_num_rows(result))
        {
            format(string,sizeof(string),"> Skipping name \"%s\" - Already in use.",name);
            SendClientMessage(playerid,COLOR_NOTES2,string);
            if(character == MAX_CHARACTER)
            {
                SendClientMessage(playerid,COLOR_NOTES2,"> Out of "#MAX_CHARACTER" names, there was none available. You have been kicked.");
                Kick(playerid);
                return db_free_result(result);
            }
        }
        else
        {
            format(string,sizeof(string),"> Found a valid name \"%s\" - Setting.",name);
            SendClientMessage(playerid,COLOR_NOTES,string);
            SendClientMessage(playerid, COLOR_NOTES, "INFO: You will need to register now to save your stats");
            PlayerPlaySound(playerid,1057,0.0,0.0,0.0);
            ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "{64CC66}Registration.", "{FFFFFF}Welcome to the server!\nYour name is detected as {64CC66}unregistered{FFFFFF}.\nYou are required to register to proceed playing.\n{64CC66}Enter your password below to complete the registration.", "Register", "Leave");
            db_free_result(result);
            return 1;
        }
        db_free_result(result);
    }
}
You need to loop through characters, format the query with the newly generated name then query the database.
Reply
#7

Quote:
Originally Posted by Stinged
Посмотреть сообщение
The problem is this:
The query is running only once.
It's formatted to %s = Name_1
It never re-formats to re-call the query to re-check if the name is available.

EDIT: You probably wont understand what I mean, so I'll explain it with comments.
pawn Код:
case DIALOG_FAIL_LOGIN:
{
    if( !response ) return SetTimerEx("KickTimer", 50, false, "i", playerid);
    if( response )
    {
        new Query[106], name[24], string[66], character = 1, DBResult:Result;
        format(name, sizeof(name), "%s_%d", GetName(playerid), character);
        format(Query, sizeof(Query), "SELECT * FROM `USERS` WHERE `NAME` = '%s'", name);
        // Let's say the query is now this
        // SELECT * FROM `USERS` WHERE `NAME` = 'Example_1'
        Result = db_query(HDF, Query);
        if(db_num_rows(Result)) // If the name exists
        {
            do
            {
                character++;
                format(name, sizeof(name), "%s_%d", GetName(playerid), character);
                // You're adding the new number to the name.
                // But where are you adding it to the query?
                format(string, sizeof(string), "Skipping name '%s' because is in use.", name), SendClientMessage(playerid, COLOR_NOTES2, string);
            }
            while(db_next_row(Result)); // It's always checking for Example_1
        }
        else
        {
            format(string, sizeof(string), "Found a valid name - '%s' - setting. . .", name), SendClientMessage(playerid, COLOR_NOTES, string);
            SetPlayerName(playerid, name);

            SendClientMessage(playerid, COLOR_NOTES, "INFO: You will need to register now to save your stats");
            PlayerPlaySound(playerid,1057,0.0,0.0,0.0);

            ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "{64CC66}Registration.", "{FFFFFF}Welcome to the server!\nYour name is detected as {64CC66}unregistered{FFFFFF}.\nYou are required to register to proceed playing.\n{64CC66}Enter your password below to complete the registration.", "Register", "Leave");
        }
        db_free_result(Result);
    }
}
Either I did something wrong.. but that made an infinite loop ;_;

@xXShadowXx: That worked, thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)