[Tutorial] Dialog Register/Login System using DJson
#1

Hello. It's my 2nd tutorial and I want to show you how to make register/login system using DJson. I know there's a lot of Tutorials like this, but i want to leave something other than Offtop on this forum
We'll need:
DJson by DracoBlue
dudb by Dracoblue

We'll begin with defining dialog IDs and color. So at top of your script we should put:
pawn Код:
#include <a_samp>
#include <djson>
#include <dudb>
#define COLOR_WHITE             0xFFFFFFFF
#define DIALOG_REGISTER         1
#define DIALOG_LOGIN            2
I think everyone who looks at this topic knows what above lines do
Now lets head to OnGameModeInit()
pawn Код:
new str[128], accname[128];
format(accname, sizeof(accname), "Accounts/%s", GetPlayerName(playerid));
    if(!fexist(accname))
    {
        format(str, 128, "{FFFFFF}Welcome {FBFF00}%s \n\n{FFFFFF}Please Register!", GetName(playerid));
        ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "Registration", str, "Register", "Quit");
    }
    if(fexist(accname))
    {
        format(str, 128, "{FFFFFF}Welcome\nAccount {FBFF00} %s\n{FFFFFF}Please Log In!", GetName(playerid));
        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Log In!", str, "Login", "Quit");
    }
Now let me explain lines, that may confuse you
format(accname, sizeof(accname), "%s", GetName(playerid)); - Allows us to read data from "Accounts" folder.
if(!fexist(accname)) - Checks if there's no player file with that name already.
Rest should be pretty simple to understand

Now lets take care of dialogs. It's the most important part of the script!

Just under OnDialogResponse put this code!
pawn Код:
new accname[128], str[128];
format(accname, sizeof(accname), "Accoutns/%s", GetPlayerName(playerid));
if(dialogid == DIALOG_REGISTER)
    {
        if(!response) return Kick(playerid);
        if(response)
        {
            if(strlen(inputtext) == 0)
            {
                format(str, 128, "{FFFFFF}Welcome {FBFF00}%s \n\n{FFFFFF}Please Register!", GetName(playerid));
        ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "Registration", str, "Register",
                return 0;
            }
            if(!fexist(accname))
            {
                djCreateFile(accname);
                djSetInt(accname,"player/password",udb_hash(inputtext),false);
                djCommit(accname);
                                format(str, 128, "{FFFFFF}Welcome\nAccount {FBFF00} %s\n{FFFFFF}Please Log In!", GetName(playerid));
                        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Log In!", str, "Login", "Quit");
            }
        }
    }
This one was for registration. I'll explain some lines.
if(!response) return Kick(playerid); - If player selects "Quit" it will kick this player.
if(strlen(inputtext) == 0) - This checks if player left the password input place blank.
djCreateFile(accname); - It creates player file in your "Accounts" folder
djSetInt(accname,"Data/Password",udb_hash(inputtext),false); - It sets the password and hashes it so noone can read it!
djCommit(accname); - This line commits changes made to the player file.

Now let's make loging in! Put this code under your register dialog
pawn Код:
if(dialogid == DIALOG_LOGIN)
    {
        if(!response) return Kick(playerid);
        if(response)
        {
            if(strlen(inputtext) == 0)
            {
                format(str, 128, "{FFFFFF}Welcome\nAccount {FBFF00} %s\n{FFFFFF}Please Log In!", GetName(playerid));
                ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Log In!", str, "Login", "Quit");
                return 0;
            }
            if(fexist(accname))
            {
                new password = djInt(accname,"player/password");
                if(udb_hash(inputtext) != password)
                {
                    format(str, 128, "{FFFFFF}Welcome\nAccount {FBFF00} %s\n{FFFFFF}Please Log In!", GetName(playerid));
                    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Log In!", str, "Login", "Quit");
                }
                else
                {
                    SendClientMessage(playerid, colorGreen, "You have successfully logged in.");
                    SpawnPlayer(playerid);
                    //Put here for example restoring player money after reloging :D
                }
            }
        }
    }
    return 1;
}
I don't have to explain anything here, because i explained it before.
And that's all, you can enjoy your register/login system!
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)