Guest User

Untitled

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