[Tutorial] A simple registration & login system with Dialogs - Using DOF2
#1

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 Код:
    #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 Код:
    #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 Код:
    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 Код:
    #define USER_FILE_PATH  "%s.ini"
    to your directory.
    pawn Код:
    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 Код:
    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 Код:
    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 Код:
    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 Код:
    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 Код:
    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 Код:
    DOF2_Exit();
    to your "OnFilterScriptExit" callback or to "OnGameModeExit" callback if it's a gamemode:
    pawn Код:
    public OnFilterScriptExit()
    {
        DOF2_Exit();
        return 1;
    }
    So you don't get the warning:
    Код:
     symbol is never used: "DOF2_Exit"
Download
Credits
Reply
#2

Good Tutorial
Reply
#3

Nice!
The first tut using DOF2

Edit: Oh, not the first but still nice!
Reply
#4

Good Job SmiT
Reply
#5

This tuturoiol will help me a lot thanks.

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

Thanks for the comments.
Reply
#7

awesome, thx!
Reply
#8

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.
Reply
#9

Really nice, good tutorial.
Reply
#10

Good
Reply
#11

"DOF2" Sucks Balls! I hate this Mother Fucking ShiT! Man! Its very bad!
Reply
#12

Code:
if(DOF2_GetString(password, "Password"))
Code:
error 033: array must be indexed (variable "DOF2_GetString")
How to fix this?
Reply
#13

Smit i cant open that dof2 site
Reply
#14

Indeed, this is really helpfull. Thanks!
Reply
#15

Nice work,congratulations.
Reply
#16

Here at OnPlayerDeath you're incrementing the kills of playerid instead of killer id.

pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
    if( killerid != INVALID_PLAYER_ID )
    {
        PlayerInfo[ playerid ][ pKills ] ++;
    }
    PlayerInfo[ playerid ][ pDeaths ] ++;
    return 1;
}
Shouldn't that be like this:
pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
    if( killerid != INVALID_PLAYER_ID )
    {
        PlayerInfo[ killerid ][ pKills ] ++;
    }
    PlayerInfo[ playerid ][ pDeaths ] ++;
    return 1;
}
Reply
#17

Good Job, Helped me alot )
Reply
#18

if i just had 1 rup i will give it to you , the first and the last login / register tuto that helped me !
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)