08-06-2019, 06:32 AM
Introduce
Hello!
I am new to this forum. I made a SAMP server script. My friend and I have created our server, and it turns out that the server is in great demand, because I haven't received a report that the server has a bug, so I decided to use my time helping people, who want to learn server scripts
Now in this tutorial I will create a [Login / Register] system, using Y_INI.
* What is Y_INI?
- [font=open_sansregular, Tahoma, Arial, sans-serif]Y_INI is a reader and writes data .INI or better known as '' FILE MANAGER SYSTEM '' Many SAMP users use Y_INI to create a LOGIN / REGISTER system[/font]
* Developer of Y_INI [font=arial, sans-serif]Alex "Y_Less" Cole[/font]
[font=open_sansregular, Tahoma, Arial, sans-serif]Tutorial[/font]
1. Add Y_INI include to the top of your script.
2. Lets define the dialogs.
3. Lets define the Colors. You can found the color here https://htmlcolorcodes.com/
4. Now define the 'Path' for .INI file.
5. Now create enum, for store our variables.
6. Now create the function for load the player's data.
7. Now create stock for UserPath. The stock function 'UserPath' is merely going to 'grab' the 'PATH' of the User's file.
8. Add this code bellow your previous stock function. ( UserPath ) * The stock above is a simple 'hasher', and will be used to hash passwords, Credits to Dracoblue.
9. Now move to 'OnPlayerConnect' callback to check whether the player is register or not.
[!] >> We'll be using the native 'fexist' function to search for our file. Parameters are set to our stock function which we've created. If the file exists, you will receive a 'Login' dialog. If it doesn't, you will receive a register dialog.
10. Now move to 'OnDialogResponse' for make dialog in register / login.
[!] Instead of using the 'if' statement to define my dialogs, I've used cases as they seem to take less space and are supposedly 'faster'. The (!response) is the function if the first Button hasn't been clicked, it will then kick the player.
The if(!strlen(inputtext)) explains if nothing has been entered into the dialog (input), you would then be prompted to another dialog which shows you 'Incorrect Password'.
If all goes well, the function INI_Open is then 'executed' which loads and opens the Userfile. Once open, the function 'INI_WriteInt' is then called and starts writing the data into the userfile. The udb_hash would generate a hash code from the player's inputtext (what you've typed). And after all this is completed, it is then closed by 'INI_Close' function.
Once finished, you will then be prompted to the 'Login' dialog.
In 'DIALOG_LOGIN', if the response is false (you have clicked 'QUIT), you would then be kicked. If given the correct information (password provided), the INI_Parsefile function would then scan and load your player's data.
11. Don't forget, you would need a way to save those variables. The OnPlayerDisconnect callback simply re-opens the files, write what-ever values which has been stored and then closes it.
12. Finally, add this to OnPlayerDeath to add value(s) to kills and deaths.
[font=open_sansregular, Tahoma, Arial, sans-serif]Download[/font]
[font=open_sansregular, Tahoma, Arial, sans-serif]Y_INI https://github.com/Southclaws/YSI-4.0/bl.../y_ini.inc[/font]
[font=open_sansregular, Tahoma, Arial, sans-serif][font=open_sansregular, Tahoma, Arial, sans-serif][font=open_sansregular, Tahoma, Arial, sans-serif]Credits[/font][/font][/font]
[font=open_sansregular, Tahoma, Arial, sans-serif][font=open_sansregular, Tahoma, Arial, sans-serif][font=open_sansregular, Tahoma, Arial, sans-serif]Y-Less for 'Y_INI'[/font][/font][/font]
Hello!
I am new to this forum. I made a SAMP server script. My friend and I have created our server, and it turns out that the server is in great demand, because I haven't received a report that the server has a bug, so I decided to use my time helping people, who want to learn server scripts
Now in this tutorial I will create a [Login / Register] system, using Y_INI.
* What is Y_INI?
- [font=open_sansregular, Tahoma, Arial, sans-serif]Y_INI is a reader and writes data .INI or better known as '' FILE MANAGER SYSTEM '' Many SAMP users use Y_INI to create a LOGIN / REGISTER system[/font]
* Developer of Y_INI [font=arial, sans-serif]Alex "Y_Less" Cole[/font]
[font=open_sansregular, Tahoma, Arial, sans-serif]Tutorial[/font]
1. Add Y_INI include to the top of your script.
PHP Code: (Select All)
#include <YSI/y_ini>
2. Lets define the dialogs.
PHP Code: (Select All)
#define DIALOG_REGISTER 1
#define DIALOG_LOGIN 2
#define DIALOG_SUCCESS_1 3
#define DIALOG_SUCCESS_2 4
3. Lets define the Colors. You can found the color here https://htmlcolorcodes.com/
PHP Code: (Select All)
#define COL_WHITE "{FFFFFF}
#define COL_RED "{FF0000}"
#define COL_BLUE "{0000FF}"
#define COL_GREEN "{00FF00}"
4. Now define the 'Path' for .INI file.
PHP Code: (Select All)
#define PATH "/Users/%s.ini"
5. Now create enum, for store our variables.
PHP Code: (Select All)
enum pInfo
{
pPass,
pCash,
pAdmin,
pKills,
pDeaths
}
new PlayerInfo[MAX_PLAYERS][pInfo];
6. Now create the function for load the player's data.
PHP Code: (Select All)
forward LoadUser_data(playerid,name[],value[]);
public LoadUser_data(playerid,name[],value[])
{
INI_Int("Password",PlayerInfo[playerid][pPass]);
INI_Int("Cash",PlayerInfo[playerid][pCash]);
INI_Int("Admin",PlayerInfo[playerid][pAdmin]);
INI_Int("Kills",PlayerInfo[playerid][pKills]);
INI_Int("Deaths",PlayerInfo[playerid][pDeaths]);
return 1;
}
7. Now create stock for UserPath. The stock function 'UserPath' is merely going to 'grab' the 'PATH' of the User's file.
PHP Code: (Select All)
stock UserPath(playerid)
{
new string[128],playername[MAX_PLAYER_NAME];
GetPlayerName(playerid,playername,sizeof(playername));
format(string,sizeof(string),PATH,playername);
return string;
}
8. Add this code bellow your previous stock function. ( UserPath ) * The stock above is a simple 'hasher', and will be used to hash passwords, Credits to Dracoblue.
PHP Code: (Select All)
stock udb_hash(buf[]) {
new length=strlen(buf);
new s1 = 1;
new s2 = 0;
new n;
for (n=0; n<length; n++)
{
s1 = (s1 + buf[n]) % 65521;
s2 = (s2 + s1) % 65521;
}
return (s2 << 16) + s1;
}
9. Now move to 'OnPlayerConnect' callback to check whether the player is register or not.
PHP Code: (Select All)
public OnPlayerConnect(playerid)
{
if(fexist(UserPath(playerid)))
{
INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""COL_WHITE"Login",""COL_WHITE"Type your password below to login.","Login","Quit");
}
else
{
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT,""COL_WHITE"Registering...",""COL_WHITE"Type your password below to register a new account.","Register","Quit");
}
return 1;
}
10. Now move to 'OnDialogResponse' for make dialog in register / login.
PHP Code: (Select All)
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch( dialogid )
{
case DIALOG_REGISTER:
{
if (!response) return Kick(playerid);
if(response)
{
if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, ""COL_WHITE"Registering...",""COL_RED"You have entered an invalid password.\n"COL_WHITE"Type your password below to register a new account.","Register","Quit");
new INI:File = INI_Open(UserPath(playerid));
INI_SetTag(File,"data");
INI_WriteInt(File,"Password",udb_hash(inputtext));
INI_WriteInt(File,"Cash",0);
INI_WriteInt(File,"Admin",0);
INI_WriteInt(File,"Kills",0);
INI_WriteInt(File,"Deaths",0);
INI_Close(File);
SetSpawnInfo(playerid, 0, 0, 1958.33, 1343.12, 15.36, 269.15, 0, 0, 0, 0, 0, 0);
SpawnPlayer(playerid);
ShowPlayerDialog(playerid, DIALOG_SUCCESS_1, DIALOG_STYLE_MSGBOX,""COL_WHITE"Success!",""COL_GREEN"Great! Your Y_INI system works perfectly. Relog to save your stats!","Ok","");
}
}
case DIALOG_LOGIN:
{
if ( !response ) return Kick ( playerid );
if( response )
{
if(udb_hash(inputtext) == PlayerInfo[playerid][pPass])
{
INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);
GivePlayerMoney(playerid, PlayerInfo[playerid][pCash]);
ShowPlayerDialog(playerid, DIALOG_SUCCESS_2, DIALOG_STYLE_MSGBOX,""COL_WHITE"Success!",""COL_GREEN"You have successfully logged in!","Ok","");
}
else
{
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""COL_WHITE"Login",""COL_RED"You have entered an incorrect password.\n"COL_WHITE"Type your password below to login.","Login","Quit");
}
return 1;
}
}
}
return 1;
}
[!] Instead of using the 'if' statement to define my dialogs, I've used cases as they seem to take less space and are supposedly 'faster'. The (!response) is the function if the first Button hasn't been clicked, it will then kick the player.
The if(!strlen(inputtext)) explains if nothing has been entered into the dialog (input), you would then be prompted to another dialog which shows you 'Incorrect Password'.
If all goes well, the function INI_Open is then 'executed' which loads and opens the Userfile. Once open, the function 'INI_WriteInt' is then called and starts writing the data into the userfile. The udb_hash would generate a hash code from the player's inputtext (what you've typed). And after all this is completed, it is then closed by 'INI_Close' function.
Once finished, you will then be prompted to the 'Login' dialog.
In 'DIALOG_LOGIN', if the response is false (you have clicked 'QUIT), you would then be kicked. If given the correct information (password provided), the INI_Parsefile function would then scan and load your player's data.
11. Don't forget, you would need a way to save those variables. The OnPlayerDisconnect callback simply re-opens the files, write what-ever values which has been stored and then closes it.
PHP Code: (Select All)
public OnPlayerDisconnect(playerid, reason)
{
new INI:File = INI_Open(UserPath(playerid));
INI_SetTag(File,"data");
INI_WriteInt(File,"Cash",GetPlayerMoney(playerid));
INI_WriteInt(File,"Admin",PlayerInfo[playerid][pAdmin]);
INI_WriteInt(File,"Kills",PlayerInfo[playerid][pKills]);
INI_WriteInt(File,"Deaths",PlayerInfo[playerid][pDeaths]);
INI_Close(File);
return 1;
}
12. Finally, add this to OnPlayerDeath to add value(s) to kills and deaths.
PHP Code: (Select All)
public OnPlayerDeath(playerid, killerid, reason)
{
PlayerInfo[killerid][pKills]++;
PlayerInfo[playerid][pDeaths]++;
return 1;
}
[font=open_sansregular, Tahoma, Arial, sans-serif]Y_INI https://github.com/Southclaws/YSI-4.0/bl.../y_ini.inc[/font]
[font=open_sansregular, Tahoma, Arial, sans-serif][font=open_sansregular, Tahoma, Arial, sans-serif][font=open_sansregular, Tahoma, Arial, sans-serif]Credits[/font][/font][/font]
[font=open_sansregular, Tahoma, Arial, sans-serif][font=open_sansregular, Tahoma, Arial, sans-serif][font=open_sansregular, Tahoma, Arial, sans-serif]Y-Less for 'Y_INI'[/font][/font][/font]