Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //INCLUDE
- #include <a_samp>
- #include <a_mysql>
- native WP_Hash(buffer[], len, const str[]); //whirlpool, for hashing our password
- //**********************************************FORWARD***********************************
- forward OnAccountCheck(playerid);
- forward OnAccountLoad(playerid);
- //DEFINE
- #define mysql_host "127.0.0.1"
- #define mysql_user "root"
- #define mysql_password ""
- #define mysql_database ""
- main() {}
- //****************************************************************************************
- //***********************************************VARIABLES*********************************
- static
- mysql, //This variable will be used to manage our database
- Name[MAX_PLAYERS][MAX_PLAYER_NAME], //We will use this variable to store player's name.
- IP[MAX_PLAYERS][16]; //We will use this variable to store player's ip.
- //***************************************************************************************
- //**********************************************ENUM**************************************
- enum PDATA //We name our enumerator as PDATA (which stands for PlayerDATA). You can name it however you want.
- {
- ID, //Will be used later to store player's ID from database so we can use it anywhere later
- Password[129], //We will load player's password into this varible from database.
- Admin, //We will load player's admin level from database into this variable so we can use it anywhere later.
- VIP, //We will load player's VIP level from database into this variable so we can use it anywhere later.
- Argent, //We will load player's money from database into this variable so we can use it anywhere later.
- Float:posX, //We will load player's X position from database into this variable so we can use it anywhere later.
- Float:posY, //We will load player's Y position from database into this variable so we can use it anywhere later.
- Float:posZ //We will load player's Z from database into this variable so we can use it anywhere later.
- }
- new pInfo[MAX_PLAYERS][PDATA]; //Variable that stores enumerator above
- //***************************************************************************************
- // **********************************************STOCK************************************
- // ******************************************************************************************
- public OnGameModeInit()
- {
- mysql_debug(1); //Let's enable debugging so we can detect a problem(if there is)
- mysql = mysql_connect(mysql_host, mysql_user, mysql_database, mysql_password); //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.
- 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.
- //If we're cool, it's time to create a table for your database.
- mysql_tquery(mysql, "CREATE TABLE IF NOT EXISTS `players`(\
- `ID` int(10) NOT NULL AUTO_INCREMENT, \
- `Username` varchar(24) NOT NULL, \
- `Password` varchar(129) NOT NULL, \
- `IP` varchar(16) NOT NULL, \
- `Admin` int(10) NOT NULL, \
- `VIP` int(10) NOT NULL, \
- `Argent` int(10) NOT NULL, \
- `posX` float NOT NULL, \
- `posY` float NOT NULL, \
- `posZ` float NOT NULL, \
- PRIMARY KEY (`ID`))", "", "");
- /*We will use mysql_tquery to execute a query
- We are creating a table called `players`.
- `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)
- `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
- `Password` varchar(129) NOT NULL, = Same as above
- `IP` varchar(16) NOT NULL, = Same as above
- `Admin` int(10) NOT NULL, = Same as above
- `VIP` int(10) NOT NULL, = Same as above
- `Money` int(10) NOT NULL, = Same as above
- `posX` float NOT NULL, = posX is player's X position. So it must be float
- `posY` float NOT NULL, = Same as above
- `posZ` float NOT NULL, = Same as above
- "", ""); = We leave this 2 parameters(callback[] and format[]) empty because we only gonna need it if we use SELECT
- */
- return 1;
- }
- public OnGameModeExit()
- {
- mysql_close(mysql);
- return 1;
- }
- public OnPlayerRequestClass(playerid, classid)
- {
- SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
- SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
- SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
- return 1;
- }
- public OnPlayerConnect(playerid)
- {
- new query[128]; //We use this variable to format our query
- GetPlayerName(playerid, Name[playerid], 24); //Getting player's name
- GetPlayerIp(playerid, IP[playerid], 16); //Getting layer's IP
- mysql_format(mysql, query, sizeof(query),"SELECT * FROM `players` WHERE `Username` = '%e' LIMIT 1", Name[playerid]);
- // - 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
- // - 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
- // - LIMIT 1; we only need 1 result to be shown
- mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid);
- //lets execute the formatted query and when the execution is done, a callback OnAccountCheck will be called
- //You can name the callback however you like
- return 1;
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- 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
- GetPlayerPos(playerid, pos[0], pos[1], pos[2]); //let's get player's position when they leave your server
- mysql_format(mysql, query, sizeof(query), "UPDATE `players` SET `Admin`=%d, `VIP`=%d, `Argent`=%d, `posX`=%f, `posY`=%f, `posZ`=%f WHERE `ID`=%d",\
- pInfo[playerid][Admin], pInfo[playerid][VIP], pInfo[playerid][Argent], pos[0], pos[1], pos[2], pInfo[playerid][ID]);
- //We update the table(`players`) by getting player's admin level, vip level, money, and positions and save them in the database
- mysql_tquery(mysql, query, "", "");
- //let's execute the query.
- return 1;
- }
- public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
- {
- new query[300];
- switch(dialogid)
- {
- case 1: //login dialog
- {
- if(!response) Kick(playerid); //if they clicked Quit, we will kick them
- new hpass[129]; //for password hashing
- WP_Hash(hpass, 129, inputtext); //hashing inputtext
- 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
- { //if the hashed password matches with the loaded password from database
- mysql_format(mysql, query, sizeof(query), "SELECT * FROM `players` WHERE `Username` = '%e' LIMIT 1", Name[playerid]);
- //let's format our query
- //once again, we select all rows in the table that has your name and limit the result
- mysql_tquery(mysql, query, "OnAccountLoad", "i", playerid);
- //lets execute the formatted query and when the execution is done, a callback OnAccountLoad will be called
- //You can name the callback however you like
- }
- else //if the hashed password didn't match with the loaded password(pInfo[playerid][Password])
- {
- //we tell them that they have inserted a wrong password
- ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Login", "In order to play, you need to login\nWrong password!", "Login", "Quit");
- }
- }
- case 2: //register dialog
- {
- if(!response) return Kick(playerid); //if they clicked Quit, we will kick them
- 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");
- //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!
- WP_Hash(pInfo[playerid][Password], 129, inputtext); //hashing inputtext
- 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]);
- //Now let's create a new row and insert player's information in it
- mysql_tquery(mysql, query, "", "");
- //let's execute the query
- }
- }
- return 1;
- }
- public OnAccountCheck(playerid)
- {
- new rows, fields; //a variable that will be used to retrieve rows and fields in the database.
- cache_get_data(rows, fields, mysql);//let's get the rows and fields from the database.
- if(rows) //if there is row
- {//then
- cache_get_row(0, 2, pInfo[playerid][Password], mysql, 129);
- //we will load their password into pInfo[playerid][Password] to be used in logging in
- //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;
- // ---> row> ID | Username | Password | IP | Admin | .... and so on
- // ^ ^ ^
- // fields 0 1 2
- //So we're getting row 0, field 2 which is password
- 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
- 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
- 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
- }
- else //if we didn't find any rows from the database, that means, no accounts were found
- {
- ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.", "Register", "Quit");
- //So we show the a dialog register
- }
- return 1;
- }
- public OnAccountLoad(playerid)
- {
- // ---> row> ID | Username | Password | IP | Admin | VIP | Money | posX | posY | posZ
- // ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
- // fields 0 1 2 3 4 5 6 7 8 9
- 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
- pInfo[playerid][VIP] = cache_get_row_int(0, 5); //Above
- pInfo[playerid][Argent] = cache_get_row_int(0, 6);//Above
- pInfo[playerid][posX] = cache_get_row_float(0, 7);//Above. Since player's position is a float, we use cache_get_row_float
- pInfo[playerid][posY] = cache_get_row_float(0, 8);//Above
- pInfo[playerid][posZ] = cache_get_row_float(0, 9);//Above
- GivePlayerMoney(playerid, pInfo[playerid][Argent]);//Let's set their money
- //For player's position, set it once they spawn(OnPlayerSpawn)
- SendClientMessage(playerid, -1, "Successful logged in"); //tell them that they have succesfully logged in
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement