SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 16/07/2011, 09:29 AM   #1
Seven_of_Nine
Huge Clucker
 
Seven_of_Nine's Avatar
 
Join Date: Feb 2011
Location: Hungary
Posts: 372
Reputation: 12
Default Login & Register system - dialogs - with y_ini

Hello!

This is my first tutorial. Let's start off.

Requirements:
  • Basic scripting knowledge
  • y_ini by ******
  • dudb & dutils by Dracoblue

Step 1
Including, definining.
Make sure you have these included:
pawn Code:
#include <YSI\y_ini>
#include <dudb>

//Path & options
#define PATH "/Users/%s.ini" //This is the path template

#define REGISTERED_MONEY 5000 //How much money registered users get
#define AUTOLOGIN //Delete this line if you don't want autologin
#define DIALOG_REGISTER 999 //Register dialog ID, feel free to modfiy
#define DIALOG_LOGIN 998 //Login dialog ID, feel free to modfiy

//Additional colors
#define white 0xFFFFFFA
#define red 0xFF0000AA
#define lime 0x00FF00FF
#define yellow 0xFFFF00FF
We'll need these later.

Step 2
Variables, enumerating and a stock function.
pawn Code:
enum iDetails { //Enumerating player data
    Pass,
    Cash,
    Score,
    Banned
};
new pInfo[MAX_PLAYERS][iDetails]; //Player data variable

new pIP[MAX_PLAYERS][16]; //For autologin

new pLogged[MAX_PLAYERS];

stock PlayerPath(playerid) { //This will give us faster access to a player's path
    new iStr[256],name[MAX_PLAYER_NAME];
    GetPlayerName(playerid,name,sizeof(name));
    format(iStr,sizeof(iStr),PATH,name);
    return iStr;
}
//ret_memcpy
#pragma unused ret_memcpy

We need the stock for fast playerfile accessing.

Step 3
OnPlayerConnect
We need to check for the player's file, then load the data if it exists.
<Autologin!>
pawn Code:
public OnPlayerConnect(playerid)
{
    pLogged[playerid] = 0;
    #if defined AUTOLOGIN
        new tmpIP[16];
        GetPlayerIp(playerid,tmpIP,sizeof(tmpIP)); //Getting IP
    #endif
    if(fexist(PlayerPath(playerid))) {
        INI_ParseFile(PlayerPath(playerid), "UserDataLoad_%s", .bExtra = true, .extra = playerid); //Calling loading callback
        #if defined AUTOLOGIN
            if(strcmp(tmpIP,pIP[playerid],true) == 0) { //Checking if the IPs match
                pLogged[playerid] = 1;
                SetPlayerScore(playerid,pInfo[playerid][Score]);
                GivePlayerMoney(playerid,pInfo[playerid][Cash]);
                SendClientMessage(playerid,lime,"You've been auto-logged in. [IP match]");
                return 1;
            }
        #endif
        ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_INPUT,"Login","Please enter your password below.","Login","Leave");
    } else {
        ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Register","Please register by entering a password below.","Register","Leave");
    }
    return 1;
}
That includes the autologin part too.

Step 4
UserDataLoad_%s
Used for loading users' data.
pawn Code:
forward UserDataLoad_data(playerid,name[],value[]);

public UserDataLoad_data(playerid,name[],value[]) { //This loads the settings from the INI file
    INI_Int("Pass",pInfo[playerid][Pass]);
    INI_String("IP",pIP[playerid],16);
    INI_Int("Admin",pInfo[playerid][Admin]);
    INI_Int("Cash",pInfo[playerid][Cash]);
    INI_Int("Score",pInfo[playerid][Score]);
    INI_Int("Banned",pInfo[playerid][Banned]);
    return 1;
}

Step 5
Register dialog (under OnDialogResponse)
We need to check for the password, and create a new .ini file.
pawn Code:
if(dialogid == DIALOG_REGISTER) {
        GetPlayerIp(playerid,pIP[playerid],16);
        if(!response) Kick(playerid);
        if(!strlen(inputtext)) return ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Register","Please enter a password.","Register","Leave");
        new INI:iFile = INI_Open(PlayerPath(playerid)); //Making the INI file and writing settings
        INI_SetTag(iFile,"data");
        INI_WriteInt(iFile,"Pass",udb_hash(inputtext));
        #if defined AUTOLOGIN
            INI_WriteString(iFile,"IP",pIP[playerid]);
        #endif
        INI_WriteInt(iFile,"Cash",REGISTERED_MONEY);
        INI_WriteInt(iFile,"Score",0);
        INI_Close(iFile);
        pLogged[playerid] = 1;
        new iStr[128];
        format(iStr,sizeof(iStr),"You've successfully registered with the password \"%s\".",inputtext);
        SendClientMessage(playerid,yellow,iStr);
        return 1;
    }

