SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 10/07/2011, 12:02 AM   #1
Kush
Gangsta
 
Join Date: May 2011
Posts: 781
Reputation: 126
Default Login and Register System - Dialogs - Using SII

Login and Register System - Dialogs - Using SII.

I've seen numerous people, wanting to switch from the older Dini file saving system to a newer one, such as Y_INI, djSON and even Fini. Most of them understand the Dini format, and examples, even tutorials of the newer systems sometimes help, but to some of the newer 'developers' this can be a struggle. I've created this tutorial to help those wishing to upgrade' to a newer system, a better system.

What is this?
This is a simple tutorial on how to make Login and Register system with dialogs using SII.


Step 1
Firstly, your going to need to download the SII include. You can find it here: http://forum.sa-mp.com/showthread.php?t=58458. Once downloaded, place it into your Pawno > include folder.

Step 2
Add the include at the top of the script.

pawn Code:
#include <SII>
The SII include contains all the needed functions you are going to use to create your Login and Register system.

Step 3
Let's define some dialogs:

pawn Code:
#define DIALOG_REGISTER 2000
#define DIALOG_LOGIN 2001

Step 4
Let's add some colors:

pawn Code:
#define WHITE "{FFFFFF}"
#define RED "{F81414}"
#define GREEN "{00FF22}"
#define LIGHTBLUE "{00CED1}"

Step 5
Create a new variable, somewhere below your defines.

pawn Code:
new gPlayerName[MAX_PLAYERS][MAX_PLAYER_NAME];

Step 6
Next, lets add an enum, also known as an enumeration.

pawn Code:
enum pInfo
{
    pPass,
    pScore,
    pCash,
    pAdmin
}
new PlayerInfo[MAX_PLAYERS][pInfo];
An enumeration stores many things in a variable. This is extremely efficient and effective, instead of creating a set of different variables.


Step 7
Lets create a stock function:

pawn Code:
stock getINI(playerid)
{
  new account[64];
  format(account,30,"Users/%s.ini",gPlayerName[playerid]);
  return account;
}
The stock function is going to load the user file.

Step 8
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.


pawn Code:
public OnPlayerConnect(playerid)
{
    GetPlayerName(playerid, gPlayerName[playerid], MAX_PLAYER_NAME);
    if (fexist(getINI(playerid)))
    {
        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""WHITE"Login",""WHITE"Type your password below to login.","Login","Quit");
    }
    else
    {
        ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT,""WHITE"Registering...",""WHITE"Type your password below to register a new account.","Register","Quit");
    }
    return 1;
}

Step 9
Go to your OnDialogResponse callback and add this in.


pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch( dialogid )
    {
        case DIALOG_REGISTER:
        {
            if (!response) return Kick(playerid);
            if (response)
            {
                if(!strlen(inputtext)) {
                ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, ""WHITE"Registering...",""RED"You have entered an invalid password.\n"WHITE"Type your password below to register a new account.","Register","Quit");
                }

                if(INI_Open(getINI(playerid))) {
                INI_WriteString("Password",inputtext);
                INI_WriteInt("Score",1);
                INI_WriteInt("Cash",5000);
                INI_WriteInt("Admin",0);

                INI_Save();
                INI_Close();

                ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""WHITE"Login",""WHITE"Type your password below to login.","Login","Quit");
            }
        }
    }
        case DIALOG_LOGIN:
        {
            if ( !response ) return Kick ( playerid );
            if( response )
            {
                if(!strlen(inputtext)) {
                ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, ""WHITE"Login",""RED"You have entered an invalid password.\n"WHITE"Type your password below to login.","Login","Quit");
                }

                if(INI_Open(getINI(playerid))) {
                INI_ReadString(PlayerInfo[playerid][pPass],"Password",20);

                if(strcmp(inputtext,PlayerInfo[playerid][pPass],false)) {
                ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, ""WHITE"Login",""RED"You have entered an incorrect password.\n"WHITE"Type your password below to login.","Login","Quit");
                }

                SetPlayerScore( playerid, INI_ReadInt("Score" ) );
                ResetPlayerMoney( playerid );
                GivePlayerMoney( playerid, INI_ReadInt( "Cash" ) );
                PlayerInfo[playerid][pAdmin] = INI_ReadInt("Admin");
                INI_Close();
                }
            }
        }
    }
    return 1;
}

