gta6287

Login/Register Tutorial by newbienoob

Jun 20th, 2012
1,321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 11.37 KB | None | 0 0
  1. // This is a comment
  2. // uncomment the line below if you want to write a filterscript
  3. //#define FILTERSCRIPT
  4.  
  5. #include <a_samp>
  6. #include <YSI\y_ini>
  7. #define dregister 2011 //Defining register dialog so it won't mixed up with other dialog
  8. #define dlogin 2012 //Defining login dialog so it won't mixed up with other dialog
  9. #define UserPath "Users/%s.ini" //Will define user account path. In this case, will store in Scriptfiles/Users. So create a file inside of your Scriptfiles folder called Users
  10. native WP_Hash(buffer[],len,const str[]); // Whirlpool native, add it at the top of your script under includes
  11. enum PlayerInfo
  12. {
  13.     Pass[129], //User's password
  14.     Adminlevel, //User's admin level
  15.     VIPlevel, //User's vip level
  16.     Money, //User's money
  17.     Scores, //User's scores
  18.     Kills, //User's kills
  19.     Deaths //User's deaths
  20. }
  21. new pInfo[MAX_PLAYERS][PlayerInfo]; //This will create a new variable so we can later use it to saving/loading user's info.
  22.  
  23. stock Path(playerid) //Will create a new stock so we can easily use it later to load/save user's data in user's path
  24. {
  25.     new str[128],name[MAX_PLAYER_NAME];
  26.     GetPlayerName(playerid,name,sizeof(name));
  27.     format(str,sizeof(str),UserPath,name);
  28.     return str;
  29. }
  30.  
  31. forward loadaccount_user(playerid, name[], value[]); //forwarding a new function to load user's data
  32. //Now we will use our own function that we have created above
  33. public loadaccount_user(playerid, name[], value[])
  34. {
  35.     INI_String("Password", pInfo[playerid][Pass],129); /*we will use INI_String to load user's password.
  36.     ("Password",.. will load user's password inside of user's path. 'pInfo[playerid][Pass]',...We have defined our user's variable above called, pInfo. Now it's time to use it here to load user's password. '129',... 129 is a length or hashed user's password. Whirlpool will hash 128 characters + NULL*/
  37.     INI_Int("AdminLevel",pInfo[playerid][Adminlevel]);/*We will use INI_Int to load user's admin level. INI_Int stands for INI_Integer. This load an admin level. */
  38.     INI_Int("VIPLevel",pInfo[playerid][VIPlevel]);//As explained above
  39.     INI_Int("Money",pInfo[playerid][Money]); //As explained above
  40.     INI_Int("Scores",pInfo[playerid][Scores]);//As explained above
  41.     INI_Int("Kills",pInfo[playerid][Kills]);//As explained above
  42.     INI_Int("Deaths",pInfo[playerid][Deaths]);//As explained above
  43.     return 1;
  44. }
  45.  
  46. #if defined FILTERSCRIPT
  47.  
  48. public OnFilterScriptInit()
  49. {
  50.     print("\n--------------------------------------");
  51.     print(" Blank Filterscript by your name here");
  52.     print("--------------------------------------\n");
  53.     return 1;
  54. }
  55.  
  56. public OnFilterScriptExit()
  57. {
  58.     return 1;
  59. }
  60.  
  61. #else
  62.  
  63. main()
  64. {
  65.     print("\n----------------------------------");
  66.     print(" Blank Gamemode by your name here");
  67.     print("----------------------------------\n");
  68. }
  69.  
  70. #endif
  71.  
  72. public OnGameModeInit()
  73. {
  74.     // Don't use these lines if it's a filterscript
  75.     SetGameModeText("Blank Script");
  76.     AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
  77.     return 1;
  78. }
  79.  
  80. public OnGameModeExit()
  81. {
  82.     return 1;
  83. }
  84.  
  85. public OnPlayerRequestClass(playerid, classid)
  86. {
  87.     SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
  88.     SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
  89.     SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
  90.     return 1;
  91. }
  92.  
  93. public OnPlayerConnect(playerid)
  94. {
  95.     new name[MAX_PLAYER_NAME]; //Making a new variable called 'name'. name[MAX_PLAYER_NAME] is created so we can use it to get player's name.
  96.     GetPlayerName(playerid,name,sizeof(name)); //Get player's name
  97.     if(fexist(Path(playerid))) /* Check if the connected user is registered or not. fexist stands for file exist. So if file exist in the files(Path(playerid)),*/
  98.     {// then
  99.         INI_ParseFile(Path(playerid),"loadaccount_user", .bExtra = true, .extra = playerid); //Will load user's data using INI_Parsefile.
  100.         ShowPlayerDialog(playerid,dlogin,DIALOG_STYLE_INPUT,"Login","Welcome back. This account is registered. \nInsert your password to login to your account","Login","Quit");/*A dialog with input style will appear so you can insert your password to login.*/
  101.     }
  102.     else //If the connected user is not registered,
  103.     {//then we will 'force' him to register :)
  104.         ShowPlayerDialog(playerid,dregister,DIALOG_STYLE_INPUT,"Register","Welcome! This account is not registered.\nEnter your own password to create a new account.","Register","Quit");
  105.         return 1;
  106.     }
  107.     return 1;
  108. }
  109.  
  110. public OnPlayerDisconnect(playerid, reason)
  111. {
  112.     //Same as OnDialogResponse, we will save their stats inside of their user's account
  113.     if(fexist(Path(playerid)))//Will check if the file is exit or not inside of User's folder that we have created.
  114.     {
  115.         new INI:file = INI_Open(Path(playerid)); //will open their their file
  116.         INI_SetTag(file,"Player's Data");//We will set a tag inside of user's account called "Player's Data"
  117.         INI_WriteInt(file,"AdminLevel",pInfo[playerid][Adminlevel]); //If you've set his/her admin level, then his/her admin level will be saved inside of his/her account
  118.         INI_WriteInt(file,"VIPLevel",pInfo[playerid][VIPlevel]);//As explained above
  119.         INI_WriteInt(file,"Money",GetPlayerMoney(playerid));//We will save his score inside of his account
  120.         INI_WriteInt(file,"Scores",GetPlayerScore(playerid));//As explained above
  121.         INI_WriteInt(file,"Kills",pInfo[playerid][Kills]);//As explained above
  122.         INI_WriteInt(file,"Deaths",pInfo[playerid][Deaths]);//As explained above
  123.         INI_Close(file);//Now after we've done saving their data, we now need to close the file
  124.         return 1;
  125.     }
  126.     return 1;
  127. }
  128.  
  129. public OnPlayerSpawn(playerid)
  130. {
  131.     return 1;
  132. }
  133.  
  134. public OnPlayerDeath(playerid, killerid, reason)
  135. {
  136.     pInfo[killerid][Kills]++;//Will give 1 score to killer and it will be saved inside of his/her account
  137.     pInfo[playerid][Deaths]++;//Will give 1 deaths each time they die and it will be saved inside of his/her account
  138.     return 1;
  139. }
  140.  
  141. public OnVehicleSpawn(vehicleid)
  142. {
  143.     return 1;
  144. }
  145.  
  146. public OnVehicleDeath(vehicleid, killerid)
  147. {
  148.     return 1;
  149. }
  150.  
  151. public OnPlayerText(playerid, text[])
  152. {
  153.     return 1;
  154. }
  155.  
  156. public OnPlayerCommandText(playerid, cmdtext[])
  157. {
  158.     if (strcmp("/mycommand", cmdtext, true, 10) == 0)
  159.     {
  160.         // Do something here
  161.         return 1;
  162.     }
  163.     return 0;
  164. }
  165.  
  166. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  167. {
  168.     return 1;
  169. }
  170.  
  171. public OnPlayerExitVehicle(playerid, vehicleid)
  172. {
  173.     return 1;
  174. }
  175.  
  176. public OnPlayerStateChange(playerid, newstate, oldstate)
  177. {
  178.     return 1;
  179. }
  180.  
  181. public OnPlayerEnterCheckpoint(playerid)
  182. {
  183.     return 1;
  184. }
  185.  
  186. public OnPlayerLeaveCheckpoint(playerid)
  187. {
  188.     return 1;
  189. }
  190.  
  191. public OnPlayerEnterRaceCheckpoint(playerid)
  192. {
  193.     return 1;
  194. }
  195.  
  196. public OnPlayerLeaveRaceCheckpoint(playerid)
  197. {
  198.     return 1;
  199. }
  200.  
  201. public OnRconCommand(cmd[])
  202. {
  203.     return 1;
  204. }
  205.  
  206. public OnPlayerRequestSpawn(playerid)
  207. {
  208.     return 1;
  209. }
  210.  
  211. public OnObjectMoved(objectid)
  212. {
  213.     return 1;
  214. }
  215.  
  216. public OnPlayerObjectMoved(playerid, objectid)
  217. {
  218.     return 1;
  219. }
  220.  
  221. public OnPlayerPickUpPickup(playerid, pickupid)
  222. {
  223.     return 1;
  224. }
  225.  
  226. public OnVehicleMod(playerid, vehicleid, componentid)
  227. {
  228.     return 1;
  229. }
  230.  
  231. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  232. {
  233.     return 1;
  234. }
  235.  
  236. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  237. {
  238.     return 1;
  239. }
  240.  
  241. public OnPlayerSelectedMenuRow(playerid, row)
  242. {
  243.     return 1;
  244. }
  245.  
  246. public OnPlayerExitedMenu(playerid)
  247. {
  248.     return 1;
  249. }
  250.  
  251. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  252. {
  253.     return 1;
  254. }
  255.  
  256. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  257. {
  258.     return 1;
  259. }
  260.  
  261. public OnRconLoginAttempt(ip[], password[], success)
  262. {
  263.     return 1;
  264. }
  265.  
  266. public OnPlayerUpdate(playerid)
  267. {
  268.     return 1;
  269. }
  270.  
  271. public OnPlayerStreamIn(playerid, forplayerid)
  272. {
  273.     return 1;
  274. }
  275.  
  276. public OnPlayerStreamOut(playerid, forplayerid)
  277. {
  278.     return 1;
  279. }
  280.  
  281. public OnVehicleStreamIn(vehicleid, forplayerid)
  282. {
  283.     return 1;
  284. }
  285.  
  286. public OnVehicleStreamOut(vehicleid, forplayerid)
  287. {
  288.     return 1;
  289. }
  290.  
  291. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  292. {
  293.     if(dialogid == dregister) //If dialog id is a register dialog
  294.     {//then
  295.         if(!response) return Kick(playerid); //If they clicked the second button "Quit", we will kick them.
  296.         if(response) //if they clicked the first button "Register"
  297.         {//then
  298.             if(!strlen(inputtext)) //If they didn't enter any password
  299.             {// then we will tell to them to enter the password to register
  300.                 ShowPlayerDialog(playerid,dregister,DIALOG_STYLE_INPUT,"Register","Welcome! This account is not registered.\nEnter your own password to create a new account.\nPlease enter the password!","Register","Quit");
  301.                 return 1;
  302.             }
  303.             //If they has entered a password for his account...
  304.             new hashpass[129]; //Now we will create a new variable to hash his/her password
  305.             WP_Hash(hashpass,sizeof(hashpass),inputtext);//We will use whirlpool to has their password
  306.             new INI:file = INI_Open(Path(playerid)); //will create a new variable to register their acount inside of Scriptfiles/Users folder
  307.             INI_SetTag(file,"Player's Data");//We will set a tag inside of user's account called "Player's Data"
  308.             INI_WriteString(file,"Password",hashpass);//This will write a hashed password in of user's account
  309.             INI_WriteInt(file,"AdminLevel",0); //Write an integer inside of user's account called "AdminLevel". We will set his level to 0 after he registered.
  310.             INI_WriteInt(file,"VIPLevel",0);//As explained above
  311.             INI_WriteInt(file,"Money",0);//Write an integer inside of user's account called "Money". We will set their money to 0 after he registered
  312.             INI_WriteInt(file,"Scores",0);//As explained above
  313.             INI_WriteInt(file,"Kills",0);//As explained above
  314.             INI_WriteInt(file,"Deaths",0);//As explained above
  315.             INI_Close(file);//Now after we've done saving their data, we now need to close the file
  316.             SendClientMessage(playerid,-1,"You have been successfully registered");//Tell to them that they have successfully registered a new account
  317.             return 1;
  318.         }
  319.     }
  320.     if(dialogid == dlogin) //If dialog id is a login dialog
  321.     {//then
  322.         if(!response) return Kick(playerid); //If they clicked the second button "Quit", we will kick them.
  323.         if(response) //if they clicked the first button "Register"
  324.         {//then
  325.             new hashpass[129]; //Will create a new variable to hash his/her password
  326.             WP_Hash(hashpass,sizeof(hashpass),inputtext); //Will hash inputted password
  327.             if(!strcmp(hashpass,pInfo[playerid][Pass])) //If they have entered a correct password
  328.             {//then
  329.                 INI_ParseFile(Path(playerid),"loadaccount_user",.bExtra = true, .extra = playerid);//We will load his account's data from user's path
  330.                 SetPlayerScore(playerid,pInfo[playerid][Scores]);//We will get their score inside of his user's account and we will set it here
  331.                 GivePlayerMoney(playerid,pInfo[playerid][Money]);//As explained above
  332.                 SendClientMessage(playerid,-1,"Welcome back! You have successfully logged in");//Tell them that they've successfully logged in
  333.             }
  334.             else //If they've entered an incorrect password
  335.             {//then
  336.                 ShowPlayerDialog(playerid,dlogin,DIALOG_STYLE_INPUT,"Login","Welcome back. This account is registered. \nInsert your password to login to your account.\nIncorrect password!","Login","Quit");//We will tell to them that they've entered an incorrect password
  337.                 return 1;
  338.             }
  339.         }
  340.     }
  341.     return 1;
  342. }
  343.  
  344. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  345. {
  346.     return 1;
  347. }
Advertisement
Add Comment
Please, Sign In to add comment