Advertisement
Guest User

Untitled

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