case DIALOG_REGISTER:


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, other functions 'INI_WriteString' 'INI_WriteInt' can be called. It is then finished by the 'INI_Save' function which saves the values which has been set and/or strings which has been entered. Finally, 'INI_Close' closes the Userfile.

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

case DIALOG_LOGIN:

The only thing which is happening (with the exception of the Userfile being opened and closed) the 'INI_ReadInt' and 'INI_ReadString' function is called. When the player logs in, it loads the players 'stats' or so to say.

Example: If the player had, $1000 dollars before he disconnected, the function 'INI_ReadInt' would read that value, which would be called with GivePlayerMoney to load the player's Money value.


Step 10
Finally, we'll need to save the player stats, when he disconnects.


pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    if(INI_Open(getINI(playerid))) {
   
    INI_WriteInt("Score",PlayerInfo[playerid][pScore]);
    INI_WriteInt("Cash",GetPlayerMoney(playerid));
    INI_WriteInt("Admin",PlayerInfo[playerid][pAdmin]);

    INI_Save();
    INI_Close();
    }
    return 1;
}
I've chose the OnPlayerDisconnect callback to save player 'stats' because this creates MUCH less lag as opposed to the OnPlayerUpdate.


Downloads:


Pastebin - http://pastebin.com/kgvZGm3e


Credits:
[DRuG]Slick - SII Include
Las Venturas CNR - The man who showed me Dini was shit!

Last edited by Kush; 30/07/2011 at 05:48 PM.
Kush is offline   Reply With Quote
Old 10/07/2011, 12:24 AM   #2
Calgon
Banned
 
Join Date: Jan 2009
Posts: 6,130
Reputation: 1127
Default Re: Login and Register System - Dialogs - Using SII

Nice tutorial, you've done quite a good job of explaining the functions, but you should perhaps consider writing a tutorial on a more modern file-orient include, such as y_files, SII has been out of date for a while - and isn't as fast as it was when the speed tests were taken years ago, plus the last time I used SII, it had trouble saving strings that contained symbols or non-alphanumerical characters.

@ Las Venturas CNR: I'm presuming that's what he meant.
Calgon is offline   Reply With Quote
Old 10/07/2011, 12:31 AM   #3
Kush
Gangsta
 
Join Date: May 2011
Posts: 781
Reputation: 126
Default Re: Login and Register System - Dialogs - Using SII

Thanks for the feedback Calg00n, I'll soon be attempting to write a Tutorial on newer ones such as y_INI and might even attempt to take a look at MySQL a bit or two

Last edited by Kush; 10/07/2011 at 04:25 AM.
Kush is offline   Reply With Quote
Old 10/07/2011, 03:57 AM   #4
HyperZ
High-roller
 
HyperZ's Avatar
 
Join Date: Aug 2009
Location: Islamabad, Pakistan
Posts: 1,341
Reputation: 66
Default Re: Login and Register System - Dialogs - Using SII

Nice Tutorial.
__________________
HyperZ is offline   Reply With Quote
Old 10/07/2011, 04:50 AM   #5
Steven82
High-roller
 
Join Date: Nov 2009
Posts: 2,431
Reputation: 186
Default Re: Login and Register System - Dialogs - Using SII

Good job. I didn't think someone would of created one for SII. Guess i was wrong.
__________________
If you read my old posts; I apologize in advance.
Steven82 is offline   Reply With Quote
Old 10/07/2011, 06:22 AM   #6
dowster
Gangsta
 
