Guest User

Untitled

a guest
Jan 9th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3. #include <core>
  4. #include <float>
  5.  
  6. #pragma tabsize 0
  7.  
  8. main()
  9. {
  10. print("\n----------------------------------");
  11. print(" Bare Script\n");
  12. print("----------------------------------\n");
  13. }
  14.  
  15. //dialozi
  16. #define host "localhost" //This will be your mysql host. Default for xampp is localhost
  17. #define user "root" //This will be your mysql username. Default for xampp is root
  18. #define db "server" //This is your database name. Remember we have created a database called server before.
  19. #define pass "" //This is your mysql password. In xampp, the password didn't set. So leave it empty.
  20.  
  21. //dialogs
  22. #define DIALOG_REGISTER 1 //dialog register id
  23. #define DIALOG_LOGIN 2 // ^
  24.  
  25. static
  26. mysql, //This variable will be used to manage our database
  27. Name[MAX_PLAYERS][24], //We will use this variable to store player's name.
  28. IP[MAX_PLAYERS][16] //We will use this variable to store player's ip.
  29. ;
  30.  
  31. native WP_Hash(buffer[], len, const str[]); //whirlpool, for hashing our password
  32.  
  33. enum PDATA //We name our enumerator as PDATA (which stands for PlayerDATA). You can name it however you want.
  34. {
  35. ID, //Will be used later to store player's ID from database so we can use it anywhere later
  36. Password[129], //We will load player's password into this varible from database
  37. Admin, //We will load player's admin level from database into this variable so we can use it anywhere later.
  38. VIP, //We will load player's VIP level from database into this variable so we can use it anywhere later.
  39. Money, //We will load player's money from database into this variable so we can use it anywhere later.
  40. Float:posX, //We will load player's X position from database into this variable so we can use it anywhere later.
  41. Float:posY, //We will load player's Y position from database into this variable so we can use it anywhere later.
  42. Float:posZ //We will load player's Z from database into this variable so we can use it anywhere later.
  43. }
  44. new pInfo[MAX_PLAYERS][PDATA]; //Variable that stores enumerator above
  45.  
  46.  
  47. //OnAccountCheck is a custom callback which means it has to be forwarded.
  48. forward OnAccountCheck(playerid);
  49.  
  50. //Now once the query has been processed;
  51. public OnAccountCheck(playerid)
  52. {
  53. new rows, fields; //a variable that will be used to retrieve rows and fields in the database.
  54. cache_get_data(rows, fields, mysql);//let's get the rows and fields from the database.
  55. if(rows) //if there is row
  56. {//then
  57. cache_get_field_content(0, "PASS", pInfo[playerid][Password], mysql, 129);
  58. //we will load player's password into pInfo[playerid][Password] to be used in logging in
  59. pInfo[playerid][ID] = cache_get_field_content_int(0, "ID"); //now let's load player's ID into pInfo[playerid][ID] so we can use it later
  60. printf("%s", pInfo[playerid][Password]); //OPTIONAL: Just for debugging. If it didn't show your password, then there must be something wrong while getting player's password
  61. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", "In order to play, you need to login", "Login", "Quit"); //And since we found a result from the database, which means, there is an account; we will show a login dialog
  62. }
  63. else //if we didn't find any rows from the database, that means, no accounts were found
  64. {
  65. ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.", "Register", "Quit");
  66. //So we show them a dialog register
  67. }
  68. return 1;
  69. }
  70.  
  71.  
  72. public OnPlayerConnect(playerid)
  73. {
  74. new query[128]; //We use this variable to format our query
  75. GetPlayerName(playerid, Name[playerid], 24); //Getting player's name
  76. GetPlayerIp(playerid, IP[playerid], 16); //Getting layer's IP
  77. mysql_format(mysql, query, sizeof(query),"SELECT `Password`, `ID` FROM `players` WHERE `Username` = '%e' LIMIT 1", Name[playerid]);
  78. mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid);
  79.  
  80.  
  81. return 1;
  82. }
  83.  
  84. public OnPlayerSpawn(playerid)
  85. {
  86. SetPlayerPos(playerid, pInfo[playerid][posX], pInfo[playerid][posZ], pInfo[playerid][posZ]);
  87. return 1;
  88. }
  89.  
  90. public OnPlayerDeath(playerid, killerid, reason)
  91. {
  92. return 1;
  93. }
  94.  
  95.  
  96.  
  97. public OnPlayerRequestClass(playerid, classid)
  98. {
  99.  
  100. return 1;
  101. }
  102.  
  103. public OnGameModeInit()
  104. {
  105. mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG); //Let's enable debugging so we can detect a problem(if there is)
  106. mysql = mysql_connect(host, user, db, pass); //This function will connect your server to database. Remember we have defined our host, username, database and password. It's time to use it here.
  107. if(mysql_errno(mysql) != 0) print("Could not connect to database!");
  108. return 1;
  109. }
  110.  
  111. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  112. {
  113. switch(dialogid)
  114. {
  115. case DIALOG_LOGIN: //login dialog
  116. {
  117. if(!response) Kick(playerid); //if they clicked Quit, we will kick them
  118. new hpass[129]; //for password hashing
  119. new query[100]; // for formatting our query.
  120. WP_Hash(hpass, 129, inputtext); //hashing inputtext
  121. if(!strcmp(hpass, pInfo[playerid][Password])) //remember we have loaded player's password into this variable, pInfo[playerid][Password] earlier. Now let's use it to compare the hashed password with password that we load
  122. { //if the hashed password matches with the loaded password from database
  123. mysql_format(mysql, query, sizeof(query), "SELECT * FROM `players` WHERE `Username` = '%e' LIMIT 1", Name[playerid]);
  124. //let's format our query
  125. //We select all rows in the table that has your name and limit the result to 1
  126. mysql_tquery(mysql, query, "OnAccountLoad", "i", playerid);
  127. //lets execute the formatted query and when the execution is done, a callback OnAccountLoad will be called
  128. //You can name the callback however you like
  129. }
  130. else //if the hashed password didn't match with the loaded password(pInfo[playerid][Password])
  131. {
  132. //we tell them that they have inserted a wrong password
  133. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", "In order to play, you need to login\nWrong password!", "Login", "Quit");
  134. }
  135. }
  136. case DIALOG_REGISTER: //register dialog
  137. {
  138. if(!response) return Kick(playerid); //if they clicked Quit, we will kick them
  139. if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.\nYour password must be at least 6 characters long!", "Register", "Quit");
  140. //strlen checks a lenght of a string. so if player types their password that is lower than 6, we tell them; Your password must be at least 6 characters long!
  141. new query[300];
  142. WP_Hash(pInfo[playerid][Password], 129, inputtext); //hashing inputtext
  143. mysql_format(mysql, query, sizeof(query), "INSERT INTO `players` (`Username`, `Password`, `IP`, `Admin`, `VIP`, `Money`, `PosX` ,`PosY`, `PosZ`) VALUES ('%e', '%s', '%s', 0, 0, 0, 0.0, 0.0, 0.0)", Name[playerid], pInfo[playerid][Password], IP[playerid]);
  144. //Now let's create a new row and insert player's information in it
  145. mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid);
  146. //let's execute the query
  147. }
  148. }
  149. return 1;
  150. }
  151.  
  152. forward OnAccountLoad(playerid);
  153. forward OnAccountRegister(playerid);
  154. //let's load player's information
  155. public OnAccountLoad(playerid)
  156. {
  157. pInfo[playerid][Admin] = cache_get_field_content_int(0, "Admin"); //we're getting a field 4 from row 0. And since it's an integer, we use cache_get_row_int
  158. pInfo[playerid][VIP] = cache_get_field_content_int(0, "VIP"); //Above
  159. pInfo[playerid][Money] = cache_get_field_content_int(0, "Money");//Above
  160. pInfo[playerid][posX] = cache_get_field_content_float(0, "PosX");//Above. Since player's position is a float, we use cache_get_field_content_float
  161. pInfo[playerid][posY] = cache_get_field_content_float(0, "PosY");//Above
  162. pInfo[playerid][posZ] = cache_get_field_content_float(0, "PosZ");//Above
  163.  
  164. GivePlayerMoney(playerid, pInfo[playerid][Money]);//Let's set their money
  165. //For player's position, set it once they spawn(OnPlayerSpawn)
  166. SendClientMessage(playerid, -1, "Successfully logged in"); //tell them that they have successfully logged in
  167. return 1;
  168. }
  169.  
  170. public OnAccountRegister(playerid)
  171. {
  172. pInfo[playerid][ID] = cache_insert_id(); //loads the ID of the player in the variable once they registered.
  173. printf("New account registered. ID: %d", pInfo[playerid][ID]); //just for debugging.
  174. return 1;
  175. }
  176.  
  177. public OnPlayerDisconnect(playerid, reason)
  178. {
  179. new query[128], Float:pos[3]; //query[128] is for formatting our query and Float:pos[3] is for getting and saving player's position
  180. GetPlayerPos(playerid, pos[0], pos[1], pos[2]); //let's get player's position when they leave your server
  181. mysql_format(mysql, query, sizeof(query), "UPDATE `players` SET `Admin`=%d, `VIP`=%d, `Money`=%d, `posX`=%f, `posY`=%f, `posZ`=%f WHERE `ID`=%d",\
  182. pInfo[playerid][Admin], pInfo[playerid][VIP], pInfo[playerid][Money], pos[0], pos[1], pos[2], pInfo[playerid][ID]);
  183. //We update the table(`players`) by getting player's admin level, vip level, money, and positions and save them in the database
  184. mysql_tquery(mysql, query, "", "");
  185. //let's execute the query.
  186. return 1;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment