[Tutorial] Login/Register System with dialog
#1

Introduction

Hi guys. This is my other tutorial. This will show you how to use and create a dialog login/register and admin system. Dialog systems are much better and harder than a simple /login & /register commands. For these dialogs, I have used the slow DINI. Now, I have heard that this job can be done even with Y_INI, but this is a DINI TuT, so you have to have these includes :

pawn Код:
#include <a_samp>
#include <dutils>
#include <Dini>
dutils is needed just for one simple step, so be sure to download it. Now, let's begin.

Full defines and includes

Now, we will need some other defines before we begin. First get some color defines. You can get them here. Now, define the new PlayerInfo. We need the player's money, admin level, ig level AND his password.

pawn Код:
enum pInfo {
    AdminLevel,
    level,
    cash,
    pw,
}
new PlayerInfo[MAX_PLAYERS][pInfo];
The new PlayerInfo will add a needed variable. Now, if you got everything (includes, defines, etc...) we should proceed to the next step.

OnPlayerConnect

Now, the second step. Here, we will need all the information of the client that logs in. If the server has created a 'database file' of him, then the server will check if the file exists (login) or it should be created (register). For this we need the file, the client's name, and the location of the file.

pawn Код:
public OnPlayerConnect(playerid) {
    new name[MAX_PLAYER_NAME], file[128]; // the name and the file
    GetPlayerName(playerid, name, MAX_PLAYER_NAME); // getting client's name
    format(file, sizeof(file), ".../Users/%s.ini", name); // the location of the file
Now, here is the file checking part with two simple commands.

pawn Код:
if(!fexist(file)) { // if the file does not exists
        SendClientMessage(playerid, COLOR_YELLOW, "You are not registered, please register");
        ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Please Register", "Enter Your Password Below", "Register", "Cancel"); // this is the dialog type
    }
    else {
        new str[128]; // if the player IS registered
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        format(str, sizeof(str), "Welcome Back ~r~%s. Enjoy!", name);
        SendClientMessage(playerid, COLOR_YELLOW, str);
        ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Please Login", "Enter Your Password Below", "Login", "Cancel");
    }
    return 1;
}
Here, you can see we have the ShowPlayerDialog lines. Well, this is our dialog.

Код:
playerid	The ID of the player to show the dialog to.
dialogid	An ID to assign this dialog to, so responses can be processed. Max dialogid is 32767. Using negative values will close any open dialog.
style	The style of the dialog.
caption[]	The title at the top of the dialog. The length of the caption can not exceed more than 64 characters before it starts to cut off.
info[]	The text to display in the dialog. Use \n to start a new line and \t to tabulate.
button1[]	The text on the left button.
button2[]	The text on the right button. Leave it blank to hide it.
Now, he should remember the dialog id's. For the register command the dialog id is 1 and for the login 2. We have used here the DIALOG_STYLE_INPUT, because we have to input something into the dialog. In this case, the password.

OnPlayerDisconnect

Under OnPlayerDisconnect you should add the dini commands that will save his password, money, level, etc...

pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
    new file[128], name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    format(file, sizeof(file), ".../Users/%s.ini", name);
    if(dini_Exists(file)) {
        dini_IntSet(file, "pw", PlayerInfo[playerid][pw]);
        dini_IntSet(file, "AdminLevel", PlayerInfo[playerid][AdminLevel]);
        dini_IntSet(file, "cash", PlayerInfo[playerid][cash]);
        dini_IntSet(file, "level", PlayerInfo[playerid][level]);
    }
    return 1;
You can see the PlayerInfo helped us here.

Now, let's proceed to dialogs.

OnDialogRespone

Here, we have to do all the job. First, let's begin with the dialog's id. The register dialog id is 1, so let's begin with this.

pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == 1) {
        new file[128], name[MAX_PLAYER_NAME], str[128];
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        format(file, sizeof(file), ".../Users/%s.ini", name);
Same thing. We should check the User folder, player's name etc... Now, you can see that if(dialogid == 1) checks the dialogid (in this case 1). If the player responds with typing his password, then we give him a response too. If he types his password we have to create the new file and dini_IntSet all the parameters (level, pw, admin level...).

pawn Код:
if(dialogid == 1) {
        new file[128], name[MAX_PLAYER_NAME], str[128];
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        format(file, sizeof(file), ".../Users/%s.ini", name);
        if(response) {
            if(strlen(inputtext)) {
                dini_Create(file);
                dini_IntSet(file, "pw", num_hash(inputtext));
                dini_IntSet(file, "AdminLevel", PlayerInfo[playerid][AdminLevel]);
                dini_IntSet(file, "cash", PlayerInfo[playerid][cash]);
                dini_IntSet(file, "level", PlayerInfo[playerid][level]);
                format(str, sizeof(str), "You are registered as ~r~%s. Your password is ~r~%s. /changepass to change it", name, inputtext);
                SendClientMessage(playerid, COLOR_SYSTEM, str);
                PlayerInfo[playerid][level] = dini_Int(file, "level");
                PlayerInfo[playerid][cash] = dini_Int(file, "cash");
                PlayerInfo[playerid][AdminLevel] = dini_Int(file, "AdminLevel");
            }
        }
        else {
            Kick(playerid);
        }
    }
Now, everything we have done is checking this client's parameters and kicking him if he kicks the "Cancel" button. Let's proceed now with the login dialog, which's id was 2. We have to check if his password is wrong or right. We'll need the dutils include for this. Now, the same thing as we done before:

pawn Код:
if(dialogid == 2) {
        new file[128], name[MAX_PLAYER_NAME], str[128];
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        format(str, sizeof(str), ".../Users/%s.ini", name);
        if(response) {
            if(strlen(inputtext)) {
Now, a line will check if his password is wrong or not

pawn Код:
if(num_hash(inputtext) != dini_Int(file, "pw")) {
This is very much needed because without it players can log even with another's password. There's only the left part of the command now and it should be easy to script it with dini.

pawn Код:
if(dialogid == 2) {
        new file[128], name[MAX_PLAYER_NAME], str[128];
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        format(str, sizeof(str), ".../Users/%s.ini", name);
        if(response) {
            if(strlen(inputtext)) {
                if(num_hash(inputtext) != dini_Int(file, "pw")) {
                    SendClientMessage(playerid, COLOR_SYSTEM, "Wrong Password");
                    ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Please Login", "Enter Your Password Below", "Login", "Cancel");
                }
                else {
                    SendClientMessage(playerid, COLOR_SYSTEM, "Succesfuly logged in");
                    PlayerInfo[playerid][level] = dini_Int(file, "level");
                    PlayerInfo[playerid][cash] = dini_Int(file, "cash");
                    GivePlayerMoney(playerid, dini_Int(file, "cash"));
                    PlayerInfo[playerid][AdminLevel] = dini_Int(file, "AdminLevel");
                }
            }
        }
        else {
            Kick(playerid);
        }
Conclusion

Last, I wanted to say that it's better to use this system, as it's more professional and it's mostly used these days. For lazy guys who want a CTRL + C and CTRL + V I am adding ALL what I have done here:

pawn Код:
public OnPlayerConnect(playerid)
{
    SendClientMessage(playerid, COLOR_BRIGHTRED, "Welcome to UK-DM");
    SendClientMessage(playerid, COLOR_BRIGHTRED, "Hope you enjoy!");
    SendClientMessage(playerid, COLOR_BRIGHTRED, "Type /commands for a list of commands");
    new name[MAX_PLAYER_NAME], file[128];
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    format(file, sizeof(file), ".../Users/%s.ini", name);
    if(!fexist(file)) {
        SendClientMessage(playerid, COLOR_YELLOW, "You are not registered, please register");
        ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Please Register", "Enter Your Password Below", "Register", "Cancel");
    }
    else {
        new str[128];
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        format(str, sizeof(str), "Welcome Back ~r~%s. Enjoy!", name);
        SendClientMessage(playerid, COLOR_YELLOW, str);
        ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Please Login", "Enter Your Password Below", "Login", "Cancel");
    }
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    new file[128], name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    format(file, sizeof(file), ".../Users/%s.ini", name);
    if(dini_Exists(file)) {
        dini_IntSet(file, "pw", PlayerInfo[playerid][pw]);
        dini_IntSet(file, "AdminLevel", PlayerInfo[playerid][AdminLevel]);
        dini_IntSet(file, "cash", PlayerInfo[playerid][cash]);
        dini_IntSet(file, "level", PlayerInfo[playerid][level]);
    }
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == 1) {
        new file[128], name[MAX_PLAYER_NAME], str[128];
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        format(file, sizeof(file), ".../Users/%s.ini", name);
        if(response) {
            if(strlen(inputtext)) {
                dini_Create(file);
                dini_IntSet(file, "pw", num_hash(inputtext));
                dini_IntSet(file, "AdminLevel", PlayerInfo[playerid][AdminLevel]);
                dini_IntSet(file, "cash", PlayerInfo[playerid][cash]);
                dini_IntSet(file, "level", PlayerInfo[playerid][level]);
                format(str, sizeof(str), "You are registered as ~r~%s. Your password is ~r~%s. /changepass to change it", name, inputtext);
                SendClientMessage(playerid, COLOR_SYSTEM, str);
                PlayerInfo[playerid][level] = dini_Int(file, "level");
                PlayerInfo[playerid][cash] = dini_Int(file, "cash");
                PlayerInfo[playerid][AdminLevel] = dini_Int(file, "AdminLevel");
            }
        }
        else {
            Kick(playerid);
        }
    }
    if(dialogid == 2) {
        new file[128], name[MAX_PLAYER_NAME], str[128];
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        format(str, sizeof(str), ".../Users/%s.ini", name);
        if(response) {
            if(strlen(inputtext)) {
                if(num_hash(inputtext) != dini_Int(file, "pw")) {
                    SendClientMessage(playerid, COLOR_SYSTEM, "Wrong Password");
                    ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Please Login", "Enter Your Password Below", "Login", "Cancel");
                }
                else {
                    SendClientMessage(playerid, COLOR_SYSTEM, "Succesfuly logged in");
                    PlayerInfo[playerid][level] = dini_Int(file, "level");
                    PlayerInfo[playerid][cash] = dini_Int(file, "cash");
                    GivePlayerMoney(playerid, dini_Int(file, "cash"));
                    PlayerInfo[playerid][AdminLevel] = dini_Int(file, "AdminLevel");
                }
            }
        }
        else {
            Kick(playerid);
        }
    }
    return 1;
}
P.s.: Just for more professionalism, add this line to avoid warnings:

pawn Код:
#pragma unused ret_memcpy
Cya' to my next TuT ! Please rate & comment

The End
Reply


Messages In This Thread
Login/Register System with dialog - by Rivera - 28.04.2011, 11:53
Re: Login/Register System with dialog - by HostiGame - 29.04.2011, 07:26
Re: Login/Register System with dialog - by Lorenc_ - 29.04.2011, 08:23
Re: Login/Register System with dialog - by Max_Coldheart - 29.04.2011, 12:28
Re: Login/Register System with dialog - by Rivera - 29.04.2011, 17:28
Re: Login/Register System with dialog - by MJ! - 30.04.2011, 07:45
Respuesta: Re: Login/Register System with dialog - by kirk - 30.04.2011, 13:28
Re: Login/Register System with dialog - by Rivera - 30.04.2011, 19:50
Re: Login/Register System with dialog - by Jmarr - 01.05.2011, 00:08
Re: Login/Register System with dialog - by Rivera - 02.05.2011, 17:00
Respuesta: Re: Login/Register System with dialog - by kirk - 02.05.2011, 17:59
Re: Login/Register System with dialog - by Rivera - 02.05.2011, 19:15
Re: Login/Register System with dialog - by Manvydas - 31.03.2012, 17:07
Re : Login/Register System with dialog - by mehdi-jumper - 31.03.2012, 18:22
Re: Login/Register System with dialog - by Djumza - 12.04.2012, 19:15
Re: Login/Register System with dialog - by Sasino97 - 13.04.2012, 16:40
Re: Login/Register System with dialog - by MarTaTa - 13.04.2012, 16:49
Re: Login/Register System with dialog - by Sasino97 - 13.04.2012, 16:57
Re: Login/Register System with dialog - by newbienoob - 14.04.2012, 08:56
Re: Login/Register System with dialog - by itrenor - 17.04.2012, 20:04
Re: Login/Register System with dialog - by Flashhiee - 01.05.2012, 15:50
Re: Login/Register System with dialog - by Rivera - 21.07.2012, 10:00
Re: Login/Register System with dialog - by Goldilox - 21.07.2012, 20:48
Re: Login/Register System with dialog - by Karl[NDZ] - 22.07.2012, 15:31
Re: Login/Register System with dialog - by zedshadowzw - 07.05.2017, 03:30
Re: Login/Register System with dialog - by Lamy - 07.05.2017, 09:49
Re: Login/Register System with dialog - by coool - 07.05.2017, 09:54
Re: Login/Register System with dialog - by zedshadowzw - 07.05.2017, 12:24
Re: Login/Register System with dialog - by zedshadowzw - 09.05.2017, 01:10
Re: Login/Register System with dialog - by organe. - 11.02.2019, 19:30

Forum Jump:


Users browsing this thread: 4 Guest(s)