SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 15/10/2011, 11:02 AM   #1
SmiT
Huge Clucker
 
Join Date: Oct 2011
Posts: 352
Reputation: 63
Default A simple registration & login system with Dialogs - Using DOF2

Introduction
  • I'll show you how to create a simple registration and login system with dialogs, using DOF2.

Double-O-Files 2

Download
  • Download Double-O-Files 2.
  • Once downloaded, Copy/Cut "Double-O-Files_2.inc" in your ../pawno/include folder.

Step 1
  • We need to include the file, so at the top of our script:
    pawn Code:
    #include <Double-O-Files_2>
    This will load all the codes from "../pawno/include/Double-O-Files_2.inc" into your script, that's what we need so we can create our system.

Step 2
  • Let's also define some Dialog ID's and colors:
    pawn Code:
    #define DIALOG_REGISTER 1
    #define DIALOG_LOGIN 2

    #define WHITE "{FFFFFF}"
    #define RED "{F81414}"
    #define GREEN "{00FF22}"

Step 3
  • Now, we need to store our variables in an enum.
  • Enumerations are a very useful system for representing large groups of data and modifying constants quickly. There are a few main uses - replacing large sets of define statements, symbolically representing array slots (these are actually the same thing but they look different) and creating new tags.
    pawn Code:
    enum P_ENUM
    {
        pKills,
        pDeaths,
        pMoney,
        pAdmin
    }
    new PlayerInfo[ MAX_PLAYERS ][ P_ENUM ];

Step 4
  • Let's create a simple stock function to return the path were our accounts will be saved, in this case: "../scriptfiles".
  • You can change the accounts path by opening "Double-O-Files_2.inc" and changing
    pawn Code:
    #define USER_FILE_PATH  "%s.ini"
    to your directory.
    pawn Code:
    stock USER_FILE(playerid)
    {
        new
            STR[ 128 ],
            P_NAME[ MAX_PLAYER_NAME ];
           
        GetPlayerName( playerid, P_NAME,  sizeof ( P_NAME ) );
        format( STR, sizeof ( STR ), USER_FILE_PATH, P_NAME);
        return
            STR;
    }

Step 5
  • Let's create a simple stock function to load the player stats.
    pawn Code:
    stock Load_Player_Stats(playerid)
    {
        PlayerInfo[ playerid ][ pKills ] = DOF2_GetInt( USER_FILE( playerid ),"Kills");
        PlayerInfo[ playerid ][ pDeaths ] = DOF2_GetInt( USER_FILE( playerid ),"Deaths");
        PlayerInfo[ playerid ][ pMoney ] = DOF2_GetInt( USER_FILE( playerid ),"Money");
        PlayerInfo[ playerid ][ pAdmin ] = DOF2_GetInt( USER_FILE( playerid ),"AdminLevel");
        GivePlayerMoney(playerid, PlayerInfo[ playerid ][ pMoney ]);
    }

Step 6
  • Let's scroll down to "OnPlayerConnect" callback, we must check when a player connects to the server, if he is registered or not:
    pawn Code:
    public OnPlayerConnect(playerid)
    {
        if( DOF2_FileExists ( USER_FILE ( playerid ) ) )
        {
            ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,"Welcome.Please log-in",""WHITE"Type your "GREEN"password "WHITE"here to log-in",#Log-in,#Quit);
        }  
        else
        {
            ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT,"Please register!",""WHITE"Type your "GREEN"password "WHITE"here to register.",#Register,#Quit);
        }
        return true;
    }
  • DOF2_FileExists will check if the player file exist. We use our stock as the parameter. If the file exist, the player will be promted to a dialog telling him to login. "Else" if the player file don't exist, he will be promted to another dialog telling him to register.

