Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- _ _ |_ _ ||_ _ _ | _ _ _ |
- (_(_)|_)(_|||_ | (_)|(-|_)(_||\/
- | /
- */
- /* Includes */
- #include <a_samp> // Loading all of SA:MP's functions/callbacks.
- #include <a_mysql> // Loading all of the MySQL functions.
- // Settings for the MySQL database.
- #define host "localhost" // Host IP address, should be 'localhost' if you're hosting it on your own computer.
- #define user "root" // Database Username.
- #define db "server" // Database Name.
- #define pass "" // Database Password, leave blank if you're using XAMPP.
- // The default settings for XAMPP can be found here: http://pastebin.com/MN7d9CsQ.
- /* Dialogs */
- #define dRegister 6287 //Dialog ID for registering.
- #define dLogin 6288 // Dialog ID for logging in.
- /* Global Variables */
- static
- mysql, //This variable will be used to manage our database
- Name[MAX_PLAYERS][24], //We will use this variable to store player's name.
- IP[MAX_PLAYERS][16] //We will use this variable to store player's ip.
- ;
- /* Forwards */
- forward OnAccountCheck(playerid);
- forward OnAccountLoad(playerid);
- forward OnAccountRegister(playerid);
- native WP_Hash(buffer[], len, const str[]); // Whirlpool, for hashing the passwords.
- // Creating an enumerator that holds player's information.
- enum pData //We name our enumerator as pData (which stands for PlayerData).
- {
- 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.
- Money, // 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.
- main()
- {
- print("main() has been called.");
- }
- public OnGameModeInit()
- {
- AddPlayerClass(0, 1721.7542, -1610.5656, 13.5469, 0.1626, 0, 0, 0, 0, 0, 0);
- SetGameModeText("MySQL server");
- DisableInteriorEnterExits();
- EnableStuntBonusForAll(false);
- ShowPlayerMarkers(PLAYER_MARKERS_MODE_OFF);
- DisableNameTagLOS();
- mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG); // Enabling debugging so we can detect a problem (if there is).
- 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.
- 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.
- return 1;
- }
- public OnGameModeExit()
- {
- return 1;
- }
- public OnPlayerRequestClass(playerid, classid)
- {
- return 1;
- }
- // Checking if the player that is connecting is registered or not.
- public OnPlayerConnect(playerid)
- {
- new query[128]; // We use this variable to format our query.
- GetPlayerName(playerid, Name[playerid], 24); // Getting the name of the player.
- GetPlayerIp(playerid, IP[playerid], 16); // Getting the IP Address of the player.
- mysql_format(mysql, query, sizeof(query),"SELECT `Password`, `ID` 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 `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.
- // - LIMIT 1; we only need 1 result to be shown
- mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid);
- // Executing the formatted query and when the execution is done, a callback OnAccountCheck will be called.
- return 1;
- }
- //Now once the query has been processed;
- 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);// Getting the rows and fields from the database.
- if(rows) // If there is row.
- {// Then.
- cache_get_field_content(0, "Password", pInfo[playerid][Password], mysql, 129);
- // We will load player's password into pInfo[playerid][Password] to be used in logging in.
- pInfo[playerid][ID] = cache_get_field_content_int(0, "ID"); // Loading 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, 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.
- }
- else // If we didn't find any rows from the database, that means, no accounts were found.
- {
- ShowPlayerDialog(playerid, dRegister, DIALOG_STYLE_INPUT, "Register", "In order to play, you need to register.", "Register", "Quit");
- // So we show them a dialog register.
- }
- return 1;
- }
- public OnAccountLoad(playerid)
- {
- 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.
- pInfo[playerid][Money] = cache_get_field_content_int(0, "Money"); // Above.
- 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.
- pInfo[playerid][posY] = cache_get_field_content_float(0, "PosY"); // Above.
- pInfo[playerid][posZ] = cache_get_field_content_float(0, "PosZ"); // Above.
- GivePlayerMoney(playerid, pInfo[playerid][Money]); // Setting their money.
- // For player's position, set it once they spawn(OnPlayerSpawn).
- SendClientMessage(playerid, -1, "Successfully logged in"); // Telling them that they have successfully logged in.
- return 1;
- }
- public OnAccountRegister(playerid)
- {
- pInfo[playerid][ID] = cache_insert_id(); // Loads the ID of the player in the variable once they registered.
- printf("New account registered. ID: %d", pInfo[playerid][ID]); // Just for debugging.
- 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]); // Getting player's position when they leave the server.
- mysql_format(mysql, query, sizeof(query), "UPDATE `players` SET `Admin`=%d, `Money`=%d, `posX`=%f, `posY`=%f, `posZ`=%f WHERE `ID`=%d",\
- pInfo[playerid][Admin], pInfo[playerid][Money], 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, "", "");
- // Executing the query.
- return 1;
- }
- public OnPlayerSpawn(playerid)
- {
- SetPlayerPos(playerid, pInfo[playerid][posX], pInfo[playerid][posZ], pInfo[playerid][posZ]);
- //Setting the player to his last saved position (position from last logout).
- return 1;
- }
- public OnPlayerDeath(playerid, killerid, reason)
- {
- return 1;
- }
- public OnVehicleSpawn(vehicleid)
- {
- return 1;
- }
- public OnVehicleDeath(vehicleid, killerid)
- {
- return 1;
- }
- public OnPlayerText(playerid, text[])
- {
- return 1;
- }
- public OnPlayerCommandText(playerid, cmdtext[])
- {
- return 1;
- }
- public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
- {
- return 1;
- }
- public OnPlayerExitVehicle(playerid, vehicleid)
- {
- return 1;
- }
- public OnPlayerStateChange(playerid, newstate, oldstate)
- {
- return 1;
- }
- public OnPlayerEnterCheckpoint(playerid)
- {
- return 1;
- }
- public OnPlayerLeaveCheckpoint(playerid)
- {
- return 1;
- }
- public OnPlayerEnterRaceCheckpoint(playerid)
- {
- return 1;
- }
- public OnPlayerLeaveRaceCheckpoint(playerid)
- {
- return 1;
- }
- public OnRconCommand(cmd[])
- {
- return 1;
- }
- public OnPlayerRequestSpawn(playerid)
- {
- return 1;
- }
- public OnObjectMoved(objectid)
- {
- return 1;
- }
- public OnPlayerObjectMoved(playerid, objectid)
- {
- return 1;
- }
- public OnPlayerPickUpPickup(playerid, pickupid)
- {
- return 1;
- }
- public OnVehicleMod(playerid, vehicleid, componentid)
- {
- return 1;
- }
- public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
- {
- return 1;
- }
- public OnVehicleRespray(playerid, vehicleid, color1, color2)
- {
- return 1;
- }
- public OnPlayerSelectedMenuRow(playerid, row)
- {
- return 1;
- }
- public OnPlayerExitedMenu(playerid)
- {
- return 1;
- }
- public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
- {
- return 1;
- }
- public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
- {
- return 1;
- }
- public OnRconLoginAttempt(ip[], password[], success)
- {
- return 1;
- }
- public OnPlayerUpdate(playerid)
- {
- return 1;
- }
- public OnPlayerStreamIn(playerid, forplayerid)
- {
- return 1;
- }
- public OnPlayerStreamOut(playerid, forplayerid)
- {
- return 1;
- }
- public OnVehicleStreamIn(vehicleid, forplayerid)
- {
- return 1;
- }
- public OnVehicleStreamOut(vehicleid, forplayerid)
- {
- return 1;
- }
- public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
- {
- switch(dialogid)
- {
- case dLogin: // Login dialog.
- {
- if(!response) Kick(playerid); // If they clicked Quit, we will kick them.
- new hpass[129]; // For password hashing.
- new query[100]; // For formatting our query.
- 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]);
- // Formating our query.
- // We select all rows in the table that has your name and limit the result to 1.
- mysql_tquery(mysql, query, "OnAccountLoad", "i", playerid);
- // Executing the formatted query and when the execution is done, a callback OnAccountLoad will be called.
- }
- 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, dLogin, DIALOG_STYLE_INPUT, "Login", "In order to play, you need to login\nWrong password!", "Login", "Quit");
- }
- }
- case dRegister: //Register dialog.
- {
- if(!response) return Kick(playerid); // If they clicked Quit, we will kick them.
- 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");
- // 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!
- new query[300];
- WP_Hash(pInfo[playerid][Password], 129, inputtext); //hashing inputtext
- 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]);
- // Creating a new row and insert player's information in it.
- mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid);
- // Executing the query.
- }
- }
- return 1;
- }
- public OnPlayerClickPlayer(playerid, clickedplayerid, source)
- {
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement