SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 31/07/2011, 09:27 AM   #1
Kush
Gangsta
 
Join Date: May 2011
Posts: 783
Reputation: 123
Default Login and Register System - Dialogs - Using Y_INI

Login and Register System - Dialogs - Using Y_INI


What is this?
A simple tutorial on how to make a Login and Register system using Y_INI.

What is Y_INI?
Y_INI is an extensive .INI based file reader and writer, also known as a 'File Management System' created by Y_Less. This is included in the YSI Library/Directory along with other useful includes such as y_commands (YCMD) and y_groups.


What's the difference between this and my file writer?
Y_INI has not only been said, but has been proven to be one of the fastest and most efficient .INI file readers and writers created. You can read more by visiting: http://forum.sa-mp.com/showthread.php?t=175565


How to 'install' y_ini?
You must first download y_INI ----> http://forum.sa-mp.com/showthread.php?t=175565. Once the download has finished, place the YSI folder in your includes folder.


Step I
Add the Y_INI Include at the beginning of the script.
pawn Code:
#include <YSI\y_ini>
This include contains all necessary functions needed to create our Login and Register system.


Step II
Lets define some Dialogs.

pawn Code:
#define DIALOG_REGISTER 1
#define DIALOG_LOGIN 2
#define DIALOG_SUCCESS_1 3
#define DIALOG_SUCCESS_2 4


Step III
Defining the 'PATH' of the .INI file.

pawn Code:
#define PATH "/Users/%s.ini"

Step IV
Lets add some colors.

pawn Code:
#define COL_WHITE "{FFFFFF}"
#define COL_RED "{F81414}"
#define COL_GREEN "{00FF22}"
#define COL_LIGHTBLUE "{00CED1}"

Step V
We're going to use an enum, to store our variables.

pawn Code:
enum pInfo
{
    pPass,
    pCash,
    pAdmin,
    pKills,
    pDeaths
}
new PlayerInfo[MAX_PLAYERS][pInfo];

Step VI
Now, we're going to create a function to Load the User's data.


pawn Code:
forward LoadUser_data(playerid,name[],value[]);
public LoadUser_data(playerid,name[],value[])
{
    INI_Int("Password",PlayerInfo[playerid][pPass]);
    INI_Int("Cash",PlayerInfo[playerid][pCash]);
    INI_Int("Admin",PlayerInfo[playerid][pAdmin]);
    INI_Int("Kills",PlayerInfo[playerid][pKills]);
    INI_Int("Deaths",PlayerInfo[playerid][pDeaths]);
    return 1;
}


Step VII
Lets create a simple stock function.

pawn Code:
stock UserPath(playerid)
{
    new string[128],playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid,playername,sizeof(playername));
    format(string,sizeof(string),PATH,playername);
    return string;
}
The stock function 'UserPath' is merely going to 'grab' the 'PATH' of the User's file.


Step VIII
Add this code below your previous stock function.
pawn Code:
/*Credits to Dracoblue*/
stock udb_hash(buf[]) {
    new length=strlen(buf);
    new s1 = 1;
    new s2 = 0;
    new n;
    for (n=0; n<length; n++)
    {
       s1 = (s1 + buf[n]) % 65521;
       s2 = (s2 + s1)     % 65521;
    }
    return (s2 << 16) + s1;
}

The stock above is a simple 'hasher', and will be used to hash passwords, Credits to Dracoblue.


Step IX
We're going to now use the 'OnPlayerConnect' callback to check whether the player is registered or not.
pawn Code:
public OnPlayerConnect(playerid)
{
    if(fexist(UserPath(playerid)))
    {
        INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);
        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""COL_WHITE"Login",""COL_WHITE"Type your password below to login.","Login","Quit");
    }
    else
    {
        ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT,""COL_WHITE"Registering...",""COL_WHITE"Type your password below to register a new account.","Register","Quit");
    }
    return 1;
}
We'll be using the native 'fexist' function to search for our file. Parameters are set to our stock function which we've created. If the file exists, you will receive a 'Login' dialog. If it doesn't, you will receive a register dialog.


Step X

pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch( dialogid )
    {
        case DIALOG_REGISTER:
        {
            if (!response) return Kick(playerid);
            if(response)
            {
                if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, ""COL_WHITE"Registering...",""COL_RED"You have entered an invalid password.\n"COL_WHITE"Type your password below to register a new account.","Register","Quit");
                new INI:File = INI_Open(UserPath(playerid));
                INI_SetTag(File,"data");
                INI_WriteInt(File,"Password",udb_hash(inputtext));
                INI_WriteInt(File,"Cash",0);
                INI_WriteInt(File,"Admin",0);
                INI_WriteInt(File,"Kills",0);
                INI_WriteInt(File,"Deaths",0);
                INI_Close(File);

                SetSpawnInfo(playerid, 0, 0, 1958.33, 1343.12, 15.36, 269.15, 0, 0, 0, 0, 0, 0);
                SpawnPlayer(playerid);
                ShowPlayerDialog(playerid, DIALOG_SUCCESS_1, DIALOG_STYLE_MSGBOX,""COL_WHITE"Success!",""COL_GREEN"Great! Your Y_INI system works perfectly. Relog to save your stats!","Ok","");
            }
        }

        case DIALOG_LOGIN:
        {
            if ( !response ) return Kick ( playerid );
            if( response )
            {
                if(udb_hash(inputtext) == PlayerInfo[playerid][pPass])
                {
                    INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);
                    GivePlayerMoney(playerid, PlayerInfo[playerid][pCash]);
                    ShowPlayerDialog(playerid, DIALOG_SUCCESS_2, DIALOG_STYLE_MSGBOX,""COL_WHITE"Success!",""COL_GREEN"You have successfully logged in!","Ok","");
                }
                else
                {
                    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""COL_WHITE"Login",""COL_RED"You have entered an incorrect password.\n"COL_WHITE"Type your password below to login.","Login","Quit");
                }
                return 1;
            }
        }
    }
    return 1;
}

Instead of using the 'if' statement to define my dialogs, I've used cases as they seem to take less space and are supposedly 'faster'. The (!response) is the function if the first Button hasn't been clicked, it will then kick the player.

The if(!strlen(inputtext)) explains if nothing has been entered into the dialog (input), you would then be prompted to another dialog which shows you 'Incorrect Password'.

If all goes well, the function INI_Open is then 'executed' which loads and opens the Userfile. Once open, the function 'INI_WriteInt' is then called and starts writing the data into the userfile. The udb_hash would generate a hash code from the player's inputtext (what you've typed). And after all this is completed, it is then closed by 'INI_Close' function.

Once finished, you will then be prompted to the 'Login' dialog.


In 'DIALOG_LOGIN', if the response is false (you have clicked 'QUIT), you would then be kicked. If given the correct information (password provided), the INI_Parsefile function would then scan and load your player's data.


Step XI
Don't forget, you would need a way to save those variables. The OnPlayerDisconnect callback simply re-opens the files, write what-ever values which has been stored and then closes it.
pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    new INI:File = INI_Open(UserPath(playerid));
    INI_SetTag(File,"data");
    INI_WriteInt(File,"Cash",GetPlayerMoney(playerid));
    INI_WriteInt(File,"Admin",PlayerInfo[playerid][pAdmin]);
    INI_WriteInt(File,"Kills",PlayerInfo[playerid][pKills]);
    INI_WriteInt(File,"Deaths",PlayerInfo[playerid][pDeaths]);
    INI_Close(File);
    return 1;
}

Step XII
Finally, add this to OnPlayerDeath to add value(s) to kills and deaths.

pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
    PlayerInfo[killerid][pKills]++;
    PlayerInfo[playerid][pDeaths]++;
    return 1;
}

Downloads:



Credits:

Y_Less - y_ini.

Last edited by Kush; 08/09/2012 at 11:05 AM.
Kush is offline   Reply With Quote
Old 31/07/2011, 09:30 AM   #2
Black_Death
Big Clucker
 
Black_Death's Avatar
 
Join Date: May 2010
Location: Not your fucking business!
Posts: 193
Reputation: 19
Default Re: Login and Register System - Dialogs - Using Y_INI

Great job
Black_Death is offline   Reply With Quote
Old 31/07/2011, 09:31 AM   #3
Tyler_Cordwell
Little Clucker
 
Join Date: Mar 2011
Posts: 32
Reputation: 7
Default Re: Login and Register System - Dialogs - Using Y_INI