dowster's Avatar
 
Join Date: Jun 2010
Location: San Fierro
Posts: 629
Reputation: 69
Default Re: Login and Register System - Dialogs - Using SII

Quote:
Originally Posted by Las Venturas CNR View Post
Nice tutorial, Yes, OnPlayerUpdate isn't too well of a saving method as it's called around 32 times a second.
A simple timer will do it though.
Honestly i never thought of a timer, if i made one for about every 2ish minutes how much lag would it cause for 500 players, or if i set one to loop through players and save. haven't worked much with timers yet.
__________________
dowster is offline   Reply With Quote
Old 10/07/2011, 07:36 AM   #7
Basicz
Gangsta
 
Basicz's Avatar
 
Join Date: Aug 2010
Location: Indonesia, Surabaya.
Posts: 605
Reputation: 35
Default Re: Login and Register System - Dialogs - Using SII

Great tutorial! Well explained, reputation ++;

But, did I found a mistake?

pawn Code:
PlayerInfo[playerid][pScore] = INI_ReadInt("Score")
You didn't use SetPlayerScore to set the player's score back, lol

pawn Code:
GivePlayerMoney(playerid, INI_ReadInt("Cash"));
Just an example, before we logged in, we hacked the money to $9999999, then we log in, we got another money too ...

So I think it would be

pawn Code:
GivePlayerMoney( playerid, - GetPlayerMoney( playerid ) + INI_ReadInt( "Cash" ) );

// Or

ResetPlayerMoney( playerid );

GivePlayerMoney( playerid, INI_ReadInt( "Cash" ) );
Basicz is offline   Reply With Quote
Old 10/07/2011, 09:44 AM   #8
Davz*|*Criss
Gangsta
 
Davz*|*Criss's Avatar
 
Join Date: Jan 2011
Posts: 778
Reputation: 15
Default Re: Login and Register System - Dialogs - Using SII

Very good tutorial: Good work mate
__________________
Join Extreme Missions Now: 176.31.192.96:7778!
Davz*|*Criss is offline   Reply With Quote
Old 10/07/2011, 02:52 PM   #9
Kush
Gangsta
 
Join Date: May 2011
Posts: 781
Reputation: 126
Default Re: Login and Register System - Dialogs - Using SII

Quote:
Originally Posted by Basicz View Post
Great tutorial! Well explained, reputation ++;

But, did I found a mistake?

pawn Code:
PlayerInfo[playerid][pScore] = INI_ReadInt("Score")
You didn't use SetPlayerScore to set the player's score back, lol

pawn Code:
GivePlayerMoney(playerid, INI_ReadInt("Cash"));
Just an example, before we logged in, we hacked the money to $9999999, then we log in, we got another money too ...

So I think it would be

pawn Code:
GivePlayerMoney( playerid, - GetPlayerMoney( playerid ) + INI_ReadInt( "Cash" ) );

// Or

ResetPlayerMoney( playerid );

GivePlayerMoney( playerid, INI_ReadInt( "Cash" ) );
Your right, I apologize for my inaccuracy. Thanks mate, fixed..
__________________
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 11/07/2011, 04:12 PM   #10
Coffeemonster
Big Clucker
 
Join Date: Nov 2010
Posts: 143
Reputation: 2
Default Re: Login and Register System - Dialogs - Using SII

Will there be y_ini version?
Coffeemonster 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
[FilterScript] [FS] ERegLog ( Advanced Register/Login System [Dialogs] ) aircombat Filterscripts 29 09/11/2014 09:30 PM
[FilterScript] Register/login system (Dini, dialogs) pantelimonfl Filterscripts 6 08/03/2011 04:13 PM
/register /login dialogs geerdinho8 Help Archive 23 01/05/2010 02:31 PM
[Help] Register/Login Dialogs andershh Help Archive 0 20/04/2010 07:09 PM


All times are GMT. The time now is 12:32 AM.


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