[Tutorial] Creating a simple register/save system using R41-2
#1

Hello !!

Introduction:

as i noticed the most of tutorials are outdated and some onces are bugged! so In this tutorial i will show you a simple and fast way to create a simple register system using The lastest version of MySQL by BlueG/maddinat0r (R41-2)

Includes:

We need 2 base includes to create our system

they are :

PHP код:
#include <a_samp> // 0.3.7 Include
#include <a_mysql>// MySQL R41-2 
Please be sure you are using lastest version of mysql include about a_samp it's dosent matter you can use the version of a_samp include that your server work with it

Show Time:

Let's Start:
1- we need to define the mysql information in the top of script

PHP код:
#define MySQL_Host ""
#define MySQL_User ""
#define MySQL_Password ""
#define MySQL_Database "" 
2 - we need to create a handle for mysql but in R41-2 we need to put MySQL: tag in the variable created!

like this :

PHP код:
new MySQL:MHandle
3 - we need to define the dialogs & User Data

PHP код:
enum
{
    
Register,
    
Login
};
enum Player
{
    
Name[MAX_PLAYERS_NAME],
    
Admin,
    
Deaths,
    
Kills
};
new 
PlayerInfo[MAX_PLAYERS][Player
4 - Adding the whirlpool hashing engine

PHP код:
native WP_Hash(buffer[], len, const str[]); 
5 - let's move to OnGamemodeInit Callback

we need to connect server to mysql server and creating the tables

About tables:

We need 1 table called accounts contaning 5 colunms (Name (string), IP(string), Password(string), Deaths(Int), Kills(Int))

so the MySQL Synatax:
PHP код:
CREATE TABLE IF NOT EXISTS `accounts` (`NameVARCHAR(24), `IPVARCHAR(16), `PasswordVARCHAR(129), `AdminINT(2), `DeathsINT(6), `KillsINT(6)) 
let's move to the callback

PHP код:
public OnGameModeInit()
{
    
// Connecting to mysql server using MHandle 
    
MHandle mysql_connect(MySQL_HostMySQL_UserMySQL_PasswordMySQL_Database);
    if(
mysql_errno() != 0// checks if the server failed to connected or not
    
{
        print(
"[MYSQL]: Failed to connect with using Following Informations: ");
        
printf(" Host: %s | User: %s | Password: ****** | Database: %s"MySQL_HostMySQL_UserMySQL_Database);
    }
    else
    {
        
printf("[MYSQL]: Connection Success to database: %s !"MySQL_Database);
        
mysql_query(MHandle"CREATE TABLE IF NOT EXISTS `accounts` (`Name` VARCHAR(24), `IP` VARCHAR(16), `Password` VARCHAR(129), `Admin` INT(2), `Deaths` INT(6), `Kills` INT(6))"); // creating tables once server got connected to mysql server
    
}
    return 
1;

6 - Now we need to setup player on connect

we need to create a separed callback get called when player connect to check if he is registred or not

so it's need be something like that:

PHP код:
forward OnPlayerAccountCheck(playerid);
public 
OnPlayerAccountCheck(playerid)
{
    new 
rows;
    
cache_get_row_count(rows); // Server count how many rows find in the mysql server
    
if(rows// if he find any row then the player registered
    
{
        
cache_get_value_name(0"Password"PlayerInfo[playerid][Password]); // here we get the password of the player account for compare it later with the entred password on the dialog
        
ShowPlayerDialog(playeridLoginDIALOG_STYLE_PASSWORD"Login""Welcome Back !\nWe miss you here !\nPlease fill you password here to get stats back!""Login""Cancel"); 
    }
    else 
// else he is not
    
{
        
ShowPlayerDialog(playeridRegisterDIALOG_STYLE_PASSWORD"Register""Welcome to our server! \nthis account not register at the database \nPlease fill a password to register your account""Register""Cancel");
    }
    return 
1;

Now let's move to OnPlayerConnect:

PHP код:
public OnPlayerConnect(playerid)
{
    new 
query[50];
    
GetPlayerName(playeridPlayerInfo[player][Name], sizeof(PlayerInfo[player][Name])); // gets player name on connect
    
mysql_format(MHandlequerysizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e'"PlayerInfo[player][Name]);
    
mysql_tquery(MHandlequery"OnPlayerAccountCheck""i"playerid); // in this step the server checks if player registered or not using the callback up ^^
    
return 1;

7 - Now The Dialogs !!!

we need to create 2 dialogs can load/insert data into the server without any bugs or lag

PHP код:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {   
        case 
Login:
        {
            new 
loginattempbuf[129];
            if(!
responseKick(playerid); 
            if(
isnull(PlayerInfo[playerid][Password])) // checks if password correctly loaded else check below :)
            
{
                
SendClientMessage(playerid, -1,"[MYSQL]: Sorry, we have some problems on database right now, Come back later !");
                
Kick(playerid);
                print(
"[MYSQL]: Password Value 'PlayerInfo[playerid][Password]' not correctly loaded! please check OnPlayerAccountCheck callback");
                print(
"[SA-MP Server]: Server shutdown..");
                
SendRconCommand("exit");
                return 
1;
            }
            
WP_Hash(bufsizeof(buf), inputtext);
            if(!
strcmp(bufPlayerInfo[playerid][Password], true)) // comparing the passwords (database & entrerd)
            

                new 
query[70], Cache:GetCache;
                
mysql_format(MHandlequerysizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e' LIMIT 1"PlayerInfo[player][Name]);
                
GetCache mysql_query(MHandlequery);
               
                
// Importing data after beigning sure he is the right user
                
new rows
                
cache_get_row_count(rows);
                if(
rows == 1
                {
                    
cache_get_value_name_int(0"Admin"PlayerInfo[playerid][Admin]); 
                    
cache_get_value_name_int(0"Deaths"PlayerInfo[playerid][Deaths]); 
                    
cache_get_value_name_int(0"Kills"PlayerInfo[playerid][Kills]);
                }
                
SendClientMessage(playerid, -1"You have successfully logged in."); 
                
cache_delete(GetCache);
            } 
            else 
// if he entred an wrrong password
            

                if(
loginattemp == 3) return Kick(playerid); // if he entred the same wrrong password 3 times he got kicked
                
SendClientMessage(playerid, -1"You have specified an incorrect password!"); 
                
ShowPlayerDialog(playeridLoginDIALOG_STYLE_PASSWORD"Login""Welcome Back !\nWe miss you here !\nPlease fill you password here to get stats back!""Login""Cancel");
                
loginattemp++; // adding +1 for every attemp
            

        }
        case 
Register:
        {
            if(!
response) return Kick(playerid); 
            if(
strlen(inputtext) < 5
            { 
                
SendClientMessage(playerid, -1"Your password must at least contain more than 4 characters."); 
                return 
ShowPlayerDialog(playeridRegisterDIALOG_STYLE_PASSWORD"Register""Welcome to our server! \nthis account not register at the database \nPlease fill a password to register your account""Register""Cancel");
            } 
            new 
                
query[287], 
                
playerip[16],
                
buf[129]
            ;
            
WP_Hash(bufsizeof(buf), inputtext); // hashing password using Whirlpool engine
            
GetPlayerIp(playeridplayeripsizeof(playerip)); 
            
mysql_format(MHandlequerysizeof(query), "INSERT INTO `accounts` (`Name`, `Password`, `IP`, `Admin`,`Deaths`, `Kills`) VALUES ('%e', '%e', '%e', 0, 0, 0)"PlayerInfo[player][Name], bufplayerip); 
            
mysql_query(MHandlequery); // here inserting the player account in the database as a registered player
        
}
    }
    return 
0;

8 - Now increasing value of Deaths & Kills on every die or kill

it's very simple you can easy do it like that:

PHP код:
public OnPlayerDeath(playeridkilleridreason)
{
    
PlayerInfo[killerid][Kills]++; // increasing Kills value for killer 
    
PlayerInfo[playerid][Deaths]++; // increasing Deaths value for the victim
    
return 1;

9 - Now last step : Save Data on player Disconnect !

we need to update the mysql server rows using a mysql synatax :

like that :
PHP код:
UPDATE `accountsSET `Kills` = '', `Deaths` = '', `Admin` = ''WHERE `name` = '' 
so the callback must be like :

PHP код:
public OnPlayerDisconnect(playeridreason)
{
    new 
querylist[120];
    
mysql_format(MHandlequerylistsizeof(querylist),
    
"UPDATE `accounts` SET `Kills` = %d, `Deaths` = %d, `Admin` = %d WHERE `name` = '%e'"
    
PlayerInfo[playerid][Kills], PlayerInfo[playerid][Deaths], PlayerInfo[playerid][Admin], PlayerInfo[player][Name]);
    
mysql_query(MHandlequerylist); 
    return 
1;

10 - How to create a admin command:

PHP код:
CMD:[nameofcommand](playeridparams[])
{
    
// check if the player is admin
    
if(PlayerInfo[playerid][Admin] == 0) return SendClientMessage(playerid, -1"you are not admin");
    
// codes here ...
    
return 1// if you return 0 the command will not created.
 

Credits:

- SAMP Team for SAMP Includes
- BlueG/maddinat0r for MySQL Includes
- oMa37 check 3th post

Thank you for reading

Yaa
Reply


Messages In This Thread
Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 11:12
Re: Creating a simple register/save system using R41-2 - by princejeet1510 - 25.01.2017, 11:17
Re: Creating a simple register/save system using R41-2 - by oMa37 - 25.01.2017, 11:59
Respuesta: Creating a simple register/save system using R41-2 - by Eloy - 25.01.2017, 12:15
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 12:22
Re: Creating a simple register/save system using R41-2 - by SyS - 25.01.2017, 12:26
Re: Creating a simple register/save system using R41-2 - by oMa37 - 25.01.2017, 12:30
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 12:38
Re: Creating a simple register/save system using R41-2 - by princejeet1510 - 25.01.2017, 12:48
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 12:53
Re: Creating a simple register/save system using R41-2 - by princejeet1510 - 25.01.2017, 12:57
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 12:59
Re: Creating a simple register/save system using R41-2 - by oMa37 - 25.01.2017, 13:03
Re: Creating a simple register/save system using R41-2 - by Quinncell - 25.01.2017, 13:06
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 13:08
Re: Creating a simple register/save system using R41-2 - by oMa37 - 25.01.2017, 13:14
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 13:28
Re: Creating a simple register/save system using R41-2 - by Vince - 25.01.2017, 13:33
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 13:39
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 14:31
Re: Creating a simple register/save system using R41-2 - by PaRking - 25.01.2017, 14:40
Re: Creating a simple register/save system using R41-2 - by Quinncell - 25.01.2017, 14:41
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 15:45
Re: Creating a simple register/save system using R41-2 - by GhostHacker9 - 25.01.2017, 15:53
Re: Creating a simple register/save system using R41-2 - by Lordzy - 25.01.2017, 16:10
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 16:14
Re: Creating a simple register/save system using R41-2 - by Lordzy - 25.01.2017, 16:30
Re: Creating a simple register/save system using R41-2 - by Yaa - 25.01.2017, 16:34
Re: Creating a simple register/save system using R41-2 - by Lordzy - 25.01.2017, 16:37
Re: Creating a simple register/save system using R41-2 - by RyderX - 27.01.2017, 13:21
Re: Creating a simple register/save system using R41-2 - by BiosMarcel - 27.01.2017, 14:12
Re: Creating a simple register/save system using R41-2 - by Vince - 27.01.2017, 14:49
Re: Creating a simple register/save system using R41-2 - by Yaa - 27.01.2017, 19:34

Forum Jump:


Users browsing this thread: 4 Guest(s)