arrow_upward

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Server [ Login / Register ]
#1
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.
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=0n<lengthn++)
 
   {
 
      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(playeridDIALOG_LOGINDIALOG_STYLE_INPUT,""COL_WHITE"Login",""COL_WHITE"Type your password below to login.","Login","Quit");
 
   }
 
   else
    
{
 
       ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_INPUT,""COL_WHITE"Registering...",""COL_WHITE"Type your password below to register a new account.","Register","Quit");
 
   }
 
   return 1;

[!] >> 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.
PHP Code: (Select All)
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
 
   switchdialogid )
 
   {
 
       case DIALOG_REGISTER:
 
       {
 
           if (!response) return Kick(playerid);
 
           if(response)
 
           {
 
               if(!strlen(inputtext)) return ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_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(playerid001958.331343.1215.36269.15000000);
 
               SpawnPlayer(playerid);
 
               ShowPlayerDialog(playeridDIALOG_SUCCESS_1DIALOG_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 );
 
           ifresponse )
 
           {
 
               if(udb_hash(inputtext) == PlayerInfo[playerid][pPass])
 
               {
 
                   INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra true, .extra playerid);
 
                   GivePlayerMoney(playeridPlayerInfo[playerid][pCash]);
 
                   ShowPlayerDialog(playeridDIALOG_SUCCESS_2DIALOG_STYLE_MSGBOX,""COL_WHITE"Success!",""COL_GREEN"You have successfully logged in!","Ok","");
 
               }
 
               else
                
{
 
                   ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_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(playeridreason)
{
 
   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(playeridkilleridreason)
{
 
   PlayerInfo[killerid][pKills]++;
 
   PlayerInfo[playerid][pDeaths]++;
 
   return 1;

[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]
#2
man . samp scripts is c++? or other lang ? can u help me in create a rpg server in samp? i'm mta scriptr and i'm lua scriptr . if you want help about lua script tell me anytime Smile
Thanks Post4VPS
#3
(08-06-2019, 11:58 AM)hamed Wrote: man . samp scripts is c++? or other lang ? can u help me in create a rpg server in samp? i'm mta scriptr and i'm lua scriptr . if you want help about lua script tell me anytime Smile


Hi hamed. To learn the basics of server scripts, you can learn at  [font=arial, sans-serif][url=https://forum.sa-mp.com/][/url]https://forum.sa-mp.com/ [/font]and also if you have problems with your script, you can ask for help there, we are ready to help.


Possibly Related Threads…
Thread
Author
Replies
Views
Last Post
2,540
08-22-2019, 05:41 PM
Last Post: Mashiro
11,478
03-21-2019, 09:12 PM
Last Post: amontes

person_pin_circle Users browsing this thread: 1 Guest(s)
Sponsors: VirMach - Host4Fun - CubeData - Evolution-Host - HostDare - Hyper Expert - Shadow Hosting - Bladenode - Hostlease - RackNerd - ReadyDedis - Limitless Hosting