Step 7
  • Let's scroll down to "OnDialogResponse" callback.
    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_LOGIN, DIALOG_STYLE_INPUT,""WHITE"Welcome.Please log-in","You have entered an "RED"invalid"WHITE" password\n"WHITE"Type your "GREEN"password "WHITE"here to log-in",#Register,#Quit);
                    DOF2_CreateFile( USER_FILE ( playerid ), inputtext );
                    DOF2_SetInt( USER_FILE ( playerid ), "Kills", 0);
                    DOF2_SetInt( USER_FILE ( playerid ), "Deaths", 0);
                    DOF2_SetInt( USER_FILE ( playerid ), "Money", 1000);
                    DOF2_SetInt( USER_FILE ( playerid ), "AdminLevel", 0);
                    DOF2_SaveFile();
                    SetSpawnInfo( playerid, 0, 0, 1958.33, 1343.12, 15.36, 269.15, 0, 0, 0, 0, 0, 0 );
                    SpawnPlayer( playerid );
                    GivePlayerMoney(playerid, 1000);
                }
            }
            case DIALOG_LOGIN:
            {
                if ( !response ) return Kick( playerid );
                if( response )
                {
                    if( DOF2_CheckLogin( USER_FILE( playerid ), inputtext ) )  
                    {
                        Load_Player_Stats(playerid);
                    }
                    else
                    {
                        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""WHITE"Login","You have entered an "RED"incorrect "WHITE"password.\n{FFFFFF}Type your "GREEN"password "WHITE"below to login.",#Log-in,#Quit);
                    }
                    return 1;
                }
            }
        }
        return 1;
    }
  • Instead of using "if" statements to check the dialogs, I used cases as they are faster. We swtich through all our dialogs, we check if "DIALOG_REGISTER" appears, the " if ( !response ) " checks if the player press the second button (Quit) if so it will kick the player. We check if the player press the first button (Register) using " if ( response ) ". The " if ( !strlen ( inputtext ) ) " will check if NOTHING has been entered in the dialog (e.g player connects and just press "Register") if so, you will be prompted to another dialog telling you the password is INCORRECT.
  • DOF2_CreateFile will create the file for the player, also create the password for him and hash it. Then we write into the file some integers using DOF2_SetInt, after that we save the file using DOF2_SaveFile(). We set the player spawn, spawn him and give him $1000.
  • Same to DIALOG_LOGIN. DOF2_CheckLogin will check if the player enteres the correct password, if so it will load the player stats using our stock. "Else" if he enteres the wrong password, he will be promted to another dialog telling him the password is incorrect.

Step 8
  • After the player disconnects, we need to write and save the player stats, so let's scroll down to "OnPlayerDisconnect" callback.
    pawn Code:
    public OnPlayerDisconnect(playerid, reason)
    {
        DOF2_SetInt( USER_FILE ( playerid ), "Kills", PlayerInfo[ playerid ][ pKills ]);
        DOF2_SetInt( USER_FILE ( playerid ), "Deaths", PlayerInfo[ playerid ][ pDeaths ]);
        DOF2_SetInt( USER_FILE ( playerid ), "Money", GetPlayerMoney( playerid ));
        DOF2_SetInt( USER_FILE ( playerid ), "AdminLevel", PlayerInfo[ playerid ][ pAdmin ]);
        DOF2_SaveFile();
        return 1;
    }

Step 9
  • We need to add values to the player "Kills" and "Deaths" so we scroll down to "OnPlayerDeath" callback.
    pawn Code:
    public OnPlayerDeath(playerid, killerid, reason)
    {
        if( killerid != INVALID_PLAYER_ID )
        {
            PlayerInfo[ playerid ][ pKills ] ++;
        }
        PlayerInfo[ playerid ][ pDeaths ] ++;
        return 1;
    }
  • Killerid is the one who killed the person. Playerid is the one who dies. We check if killerid isn't equal to INVALID_PLAYER_ID. We increase the killerid kills, and we increase the playerid deaths.

Final Step
  • Add
    pawn Code:
    DOF2_Exit();
    to your "OnFilterScriptExit" callback or to "OnGameModeExit" callback if it's a gamemode:
    pawn Code:
    public OnFilterScriptExit()
    {
        DOF2_Exit();
        return 1;
    }
    So you don't get the warning:
    Code:
     symbol is never used: "DOF2_Exit"

Download

Credits
Attached Files
File Type: rar Dof2.rar (11.4 KB, 381 views)

Last edited by SmiT; 29/10/2011 at 09:02 PM.
SmiT is offline   Reply With Quote
Old 15/10/2011, 01:48 PM   #2
Mr_Scripter
Huge Clucker
 
Mr_Scripter's Avatar
 
Join Date: Mar 2011
Posts: 370
Reputation: 33
Default Re: A simple registration & login system with Dialogs - Using DOF2

Good Tutorial
__________________

Current Project: Stunt-DM-Drift-Race.
Done: 0%
Looking for 2Mappers... Pm Me

Gamemodes
SanAndreas Team DeathMatch Gamemode-4 hours work

Last edited by Mr_Scripter; 20/10/2011 at 03:49 PM.
Mr_Scripter is offline   Reply With Quote
Old 20/10/2011, 03:09 PM   #3
Jantjuh
Gangsta
 
Jantjuh's Avatar
 
Join Date: Jul 2010
Location: Bailando
Posts: 579
Reputation: 742
Default Re: A simple registration & login system with Dialogs - Using DOF2

Nice!
The first tut using DOF2

Edit: Oh, not the first but still nice!

Last edited by Jantjuh; 21/10/2011 at 10:12 AM.
Jantjuh is offline   Reply With Quote
Old 20/10/2011, 03:11 PM   #4
BetaLaxx
Big Clucker
 
BetaLaxx's Avatar
 
Join Date: Sep 2011
Location: Oman.
Posts: 125
Reputation: 13
Default Re: A simple registration & login system with Dialogs - Using DOF2

Good Job SmiT
__________________
Are you bored?
BetaLaxx is offline   Reply With Quote
Old 22/10/2011, 10:25 AM   #5
lordturhan
Huge Clucker
 
lordturhan's Avatar
 
Join Date: Oct 2010
Posts: 314
Reputation: 10
Default Re: A simple registration & login system with Dialogs - Using DOF2

This tuturoiol will help me a lot thanks.

Edit:Also Please can you show how to make more commands.

Last edited by lordturhan; 22/10/2011 at 11:06 AM.
lordturhan is offline   Reply With Quote
Old 23/10/2011, 12:20 PM   #6
SmiT
Huge Clucker
 
Join Date: Oct 2011
Posts: 352
Reputation: 63
Default Re: A simple registration & login system with Dialogs - Using DOF2

Thanks for the comments.
SmiT is offline   Reply With Quote
Old 05/11/2011, 10:27 PM   #7
Dredd
Huge Clucker
 
Dredd's Avatar
 
Join Date: Oct 2011
Posts: 431
Reputation: 468
Default Re: A simple registration & login system with Dialogs - Using DOF2

awesome, thx!
Dredd is offline   Reply With Quote
Old 14/01/2012, 09:28 PM   #8
bestr32
Big Clucker
 
Join Date: Mar 2011
Location: iLegaL.exe
Posts: 126
Reputation: 4
Default Re: A simple registration & login system with Dialogs - Using DOF2

Very good, thanks it really helped me for making my server(My GM). But I got 1 bug. When I registered, logging in didn't pop-up so I couldn't change skins than I just quited and logged in.... I don't know what was that.
bestr32 is offline   Reply With Quote
Old 14/01/2012, 09:31 PM   #9
Brian_G
Big Clucker
 
Brian_G's Avatar
 
Join Date: Jun 2010
Posts: 84
Reputation: 22
Default Re: A simple registration & login system with Dialogs - Using DOF2

Really nice, good tutorial.
__________________
Forums:
SFCNR Image Hosting:
Brian_G is offline   Reply With Quote
Old 13/02/2012, 03:17 PM   #10
Nezinuu
Little Clucker
 
Join Date: Feb 2012
Posts: 14
Reputation: 0
Default Re: A simple registration & login system with Dialogs - Using DOF2

Good
Nezinuu 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] Register & Login System with Dialogs - DOF2 Jafet_Macario Tutorials 33 21/08/2018 09:10 AM
I need help with a registration/login system. omgkillerzambies Help Archive 9 24/06/2011 05:12 AM
Login & Registration system wrong BizzyD Help Archive 4 25/03/2011 09:29 PM
[FilterScript] [FS] MafiaGuy's Registration System (DINI/INI - Dialogs) MafiaGuy™ Filterscripts 9 25/04/2010 01:40 AM


All times are GMT. The time now is 06:07 PM.


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