[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
#2

Worked out good!

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

When i hit register the server crashes.
Reply
#3

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

Its a good tutorial, but you could use more updated .ini file saving system such as Y_ini to make this with.
Reply
#5

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
Reply
#6

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

pawn Код:
../Users/%s.ini
Reply
#7

Quote:
Originally Posted by MJ!
Посмотреть сообщение
First, you must have the folder Users.
Then, it's a mistake
pawn Код:
.../Users/%s.ini
It's .. , not ...

pawn Код:
../Users/%s.ini
Doesnt matter it works like this too

Код:
Users/%s.ini
I do use it like that, just create the folder Users.
Reply
#8

That's right. This is not a mistake it will be working in anytime. Just more professional
Reply
#9

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

'k Now

Quote:

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.

Go to your script folder, and create a new folder named 'Users' that's all

Quote:

You forgot to add this
pawn Code:
#pragma unused ret_memcpy
Not needed. It's just more professional 'cause without it you will get a warning...
Reply
#11

Quote:
Originally Posted by Jmarr
View Post
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.
Inside the scriptfile folder you create the folder called ''Users''
Reply
#12

Quote:

Inside the scriptfile folder you create the folder called ''Users''

You can create the folder anywhere.
Reply
#13

Very very thx tutorial 100procent working
Reply
#14

Good but with num_hash we can't see the password ^^
Reply
#15

Hello...
This is my problem :
When i start server, dialog showup, but when i type my password and press REGISTER, there is no Classes, cant spawn and 5 sec. after that server loses connection.. :/
Reply
#16

Quote:
Originally Posted by kirk
View Post
Doesnt matter it works like this too

Code:
Users/%s.ini
I do use it like that, just create the folder Users.
Quote:
Originally Posted by Jmarr
View Post
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.
The standard file functions provided with sa-mp server can't create Folders, you have to create them manually.
Reply
#17

Very nice but when I hit "Compile" It shows me some errors , which actually are the same but on diferent places so here is the error
Quote:

undefined symbol "AdminLevel"

P.S. I am using SAM[P]CE does it matter ?
Reply
#18

Quote:
Originally Posted by MarTaTa
View Post
Very nice but when I hit "Compile" It shows me some errors , which actually are the same but on diferent places so here is the error


P.S. I am using SAM[P]CE does it matter ?
In your code there's no variable defined called AdminLevel

And no, it doesn't matter what IDE you are using, the compiler is the same, pawncc
Reply
#19

It gave me an errors

Code:
C:\Users\abc123\Desktop\samp\gamemodes\loginreigster.pwn(103) : error 028: invalid subscript (not an array or too many subscripts): "pInfo"
C:\Users\abc123\Desktop\samp\gamemodes\loginreigster.pwn(103) : warning 215: expression has no effect
C:\Users\abc123\Desktop\samp\gamemodes\loginreigster.pwn(103) : error 001: expected token: ";", but found "]"
C:\Users\abc123\Desktop\samp\gamemodes\loginreigster.pwn(103) : error 029: invalid expression, assumed zero
C:\Users\abc123\Desktop\samp\gamemodes\loginreigster.pwn(103) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


4 Errors.
Code:
dini_IntSet(file, "pw", pInfo[playerid][pw]);  //error line
EDIT: FIXED !
Reply
#20

The code works perfectly except for one thing. I register in my server but it doesn't save the registration. If I try to log back in it says I'm not registered and I have to keep on registering and it doesn't save anything. Please. help?
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)