Advertisement
Guest User

Untitled

a guest
Jan 6th, 2014
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.32 KB | None | 0 0
  1. //INCLUDE
  2. #include <a_samp>
  3. #include <a_mysql>
  4.  
  5. native WP_Hash(buffer[], len, const str[]); //whirlpool, for hashing our password
  6.  
  7. //**********************************************FORWARD***********************************
  8. forward OnAccountCheck(playerid);
  9. forward OnAccountLoad(playerid);
  10.  
  11. //DEFINE
  12. #define mysql_host "127.0.0.1"
  13. #define mysql_user "root"
  14. #define mysql_password ""
  15. #define mysql_database ""
  16.  
  17. main() {}
  18.  
  19. //****************************************************************************************
  20. //***********************************************VARIABLES*********************************
  21.  
  22. static
  23. mysql, //This variable will be used to manage our database
  24. Name[MAX_PLAYERS][MAX_PLAYER_NAME], //We will use this variable to store player's name.
  25. IP[MAX_PLAYERS][16]; //We will use this variable to store player's ip.
  26.  
  27. //***************************************************************************************
  28. //**********************************************ENUM**************************************
  29.  
  30. enum PDATA //We name our enumerator as PDATA (which stands for PlayerDATA). You can name it however you want.
  31. {
  32. ID, //Will be used later to store player's ID from database so we can use it anywhere later
  33. Password[129], //We will load player's password into this varible from database.
  34. Admin, //We will load player's admin level from database into this variable so we can use it anywhere later.
  35. VIP, //We will load player's VIP level from database into this variable so we can use it anywhere later.
  36. Argent, //We will load player's money from database into this variable so we can use it anywhere later.
  37. Float:posX, //We will load player's X position from database into this variable so we can use it anywhere later.
  38. Float:posY, //We will load player's Y position from database into this variable so we can use it anywhere later.
  39. Float:posZ //We will load player's Z from database into this variable so we can use it anywhere later.
  40.  
  41. }
  42.  
  43. new pInfo[MAX_PLAYERS][PDATA]; //Variable that stores enumerator above
  44.  
  45. //***************************************************************************************
  46. // **********************************************STOCK************************************
  47.  
  48.  
  49. // ******************************************************************************************
  50.  
  51. public OnGameModeInit()
  52. {
  53. mysql_debug(1); //Let's enable debugging so we can detect a problem(if there is)
  54. mysql = mysql_connect(mysql_host, mysql_user, mysql_database, mysql_password); //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.
  55. if(mysql_errno(mysql) != 0) print("Could not connect to database!"); //This will tell if your connection to database is successful or not. If it's not, check your host, username, database and password. Make sure they all right.
  56.  
  57. //If we're cool, it's time to create a table for your database.
  58. mysql_tquery(mysql, "CREATE TABLE IF NOT EXISTS `players`(\
  59. `ID` int(10) NOT NULL AUTO_INCREMENT, \
  60. `Username` varchar(24) NOT NULL, \
  61. `Password` varchar(129) NOT NULL, \
  62. `IP` varchar(16) NOT NULL, \
  63. `Admin` int(10) NOT NULL, \
  64. `VIP` int(10) NOT NULL, \
  65. `Argent` int(10) NOT NULL, \
  66. `posX` float NOT NULL, \
  67. `posY` float NOT NULL, \
  68. `posZ` float NOT NULL, \
  69. PRIMARY KEY (`ID`))", "", "");
  70. /*We will use mysql_tquery to execute a query
  71. We are creating a table called `players`.
  72. `ID` int(10) NOT NULL AUTO_INCREMENT, = We are creating a column inside of the table called ID and it must be int since ID is an integer. NOT NULL means the column must not be blank(e.g not inserting value into it). AUTO_INCREMENT, it automatically generates a new ID everytime player registers(1,2,3,4 ... and so on)
  73. `Username` varchar(24) NOT NULL, = We are creating a column inside of the table called Username. It will be used for storing player's name in it and since player's name is a string, we must use varchar
  74. `Password` varchar(129) NOT NULL, = Same as above
  75. `IP` varchar(16) NOT NULL, = Same as above
  76. `Admin` int(10) NOT NULL, = Same as above
  77. `VIP` int(10) NOT NULL, = Same as above
  78. `Money` int(10) NOT NULL, = Same as above
  79. `posX` float NOT NULL, = posX is player's X position. So it must be float
  80. `posY` float NOT NULL, = Same as above
  81. `posZ` float NOT NULL, = Same as above
  82. "", ""); = We leave this 2 parameters(callback[] and format[]) empty because we only gonna need it if we use SELECT
  83. */
  84.  
  85. return 1;
  86. }
  87.  
  88. public OnGameModeExit()
  89. {
  90. mysql_close(mysql);
  91. return 1;
  92. }
  93.  
  94. public OnPlayerRequestClass(playerid, classid)
  95. {
  96. SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
  97. SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
  98. SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
  99. return 1;
  100. }
  101.  
  102. public OnPlayerConnect(playerid)
  103. {
  104. new query[128]; //We use this variable to format our query
  105. GetPlayerName(playerid, Name[playerid], 24); //Getting player's name
  106. GetPlayerIp(playerid, IP[playerid], 16); //Getting layer's IP
  107. mysql_format(mysql, query, sizeof(query),"SELECT * FROM `players` WHERE `Username` = '%e' LIMIT 1", Name[playerid]);
  108. // - We use mysql_format instead of format because we can use an %e specifier. %e specifier escapes a string so we can avoid sql injection which means we don't have to use mysql_real_escape_string
  109. // - Formatting our query; SELECT * FROM `players` WHERE `Username`='%e' means we are selecting all rows in the table called `players` that has your name in Username column
  110. // - LIMIT 1; we only need 1 result to be shown
  111. mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid);
  112. //lets execute the formatted query and when the execution is done, a callback OnAccountCheck will be called
  113. //You can name the callback however you like
  114. return 1;
  115. }
  116.  
  117. public OnPlayerDisconnect(playerid, reason)
  118. {
  119. 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
  120. GetPlayerPos(playerid, pos[0], pos[1], pos[2]); //let's get player's position when they leave your server
  121. mysql_format(mysql, query, sizeof(query), "UPDATE `players` SET `Admin`=%d, `VIP`=%d, `Argent`=%d, `posX`=%f, `posY`=%f, `posZ`=%f WHERE `ID`=%d",\
  122. pInfo[playerid][Admin], pInfo[playerid][VIP], pInfo[playerid][Argent], pos[0], pos[1], pos[2], pInfo[playerid][ID]);
  123. //We update the table(`players`) by getting player's admin level, vip level, money, and positions and save them in the database
  124. mysql_tquery(mysql, query, "", "");
  125. //let's execute the query.
  126.  
  127. return 1;
  128. }
  129.  
  130. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  131. {
  132. new query[300];
  133. switch(dialogid)
  134. {
  135. case 1: //login dialog
  136. {
  137. if(!response) Kick(playerid); //if they clicked Quit, we will kick them
  138. new hpass[129]; //for password hashing
  139. WP_Hash(hpass, 129, inputtext); //hashing inputtext
  140. 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
  141. { //if the hashed password matches with the loaded password from database
  142. mysql_format(mysql, query, sizeof(query), "SELECT * FROM `players` WHERE `Username` = '%e' LIMIT 1", Name[playerid]);
  143. //let's format our query
  144. //once again, we select all rows in the table that has your name and limit the result
  145. mysql_tquery(mysql, query, "OnAccountLoad", "i", playerid);
  146. //lets execute the formatted query and when the execution is done, a callback OnAccountLoad will be called
  147. //You can name the callback however you like
  148. }
  149. else //if the hashed password didn't match with the loaded password(pInfo[playerid][Password])
  150. {
  151. //we tell them that they have inserted a wrong password
  152. ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Login", "In order to play, you need to login\nWrong password!", "Login", "Quit");
  153. }
  154. }
  155. case 2: //register dialog
  156. {
  157. if(!response) return Kick(playerid); //if they clicked Quit, we will kick them
  158. if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.\nYour password must be at least 6 characters long!", "Register", "Quit");
  159. //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!
  160. WP_Hash(pInfo[playerid][Password], 129, inputtext); //hashing inputtext
  161. 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]);
  162. //Now let's create a new row and insert player's information in it
  163. mysql_tquery(mysql, query, "", "");
  164. //let's execute the query
  165. }
  166. }
  167.  
  168.  
  169. return 1;
  170. }
  171.  
  172. public OnAccountCheck(playerid)
  173. {
  174. new rows, fields; //a variable that will be used to retrieve rows and fields in the database.
  175. cache_get_data(rows, fields, mysql);//let's get the rows and fields from the database.
  176. if(rows) //if there is row
  177. {//then
  178. cache_get_row(0, 2, pInfo[playerid][Password], mysql, 129);
  179. //we will load their password into pInfo[playerid][Password] to be used in logging in
  180. //0 is row, 2 is field. If you're wondering why row is 0; remember we used LIMIT 1 while checking player's account. LIMIT is a limitation of results to be shown. Since we used LIMIT 1, it'd be like;
  181. // ---> row> ID | Username | Password | IP | Admin | .... and so on
  182. // ^ ^ ^
  183. // fields 0 1 2
  184. //So we're getting row 0, field 2 which is password
  185.  
  186. pInfo[playerid][ID] = cache_get_row_int(0, 0); //now let's load player's ID into pInfo[playerid][ID] so we can use it later
  187. 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
  188. ShowPlayerDialog(playerid, 1, 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
  189. }
  190. else //if we didn't find any rows from the database, that means, no accounts were found
  191. {
  192. ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.", "Register", "Quit");
  193. //So we show the a dialog register
  194. }
  195. return 1;
  196. }
  197.  
  198.  
  199. public OnAccountLoad(playerid)
  200. {
  201. // ---> row> ID | Username | Password | IP | Admin | VIP | Money | posX | posY | posZ
  202. // ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
  203. // fields 0 1 2 3 4 5 6 7 8 9
  204.  
  205. pInfo[playerid][Admin] = cache_get_row_int(0, 4); //we're getting a field 4 from row 0. And since it's an integer, we use cache_get_row_int
  206. pInfo[playerid][VIP] = cache_get_row_int(0, 5); //Above
  207. pInfo[playerid][Argent] = cache_get_row_int(0, 6);//Above
  208. pInfo[playerid][posX] = cache_get_row_float(0, 7);//Above. Since player's position is a float, we use cache_get_row_float
  209. pInfo[playerid][posY] = cache_get_row_float(0, 8);//Above
  210. pInfo[playerid][posZ] = cache_get_row_float(0, 9);//Above
  211.  
  212. GivePlayerMoney(playerid, pInfo[playerid][Argent]);//Let's set their money
  213. //For player's position, set it once they spawn(OnPlayerSpawn)
  214. SendClientMessage(playerid, -1, "Successful logged in"); //tell them that they have succesfully logged in
  215. return 1;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement