Jump to content

[Tutorial] Register/Login sistem MySQL R41+


Leonard
 Share

Recommended Posts

  • Administrator
De ceva timp maddinat0r a lansat versiunea de MySQL r40 pentru sa-mp, dar nu am vazut niciun tutorial pentru login/register pe mysql r40, dupa cum bine stim au fost schimbate cateva chestii in el.
In primu rand avem nevoie de MySQL r41-2 : Click aici
In al doilea rand mai avem nevoie de XAMPP : Click aici
In al treilea rand avem nevoie de serverul propriu zis de samp : Click aici
 
 
Dupa ce am terminat cu downloadu pornim un gamemode cu New si incepem:
1. La inceputul gamemodu-lui punem:
#include <a_mysql>

2. Apoi definim datele de conectare la baza de date

#define mysql_host  "127.0.0.1" //ip baza de date
#define mysql_db    "server"    //nume baza de date
#define mysql_user  "root"      //user baza de date
#define mysql_pass  ""          //parola baza de date

3.Vom crea 2 Enum-uri. Unul cu dialogurile, altu cu datele pe care le va avea playerul

enum
{
	DIALOG_REGISTER,
	DIALOG_LOGIN,
	DIALOG_AGE,
	DIALOG_SEX,
	DIALOG_EMAIL
};
enum pInfo
{
	pID,
	pPassword,
	pAdmin,
	pMoney,
	pHelper
};
new PlayerInfo[MAX_PLAYERS][pInfo]; //Aceasta este o variabila de tip array, cu ea vom verifica datele playerilor

4.Sus vom mai pune un new

new MySQL:g_Sql; // handle(maner) = face conexiunea intre server si baza de date

5.Acum vom crea un callback care va face legatura la baza de date

forward MySQLConnect();
public MySQLConnect()
{
    g_Sql = mysql_connect(mysql_host, mysql_user, mysql_pass, mysql_db);
    if(mysql_errno() != 0)
    {
        printf("Connexiunea la baza de date '%s' nu a reusit. ---> Server ShutDown!", mysql_db);
        SendRconCommand("exit"); //daca vor exista erori ne va afisamesajul si va inchide consola
    }
    else
    {
        printf("Conexiunea la baza de date '%s' cu userul '%s' a reusit", mysql_db, mysql_user);// daca nu vor exista erori ne va afisa mesajul
    }
	return 1;
}

6.callback-ul MySQLConnect il vom pune la OnGameModeInit

public OnGameModeInit()
{
    MySQLConnect();
// liniile voastre
}

7.Vom creea un stock pentru a ne ajuta sa selectam numele playerului

stock GetName(playerid)
{
	new Name[MAX_PLAYER_NAME];
	GetPlayerName(playerid, Name, sizeof(Name));
	return Name;
}

8.Acum vom crea un nou callback la care vom apela cand playerul se conecteza, el va arata daca are cont sau nu

forward OnPlayerLogin(playerid);
public OnPlayerLogin(playerid)
{
	new rows, fields;
	cache_get_row_count(rows);
	cache_get_field_count(fields);
	if(rows)
	{
	    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logheaza-te!", "Te rog sa-ti introduci parola mai jos", "Login", "Exit");//daca playerul are randuri i se va afisa dialogul de login
	}
	else
	{
	    ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Inregistrare", "Trebuie sa iti creezi un cont pentru a juca pe server. \nTasteaza mai jos o paorla pentru a te inregistra.", "Inregistrare", "Exit");//daca playerul nu are randuri i se va afisa dialogul de inregistrare
	}
	return 1;
}

9.Acum ca am creat callback-ul care ne ajuta sa aflam daca playerul are cont sau nu mergem la OnPlayerConnect si punem

public OnPlayerConnect(playerid)
{
	new query[100];
	mysql_format(g_Sql, query, sizeof(query), "SELECT * FROM users WHERE Name = '%s'", GetName(playerid));
	mysql_tquery(g_Sql, query, "OnPlayerLogin", "i", playerid);//se va interoga OnPlayerLogin pentru a ne arata daca are sau nu cont acel player
	return 1;
}

10.Acum vom creea dialogurile, mergem la OnDialogResponse

new query[128], ip[25];
	switch(dialogid)
	{
	    case DIALOG_REGISTER:
	    {
			if(!response) return Kick(playerid);
			if(response)
			{
			    if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Parola Incorecta!", "Introdu-ti o parola pentru a te  inregistra pe server!", "Inregistreaza", "Exit");
			    GetPlayerIp(playerid, ip, sizeof(ip));
				SCM(playerid, COLOR_LIGHTBLUE, "Te rog sa-ti introduci varsta.");
				mysql_format(g_Sql, query, sizeof(query), "INSERT INTO users (Name, Password, IP, Admin, Helper, Money) VALUES ('%s', '%e', '%s', '0','0','1500')", GetName(playerid), inputtext, ip);
			    mysql_query(g_Sql, query);
				ShowPlayerDialog(playerid, DIALOG_AGE, DIALOG_STYLE_INPUT, "Varsta!", "Te rog sa-ti introduci mai jos varsta.", "Continua", "Exit");
			}
			return 1;
		}
		case DIALOG_LOGIN:
		{
		    if(!response) return Kick(playerid);
		    if(response)
		    {
				if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Parola Incorecta!", "Te rog sa-ti introduci parola corecta mai jos\nDaca ai uitat parola o poti recupera pe panel.", "Login", "Exit");
				mysql_format(g_Sql, query, sizeof(query), "SELECT * FROM `users` WHERE `Name`='%e' AND `Password`='%s'", GetName(playerid), inputtext);
				mysql_tquery(g_Sql, query, "OnLogin", "i", playerid);
		    }
		    return 1;
		}
		case DIALOG_AGE:
		{
		    if(!response) return ShowPlayerDialog(playerid, DIALOG_AGE, DIALOG_STYLE_INPUT, "Varsta!", "Te rog sa-ti introduci mai jos varsta.", "Continua", "Exit");
		    if(response)
		    {
		    	if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_AGE, DIALOG_STYLE_INPUT, "Varsta!", "Te rog sa-ti introduci mai jos varsta.", "Continua", "Exit");
				SCM(playerid, COLOR_LIGHTBLUE, "Te rog sa-ti introduci email-ul, pentru contact si a pentru a-ti putea recupera contul.");
				mysql_format(g_Sql, query, sizeof(query), "UPDATE `users` SET `Age`='%e' WHERE `Name`='%e'", inputtext, GetName(playerid));
				mysql_query(g_Sql, query);
				ShowPlayerDialog(playerid, DIALOG_EMAIL, DIALOG_STYLE_INPUT, "Ëmail", "Te rog sa-ti introduci email-ul mai jos.", "Continua", "Exit");
			}
		    return 1;
		}
		case DIALOG_EMAIL:
		{
		    if(!response) return ShowPlayerDialog(playerid, DIALOG_EMAIL, DIALOG_STYLE_INPUT, "Ëmail", "Te rog sa-ti introduci email-ul mai jos.", "Continua", "Exit");
		    if(response)
		    {
		        if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_EMAIL, DIALOG_STYLE_INPUT, "Ëmail", "Te rog sa-ti introduci email-ul mai jos.", "Continua", "Exit");
		        SCM(playerid, COLOR_LIGHTBLUE, "Te rog sa-ti alegi sexul.");
                mysql_format(g_Sql, query, sizeof(query), "UPDATE `users` SET `Email`='%e' WHERE `Name`='%e'", inputtext, GetName(playerid));
				mysql_query(g_Sql, query);
				ShowPlayerDialog(playerid, DIALOG_SEX, DIALOG_STYLE_MSGBOX, "Gen", "Te rog sa-ti alegi genul de mai jos.", "Masculin", "Feminin");
		    }
		    return 1;
		}
		case DIALOG_SEX:
		{
			if(response)
			{
                mysql_format(g_Sql, query, sizeof(query), "UPDATE `users` SET `Sex`='1' WHERE `Name`='%e'", GetName(playerid));
				mysql_query(g_Sql, query);
				SCM(playerid, COLOR_LIGHTBLUE, "Inregistrarea a fost un succes!");
                ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logheaza-te!", "Te rog sa-ti introduci parola mai jos", "Login", "Exit");
			}
			if(!response)
			{
			    mysql_format(g_Sql, query, sizeof(query), "UPDATE `users` SET `Sex`='0' WHERE `Name`='%e'", GetName(playerid));
				mysql_query(g_Sql, query);
				SCM(playerid, COLOR_LIGHTBLUE, "Inregistrarea a fost un succes!");
                ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logheaza-te!", "Te rog sa-ti introduci parola mai jos", "Login", "Exit");
			}
			return 1;
		}
	}

Pentru a nu exista erori puneti urmatorul define

#define SCM SendClientMessage

11.Acum vom creea callbackul la care apeleaza dialogul de login

forward OnLogin(playerid);
public OnLogin(playerid)
{
	new rows, fields, temporar[200];
	cache_get_row_count(rows);
	cache_get_field_count(fields);
	if(rows)
	{
	    PlayerInfo[playerid][pPassword] = cache_get_value_name(0, "Password", temporar);
	    PlayerInfo[playerid][pID] = cache_get_value_name(0, "ID", temporar);
	    cache_get_value_int(0, "Money", PlayerInfo[playerid][pMoney]);
	    SpawnPlayer(playerid);
	}
	else
	{
	    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Parola Incorecta!", "Te rog sa-ti introduci parola corecta mai jos\nDaca ai uitat parola o poti recupera pe panel.", "Login", "Exit");
	}
	SetPlayerCash(playerid, PlayerInfo[playerid][pMoney]);
	return 1;
}

12.Mergem la OnGameModeExit si punem

mysql_close( g_Sql );

13.Acum va arat cum se creaza baza de date

 

a) cream o baza de date cu numele server

yTZpMjK.png

b)Se selecteaza noua baza de date

yuk7VZn.png

c)Se creaza tabelul cu cele 10 coloane

2eNzzVO.png

d)Se definesc coloanele cu setarile, ca in poza de maijos

ieeFwqY.png

 
La tip INT = intenger= numar de cifre.
Varchar = string = sir de caractere(litere)
Pentru a exista probleme puneti la coloana ID autoincrement.
 
Tutorial creat 100% de WARfree. Daca repostati pastrati creditele si sursa adica Leaks.ro
Tutorial creat Special pentru Leaks.Ro

 

Link to comment
Share on other sites

Tutorialul e facut la perfectie si testat de sute de ori, nu are cum sa nu-ti mearga.

Defapt, nu merge imi tot apare chestia asta la compilare error 017: undefined symbol "SetPlayerCash" si am mysql care trebuie

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...