Step 6
Login dialog (under OnDialogResponse)
Checking for the player's password..
pawn Code:
if(dialogid == DIALOG_LOGIN) {
        if(!response) Kick(playerid);
        new iStr[128],gTries;
        if(gTries == 0) gTries = 1;
        if(gTries == 3) { // 3 tries = kick
            new pName[30];
            GetPlayerName(playerid,pName,sizeof(pName));
            format(iStr,sizeof(iStr),"%s has been kicked for exceeding login tries.",pName);
            SendClientMessageToAll(red,iStr);
            return Kick(playerid);
        }
        if(!strlen(inputtext)) {
            format(iStr,sizeof(iStr),"Please enter your password. Tries: %i/3",gTries);
            return ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_INPUT,"Login",iStr,"Login","Leave");
        }
        if(udb_hash(inputtext) == pInfo[playerid][Pass]) {
            pLogged[playerid] = 1;
            SendClientMessage(playerid,lime,"You've successfully logged in.");
            SetPlayerScore(playerid,pInfo[playerid][Score]); //Loading player score
            GivePlayerMoney(playerid,pInfo[playerid][Cash]); //Loading player money
        } else {
            format(iStr,sizeof(iStr),"Incorrect password. Tries: %i/3",gTries);
            ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Register",iStr,"Login","Leave");
            gTries++;
            return 1;
        }

Step 7
Blocking the player from spawning. (because they can spawn by pressing the "Spawn" button in the class selection)
pawn Code:
public OnPlayerRequestSpawn(playerid)
{
    if(pLogged[playerid] == 0) return SendClientMessage(playerid,yellow,"You must register or login before spawning!");
    return 1;
}

Step 8
Disconnection save, this saves user settings in the player's INI file
pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    if(pLogged[playerid] == 1) {
        new INI:iFile = INI_Open(PlayerPath(playerid));
        INI_SetTag(iFile,"data");
        INI_WriteInt(iFile,"Cash",GetPlayerMoney(playerid));
        INI_WriteInt(iFile,"Score",GetPlayerScore(playerid));
        INI_Close(iFile);
    }
    pLogged[playerid] = 0;
    return 1;
}

Whole code:
PasteBin

Tested, works fine.

Download links:
dutils download link
dudb download link
YSI download link
__________________
- seven

Last edited by Seven_of_Nine; 16/07/2011 at 11:14 AM. Reason: Download links + lil' bit more explanation
Seven_of_Nine is offline   Reply With Quote
Old 16/07/2011, 10:18 AM   #2
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 3,800
Reputation: 1160
Default Re: Login & Register system - dialogs - with y_ini

Nice tutorial, though you do need to explain several things in the tutorial, otherwise this is basicly a snippet page.

Y_ini ftw.
__________________
Join the best Cops And Robbers in SA-MP, today. svr.sfcnr.com:7777

Lorenc_ is offline   Reply With Quote
Old 16/07/2011, 10:29 AM   #3
Basicz
Gangsta
 
Basicz's Avatar
 
Join Date: Aug 2010
Location: Indonesia, Surabaya.
Posts: 606
Reputation: 35
Default Re: Login & Register system - dialogs - with y_ini

Eh

pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
        pLogged[playerid] = 0;
        new INI:iFile = INI_Open(PlayerPath(playerid));
        INI_SetTag(iFile,"data");
        INI_WriteInt(iFile,"Cash",GetPlayerMoney(playerid));
        INI_WriteInt(iFile,"Score",GetPlayerScore(playerid));
        INI_Close(iFile);
        return 1;
}

You should add if the player is logged in tho'

pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
        if ( pLogged[ playerid ] ) {
            new INI:iFile = INI_Open(PlayerPath(playerid));
            INI_SetTag(iFile,"data");
            INI_WriteInt(iFile,"Cash",GetPlayerMoney(playerid));
            INI_WriteInt(iFile,"Score",GetPlayerScore(playerid));
            INI_Close(iFile);
        }

        pLogged[ playerid ] = 0;
        return 1;
}

I think it could be useful.

--

Nice tutorial!
__________________

- Quina, from the game " Final Fantasy IX "
Basicz is offline   Reply With Quote
Old 16/07/2011, 10:59 AM   #4
Seven_of_Nine
Huge Clucker
 
Seven_of_Nine's Avatar
 
Join Date: Feb 2011
Location: Hungary
Posts: 372
Reputation: 12
Default Re: Login & Register system - dialogs - with y_ini

@Basicz, the player should be logged in because it is a must-register system.
__________________
- seven
Seven_of_Nine is offline   Reply With Quote
Old 16/07/2011, 11:05 AM   #5
Markx
Gangsta
 
Markx's Avatar
 
Join Date: Dec 2010
Location: Zagreb, Croatia
Posts: 944
Reputation: 39
Default Re: Login & Register system - dialogs - with y_ini

Great tutorial!
__________________
...Left samp for now, hope i gonna be back someday...

Markx is offline   Reply With Quote
Old 16/07/2011, 11:11 AM   #6
Basicz
Gangsta
 
Basicz's Avatar
 
Join Date: Aug 2010
Location: Indonesia, Surabaya.
Posts: 606
Reputation: 35
Default Re: Login & Register system - dialogs - with y_ini

@ Seven_of_Nine

When you kick the player in the registration dialog, just think..

( It will kick the player, sends OnPlayerDisconnect, the file wouldn't be found xD ).
__________________

- Quina, from the game " Final Fantasy IX "
Basicz is offline   Reply With Quote
Old 16/07/2011, 11:15 AM   #7
Seven_of_Nine
Huge Clucker
 
Seven_of_Nine's Avatar
 
Join Date: Feb 2011
Location: Hungary
Posts: 372
Reputation: 12
Default Re: Login & Register system - dialogs - with y_ini

Ok, corrected

I think commands are better (/login /register) but yeh.. I made this too
__________________
- seven
Seven_of_Nine is offline   Reply With Quote
Old 16/07/2011, 11:37 AM   #8
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 3,800
Reputation: 1160
Default Re: Login & Register system - dialogs - with y_ini

Still add it because there are many ways of preventing the actual dialog from coming, mostly with some lagg and a command that shows a dialog will clear the dialog
__________________
Join the best Cops And Robbers in SA-MP, today. svr.sfcnr.com:7777

Lorenc_ is offline   Reply With Quote
Old 16/07/2011, 03:35 PM   #9
Coffeemonster
Big Clucker
 
Join Date: Nov 2010
Posts: 143
Reputation: 2
Default Re: Login & Register system - dialogs - with y_ini

Uhmm...you can register just by leaving the field blank and pressing 'Register'. Also the account didn't save anywhere :/
Coffeemonster is offline   Reply With Quote
Old 17/07/2011, 07:49 AM   #10
System64
Gangsta
 
System64's Avatar
 
Join Date: May 2011
Location: Zadar, Croatia
Posts: 933
Reputation: 143
Default Re: Login & Register system - dialogs - with y_ini

this is too long, I have a smaller code but sometinhg does't work, when I fix it i will post tutorial, anyway, nice tutorial, praise for their efforts, and you should use Whirlpool
__________________
All my works
I left SA:MP! Maybe once I start scripting again but now I'm based on web programming
Soon, I'll buy domain system32.com.hr but for now you can contact me on LogoOff or on my Youtube channel!
System64 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
[FilterScript] Login/Register using ZCMD SSCANF & Y_INI.( Admin commands + VIP System + VIP Commands! ) Zh3r0 Filterscripts 116 09/01/2015 11:57 AM
[FilterScript] [FS] ERegLog ( Advanced Register/Login System [Dialogs] ) aircombat Filterscripts 29 09/11/2014 10:30 PM
[Tutorial] Login and Register System - Dialogs - Using SII Kush Tutorials 69 02/07/2013 06:19 PM
[FilterScript] Register/login system (Dini, dialogs) pantelimonfl Filterscripts 6 08/03/2011 05:13 PM


All times are GMT. The time now is 03:35 AM.


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