25.01.2017, 11:12
(
Последний раз редактировалось Yaa; 27.01.2017 в 13:43.
)
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 :
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
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 :
3 - we need to define the dialogs & User Data
4 - Adding the whirlpool hashing engine
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:
let's move to the callback
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:
Now let's move to OnPlayerConnect:
7 - Now The Dialogs !!!
we need to create 2 dialogs can load/insert data into the server without any bugs or lag
8 - Now increasing value of Deaths & Kills on every die or kill
it's very simple you can easy do it like that:
9 - Now last step : Save Data on player Disconnect !
we need to update the mysql server rows using a mysql synatax :
like that :
so the callback must be like :
10 - How to create a admin command:
Credits:
- SAMP Team for SAMP Includes
- BlueG/maddinat0r for MySQL Includes
- oMa37 check 3th post
Thank you for reading
Yaa
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
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 ""
like this :
PHP код:
new MySQL:MHandle;
PHP код:
enum
{
Register,
Login
};
enum Player
{
Name[MAX_PLAYERS_NAME],
Admin,
Deaths,
Kills
};
new PlayerInfo[MAX_PLAYERS][Player]
PHP код:
native WP_Hash(buffer[], len, const str[]);
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` (`Name` VARCHAR(24), `IP` VARCHAR(16), `Password` VARCHAR(129), `Admin` INT(2), `Deaths` INT(6), `Kills` INT(6))
PHP код:
public OnGameModeInit()
{
// Connecting to mysql server using MHandle
MHandle = mysql_connect(MySQL_Host, MySQL_User, MySQL_Password, MySQL_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_Host, MySQL_User, MySQL_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;
}
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(playerid, Login, DIALOG_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(playerid, Register, DIALOG_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;
}
PHP код:
public OnPlayerConnect(playerid)
{
new query[50];
GetPlayerName(playerid, PlayerInfo[player][Name], sizeof(PlayerInfo[player][Name])); // gets player name on connect
mysql_format(MHandle, query, sizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e'", PlayerInfo[player][Name]);
mysql_tquery(MHandle, query, "OnPlayerAccountCheck", "i", playerid); // in this step the server checks if player registered or not using the callback up ^^
return 1;
}
we need to create 2 dialogs can load/insert data into the server without any bugs or lag
PHP код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case Login:
{
new loginattemp, buf[129];
if(!response) Kick(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(buf, sizeof(buf), inputtext);
if(!strcmp(buf, PlayerInfo[playerid][Password], true)) // comparing the passwords (database & entrerd)
{
new query[70], Cache:GetCache;
mysql_format(MHandle, query, sizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e' LIMIT 1", PlayerInfo[player][Name]);
GetCache = mysql_query(MHandle, query);
// 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(playerid, Login, DIALOG_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(playerid, Register, DIALOG_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(buf, sizeof(buf), inputtext); // hashing password using Whirlpool engine
GetPlayerIp(playerid, playerip, sizeof(playerip));
mysql_format(MHandle, query, sizeof(query), "INSERT INTO `accounts` (`Name`, `Password`, `IP`, `Admin`,`Deaths`, `Kills`) VALUES ('%e', '%e', '%e', 0, 0, 0)", PlayerInfo[player][Name], buf, playerip);
mysql_query(MHandle, query); // here inserting the player account in the database as a registered player
}
}
return 0;
}
it's very simple you can easy do it like that:
PHP код:
public OnPlayerDeath(playerid, killerid, reason)
{
PlayerInfo[killerid][Kills]++; // increasing Kills value for killer
PlayerInfo[playerid][Deaths]++; // increasing Deaths value for the victim
return 1;
}
we need to update the mysql server rows using a mysql synatax :
like that :
PHP код:
UPDATE `accounts` SET `Kills` = '', `Deaths` = '', `Admin` = '', WHERE `name` = ''
PHP код:
public OnPlayerDisconnect(playerid, reason)
{
new querylist[120];
mysql_format(MHandle, querylist, sizeof(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(MHandle, querylist);
return 1;
}
PHP код:
CMD:[nameofcommand](playerid, params[])
{
// 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.
}
- SAMP Team for SAMP Includes
- BlueG/maddinat0r for MySQL Includes
- oMa37 check 3th post
Thank you for reading
Yaa