//OnAccountCheck is a custom callback which means it has to be forwarded.
forward OnAccountCheck(playerid);
//Now once the query has been processed;
public OnAccountCheck(playerid)
{
new rows, fields, str[128]; //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 (cache_num_rows()) // if (rows)
{//then
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
format(str, sizeof(str), "In order to play, you need to login!\n\nYour Account Name:%s\n\nYour Account ID:%d\n\nYour IP: %s",Name[playerid], pInfo[playerid][ID], IP[playerid]);
ShowPlayerDialog(playerid, dlogin, DIALOG_STYLE_INPUT, "Login", str, "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, dregister, DIALOG_STYLE_INPUT, "Register", "you have to register to play.", "Register", "Quit");
//So we show them a dialog register
}
return 1;
}
switch(dialogid)
{
case dlogin: //login dialog
{
if(!response) Kick(playerid); //if they clicked Quit, we will kick them
new hpass[129]; //for password hashing
new query[100]; // 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 `players` WHERE `Username` = '%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, dlogin, DIALOG_STYLE_INPUT, "Login", "In order to play, you need to login\nWrong password!", "Login", "Quit");
}
}
case dregister: //register dialog
{
if(!response) return Kick(playerid); //if they clicked Quit, we will kick them
if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, dregister, DIALOG_STYLE_INPUT, "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[300];
WP_Hash(pInfo[playerid][Password], 129, inputtext); //hashing inputtext
mysql_format(mysql, query, sizeof(query), "INSERT INTO `players` (`Username`, `Password`, `IP`, `Admin`,`Money`, `Cookies`,`Kills`,`Deaths`,`Score`,`Banned` , `Bannedfor`, `Bannedby`,`BannedDate`) VALUES ('%e', '%s', '%s', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)", Name[playerid], pInfo[playerid][Password], IP[playerid]);
//Now let's create a new row and insert player's information in it
printf("the query %s",query);
mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid);
//let's execute the query
}
public SavePlayerData()
{
new playerid;
if(IsPlayerConnected(playerid))
{
for (new i = 0; i < MAX_PLAYERS; i++)
{
new query[300];
mysql_format(mysql, query, sizeof(query), "UPDATE `players` SET `Admin`=%d,`Money`=%d,`Cookies`=%d,`Kills`=%d,`Deaths`=%d,`Score`=%d,`Banned`=%d,`Bannedfor`=%s,`Bannedby`=%s,`BannedDate`=%d, WHERE `ID`=%d",\
pInfo[i][Admin], pInfo[i][Money], pInfo[i][Cookies], pInfo[i][Kills], pInfo[i][Deaths],pInfo[i][Score],pInfo[i][Banned], pInfo[i][Bannedfor], pInfo[i][Bannedby], pInfo[i][BannedDate] ,pInfo[i][ID]);
//We update the table(`players`) by getting player's admin level, vip level, money, and positions and save them in the database
mysql_tquery(mysql, query, "", "");
}
new str[128];
SendClientMessageToAll(0x009D00DB,"Your player data have been saved into(Database)");
format(str, sizeof(str),"* Admin AutoSave (ID:007) saved all regged player stats (Total %d)",GetOnLinePlayers());
IRC_GroupSay(groupID, IRC_CHANNEL, str);
}
return 1;
}
forward OnAccountLoad(playerid);
forward OnAccountRegister(playerid);
//let's load player's information
public OnAccountLoad(playerid)
{
new str[128];
pInfo[playerid][Admin] = cache_get_field_content_int(0, "Admin"); //we're getting a field 4 from row 0. And since it's an integer, we use cache_get_row_int
pInfo[playerid][Money] = cache_get_field_content_int(0, "Money");//Above
pInfo[playerid][Cookies] = cache_get_field_content_int(0, "Cookies");//Above
pInfo[playerid][Kills] = cache_get_field_content_int(0, "Kills");//Above
pInfo[playerid][Deaths] = cache_get_field_content_int(0, "Deaths");//Above
pInfo[playerid][Score] = cache_get_field_content_int(0, "Score");//Above
pInfo[playerid][Banned] = cache_get_field_content_int(0, "Banned");
pInfo[playerid][Banned] = cache_get_field_content_int(0, "Bannedfor");
pInfo[playerid][Banned] = cache_get_field_content_int(0, "Bannedby");
pInfo[playerid][BannedDate] = cache_get_field_content_int(0,"BannedDate");
GivePlayerMoney(playerid, pInfo[playerid][Money]);//Let's set their money
//For player's position, set it once they spawn(OnPlayerSpawn)
format(str, sizeof(str), "8,2%s Successfully logged in as Level %d",Name[playerid],pInfo[playerid][Admin]);
IRC_GroupSay(groupID, IRC_CHANNEL, str);
format(str, sizeof(str), "%s Successfully logged in as Level %d",Name[playerid],pInfo[playerid][Admin]);
SendClientMessage(playerid, msgs, str); //tell them that they have successfully logged in
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 registerd. ID: %d", pInfo[playerid][ID]); //just for debugging.
return 1;
}
new query[128]; //query[128] is for formatting our query and Float:pos[3] is for getting and saving player's position
mysql_format(mysql, query, sizeof(query), "UPDATE `players` SET `Admin`=%d,`Money`=%d,`Cookies`=%d,`Kills`=%d,`Deaths`=%d,`Score`=%d,`Banned`=%d,`Bannedfor`=%s,`Bannedby`=%s,`BannedDate`=%d, WHERE `ID`=%d",\
pInfo[playerid][Admin], pInfo[playerid][Money], pInfo[playerid][Cookies], pInfo[playerid][Kills], pInfo[playerid][Deaths],pInfo[playerid][Score],pInfo[playerid][Banned], pInfo[playerid][Bannedfor], pInfo[playerid][Bannedby], pInfo[playerid][BannedDate] ,pInfo[playerid][ID]);
mysql_tquery(mysql, query, "", "");
public OnPlayerDisconnect(playerid, reason)
{
new name[MAX_PLAYER_NAME]; GetPlayerName(playerid, name, MAX_PLAYER_NAME);
mysql_format(mysql, query, sizeof query, "SELECT * FROM `players` WHERE `Username`='%s'", name);
mysql_tquery(mysql, query, "SavePlayerData", "i", playerid);
return 1;
}
pawn Код:
|
He wasn't even supposed to use a select statement.
You are supposed to use an UPDATE or an INSERT depending on what you're looking for. |