Guest User

Untitled

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