SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 28/04/2011, 11:53 AM   #1
Rivera
Big Clucker
 
Join Date: Feb 2011
Posts: 146
Reputation: 7
Default Login/Register System with dialog

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 Code:
#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 Code:
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 Code:
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 Code:
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.

Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
#pragma unused ret_memcpy

Cya' to my next TuT ! Please rate & comment

The End
__________________
My works

[Tutorial]Full usage of dcmd(updated)
[Tutorial]Login/Register System with dialogs


// Prefered Servers------------------------------

US-RP.INFO / Best Roleplay Server / 213.5.176.156:7777

^^__^^

Last edited by Rivera; 02/05/2011 at 08:04 PM.
Rivera is offline   Reply With Quote
Old 29/04/2011, 07:26 AM   #2
HostiGame
Little Clucker
 
HostiGame's Avatar
 
Join Date: Jul 2010
Posts: 10
Reputation: 0
Default Re: Login/Register System with dialog

Worked out good!

PS: you forgot #include <dini> :P You're welcome.

When i hit register the server crashes.
HostiGame is offline   Reply With Quote
Old 29/04/2011, 08:23 AM   #3
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 4,120
Reputation: 901
Default Re: Login/Register System with dialog

Nice although DINI is so old, you're better of having a new system then that. Well Old and Slow = rubbish. Not saying you're TUT is rubbish but i suggest you're Next TUT thats about registering, maybe y_ini?

Anyway once again nice.
__________________

192.169.82.202:7777 - An awesome CnR server consisting of doozy features! Come, we have players!


Lorenc_ is offline   Reply With Quote
Old 29/04/2011, 12:28 PM   #4
Max_Coldheart
Guest
 
Posts: n/a
Default Re: Login/Register System with dialog

Its a good tutorial, but you could use more updated .ini file saving system such as Y_ini to make this with.
  Reply With Quote
Old 29/04/2011, 05:28 PM   #5
Rivera
Big Clucker
 
Join Date: Feb 2011
Posts: 146
Reputation: 7
Default Re: Login/Register System with dialog

Yeah I know I can use INI_Load or w/e but I like the classic way

Quote:
When i hit register the server crashes.
Never tested it, but it's surely your problem, as many guys told me it works
__________________
My works

[Tutorial]Full usage of dcmd(updated)
[Tutorial]Login/Register System with dialogs


// Prefered Servers------------------------------

US-RP.INFO / Best Roleplay Server / 213.5.176.156:7777

^^__^^
Rivera is offline   Reply With Quote
Old 30/04/2011, 07:45 AM   #6
MJ!
Huge Clucker
 
Join Date: Nov 2009
Posts: 214
Reputation: 0
Default Re: Login/Register System with dialog

First, you must have the folder Users.
Then, it's a mistake
pawn Code:
.../Users/%s.ini
It's .. , not ...

pawn Code:
../Users/%s.ini
MJ! is offline   Reply With Quote
Old 30/04/2011, 01:28 PM   #7
kirk
Big Clucker
 
Join Date: Oct 2010
Posts: 193
Reputation: 59
Default Respuesta: Re: Login/Register System with dialog

Quote:
Originally Posted by MJ! View Post
First, you must have the folder Users.
Then, it's a mistake
pawn Code:
.../Users/%s.ini
It's .. , not ...

pawn Code:
../Users/%s.ini
Doesnt matter it works like this too

Code:
Users/%s.ini
I do use it like that, just create the folder Users.
__________________
kirk is offline   Reply With Quote
Old 30/04/2011, 07:50 PM   #8
Rivera
Big Clucker
 
Join Date: Feb 2011
Posts: 146
Reputation: 7
Default Re: Login/Register System with dialog

That's right. This is not a mistake it will be working in anytime. Just more professional
__________________
My works

[Tutorial]Full usage of dcmd(updated)
[Tutorial]Login/Register System with dialogs


// Prefered Servers------------------------------

US-RP.INFO / Best Roleplay Server / 213.5.176.156:7777

^^__^^
Rivera is offline   Reply With Quote
Old 01/05/2011, 12:08 AM   #9
Jmarr
Big Clucker
 
Join Date: Jun 2010
Location: Canada
Posts: 51
Reputation: 0
Default Re: Login/Register System with dialog

Where do I create folder "Users"? I tried registering but no couldn't find a Users folder or a file with my name on it.
__________________



Beginner Pawner
Jmarr is offline   Reply With Quote
Old 02/05/2011, 12:13 PM   #10
bogdan.trial
Little Clucker
 
Join Date: May 2010
Posts: 13
Reputation: 0
Default Re: Login/Register System with dialog

You forgot to add this
Code:
#pragma unused ret_memcpy
bogdan.trial 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] Dialog Register/Login System using DJson sekol Tutorials 12 20/09/2012 08:21 PM
Login/Register System with dialog Rivera Help Archive 3 27/04/2011 12:56 PM
[Help] Register/Login System with DIALOG BMGP Help Archive 5 14/12/2010 06:34 PM
How to make login/register system with DIALOG? juseong111 Help Archive 3 20/10/2009 01:20 PM


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


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