nice guide i will learn it right now
Tyler_Cordwell is offline   Reply With Quote
Old 31/07/2011, 09:38 AM   #4
Kush
Gangsta
 
Join Date: May 2011
Posts: 783
Reputation: 123
Default Re: Login and Register System - Dialogs - Using Y_INI

Thanks!
__________________
Quote:
Originally Posted by Dripac
i have more reputation than you, that means i am more clever than you my friend
Kush is offline   Reply With Quote
Old 31/07/2011, 09:44 AM   #5
kingchandio
Big Clucker
 
Join Date: Jan 2011
Posts: 111
Reputation: 1
Default Re: Login and Register System - Dialogs - Using Y_INI

wow very nice i was looking for easy tutorial now i can learn y_ini thank you so much you are helpul guy... how can up ur repu?
kingchandio is offline   Reply With Quote
Old 31/07/2011, 09:56 AM   #6
Basicz
Gangsta
 
Basicz's Avatar
 
Join Date: Aug 2010
Location: Indonesia, Surabaya.
Posts: 621
Reputation: 35
Default Re: Login and Register System - Dialogs - Using Y_INI

@kingchandio, just press the star button at below of his avatar ( below : Reputation: 10 ).

NICE, now I got my script working again after I used my sucky stock :\

pawn Code:
reputation[ Kush ] ++;

TIP: Explain more parts!
__________________

- Quina, from the game " Final Fantasy IX "
Basicz is offline   Reply With Quote
Old 31/07/2011, 09:59 AM   #7
Kush
Gangsta
 
Join Date: May 2011
Posts: 783
Reputation: 123
Default Re: Login and Register System - Dialogs - Using Y_INI

Quote:
Originally Posted by Basicz View Post
@kingchandio, just press the star button at below of his avatar ( below : Reputation: 10 ).

NICE, now I got my script working again after I used my sucky stock :\

pawn Code:
reputation[ Kush ] ++;

TIP: Explain more parts!
I've done them in the previous tutorial, no one cares but to download the .pwn file.
__________________
Quote:
Originally Posted by Dripac
i have more reputation than you, that means i am more clever than you my friend
Kush is offline   Reply With Quote
Old 31/07/2011, 10:10 AM   #8
kingchandio
Big Clucker
 
Join Date: Jan 2011
Posts: 111
Reputation: 1
Default Re: Login and Register System - Dialogs - Using Y_INI

one little problem i have added /kill cmd and i entered 5 times but still my data on dissconnect looks

Quote:
[data]
Password = 104268307
Cash = 0
Admin = 0
Kills = 0
Deaths = 0
kingchandio is offline   Reply With Quote
Old 31/07/2011, 10:28 AM   #9
Kush
Gangsta
 
Join Date: May 2011
Posts: 783
Reputation: 123
Default Re: Login and Register System - Dialogs - Using Y_INI

Quote:
Originally Posted by kingchandio View Post
one little problem i have added /kill cmd and i entered 5 times but still my data on dissconnect looks
PHP Code:
CMD:kill(playeridparams[])
{
    
PlayerInfo[playerid][pDeaths]++;
    return 
1;

You have to be killed by a player in order for the value to change.

The command given would be an example of being killed by a player, only in command form
__________________
Quote:
Originally Posted by Dripac
i have more reputation than you, that means i am more clever than you my friend
Kush is offline   Reply With Quote
Old 31/07/2011, 10:31 AM   #10
kingchandio
Big Clucker
 
Join Date: Jan 2011
Posts: 111
Reputation: 1
Default Re: Login and Register System - Dialogs - Using Y_INI

oh i see thanks
kingchandio is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] Login & Register system - dialogs - with y_ini Seven_of_Nine Tutorials 30 17/12/2016 09:20 AM
[FilterScript] [FS] ERegLog ( Advanced Register/Login System [Dialogs] ) aircombat Filterscripts 29 09/11/2014 09:30 PM
[Tutorial] Login and Register System - Dialogs - Using SII Kush Tutorials 69 02/07/2013 05:19 PM
[FilterScript] Register/login system (Dini, dialogs) pantelimonfl Filterscripts 6 08/03/2011 04:13 PM


All times are GMT. The time now is 10:52 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.