Advertisement
Guest User

Untitled

a guest
Sep 6th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.41 KB | None | 0 0
  1. // SA-MP Roleplay Server by Furzer
  2.  
  3. #include <a_samp>
  4. #include <a_mysql>
  5. #include <zcmd>
  6. #include <sscanf2>
  7. #include <data>
  8.  
  9. #define mysql_host "localhost" //your destination server
  10. #define mysql_user "root" //default user name of wampserver
  11. #define mysql_password "" //wampserver has no default password unless you have set one.
  12. #define mysql_database "rp"//the name of your database
  13.  
  14. new MySQL:mysql;
  15. new field[128];
  16.  
  17. #define mysql_fetch_float(%0,%1) mysql_fetch_field(%0,field); \
  18. %1=floatstr(field)
  19. #define mysql_fetch_string(%0,%1) mysql_fetch_field(%0,%1)
  20. #define mysql_fetch_int(%0,%1) mysql_fetch_field(%0,field); \
  21. %1=strval(field)
  22.  
  23. main()
  24. {
  25. print("\n----------------------------------");
  26. print(" Blank Gamemode by your name here");
  27. print("----------------------------------\n");
  28. }
  29.  
  30. enum PlayerInfo
  31. {
  32. ID, // id of the player
  33. Name[24], // name of the player
  34. pAdmin, // admin level of the player
  35. pMoney, //money of the player
  36. pKills, // Kills of the player
  37. pDeaths, // deaths of the player
  38. pLevel, // score of the player
  39. IP[16], // Storing the ip of the player
  40. Logged, // Players logged in or not variable
  41. IsRegistered //checks if the player is registered or not.
  42. };
  43. new pInfo[MAX_PLAYERS][PlayerInfo];
  44.  
  45. stock MySQL_Register(playerid, passwordstring[])
  46. {
  47. new Query[300];
  48. mysql_real_escape_string();(pInfo[playerid][Name], pInfo[playerid][Name]);
  49. // escaping the name of the player to avoid sql_injections.
  50. mysql_real_escape_string();(pInfo[playerid][IP], pInfo[playerid][IP]);
  51. // escaping the IP of the player to avoid sql_injections.
  52. // as you might've seen we haven't escaped the password here because it was already escaped in our register dialog
  53. format(Query, sizeof(Query), "INSERT INTO `playerdata` (`name`, `password`, `ip`) VALUES('%s', '%d', '%s')", pInfo[playerid][Name], udb_hash(passwordstring), pInfo[playerid][IP]); // Here we use the INSERT option and insert the name, password, and ip of the player in the database.
  54. // we don't insert the score, admin, or any other variable because its automatically 0.
  55. mysql_query(Query);
  56. // here we do not need to mysql_store_result or mysql_free_result
  57. // because we are only inserting data in the database not selecting it
  58. //next we set the players logged variable to 1.
  59. //and the isregistered variable to 1 aswell.
  60. SendClientMessage(playerid, -1, "You have now been successfully registered on this server!");
  61. pInfo[playerid][Logged] = 1; //Sets the login variable to 1, meaning logged in.
  62. pInfo[playerid][IsRegistered] = 1; // sets the registered variable to 1. meaning registered.
  63. return 1;
  64. }
  65.  
  66. stock MySQL_Login(playerid)
  67. {
  68. new Query[500];
  69. mysql_real_escape_string()(pInfo[playerid][Name], pInfo[playerid][Name]); // escaping the name of the player to avoid sql_injections.
  70. format(Query, sizeof(Query), "SELECT * FROM `playerdata` WHERE `name` COLLATE latin1_general_cs = '%s' LIMIT 1", pInfo[playerid][Name]);
  71. mysql_query(Query);
  72. // here we select all of the user's data in the database and store it
  73. while(mysql_fetch_field(Query))
  74. // here after the server has selected the user
  75. //from the database and stored its data we extract that data onto our enums.
  76. {
  77. mysql_fetch_int("id", pInfo[playerid][ID]);
  78. // the special identifier of a user called "id"
  79. mysql_fetch_int("admin", pInfo[playerid][pAdmin]);
  80. // the admin level of the player
  81. mysql_fetch_int("score", pInfo[playerid][pLevel]); SetPlayerScore(playerid, pInfo[playerid][pLevel]);
  82. // here we fetch the score and save it to the enum and also save it to the server by using setplayerscore
  83. mysql_fetch_int("money", pInfo[playerid][pMoney]); GivePlayerMoney(playerid, pInfo[playerid][pMoney]);
  84. // here we fetch the score and save it to the enum and also save it to the server by using setplayerscore
  85. mysql_fetch_int("kills", pInfo[playerid][pKills]);
  86. // the amount of kills a player has
  87. mysql_fetch_int("deaths", pInfo[playerid][pDeaths]);
  88. // the amount of deaths a player has
  89. //
  90. // the way to fetch a users stats from the database is:
  91. //mysql_fetch_int("table_name", variable_to_store_in); remember the "table_name" is case sensitive!
  92. }
  93.  
  94. // here we free our result and end the SELECT process. Remember this is very important otherwise you may receive high amount of lag in your server!
  95. pInfo[playerid][Logged] = 1; // sets the logged variable to 1 meaning logged in.
  96. return 1;
  97. }
  98.  
  99. public OnGameModeInit()
  100. {
  101. mysql = mysql_init(LOG_ALL); // Tells sql to log all mysql features used in the script
  102. new Connection = mysql_connect(mysql_host, mysql_user, mysql_password, mysql_database, mysql); // connects with the mysql database
  103. if(Connection) //checks if the database is successfully connected
  104. {
  105. new dest[200];
  106. mysql_stat(dest); // display the mysql database statistics.
  107. printf(dest);
  108. printf(">> MySQL connection successfully initialized"); // if it is connected it will display this line, if not then it wont display anything.
  109. }
  110. return 1;
  111. }
  112.  
  113. public OnGameModeExit()
  114. {
  115. return 1;
  116. }
  117.  
  118. public OnPlayerRequestClass(playerid, classid)
  119. {
  120. SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
  121. SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
  122. SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
  123. return 1;
  124. }
  125.  
  126. public OnPlayerConnect(playerid)
  127. {
  128. new Query[500];
  129. GetPlayerName(playerid, pInfo[playerid][Name], 24); //gets the player's name and stores it to to your enum pInfo[playerid][Name]
  130. GetPlayerIp(playerid, pInfo[playerid][IP], 16); //Gets the IP of the player and stores it to pInfo[playerid][IP]
  131. mysql_real_escape_string();(pInfo[playerid][Name], pInfo[playerid][Name]); // now we have to escape the name inorder to escape any mysql injections. ([url]http://en.wikipedia.org/wiki/SQL_injection[/url])
  132. format(Query, 500, "SELECT `name` FROM `playerdata` WHERE `name` COLLATE latin1_general_cs = '%s' LIMIT 1", pInfo[playerid][Name]); // here we are selecting the name of the player who logged in from the database.
  133. mysql_query(Query); // we query the statement above
  134. if(cache_num_rows() > 0) // if the database has more then one table with the given name
  135. {//then
  136. ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login", "This account is registered! Please log in:", "Login", ""); // show the login dialog and tell the player to login.
  137. }
  138. else // else if the database found no tables with the given name
  139. { // then
  140. ShowPlayerDialog(playerid, 2, DIALOG_STYLE_PASSWORD, "Register", "This account not registered yet! Please register:", "Register", "");// show the register dialog and tell the player to register
  141. }
  142. //You must always free the mysql result to avoid
  143. //there being massive memory usage.
  144. return 1;
  145. }
  146.  
  147. SavePlayer(playerid)
  148. {
  149. if(pInfo[playerid][Logged] == 1)
  150. // checks if the player is logged
  151. {
  152. new Query[500];
  153. format(Query, 500, "UPDATE `playerdata` SET `admin` = '%d', `score` = '%d', `money` = '%d', `kills` = '%d', `deaths` = '%d' WHERE `id` = '%d' LIMIT 1",
  154. pInfo[playerid][pAdmin],
  155. pInfo[playerid][pLevel],
  156. pInfo[playerid][pMoney],
  157. pInfo[playerid][pKills],
  158. pInfo[playerid][pDeaths],
  159. pInfo[playerid][ID]);
  160. mysql_query(Query);
  161. //this basically gets the variables and stores it to the players special identifier called "ID".
  162. }
  163. }
  164.  
  165. public OnPlayerDisconnect(playerid, reason)
  166. {
  167. SavePlayer(playerid);
  168. return 1;
  169. }
  170.  
  171. public OnPlayerSpawn(playerid)
  172. {
  173. return 1;
  174. }
  175.  
  176. public OnPlayerDeath(playerid, killerid, reason)
  177. {
  178. return 1;
  179. }
  180.  
  181. public OnVehicleSpawn(vehicleid)
  182. {
  183. return 1;
  184. }
  185.  
  186. public OnVehicleDeath(vehicleid, killerid)
  187. {
  188. return 1;
  189. }
  190.  
  191. public OnPlayerText(playerid, text[])
  192. {
  193. return 1;
  194. }
  195.  
  196. public OnPlayerCommandText(playerid, cmdtext[])
  197. {
  198. if (strcmp("/mycommand", cmdtext, true, 10) == 0)
  199. {
  200. // Do something here
  201. return 1;
  202. }
  203. return 0;
  204. }
  205.  
  206. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  207. {
  208. return 1;
  209. }
  210.  
  211. public OnPlayerExitVehicle(playerid, vehicleid)
  212. {
  213. return 1;
  214. }
  215.  
  216. public OnPlayerStateChange(playerid, newstate, oldstate)
  217. {
  218. return 1;
  219. }
  220.  
  221. public OnPlayerEnterCheckpoint(playerid)
  222. {
  223. return 1;
  224. }
  225.  
  226. public OnPlayerLeaveCheckpoint(playerid)
  227. {
  228. return 1;
  229. }
  230.  
  231. public OnPlayerEnterRaceCheckpoint(playerid)
  232. {
  233. return 1;
  234. }
  235.  
  236. public OnPlayerLeaveRaceCheckpoint(playerid)
  237. {
  238. return 1;
  239. }
  240.  
  241. public OnRconCommand(cmd[])
  242. {
  243. return 1;
  244. }
  245.  
  246. public OnPlayerRequestSpawn(playerid)
  247. {
  248. return 1;
  249. }
  250.  
  251. public OnObjectMoved(objectid)
  252. {
  253. return 1;
  254. }
  255.  
  256. public OnPlayerObjectMoved(playerid, objectid)
  257. {
  258. return 1;
  259. }
  260.  
  261. public OnPlayerPickUpPickup(playerid, pickupid)
  262. {
  263. return 1;
  264. }
  265.  
  266. public OnVehicleMod(playerid, vehicleid, componentid)
  267. {
  268. return 1;
  269. }
  270.  
  271. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  272. {
  273. return 1;
  274. }
  275.  
  276. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  277. {
  278. return 1;
  279. }
  280.  
  281. public OnPlayerSelectedMenuRow(playerid, row)
  282. {
  283. return 1;
  284. }
  285.  
  286. public OnPlayerExitedMenu(playerid)
  287. {
  288. return 1;
  289. }
  290.  
  291. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  292. {
  293. return 1;
  294. }
  295.  
  296. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  297. {
  298. return 1;
  299. }
  300.  
  301. public OnRconLoginAttempt(ip[], password[], success)
  302. {
  303. return 1;
  304. }
  305.  
  306. public OnPlayerUpdate(playerid)
  307. {
  308. return 1;
  309. }
  310.  
  311. public OnPlayerStreamIn(playerid, forplayerid)
  312. {
  313. return 1;
  314. }
  315.  
  316. public OnPlayerStreamOut(playerid, forplayerid)
  317. {
  318. return 1;
  319. }
  320.  
  321. public OnVehicleStreamIn(vehicleid, forplayerid)
  322. {
  323. return 1;
  324. }
  325.  
  326. public OnVehicleStreamOut(vehicleid, forplayerid)
  327. {
  328. return 1;
  329. }
  330.  
  331. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  332. {
  333. if(dialogid == 2) //If Dialog is our register dialog
  334. {
  335. if(response) //If they click the button register
  336. {
  337. if(!strlen(inputtext) || strlen(inputtext) > 68) //checks if password is more then 68 characters or nothing.
  338. {
  339. SendClientMessage(playerid, 0xFF0000, "You must insert a password between 1-68 characters!");
  340. ShowPlayerDialog(playerid, 2, DIALOG_STYLE_PASSWORD, "Register", "This account not registered yet! Please register:", "Register", "");// show the register dialog again.
  341. }
  342. else if(strlen(inputtext) > 0 && strlen(inputtext) < 68) // if the password is in between 1 - 68 characters
  343. {
  344. new escpass[100];
  345. mysql_real_escape_string(inputtext, escpass); // here we escape the data again to avoid any mysql injection,
  346. //remember to always to do this when SELECT'ing or INSERT'ing any data in the database
  347. MySQL_Register(playerid, escpass); // Here we are going to another function to register the player.
  348. }
  349. }
  350. if(!response)
  351. {
  352. SendClientMessage(playerid, 0xFF0000, "You must register before you can login!");
  353. ShowPlayerDialog(playerid, 2, DIALOG_STYLE_PASSWORD, "Register", "This account not registered yet! Please register:", "Register", "");// show the register dialog again.
  354. }
  355. }
  356. if(dialogid == 1) //Dialog login
  357. {
  358. if(!response) //If they click the cancel button
  359. {
  360. SendClientMessage(playerid, 0xFF0000, "You must login before you spawn!"); //Sends the client a error message
  361. Kick(playerid); // and kicks him. ( you can change it to show the player the login dialog again by uncommenting the bottem line and commenting this one.
  362. //ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login", "This account is registered! Please log in:", "Login", "");
  363. }
  364. if(response) //If the player clicked login
  365. {
  366. if(!strlen(inputtext) || strlen(inputtext) > 68) //if the password is not 1 to 100 characters
  367. {
  368. SendClientMessage(playerid, 0xFF0000, "You must insert a password between 1-68 characters!"); //Sends the client a error message
  369. ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login","Your user is registered! Please login with your password below!\n{FF0000} Please enter a password between 0 and 68 characters","Login","Cancel");
  370. return 1;
  371. }
  372. mysql_real_escape_string(inputtext, inputtext); //Here we escape the inputtext to avoid SQL injections as stated above.
  373. mysql_real_escape_string(pInfo[playerid][Name], pInfo[playerid][Name]); // escapeing ^^
  374. new Query[500];
  375. format(Query, 500, "SELECT * FROM `playerdata` WHERE `name` COLLATE latin1_general_cs = '%s' AND `password` = '%d'", pInfo[playerid][Name], udb_hash(inputtext)); // now here check the database if the player has given the proper password.HTTP
  376. mysql_query(Query);
  377. if(mysql_num_rows() > 0) { // if the password the player provided is correct and matches the database
  378. MySQL_Login(playerid); // we will call this function and log the player in.
  379. } else {
  380. //other wise this means that the password that the player
  381. //typed was incorrect and we will resend the dialog.
  382. ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login","Your user is registered! Please login with your password below!\n The password you typed was incorrect!","Login","Cancel"); //Shows our login dialog again.
  383. }
  384. }
  385. }
  386. return 1;
  387. }
  388.  
  389. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  390. {
  391. return 1;
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement