public OnPlayerConnect(playerid)
{
SetPlayerColor(playerid, 0xBEBEBEFF);
new query[1024];
GetPlayerName(playerid, Name[playerid], 24);
GetPlayerIp(playerid, IP[playerid], 16);
mysql_format(mysql, query, sizeof(query),"SELECT `Password`, `ID` FROM `accounts` WHERE `Name` = '%e' LIMIT 1", Name[playerid]);
// - We use mysql_format instead of format because we can use an %e specifier. %e specifier escapes a string so we can avoid sql injection which means we don't have to use mysql_real_escape_string
// - Formatting our query; SELECT `Password`, `ID` FROM `players` WHERE `Username`='%e' means we are selecting a Password and ID's column in the table that has player's name in Username column.
// - LIMIT 1; we only need 1 result to be shown
mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid);
//lets execute the formatted query and when the execution is done, a callback OnAccountCheck will be called
//You can name the callback however you like
return 1;
}
forward OnAccountCheck(playerid);
public OnAccountCheck(playerid)
{
new rows, fields; //a variable that will be used to retrieve rows and fields in the database.
cache_get_data(rows, fields, mysql);//let's get the rows and fields from the database.
if(rows)
{
cache_get_field_content(0, "Password", pInfo[playerid][Password], mysql, 129);
//we will load player's password into pInfo[playerid][Password] to be used in logging in
pInfo[playerid][ID] = cache_get_field_content_int(0, "ID"); //now let's load player's ID into pInfo[playerid][ID] so we can use it later
//printf("%s", pInfo[playerid][Password]); //OPTIONAL: Just for debugging. If it didn't show your password, then there must be something wrong while getting player's password
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "In order to play, you need to login", "Login", "Quit"); //And since we found a result from the database, which means, there is an account; we will show a login dialog
}
else //if we didn't find any rows from the database, that means, no accounts were found
{
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "In order to play, you need to register.", "Register", "Quit");
//So we show them a dialog register
}
return 1;
}
public OnAccountRegister(playerid)
{
pInfo[playerid][ID] = cache_insert_id(); //loads the ID of the player in the variable once they registered.
printf("New account registered. ID: %d", pInfo[playerid][ID]); //just for debugging.
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
new Query[256];
GetPlayerPos(playerid, pInfo[playerid][posX], pInfo[playerid][posY], pInfo[playerid][posZ]);
mysql_format(mysql, Query, sizeof(Query), "UPDATE accounts SET Score='%d', VIP='%d', Money='%d', PosX='%.6f', PosY='%.6f', PosZ='%.6f' WHERE ID='%d'",
pInfo[playerid][Score], pInfo[playerid][VIP], pInfo[playerid][Money], pInfo[playerid][posX], pInfo[playerid][posY], pInfo[playerid][posZ], pInfo[playerid][ID]);
printf("%s", Query);
mysql_tquery(mysql, Query, "", "");
return 1;
}
public OnPlayerSpawn(playerid)
{
SetPlayerMapIcon( playerid, 1, 701.8041,-519.4351,16.3318, 55, 0, MAPICON_LOCAL );
SetPlayerMapIcon( playerid, 2, 661.3626,-573.4266,16.3359, 52, 0, MAPICON_LOCAL );
SetPlayerColor(playerid, 0xFFFFFFFF);
SetPlayerInterior(playerid,0);
SetPlayerVirtualWorld(playerid,0);
SetTimerEx("SetPos", 600, false, "i", playerid);
SetPlayerSkin(playerid, pInfo[playerid][pSkin]);
SetPlayerPos(playerid, pInfo[playerid][posX], pInfo[playerid][posZ], pInfo[playerid][posZ]);
return 1;
}
forward SetPos(playerid);
public SetPos(playerid)
{
SetPlayerPos(playerid, pInfo[playerid][posX], pInfo[playerid][posZ], pInfo[playerid][posZ]);
return 1;
}
OnPlayerDisconnect is unreliable for many things. This callback gets called after the player has already disconnected so you can't get his position anymore.
|
Iґve already fixxed the problem with saving/reading the floats but it rounds up/down itself and thats kinda f***ed up
The other problem is that I cant register a new account, it usually should create a new row with the next higher ID but it just doesnt How can I fix this 2 problems? |
The above code doesn't have any problems, show your register dialog.
|
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case DIALOG_LOGIN: //login dialog
{
if(!response) Kick(playerid); //if they clicked Quit, we will kick them
new hpass[129]; //for password hashing
new query[1024]; // for formatting our query.
WP_Hash(hpass, 129, inputtext); //hashing inputtext
if(!strcmp(hpass, pInfo[playerid][Password])) //remember we have loaded player's password into this variable, pInfo[playerid][Password] earlier. Now let's use it to compare the hashed password with password that we load
{ //if the hashed password matches with the loaded password from database
mysql_format(mysql, query, sizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e' LIMIT 1", Name[playerid]);
//let's format our query
//We select all rows in the table that has your name and limit the result to 1
mysql_tquery(mysql, query, "OnAccountLoad", "i", playerid);
//lets execute the formatted query and when the execution is done, a callback OnAccountLoad will be called
//You can name the callback however you like
}
else //if the hashed password didn't match with the loaded password(pInfo[playerid][Password])
{
//we tell them that they have inserted a wrong password
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "In order to play, you need to login\nWrong password!", "Login", "Quit");
}
}
case DIALOG_REGISTER: //register dialog
{
if(!response) return Kick(playerid); //if they clicked Quit, we will kick them
if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "In order to play, you need to register.\nYour password must be at least 6 characters long!", "Register", "Quit");
//strlen checks a lenght of a string. so if player types their password that is lower than 6, we tell them; Your password must be at least 6 characters long!
new query[1024];
WP_Hash(pInfo[playerid][Password], 129, inputtext); //hashing inputtext
mysql_format(mysql, query, sizeof(query), "INSERT INTO `accounts` (`Name`, `Password`, `PosX`, `PosY`, `PosZ`, `FacingAngel`) VALUES ('%e', '%s', 701.8041,-519.4351,16.3318,261.1537)", Name[playerid], pInfo[playerid][Password]);
//Now let's create a new row and insert player's information in it
mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid);
//let's execute the query
}
}
return 1;
}
Here we go
PHP код:
|