Advertisement
willbedie

[MySQL R41-4] Login / Register Base Script (BCrypt)

Oct 25th, 2018
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3. #include <bcrypt>
  4. #include <easyDialog>
  5.  
  6. #define BCRYPT_COST 12
  7.  
  8. #define MYSQL_HOSTNAME "localhost" // Change this to your own MySQL hostname
  9. #define MYSQL_USERNAME "root" // Change this
  10. #define MYSQL_PASSWORD "" // If you have a password, type it there. If you don't leave it blank.
  11. #define MYSQL_DATABASE "willbedie" // Change this
  12.  
  13. new
  14. MySQL: Database,
  15. bool:LoggedIn[MAX_PLAYERS]
  16. ;
  17.  
  18. enum PlayerData
  19. {
  20. user_id,
  21. user_cash,
  22. user_kills,
  23. user_deaths
  24. };
  25. new PlayerInfo[MAX_PLAYERS][PlayerData];
  26.  
  27. public OnGameModeInit()
  28. {
  29. Database = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
  30. if(Database == MYSQL_INVALID_HANDLE || mysql_errno(Database) != 0)
  31. {
  32. print("SERVER: MySQL Connection failed, shutting the server down!");
  33. SendRconCommand("exit");
  34. return 1;
  35. }
  36.  
  37. SetGameModeText("willbedie");
  38. print("SERVER: MySQL Connection was successful.");
  39. return 1;
  40. }
  41.  
  42. public OnPlayerConnect(playerid)
  43. {
  44. new query[200];
  45. mysql_format(Database, query, sizeof(query), "SELECT * FROM `players` WHERE `Username` = '%e'", GetName(playerid));
  46. mysql_tquery(Database, query, "CheckAccount", "d", playerid);
  47. return 1;
  48. }
  49.  
  50. public OnPlayerDisconnect(playerid)
  51. {
  52. new query[200];
  53. mysql_format(Database, query, sizeof(query), "UPDATE `players` SET `Cash` = '%i', `Kills` = '%i', `Deaths` = '%i' WHERE `ID` = '%i'", PlayerInfo[playerid][user_cash], PlayerInfo[playerid][user_kills], PlayerInfo[playerid][user_deaths], PlayerInfo[playerid][user_id]);
  54. mysql_query(Database, query);
  55. return 1;
  56. }
  57.  
  58. forward CheckAccount(playerid);
  59. public CheckAccount(playerid)
  60. {
  61. new string[300];
  62. if(cache_num_rows())
  63. {
  64. format(string, sizeof(string), "{FFFFFF}Welcome back to {AFAFAF}Server{FFFFFF}%s. Please input your password below to log-in.", GetName(playerid));
  65. Dialog_Show(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login to the server", string, "Login", "Dip");
  66. }
  67. else
  68. {
  69. format(string, sizeof(string), "{FFFFFF}Welcome to our server, %s. Please type a strong password below to continue.", GetName(playerid));
  70. Dialog_Show(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register to the server", string, "Register", "Dip");
  71. }
  72. return 1;
  73. }
  74.  
  75. Dialog:DIALOG_REGISTER(playerid, response, listitem, inputtext[])
  76. {
  77. if(response)
  78. {
  79. bcrypt_hash(inputtext, BCRYPT_COST, "OnPasswordHashed", "d", playerid);
  80. }
  81. else
  82. Kick(playerid);
  83. return 1;
  84. }
  85.  
  86. Dialog:DIALOG_LOGIN(playerid, response, listitem, inputtext[])
  87. {
  88. if(response)
  89. {
  90. new query[300], Password[BCRYPT_HASH_LENGTH];
  91. mysql_format(Database, query, sizeof(query), "SELECT `Password` FROM `players` WHERE `Username` = '%e'", GetName(playerid));
  92. mysql_query(Database, query);
  93. cache_get_value_name(0, "Password", Password, BCRYPT_HASH_LENGTH);
  94. bcrypt_check(inputtext, Password, "OnPasswordChecked", "d", playerid);
  95. }
  96. else
  97. Kick(playerid);
  98. return 1;
  99. }
  100.  
  101. forward OnPasswordHashed(playerid);
  102. public OnPasswordHashed(playerid)
  103. {
  104. new hash[BCRYPT_HASH_LENGTH], query[300];
  105. bcrypt_get_hash(hash);
  106. mysql_format(Database, query, sizeof(query), "INSERT INTO `players` (`Username`, `Password`, `IPAddress`, `Cash`, `Kills`, `Deaths`) VALUES ('%e', '%e', '%e', 0, 0, 0)", GetName(playerid), hash, ReturnIP(playerid));
  107. mysql_tquery(Database, query, "OnPlayerRegister", "d", playerid);
  108. return 1;
  109. }
  110.  
  111. forward OnPlayerRegister(playerid);
  112. public OnPlayerRegister(playerid)
  113. {
  114. SpawnPlayer(playerid);
  115. SendClientMessage(playerid, -1, "You have been successfully registered in our server.");
  116. return 1;
  117. }
  118.  
  119. forward OnPasswordChecked(playerid);
  120. public OnPasswordChecked(playerid)
  121. {
  122. new bool:match = bcrypt_is_equal();
  123. if(match)
  124. {
  125. new query[300];
  126. mysql_format(Database, query, sizeof(query), "SELECT * FROM `players` WHERE `Username` = '%e'", GetName(playerid));
  127. mysql_tquery(Database, query, "OnPlayerLoad", "d", playerid);
  128. }
  129. else
  130. {
  131. new string[100];
  132. format(string, sizeof(string), "Wrong Password!\nPlease type your correct password below.");
  133. Dialog_Show(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login to our server", string, "Register", "Dip");
  134. }
  135. return 1;
  136. }
  137.  
  138. forward OnPlayerLoad(playerid);
  139. public OnPlayerLoad(playerid)
  140. {
  141. cache_get_value_name_int(0, "ID", PlayerInfo[playerid][user_id]);
  142. cache_get_value_name_int(0, "Cash", PlayerInfo[playerid][user_cash]);
  143. cache_get_value_name_int(0, "Kills", PlayerInfo[playerid][user_kills]);
  144. cache_get_value_name_int(0, "Deaths", PlayerInfo[playerid][user_deaths]);
  145.  
  146. LoggedIn[playerid] = true;
  147. SendClientMessage(playerid, -1, "Welcome back to our server.");
  148. return 1;
  149. }
  150.  
  151. GetName(playerid)
  152. {
  153. new name[MAX_PLAYER_NAME];
  154. GetPlayerName(playerid, name, sizeof(name));
  155. return name;
  156. }
  157.  
  158. ReturnIP(playerid)
  159. {
  160. new PlayerIP[17];
  161. GetPlayerIp(playerid, PlayerIP, sizeof(PlayerIP));
  162. return PlayerIP;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement