Advertisement
willbedie

[MySQL R41-4] Login / Register Base Script

Sep 20th, 2018
9,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3. #include <easyDialog>
  4.  
  5. #define MYSQL_HOSTNAME "localhost" // Change this to your own MySQL hostname
  6. #define MYSQL_USERNAME "root" // As above
  7. #define MYSQL_PASSWORD "" // Change this if you're using a password for your MySQL setup, I'm not using any so I'll leave it blank.
  8. #define MYSQL_DATABASE "willbedie" // Change this to your own MySQL Database
  9.  
  10. new
  11. MySQL: Database, // This is the handle.
  12. PlayerName[MAX_PLAYERS][30], // We will use this to store player's name
  13. PlayerIP[MAX_PLAYERS][17] // We will use this to store a player's IP Address
  14. ;
  15.  
  16. native WP_Hash(buffer[], len, const str[]); //This is a Whirlpool function, we will need that to store the passwords.
  17.  
  18. enum PlayerData // We'll create a new enum to store player's information(data)
  19. {
  20. ID,
  21. Password[129],
  22. Cash,
  23. Kills,
  24. Deaths
  25. };
  26. new PlayerInfo[MAX_PLAYERS][PlayerData];
  27.  
  28. public OnGameModeInit()
  29. {
  30. new MySQLOpt: option_id = mysql_init_options();
  31.  
  32. mysql_set_option(option_id, AUTO_RECONNECT, true); // it automatically reconnects when loosing connection to mysql server
  33.  
  34. Database = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE, option_id); // AUTO_RECONNECT is enabled for this connection handle only
  35. if (Database == MYSQL_INVALID_HANDLE || mysql_errno(Database) != 0)
  36. {
  37. print("MySQL connection failed. Server is shutting down."); // Read below
  38. SendRconCommand("exit"); // close the server if there is no connection
  39. return 1;
  40. }
  41.  
  42. SetGameModeText("SERVER VERSION");
  43. print("MySQL connection is successful."); // If the MySQL connection was successful, we'll print a debug!
  44. return 1;
  45. }
  46.  
  47. public OnPlayerConnect(playerid)
  48. {
  49. new query[140];
  50. GetPlayerName(playerid, PlayerName[playerid], 30); // This will get the player's name
  51. GetPlayerIp(playerid, PlayerIP[playerid], 16); // This will get the player's IP Address
  52.  
  53. mysql_format(Database, query, sizeof(query), "SELECT `Password`, `ID` FROM `users` WHERE `Username` = '%e' LIMIT 0, 1", PlayerName[playerid]); // We are selecting the password and the ID from the player's name
  54. mysql_tquery(Database, query, "CheckPlayer", "i", playerid);
  55. return 1;
  56. }
  57.  
  58. public OnPlayerDisconnect(playerid, reason)
  59. {
  60. SavePlayer(playerid);
  61. }
  62.  
  63. Dialog:DIALOG_LOGIN(playerid, response, listitem, inputtext[])
  64. {
  65. if(!response)
  66. return Kick(playerid); // If the player has pressed exit, kick them.
  67.  
  68. new password[129], query[100];
  69. WP_Hash(password, 129, inputtext); // We're going to hash the password the player has written in the login dialog
  70. if(!strcmp(password, PlayerInfo[playerid][Password])) // This will check if the password we used to register with matches
  71. { // If it matches
  72. mysql_format(Database, query, sizeof(query), "SELECT * FROM `users` WHERE `Username` = '%e' LIMIT 0, 1", PlayerName[playerid]);
  73. mysql_tquery(Database, query, "LoadPlayer", "i", playerid); //Let's call LoadPlayer.
  74. }
  75. else // If the password doesn't match.
  76. {
  77. Dialog_Show(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "{FF0000}Wrong Password!\n{FFFFFF}Type your correct password below to continue and sign in to your account", "Login", "Exit");
  78. // We will show this dialog to the player and tell them they have wrote an incorrect password.
  79. }
  80. return 1;
  81. }
  82.  
  83. Dialog:DIALOG_REGISTER(playerid, response, listitem, inputtext[])
  84. {
  85. if(!response)
  86. return Kick(playerid);
  87.  
  88. if(strlen(inputtext) < 3) return Dialog_Show(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "{FF0000}Short Password!\n{FFFFFF}Type a 3+ characters password if you want to register and play on this server", "Register", "Exit");
  89. //If the password is less than 3 characters, show them a dialog telling them to input a 3+ characters password
  90. new query[300];
  91. WP_Hash(PlayerInfo[playerid][Password], 129, inputtext); // Hash the password the player has wrote to the register dialog using Whirlpool.
  92. mysql_format(Database, query, sizeof(query), "INSERT INTO `users` (`Username`, `Password`, `IP`, `Cash`, `Kills`, `Deaths`) VALUES ('%e', '%e', '%e', 0, 0, 0)", PlayerName[playerid], PlayerInfo[playerid][Password], PlayerIP[playerid]);
  93. // Insert player's information into the MySQL database so we can load it later.
  94. mysql_pquery(Database, query, "RegisterPlayer", "i", playerid); // We'll call this as soon as the player successfully registers.
  95. return 1;
  96. }
  97.  
  98. forward CheckPlayer(playerid);
  99. public CheckPlayer(playerid)
  100. {
  101. new rows, string[150];
  102. cache_get_row_count(rows);
  103.  
  104. if(rows) // If row exists
  105. {
  106. cache_get_value_name(0, "Password", PlayerInfo[playerid][Password], 129); // Load the player's password
  107. cache_get_value_name_int(0, "ID", PlayerInfo[playerid][ID]); // Load the player's ID.
  108. format(string, sizeof(string), "Welcome back to the server.\nPlease type your password below to login to your account."); // A dialog will pop up telling the player to write they password below to login.
  109. Dialog_Show(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", string, "Login", "Exit");
  110. }
  111. else // If there are no rows, we need to show the register dialog!
  112. {
  113. format(string, sizeof(string), "Welcome to our server.\nIf you want to play here, you must register an account. Type a strong password below to register."); // A dialog with this note will pop up telling the player to register his acocunt.
  114. Dialog_Show(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", string, "Register", "Exit");
  115. }
  116. return 1;
  117. }
  118.  
  119. forward LoadPlayer(playerid);
  120. public LoadPlayer(playerid)
  121. {
  122. cache_get_value_name_int(0, "Cash", PlayerInfo[playerid][Cash]);
  123. cache_get_value_name_int(0, "Kills", PlayerInfo[playerid][Kills]);
  124. cache_get_value_name_int(0, "Deaths", PlayerInfo[playerid][Deaths]);
  125.  
  126. GivePlayerMoney(playerid, PlayerInfo[playerid][Cash]); //Load the player's cash and give it to them.
  127. return 1;
  128. }
  129.  
  130. forward SavePlayer(playerid);
  131. public SavePlayer(playerid)
  132. {
  133. new query[140];
  134. mysql_format(Database, query, sizeof(query), "UPDATE `users` SET `Cash` = '%d', `Kills` = '%d', `Deaths` = '%d' WHERE `ID` = '%d'", PlayerInfo[playerid][Cash], PlayerInfo[playerid][Kills], PlayerInfo[playerid][Deaths], PlayerInfo[playerid][ID]);
  135. // We will format the query to save the player and we will use this as soon as a player disconnects.
  136. mysql_tquery(Database, query); //We will execute the query.
  137. return 1;
  138. }
  139.  
  140. forward RegisterPlayer(playerid);
  141. public RegisterPlayer(playerid)
  142. {
  143. PlayerInfo[playerid][ID] = cache_insert_id();
  144. printf("A new account with the id of %d has been registered!", PlayerInfo[playerid][ID]); // You can remove this if you want, I just used it to debug.
  145. return 1;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement