[Tutorial] Creating a simple register/save system using R41-2
#17

Okay Mr Pro scripter i will tell you what exactly is optmisation is.It is not actually reducing no of lines in a script.It how you hacks with compiler and machine.Also overdoing an optmisation is also bad programming do the optmisation where ever necessary and never should it compromise it with readability.
Lets take a tiny portion of your script.
Code:
public OnPlayerConnect(playerid) 
{ 
    new query[50], pname[MAX_PLAYERS_NAME]; 
    GetPlayerName(playerid, pname, sizeof(pname)); // gets player name on connect 
    mysql_format(MHandle, query, sizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e'", pname); 
    mysql_tquery(MHandle, query, "OnPlayerAccountCheck", "i", playerid); // in this step the server checks if player registered or not using the callback up ^^ 
    return 1; 
}
Now this
Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) 
{ 
    switch(dialogid) 
    {    
        case Login: 
        { 
            static loginattemp, buf[129], pname[MAX_PLAYERS_NAME]; 
            GetPlayerName(playerid, pname, sizeof(pname)); 

            if(!response) Kick(playerid);  
            if(isnull(PlayerInfo[playerid][Password])) // checks if password correctly loaded else check below :) 
            { 
                SendClientMessage(playerid, -1,"[MYSQL]: Sorry, we have some problems on database right now, Come back later !"); 
                Kick(playerid); 
                print("[MYSQL]: Password Value 'PlayerInfo[playerid][Password]' not correctly loaded! please check OnPlayerAccountCheck callback"); 
                print("[SA-MP Server]: Server shutdown.."); 
                SendRconCommand("exit"); 
                return 1; 
            } 
            WP_Hash(buf, sizeof(buf), inputtext); 
            if(!strcmp(buf, PlayerInfo[playerid][Password], true)) // comparing the passwords (database & entrerd) 
            {  
                new query[70], Cache:GetCache; 
                mysql_format(MHandle, query, sizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e' LIMIT 1", pname); 
                GetCache = mysql_query(MHandle, query); 
                
                // Importing data after beigning sure he is the right user 
                new rows;  
                cache_get_row_count(rows); 
                if(rows == 1)  
                { 
                    cache_get_value_name_int(0, "Admin", PlayerInfo[playerid][Admin]);  
                    cache_get_value_name_int(0, "Deaths", PlayerInfo[playerid][Deaths]);  
                    cache_get_value_name_int(0, "Kills", PlayerInfo[playerid][Kills]); 
                } 
                SendClientMessage(playerid, -1, "You have successfully logged in.");  
                cache_delete(GetCache); 
            }  
            else // if he entred an wrrong password 
            {  
                if(loginattemp == 3) return Kick(playerid); // if he entred the same wrrong password 3 times he got kicked 
                SendClientMessage(playerid, -1, "You have specified an incorrect password!");  
                ShowPlayerDialog(playerid, Login, DIALOG_STYLE_PASSWORD, "Login", "Welcome Back !\nWe miss you here !\nPlease fill you password here to get stats back!", "Login", "Cancel"); 
                loginattemp++; // adding +1 for every attemp 
            }  
        } 
        case Register: 
        { 
            if(!response) return Kick(playerid);  
            if(strlen(inputtext) < 5)  
            {  
                SendClientMessage(playerid, -1, "Your password must at least contain more than 4 characters.");  
                return ShowPlayerDialog(playerid, Register, DIALOG_STYLE_PASSWORD, "Register", "Welcome to our server! \nthis account not register at the database \nPlease fill a password to register your account", "Register", "Cancel"); 
            }  
            new  
                query[287],  
                playername[MAX_PLAYER_NAME],  
                playerip[16], 
                buf[129] 
            ; 
            WP_Hash(buf, sizeof(buf), inputtext); // hashing password using Whirlpool engine 
            GetPlayerName(playerid, playername, sizeof(playername));  
            GetPlayerIp(playerid, playerip, sizeof(playerip));  
            mysql_format(MHandle, query, sizeof(query), "INSERT INTO `accounts` (`Name`, `Password`, `IP`, `Admin`,`Deaths`, `Kills`) VALUES ('%e', '%e', '%e', 0, 0, 0)", playername, buf, playerip);  
            mysql_query(MHandle, query); // here inserting the player account in the database as a registered player 
        } 
    } 
    return 0; 
}
You are creating 3 variables to hold the player name in 3 different situations thats just fucking stupidness.
if i were you i will do create an global array to hold the player name,if its thats case i will add it in player enum

Also take a look @ here

Code:
static loginattemp
what the hell? You know what you actually doing ? Your code never going to work even. The code will not allow any player to login if one of the player failed the attempt.Because you are using a static variable whose copy will be created only once in the stack.
Now you can use char arrays instead of plain arrays which will much more memory efficient.
Code:
array_name[size char];
i can go even more and you can get more chilled.
Needless to say 1 star from me.Keep trying.....
Reply


Messages In This Thread
Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 11:12
Re: Creating a simple register/save system using R41-2 - by princejeet1510 - 25.01.2017, 11:17
Re: Creating a simple register/save system using R41-2 - by oMa37 - 25.01.2017, 11:59
Respuesta: Creating a simple register/save system using R41-2 - by Eloy - 25.01.2017, 12:15
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 12:22
Re: Creating a simple register/save system using R41-2 - by SyS - 25.01.2017, 12:26
Re: Creating a simple register/save system using R41-2 - by oMa37 - 25.01.2017, 12:30
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 12:38
Re: Creating a simple register/save system using R41-2 - by princejeet1510 - 25.01.2017, 12:48
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 12:53
Re: Creating a simple register/save system using R41-2 - by princejeet1510 - 25.01.2017, 12:57
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 12:59
Re: Creating a simple register/save system using R41-2 - by oMa37 - 25.01.2017, 13:03
Re: Creating a simple register/save system using R41-2 - by Quinncell - 25.01.2017, 13:06
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 13:08
Re: Creating a simple register/save system using R41-2 - by oMa37 - 25.01.2017, 13:14
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 13:28
Re: Creating a simple register/save system using R41-2 - by Vince - 25.01.2017, 13:33
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 13:39
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 14:31
Re: Creating a simple register/save system using R41-2 - by PaRking - 25.01.2017, 14:40
Re: Creating a simple register/save system using R41-2 - by Quinncell - 25.01.2017, 14:41
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 15:45
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 15:53
Re: Creating a simple register/save system using R41-2 - by Lordzy - 25.01.2017, 16:10
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 16:14
Re: Creating a simple register/save system using R41-2 - by Lordzy - 25.01.2017, 16:30
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 16:34
Re: Creating a simple register/save system using R41-2 - by Lordzy - 25.01.2017, 16:37
Re: Creating a simple register/save system using R41-2 - by RyderX - 27.01.2017, 13:21
Re: Creating a simple register/save system using R41-2 - by BiosMarcel - 27.01.2017, 14:12
Re: Creating a simple register/save system using R41-2 - by Vince - 27.01.2017, 14:49
Re: Creating a simple register/save system using R41-2 - by Yaa - 27.01.2017, 19:34

Forum Jump:


Users browsing this thread: 5 Guest(s)