24.12.2011, 17:37
(
Последний раз редактировалось FireCat; 11.11.2012 в 17:48.
)
Hello all, so I'l be explaining how to make a simple/basic register system, MySQL based.
This will be just a start of, with money and score, then you can edit more you're self.
What will I need?
For this tutorial, we will be using BlueG mysql plugin,and sscanf.
[REL] MySQL Plugin R6-2 https://sampforum.blast.hk/showthread.php?tid=56564
sscanf 2.0 - Fixed for 0.3d https://sampforum.blast.hk/showthread.php?tid=120356
After you installed it all continue :b
Creating the table
So connect to your phpmyadmin, or what ever your using,
And it should look something like this:
Then click on your database, and create a new table with 3 rows (as i said, im just explaining with the money)
1 For the Username, 1for the Password and 1 for the Money
Now it should look like this:
Sorry mine is in portuese :/
So where it says field you will put Money
And change the type to Int then press save.
Now errors, or mistakes? Great! Lets continue
Scripting
So on the top of your script you should add
#include <a_mysql>
So now any where in your script except functions or stocks
(I recomend, on top of OnGameModeInit) add:
This is much better and organized, to know your database info.
And in GameModeInit add
What will this do?
So it will connect your server to the database, if it doesn't try adding still OnGameModeInit
mysql_debug(1);
And check Debug.txt in your server directory.
Now lets check if the player is registered
Add this under OnPlayerConnect, as so:
This will select and store the result, retrieved from your table.
Without this, it wouldn't check if people were registered or not.
So there is checking if he's registered or not, if not then show the dialog of register, else show the login dialog.
Did you notice i added mysql_free_result();?
Why? Because we used mysql_store_result() so lets free it for less memory usage.
Then we are using mysql_num_rows() != 0 to see if there are more than 0 rows.
If so it means he is registered
Lets check the register dialog code:
Why are we using:
mysql_real_escape_string(inputtext, EscapedText);?
So for example he can't use his password as DROP `Users`
That would delete your table Users. So with that function even if he uses that it won't delete your table.
After we are using INSERT INTO `Tablename` So in this case, it will insert the Username the password, and the money, then we are using VALUES, so it will insert in those rows, the selected values.
So in this case it will insert Username, his password, and his money
Lets try the login, so add this at the end of your script
So as you can see we are using mysql_real_escape_string() again
And this will check if the password is correct or not. Is not kick player, else load his status.
So we are using mysql_num_rows() != 0 again, so it will select the username and his password.
If the password is wrong, it wont select the password.
Add this in the OnDialogResponse
Here we are just trying to check if he inputs a password, else check if the password is correct.
So on top of OnGameModeInit add this:
Thats the enum for your players, where his status will be loaded.
Now lets make it load the status:
The first s is for his username, the second s is for his password, the third s is for his money.
Then below you can see we are giving his money.
So we are using mysq_fetch_row_format so it selects the selected row in sscanf, then put a |
Then it will separate each row by a | and in sscanf we are selecting 3 rows.
Any questions, or errors?
Ask'em here, ill try my best to help you, even tho im not a mysql genie :b
This will be just a start of, with money and score, then you can edit more you're self.
What will I need?
For this tutorial, we will be using BlueG mysql plugin,and sscanf.
[REL] MySQL Plugin R6-2 https://sampforum.blast.hk/showthread.php?tid=56564
sscanf 2.0 - Fixed for 0.3d https://sampforum.blast.hk/showthread.php?tid=120356
After you installed it all continue :b
Creating the table
So connect to your phpmyadmin, or what ever your using,
And it should look something like this:
Then click on your database, and create a new table with 3 rows (as i said, im just explaining with the money)
1 For the Username, 1for the Password and 1 for the Money
Now it should look like this:
Sorry mine is in portuese :/
So where it says field you will put Money
And change the type to Int then press save.
Now errors, or mistakes? Great! Lets continue
Scripting
So on the top of your script you should add
#include <a_mysql>
So now any where in your script except functions or stocks
(I recomend, on top of OnGameModeInit) add:
This is much better and organized, to know your database info.
pawn Код:
#define mysql_host "YourHostIp"
#define mysql_user "YourMysqlUser"
#define mysql_password "YourUserPassword"
#define mysql_database "YourDatabaseName"
pawn Код:
public OnGameModeInit()
{
mysql_connect(mysql_host,mysql_user,mysql_database ,mysql_password);
return 1;
}
So it will connect your server to the database, if it doesn't try adding still OnGameModeInit
mysql_debug(1);
And check Debug.txt in your server directory.
Now lets check if the player is registered
Add this under OnPlayerConnect, as so:
pawn Код:
public OnPlayerConnect(playerid)
{
new Query[80],pName[24],string[164];
GetPlayerName(playerid,pName,24);
format(Query,sizeof(Query),"SELECT `Username` FROM `Users` WHERE `Username` = '%s' LIMIT 1;",pName);
mysql_query(Query);
mysql_store_result();
return 1;
}
Without this, it wouldn't check if people were registered or not.
pawn Код:
public OnPlayerConnect(playerid)
{
new Query[80],pName[24],string[164];
GetPlayerName(playerid,pName,24);
format(Query,sizeof(Query),"SELECT `Username` FROM `Users` WHERE `Username` = '%s' LIMIT 1;",pName);
mysql_query(Query);
mysql_store_result();
if(mysql_num_rows() != 0)//if number of rows is different from 0 then continue
{
format(string,sizeof(string),"Hey, %s! \nYour account is registered.\nPlease enter the password to log in!",pName);
ShowPlayerDialog(playerid,0,DIALOG_STYLE_INPUT,"Lo g in",string,"Login","");
}
else
{
format(string,sizeof(string),"Hey, %s! \nYour account is not registered. \nPlease register to continue!",pName);
ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Re gister",string,"Register","");
}
mysql_free_result();
return 1;
}
Did you notice i added mysql_free_result();?
Why? Because we used mysql_store_result() so lets free it for less memory usage.
Then we are using mysql_num_rows() != 0 to see if there are more than 0 rows.
If so it means he is registered
Lets check the register dialog code:
pawn Код:
if(dialogid == 1)
{
if(strlen(inputtext) == 0)
{
ShowPlayerDialog(playerid,Regdialog,DIALOG_STYLE_I NPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose the password"cblue" for it! \n","Register!","");
}
else
{
new EscapedText[60];
mysql_real_escape_string(inputtext, EscapedText);
format(Query,sizeof(Query),"INSERT INTO `Users` (Username,Password,Money) VALUES ('%s','%s,'0')",GetPName(playerid),EscapedText);
mysql_query(Query);
SendClientMessage(playerid,green,"You have been successfully registered!");
GivePlayerMoney(playerid,5000);
SetPlayerScore(playerid,100);
}
}
mysql_real_escape_string(inputtext, EscapedText);?
So for example he can't use his password as DROP `Users`
That would delete your table Users. So with that function even if he uses that it won't delete your table.
After we are using INSERT INTO `Tablename` So in this case, it will insert the Username the password, and the money, then we are using VALUES, so it will insert in those rows, the selected values.
So in this case it will insert Username, his password, and his money
Lets try the login, so add this at the end of your script
pawn Код:
stock LoginPlayer(playerid,const password[])
{
new EscapedText[60];
mysql_real_escape_string(password, EscapedText);
format(Query,sizeof(Query),"SELECT * FROM `Users` WHERE `Username` = '%s' AND `Password` = '%s'",GetPName(playerid),EscapedText);
mysql_query(Query);
mysql_store_result();
if(mysql_num_rows() != 0)
{
SendClientMessage(playerid,green,"You have been logged in!");
LoadStats(playerid);
}
else
{
SendClientMessage(playerid,red,"Wrong password!");
Kick(playerid);
}
mysql_free_result();
return 1;
}
And this will check if the password is correct or not. Is not kick player, else load his status.
So we are using mysql_num_rows() != 0 again, so it will select the username and his password.
If the password is wrong, it wont select the password.
Add this in the OnDialogResponse
pawn Код:
if(dialogid == Logindialog)
{
if(strlen(inputtext) == 0)
{
ShowPlayerDialog(playerid,Regdialog,DIALOG_STYLE_I NPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose the password for it! \n","Register!","");
}
else
{
LoginPlayer(playerid,inputtext);
}
}
So on top of OnGameModeInit add this:
pawn Код:
enum PlayerInfo
{
Username[23],
Password[24],
Money
}
new PInfo[MAX_PLAYERS][PlayerInfo]
Now lets make it load the status:
pawn Код:
stock LoadStats(playerid)
{
new pName[24],Query[80];
GetPlayerName(playerid,pName,24);
format(Query, sizeof(Query), "SELECT * FROM `Users` WHERE `Username` = '%s' ", pName);
mysql_query(Query);
mysql_store_result();
mysql_fetch_row_format(Query, "|");
sscanf(Query, "e<p<|>s[24]s[23]i>", PInfo[playerid]);
mysql_free_result();
GivePlayerMoney(playerid,PInfo[playerid][Money]);
return 1;
}
Then below you can see we are giving his money.
So we are using mysq_fetch_row_format so it selects the selected row in sscanf, then put a |
Then it will separate each row by a | and in sscanf we are selecting 3 rows.
Any questions, or errors?
Ask'em here, ill try my best to help you, even tho im not a mysql genie :b