Advertisement
Guest User

cobalt rp

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