PDA

View Full Version : [Tutorial] Creating password changing system using dialogs



SergiuOfficial
18-06-17, 11:10
Introduction
I have not seen any kind of tutorial related to password changing system with dialogs and nicely written code (with no loose indentation and bugs or bad coding skills). This code is fully written and tested by myself. And i will recommend everyone to read every single code and information instead of copy and pasting (which I'm personally sure that most people do). I basically made this for my gamemode, using the AndySedeyn's Y_INI tutorial and I will prefer, you guys to use the AndySedeyn's tutorial (link is given below).

Necessities
- A script which uses YSI library and whirlpool or a new script which uses AndySedeyn's tutorial : http://forum.sa-mp.com/showthread.php?t=597639.
- The YSI library: http://forum.sa-mp.com/showthread.php?t=570883
- Whirlpool: http://forum.sa-mp.com/showthread.php?t=570945
- ZCMD command processor : http://forum.sa-mp.com/showthread.php?t=91354

Script

PHP Code:
enum Dialog_Data // all of the data of the 'enum' initialiser, is saved in the memory.
{
DIALOG_CHANGEPS,
DIALOG_CHANGEPS2
};
As we are going to use dialogs for changing the password, I prefer using the dialog ids with 'enum' initialiser, for ease and no tension related to mixing/collision of the dialog ids.

PHP Code:
CMD:changepass(playerid, params[])
{
ShowPlayerDialog(playerid, DIALOG_CHANGEPS, DIALOG_STYLE_INPUT, "Changing Password - Step 1", "Input your current password, in order to change your password.", "Next", "Cancel"); // showing the player a dialog
return 1;
}
We have created a command using the ZCMD command processor, and we are showing the player a dialog, to enter his current password, so he/she can move on to the next and final step of changing password.

PHP Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case DIALOG_CHANGEPS:
{
if(response)
{
new hashpass[129]; // the whirlpool uses a 129 length string for hashing the password.

WP_Hash(hashpass, sizeof(hashpass), inputtext); // to hash the input data

if(!strcmp(hashpass, PlayerInfo[playerid][Password])) // if the password matches, continue to step 2
{
ShowPlayerDialog(playerid, DIALOG_CHANGEPS2, DIALOG_STYLE_INPUT, "Changing Password - Step 2", "Input your desired new password, in order to comple the process.", "Okay", "Cancel"); // showing the player a second and final dialog to enter his new password
}
else
{ // if the password is incorrect, re-show them to enter the current and correct password
SendClientMessage(playerid, darkred, "ERROR: You have entered an incorrect password."); // no need of explanation
ShowPlayerDialog(playerid, DIALOG_CHANGEPS, DIALOG_STYLE_INPUT, "Changing Password - Step 1", "Input your current password, in order to change your password.", "Next", "Cancel"); // showing player the dialog to enter his correct and current password
}
}
return 1;
} // end of the dialog part one
read the commented lines in the code above.

PHP Code:
case DIALOG_CHANGEPS2:
{
if(response)
{
if(!strlen(inputtext)) // if nothing is entered in the dialog
{
SendClientMessage(playerid, darkred, "ERROR: You must enter a password.");
}
new string[128];

format(string, sizeof(string), "Your password is: %s", inputtext); // we have formatted a string to include variables inside it
SendClientMessage(playerid, -1, string); // sending the above string message

new INI:file = INI_Open(UserPath(playerid)); // opened the player's save file
WP_Hash(PlayerInfo[playerid][Password], 129, inputtext); // hashed the password in the player enum
INI_WriteString(file, "Password", PlayerInfo[playerid][Password]); // replaced the old hashed password with the new one
INI_Close(file);
return 1;
}
}
}
return 0;
} // On Dialog Response code end
Read the commented lines in the code above.

And we are done. If you find any issue or bugs or typos, feel free to notify me. If you liked the tutorial and/or going to use it.