SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 25/01/2017, 12:12 PM   #1
Yaa
Banned
 
Join Date: May 2016
Location: = GetPlayerHouse("Yaa", 0.2);
Posts: 598
Reputation: 42
Smile Creating a simple register/save system using R41-2

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 Code:
#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 Code:
#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 Code:
new MySQL:MHandle
3 - we need to define the dialogs & User Data

PHP Code:
enum
{
    
Register,
    
Login
};

enum Player
{
    
Name[MAX_PLAYERS_NAME],
    
Admin,
    
Deaths,
    
Kills
};

new 
PlayerInfo[MAX_PLAYERS][Player
4 - Adding the whirlpool hashing engine

PHP Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
UPDATE `accountsSET `Kills` = '', `Deaths` = '', `Admin` = ''WHERE `name` = '' 
so the callback must be like :

PHP Code:
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 Code:
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

Last edited by Yaa; 27/01/2017 at 02:43 PM.
Yaa is offline   Reply With Quote
Old 25/01/2017, 12:17 PM   #2
princejeet1510
Big Clucker
 
princejeet1510's Avatar
 
Join Date: Mar 2016
Location: Trying to figure it out.
Posts: 115
Reputation: 4
Default Re: Creating a simple register/save system using R41-2

Love you man.... Didn't tested yet, but the only thing I was looking for!
+Rep.
__________________
princejeet1510 is offline   Reply With Quote
Old 25/01/2017, 12:59 PM   #3
oMa37
Gangsta
 
Join Date: Feb 2016
Posts: 968
Reputation: 155
Default Re: Creating a simple register/save system using R41-2

1.

Quote:
Originally Posted by Yaa View Post
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)
Click Here.
This is far better than your tutorial.

2.

Quote:
Originally Posted by Yaa View Post
4 - Adding the whirlpool hashing engine

PHP Code:
native WP_Hash(buffer[], len, const str[]); 
http://wiki.sa-mp.com/wiki/SHA256_PassHash

3. There are too much spelling mistakes.
4. Use mysql_format instead of format while calling a query, Also don't forget to escape the text (like player name).
5. You don't need to put integers inside ' ', Only strings.

That's not all.

1 Star.
oMa37 is offline   Reply With Quote
Old 25/01/2017, 01:15 PM   #4
Eloy
Huge Clucker
 
Join Date: May 2016
Location: Venezuela
Posts: 260
Reputation: 4
Default Respuesta: Creating a simple register/save system using R41-2

Use a especific %e for users donīt send Escapes strings

Change

PHP Code:
mysql_format(MHandlequerysizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%s' LIMIT 1"pname); 
To

PHP Code:
mysql_format(MHandlequerysizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e' LIMIT 1"pname); 
Eloy is offline   Reply With Quote
Old 25/01/2017, 01:22 PM   #5
Yaa
Banned
 
Join Date: May 2016
Location: = GetPlayerHouse("Yaa", 0.2);
Posts: 598
Reputation: 42
Default Re: Creating a simple register/save system using R41-2

Quote:
Originally Posted by oMa37 View Post
1.



Click Here.
This is far better than your tutorial.
i see not tutorial here

Quote:
Originally Posted by oMa37 View Post
check title please.

Quote:
Originally Posted by oMa37 View Post
3. There are too much spelling mistakes.
give me the mistakes lemme fix it ?

Quote:
Originally Posted by oMa37 View Post
4. Use mysql_format instead of format while calling a query, Also don't forget to escape the text (like player name).
it's dosen't matter mysql_format same as format but i will use it

Quote:
Originally Posted by oMa37 View Post
5. You don't need to put integers inside ' ', Only strings.
also doesn't matter the synatax always keep work with it or without it

Quote:
Originally Posted by oMa37 View Post
That's not all.

1 Star.
Thank you for you feed back.
Yaa is offline   Reply With Quote
Old 25/01/2017, 01:26 PM   #6
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,948
Reputation: 504
Default Re: Creating a simple register/save system using R41-2

Quote:
Originally Posted by Yaa View Post
also doesn't matter the synatax always keep work with it or without it
Nope using ' on integers or other data type forces mysql to do a implicit conversion from strings to integer which is an overload.
SyS is offline   Reply With Quote
Old 25/01/2017, 01:30 PM   #7
oMa37
Gangsta
 
Join Date: Feb 2016
Posts: 968
Reputation: 155
Default Re: Creating a simple register/save system using R41-2

Quote:
Originally Posted by Yaa View Post
i see not tutorial here
Stop being a dumbass.

Quote:
Originally Posted by Yaa View Post
check title please.
What?

Quote:
Originally Posted by Yaa View Post
it's dosen't matter mysql_format same as format but i will use it
It does matter. http://wiki.sa-mp.com/wiki/MySQL#mysql_format

--

You seem like you can't handle the criticisms, Don't bother releasing stuff then.
oMa37 is offline   Reply With Quote
Old 25/01/2017, 01:38 PM   #8
Yaa
Banned
 
Join Date: May 2016
Location: = GetPlayerHouse("Yaa", 0.2);
Posts: 598
Reputation: 42
Default Re: Creating a simple register/save system using R41-2

Quote:
Originally Posted by oMa37 View Post
Stop being a dumbass.



What?



It does matter. http://wiki.sa-mp.com/wiki/MySQL#mysql_format

--

You seem like you can't handle the criticisms, Don't bother releasing stuff then.
Thank you.

i learned something today

edited with new edits !

@Sreyas ty
Yaa is offline   Reply With Quote
Old 25/01/2017, 01:48 PM   #9
princejeet1510
Big Clucker
 
princejeet1510's Avatar
 
Join Date: Mar 2016
Location: Trying to figure it out.
Posts: 115
Reputation: 4
Default Re: Creating a simple register/save system using R41-2

I would still just say good job yup there were some corrections to be made which you already did......
But I would say to oMa37 atleast he made the tutorial which I really haven't seen yet with a update....Why didn't you made that?
Why you didn't get idea on this topic earlier?
__________________
princejeet1510 is offline   Reply With Quote
Old 25/01/2017, 01:53 PM   #10
GhostHacker9
Big Clucker
 
GhostHacker9's Avatar
 
Join Date: Jan 2017
Location: New Zealand
Posts: 105
Reputation: 109
Default Re: Creating a simple register/save system using R41-2

Quote:
Originally Posted by princejeet1510 View Post
I would still just say good job yup there were some corrections to be made which you already did......
But I would say to oMa37 atleast he made the tutorial which I really haven't seen yet with a update....Why didn't you made that?
Why you didn't get idea on this topic earlier?
Because a tutorial should not be built on without enough researching on the subjects as you are teaching someone and you should be aware of every part of that topic. No one is not that insane to make a tutorial under 5 minutes like the op did.i'm pretty sure if oma did it he would have done it by researching and it would be much better than this guy's because he lacks basics. I would prefer Vince,Sreyas, Andysedyen,Konstantinos or Lordzy to make a tutorial.You should take a look @ that tutorials.
GhostHacker9 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] [TUT] Creating a Register System using Dini Eazy_Efolife Tutorials 192 20/08/2014 04:26 PM
Register System not creating file. rangerxxll Scripting Help 4 05/03/2013 09:09 PM
[Tutorial] Creating a dialog register/login system using Dini [NWA]Hannes Tutorials 3 17/06/2012 05:35 PM
Y_ini Register System Not Creating Files LeetModz Scripting Help 10 02/05/2012 09:04 PM
[Tutorial] Creating a simple register and login system. AcId n RaPiD Help Archive 0 21/11/2009 02:23 PM


All times are GMT. The time now is 09:29 PM.


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