Advertisement
Guest User

MYSQL

a guest
May 28th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.68 KB | None | 0 0
  1. // GM Sa-Mp BlackFlash
  2.  
  3. #include <a_samp> //Without this, we won't be able to use all samp functions/callbacks
  4. #include <a_mysql> //Without this, we won't be able to use all mysql functions
  5.  
  6. #define host "localhost" //This will be your mysql host. Default for xampp is localhost
  7. #define user "root" //This will be your mysql username. Default for xampp is root
  8. #define db "flash" //This is your database name. Remember we have created a database called server before.
  9. #define pass "" //This is your mysql password. In xampp, the password didn't set. So leave it empty.
  10.  
  11. //dialogs
  12. #define dregister 6287 //dialog register id
  13. #define dlogin 6288 // ^
  14.  
  15. static
  16. mysql, //This variable will be used to manage our database
  17. Name[MAX_PLAYERS][24], //We will use this variable to store player's name.
  18. IP[MAX_PLAYERS][16] //We will use this variable to store player's ip.
  19. ;
  20.  
  21. native WP_Hash(buffer[], len, const str[]); //whirlpool, for hashing our password
  22.  
  23. enum PDATA //We name our enumerator as PDATA (which stands for PlayerDATA). You can name it however you want.
  24. {
  25. ID, //Will be used later to store player's ID from database so we can use it anywhere later
  26. Password[129], //We will load player's password into this varible from database
  27. Admin, //We will load player's admin level from database into this variable so we can use it anywhere later.
  28. VIP, //We will load player's VIP level from database into this variable so we can use it anywhere later.
  29. Money, //We will load player's money from database into this variable so we can use it anywhere later.
  30. Float:posX, //We will load player's X position from database into this variable so we can use it anywhere later.
  31. Float:posY, //We will load player's Y position from database into this variable so we can use it anywhere later.
  32. Float:posZ //We will load player's Z from database into this variable so we can use it anywhere later.
  33.  
  34. }
  35. new pInfo[MAX_PLAYERS][PDATA]; //Variable that stores enumerator above
  36.  
  37. public OnGameModeInit()
  38. {
  39. mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG); //Let's enable debugging so we can detect a problem(if there is)
  40. 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.
  41. 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.
  42. return 1;
  43. }
  44. main()
  45. {
  46. print("\n----------------------------------");
  47. print(" Blank Gamemode by your name here");
  48. print("----------------------------------\n");
  49. }
  50.  
  51. public OnGameModeExit()
  52. {
  53. return 1;
  54. }
  55.  
  56. public OnPlayerRequestClass(playerid, classid)
  57. {
  58. SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
  59. SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
  60. SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
  61. return 1;
  62. }
  63.  
  64. public OnPlayerConnect(playerid)
  65. {
  66. new query[128]; //We use this variable to format our query
  67. GetPlayerName(playerid, Name[playerid], 24); //Getting player's name
  68. GetPlayerIp(playerid, IP[playerid], 16); //Getting layer's IP
  69. mysql_format(mysql, query, sizeof(query),"SELECT `Password`, `ID` FROM `players` WHERE `Username` = '%e' LIMIT 1", Name[playerid]);
  70. // - 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
  71. // - Formatting our query; SELECT `Password`, `ID` FROM `players` WHERE `Username`='%e' means we are selecting a Password and ID's column in the table that has player's name in Username column.
  72. // - LIMIT 1; we only need 1 result to be shown
  73. mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid);
  74. //lets execute the formatted query and when the execution is done, a callback OnAccountCheck will be called
  75. //You can name the callback however you like
  76.  
  77. return 1;
  78. }
  79.  
  80. forward OnAccountCheck(playerid);
  81.  
  82. //Now once the query has been processed;
  83. public OnAccountCheck(playerid)
  84. {
  85. new rows, fields; //a variable that will be used to retrieve rows and fields in the database.
  86. cache_get_data(rows, fields, mysql);//let's get the rows and fields from the database.
  87. if(rows) //if there is row
  88. {//then
  89. cache_get_field_content(0, "PASS", pInfo[playerid][Password], mysql, 129);
  90. //we will load player's password into pInfo[playerid][Password] to be used in logging in
  91. 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
  92. 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
  93. ShowPlayerDialog(playerid, dlogin, 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
  94. }
  95. else //if we didn't find any rows from the database, that means, no accounts were found
  96. {
  97. ShowPlayerDialog(playerid, dregister, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.", "Register", "Quit");
  98. //So we show them a dialog register
  99. }
  100. return 1;
  101. }
  102.  
  103. public OnPlayerDisconnect(playerid, reason)
  104. {
  105. 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
  106. GetPlayerPos(playerid, pos[0], pos[1], pos[2]); //let's get player's position when they leave your server
  107. mysql_format(mysql, query, sizeof(query), "UPDATE `players` SET `Admin`=%d, `VIP`=%d, `Money`=%d, `posX`=%f, `posY`=%f, `posZ`=%f WHERE `ID`=%d",\
  108. pInfo[playerid][Admin], pInfo[playerid][VIP], pInfo[playerid][Money], pos[0], pos[1], pos[2], pInfo[playerid][ID]);
  109. //We update the table(`players`) by getting player's admin level, vip level, money, and positions and save them in the database
  110. mysql_tquery(mysql, query, "", "");
  111. //let's execute the query.
  112. return 1;
  113. }
  114.  
  115. public OnPlayerSpawn(playerid)
  116. {
  117. SetPlayerPos(playerid, pInfo[playerid][posX], pInfo[playerid][posZ], pInfo[playerid][posZ]);
  118. //Set player's position to the last saved position.
  119. return 1;
  120. }
  121.  
  122. public OnPlayerDeath(playerid, killerid, reason)
  123. {
  124. return 1;
  125. }
  126.  
  127. public OnVehicleSpawn(vehicleid)
  128. {
  129. return 1;
  130. }
  131.  
  132. public OnVehicleDeath(vehicleid, killerid)
  133. {
  134. return 1;
  135. }
  136.  
  137. public OnPlayerText(playerid, text[])
  138. {
  139. return 1;
  140. }
  141.  
  142. public OnPlayerCommandText(playerid, cmdtext[])
  143. {
  144. if (strcmp("/mycommand", cmdtext, true, 10) == 0)
  145. {
  146. // Do something here
  147. return 1;
  148. }
  149. return 0;
  150. }
  151.  
  152. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  153. {
  154. return 1;
  155. }
  156.  
  157. public OnPlayerExitVehicle(playerid, vehicleid)
  158. {
  159. return 1;
  160. }
  161.  
  162. public OnPlayerStateChange(playerid, newstate, oldstate)
  163. {
  164. return 1;
  165. }
  166.  
  167. public OnPlayerEnterCheckpoint(playerid)
  168. {
  169. return 1;
  170. }
  171.  
  172. public OnPlayerLeaveCheckpoint(playerid)
  173. {
  174. return 1;
  175. }
  176.  
  177. public OnPlayerEnterRaceCheckpoint(playerid)
  178. {
  179. return 1;
  180. }
  181.  
  182. public OnPlayerLeaveRaceCheckpoint(playerid)
  183. {
  184. return 1;
  185. }
  186.  
  187. public OnRconCommand(cmd[])
  188. {
  189. return 1;
  190. }
  191.  
  192. public OnPlayerRequestSpawn(playerid)
  193. {
  194. return 1;
  195. }
  196.  
  197. public OnObjectMoved(objectid)
  198. {
  199. return 1;
  200. }
  201.  
  202. public OnPlayerObjectMoved(playerid, objectid)
  203. {
  204. return 1;
  205. }
  206.  
  207. public OnPlayerPickUpPickup(playerid, pickupid)
  208. {
  209. return 1;
  210. }
  211.  
  212. public OnVehicleMod(playerid, vehicleid, componentid)
  213. {
  214. return 1;
  215. }
  216.  
  217. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  218. {
  219. return 1;
  220. }
  221.  
  222. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  223. {
  224. return 1;
  225. }
  226.  
  227. public OnPlayerSelectedMenuRow(playerid, row)
  228. {
  229. return 1;
  230. }
  231.  
  232. public OnPlayerExitedMenu(playerid)
  233. {
  234. return 1;
  235. }
  236.  
  237. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  238. {
  239. return 1;
  240. }
  241.  
  242. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  243. {
  244. return 1;
  245. }
  246.  
  247. public OnRconLoginAttempt(ip[], password[], success)
  248. {
  249. return 1;
  250. }
  251.  
  252. public OnPlayerUpdate(playerid)
  253. {
  254. return 1;
  255. }
  256.  
  257. public OnPlayerStreamIn(playerid, forplayerid)
  258. {
  259. return 1;
  260. }
  261.  
  262. public OnPlayerStreamOut(playerid, forplayerid)
  263. {
  264. return 1;
  265. }
  266.  
  267. public OnVehicleStreamIn(vehicleid, forplayerid)
  268. {
  269. return 1;
  270. }
  271.  
  272. public OnVehicleStreamOut(vehicleid, forplayerid)
  273. {
  274. return 1;
  275. }
  276.  
  277. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  278. {
  279. switch(dialogid)
  280. {
  281. case dlogin: //login dialog
  282. {
  283. if(!response) Kick(playerid); //if they clicked Quit, we will kick them
  284. new hpass[129]; //for password hashing
  285. new query[100]; // for formatting our query.
  286. WP_Hash(hpass, 129, inputtext); //hashing inputtext
  287. 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
  288. { //if the hashed password matches with the loaded password from database
  289. mysql_format(mysql, query, sizeof(query), "SELECT * FROM `players` WHERE `Username` = '%e' LIMIT 1", Name[playerid]);
  290. //let's format our query
  291. //We select all rows in the table that has your name and limit the result to 1
  292. mysql_tquery(mysql, query, "OnAccountLoad", "i", playerid);
  293. //lets execute the formatted query and when the execution is done, a callback OnAccountLoad will be called
  294. //You can name the callback however you like
  295. }
  296. else //if the hashed password didn't match with the loaded password(pInfo[playerid][Password])
  297. {
  298. //we tell them that they have inserted a wrong password
  299. ShowPlayerDialog(playerid, dlogin, DIALOG_STYLE_INPUT, "Login", "In order to play, you need to login\nWrong password!", "Login", "Quit");
  300. }
  301. }
  302. case dregister: //register dialog
  303. {
  304. if(!response) return Kick(playerid); //if they clicked Quit, we will kick them
  305. if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, dregister, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.\nYour password must be at least 6 characters long!", "Register", "Quit");
  306. //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!
  307. new query[300];
  308. WP_Hash(pInfo[playerid][Password], 129, inputtext); //hashing inputtext
  309. 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]);
  310. //Now let's create a new row and insert player's information in it
  311. mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid);
  312. //let's execute the query
  313. }
  314. }
  315. return 1;
  316. }
  317.  
  318. forward OnAccountLoad(playerid);
  319. forward OnAccountRegister(playerid);
  320. //let's load player's information
  321. public OnAccountLoad(playerid)
  322. {
  323. 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
  324. pInfo[playerid][VIP] = cache_get_field_content_int(0, "VIP"); //Above
  325. pInfo[playerid][Money] = cache_get_field_content_int(0, "Money");//Above
  326. 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
  327. pInfo[playerid][posY] = cache_get_field_content_float(0, "PosY");//Above
  328. pInfo[playerid][posZ] = cache_get_field_content_float(0, "PosZ");//Above
  329.  
  330. GivePlayerMoney(playerid, pInfo[playerid][Money]);//Let's set their money
  331. //For player's position, set it once they spawn(OnPlayerSpawn)
  332. SendClientMessage(playerid, -1, "Successfully logged in"); //tell them that they have successfully logged in
  333. return 1;
  334. }
  335.  
  336. public OnAccountRegister(playerid)
  337. {
  338. pInfo[playerid][ID] = cache_insert_id(); //loads the ID of the player in the variable once they registered.
  339. printf("New account registered. ID: %d", pInfo[playerid][ID]); //just for debugging.
  340. return 1;
  341. }
  342.  
  343. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  344. {
  345. return 1;
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement