Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Includes
- */
- #include <a_samp>
- #include <zcmd>
- #include <sscanf>
- #include <mysql>
- #include <data>
- #include <YSI\y_ini>
- /*
- Defines
- */
- #define COLOR_BLACK 0x000000AA
- #define COLOR_GREEN 0xFF0000
- #define COLOR_RED 0xFF0000AA
- #define COLOR_WHITE 0xFFFFFFAA
- #define COLOR_LIGHTBLUE 0x33CCFFAA
- #define notadmin "You are not authorised to use that command!"
- #define mysql_host "localhost" //your destination server
- #define mysql_user "root" //default user name of wampserver
- #define mysql_password "seanom" //wampserver has no default password unless you have set one.
- #define mysql_database "sa-mpsql"//the name of your database
- #define MAX_ADMIN_LEVEL 7
- #define SETLEVEL_ADMIN 7
- /*
- DIALOG DEFINES
- */
- #define DIALOG_UPDATES 3
- #define DIALOG_ACMDS 4
- #define DIALOG_RULES 5
- /*
- SCRIPT BEGIN
- */
- new MySQL:mysql;
- new field[128];
- #define mysql_fetch_float(%0,%1) mysql_fetch_field(%0,field); \
- %1=floatstr(field)
- #define mysql_fetch_string(%0,%1) mysql_fetch_field(%0,%1)
- #define mysql_fetch_int(%0,%1) mysql_fetch_field(%0,field); \
- %1=strval(field)
- main()
- {
- print("\n----------------------------------");
- print(" Instant Roleplay by Mahony ");
- print("----------------------------------\n");
- }
- // ENUM FOR SQL SYSTEM
- enum PlayerInfo
- {
- ID, // id of the player
- Nick[24], // name of the player
- pAdmin, // admin level of the player
- pMoney, //money of the player
- pKills, // Kills of the player
- pDeaths, // deaths of the player
- pScore, // score of the player
- IP[16], // Storing the ip of the player
- Logged, // Players logged in or not variable
- IsRegistered //checks if the player is registered or not.
- };
- new pInfo[MAX_PLAYERS][PlayerInfo];
- // END OF ENUM
- public OnGameModeInit()
- {
- SetGameModeText("Instant Roleplay");
- AddPlayerClass(0,1481.3726,-1765.3018,18.7958,1.6393,0,0,0,0,0,0);
- mysql = mysql_init(LOG_ALL); // Tells sql to log all mysql features used in the script
- new Connection = mysql_connect(mysql_host, mysql_user, mysql_password, mysql_database, mysql); // connects with the mysql database
- if(Connection) //checks if the database is successfully connected
- {
- new dest[200];
- mysql_stat(dest); // display the mysql database statistics.
- printf(dest);
- printf(">> MySQL connection successfully initialized"); // if it is connected it will display this line, if not then it wont display anything.
- }
- return 1;
- }
- public OnGameModeExit()
- {
- 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[500];
- GetPlayerName(playerid, pInfo[playerid][Nick], 24); //gets the player's name and stores it to to your enum pInfo[playerid][Nick]
- GetPlayerIp(playerid, pInfo[playerid][IP], 16); //Gets the IP of the player and stores it to pInfo[playerid][IP]
- mysql_real_escape_string(pInfo[playerid][Nick], pInfo[playerid][Nick]); // now we have to escape the name inorder to escape any mysql injections. ([url]http://en.wikipedia.org/wiki/SQL_injection[/url])
- format(Query, 500, "SELECT `nick` FROM `playerdata` WHERE `nick` COLLATE latin1_general_cs = '%s' LIMIT 1", pInfo[playerid][Nick]); // here we are selecting the name of the player who logged in from the database.
- mysql_query(Query); // we query the statement above
- mysql_store_result(); // next we store the result inorder for it to be used further ahead.
- if(mysql_num_rows() > 0) // if the database has more then one table with the given name
- {//then
- ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login", "This account is registered! Please log in:", "Login", ""); // show the login dialog and tell the player to login.
- }
- else // else if the database found no tables with the given name
- { // then
- ShowPlayerDialog(playerid, 2, DIALOG_STYLE_PASSWORD, "Register", "This account not registered yet! Please register:", "Register", "");// show the register dialog and tell the player to register
- }
- mysql_free_result(); // here we free the result we stored in the beginning as we do not need it anymore.
- //You must always free the mysql result to avoid
- //there being massive memory usage.
- return 1;
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- SavePlayer(playerid);
- return 1;
- }
- public OnPlayerSpawn(playerid)
- {
- ShowPlayerDialog(playerid, DIALOG_RULES, DIALOG_STYLE_MSGBOX, "Server Rules", "- No Cheating\n- No Spamming\n- Respect Admins\n\nDo you agree to these rules?", "Yes", "No");
- return 1;
- }
- public OnPlayerDeath(playerid, killerid, reason)
- {
- pInfo[killerid][pKills]++;
- pInfo[playerid][pDeaths]++;
- return 1;
- }
- /*
- DIALOGS ***WARNING VERY LONG***
- */
- public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
- {
- if(dialogid == 5)
- {
- if(response) // If they clicked 'Yes' or pressed enter
- {
- SendClientMessage(playerid, COLOR_GREEN, "Thank you for agreeing to the server rules!");
- }
- else // Pressed ESC or clicked cancel
- {
- SendClientMessage(playerid, COLOR_RED, "You MUST agree to the server rules to play here.");
- //For info & code of this function please refer to the bottom of this article.
- }
- }
- if(dialogid == 2) //If Dialog is our register dialog
- {
- if(response) //If they click the button register
- {
- if(!strlen(inputtext) || strlen(inputtext) > 68) //checks if password is more then 68 characters or nothing.
- {
- SendClientMessage(playerid, 0xFF0000, "You must insert a password between 1-68 characters!");
- ShowPlayerDialog(playerid, 2, DIALOG_STYLE_PASSWORD, "Register", "This account not registered yet! Please register:", "Register", "");// show the register dialog again.
- }
- else if(strlen(inputtext) > 0 && strlen(inputtext) < 68) // if the password is in between 1 - 68 characters
- {
- new escpass[100];
- mysql_real_escape_string(inputtext, escpass); // here we escape the data again to avoid any mysql injection,
- //remember to always to do this when SELECT'ing or INSERT'ing any data in the database
- MySQL_Register(playerid, escpass); // Here we are going to another function to register the player.
- }
- }
- if(!response)
- {
- SendClientMessage(playerid, 0xFF0000, "You must register before you can login!");
- ShowPlayerDialog(playerid, 2, DIALOG_STYLE_PASSWORD, "Register", "This account not registered yet! Please register:", "Register", "");// show the register dialog again.
- }
- }
- if(dialogid == 1) //Dialog login
- {
- if(!response) //If they click the cancel button
- {
- SendClientMessage(playerid, 0xFF0000, "You must login before you spawn!"); //Sends the client a error message
- Kick(playerid); // and kicks him. ( you can change it to show the player the login dialog again by uncommenting the bottem line and commenting this one.
- //ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login", "This account is registered! Please log in:", "Login", "");
- }
- if(response) //If the player clicked login
- {
- if(!strlen(inputtext) || strlen(inputtext) > 68) //if the password is not 1 to 100 characters
- {
- SendClientMessage(playerid, 0xFF0000, "You must insert a password between 1-68 characters!"); //Sends the client a error message
- ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login","Your user is registered! Please login with your password below!\n{FF0000} Please enter a password between 0 and 68 characters","Login","Cancel");
- return 1;
- }
- mysql_real_escape_string(inputtext, inputtext); //Here we escape the inputtext to avoid SQL injections as stated above.
- mysql_real_escape_string(pInfo[playerid][Nick], pInfo[playerid][Nick]); // escapeing ^^
- new Query[500];
- format(Query, 500, "SELECT * FROM `playerdata` WHERE `nick` COLLATE latin1_general_cs = '%s' AND `password` = '%d'", pInfo[playerid][Nick], udb_hash(inputtext)); // now here check the database if the player has given the proper password.HTTP
- mysql_query(Query);
- mysql_store_result();
- if(mysql_num_rows() > 0) { // if the password the player provided is correct and matches the database
- MySQL_Login(playerid); // we will call this function and log the player in.
- } else {
- //other wise this means that the password that the player
- //typed was incorrect and we will resend the dialog.
- ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Login","Your user is registered! Please login with your password below!\n The password you typed was incorrect!","Login","Cancel"); //Shows our login dialog again.
- }
- mysql_free_result(); //Remember to always free a result if you stored one!
- }
- }
- return 1;
- }
- /*
- STOCKS///FUCTIONS
- */
- forward KickPublic(playerid);
- public KickPublic(playerid) { Kick(playerid); }
- stock KickWithMessage(playerid, color, message[])
- {
- SendClientMessage(playerid, color, message);
- SetTimerEx("KickPublic", 1000, 0, "d", playerid); //Delay of 1 second before kicking the player so he recieves the message
- }
- stock MySQL_Register(playerid, passwordstring[])
- {
- new Query[300];
- mysql_real_escape_string(pInfo[playerid][Nick], pInfo[playerid][Nick]);
- // escaping the name of the player to avoid sql_injections.
- mysql_real_escape_string(pInfo[playerid][IP], pInfo[playerid][IP]);
- // escaping the IP of the player to avoid sql_injections.
- // as you might've seen we haven't escaped the password here because it was already escaped in our register dialog
- format(Query, sizeof(Query), "INSERT INTO `playerdata` (`nick`, `password`, `ip`) VALUES('%s', '%d', '%s')", pInfo[playerid][Nick], udb_hash(passwordstring), pInfo[playerid][IP]); // Here we use the INSERT option and insert the name, password, and ip of the player in the database.
- // we don't insert the score, admin, or any other variable because its automatically 0.
- mysql_query(Query);
- // here we do not need to mysql_store_result or mysql_free_result
- // because we are only inserting data in the database not selecting it
- //next we set the players logged variable to 1.
- //and the isregistered variable to 1 aswell.
- SendClientMessage(playerid, -1, "You have now been successfully registered on this server! Click 'Spawn' to begin playing!");
- pInfo[playerid][Logged] = 1; //Sets the login variable to 1, meaning logged in.
- pInfo[playerid][IsRegistered] = 1; // sets the registered variable to 1. meaning registered.
- return 1;
- }
- stock MySQL_Login(playerid)
- {
- new Query[500];
- mysql_real_escape_string(pInfo[playerid][Nick], pInfo[playerid][Nick]); // escaping the name of the player to avoid sql_injections.
- format(Query, sizeof(Query), "SELECT * FROM `playerdata` WHERE `nick` COLLATE latin1_general_cs = '%s' LIMIT 1", pInfo[playerid][Nick]);
- mysql_query(Query);
- // here we select all of the user's data in the database and store it
- mysql_store_result();
- while(mysql_fetch_row(Query))
- // here after the server has selected the user
- //from the database and stored its data we extract that data onto our enums.
- {
- mysql_fetch_int("id", pInfo[playerid][ID]);
- // the special identifier of a user called "id"
- mysql_fetch_int("admin", pInfo[playerid][pAdmin]);
- // the admin level of the player
- mysql_fetch_int("score", pInfo[playerid][pScore]); SetPlayerScore(playerid, pInfo[playerid][pScore]);
- // here we fetch the score and save it to the enum and also save it to the server by using setplayerscore
- mysql_fetch_int("money", pInfo[playerid][pMoney]); GivePlayerMoney(playerid, pInfo[playerid][pMoney]);
- // here we fetch the score and save it to the enum and also save it to the server by using setplayerscore
- mysql_fetch_int("kills", pInfo[playerid][pKills]);
- // the amount of kills a player has
- mysql_fetch_int("deaths", pInfo[playerid][pDeaths]);
- // the amount of deaths a player has
- //
- // the way to fetch a users stats from the database is:
- //mysql_fetch_int("table_name", variable_to_store_in); remember the "table_name" is case sensitive!
- }
- mysql_free_result();
- // here we free our result and end the SELECT process. Remember this is very important otherwise you may receive high amount of lag in your server!
- SendClientMessage(playerid, -1, "You have now been successfully logged into this server! Click 'Spawn' to begin playing!");
- pInfo[playerid][Logged] = 1; // sets the logged variable to 1 meaning logged in.
- return 1;
- }
- SavePlayer(playerid)
- {
- if(pInfo[playerid][Logged] == 1)
- // checks if the player is logged
- {
- new Query[500];
- format(Query, 500, "UPDATE `playerdata` SET `admin` = '%d', `score` = '%d', `money` = '%d', `kills` = '%d', `deaths` = '%d' WHERE `id` = '%d' LIMIT 1",
- pInfo[playerid][pAdmin],
- pInfo[playerid][pScore],
- pInfo[playerid][pMoney],
- pInfo[playerid][pKills],
- pInfo[playerid][pDeaths],
- pInfo[playerid][ID]);
- mysql_query(Query);
- //this basically gets the variables and stores it to the players special identifier called "ID".
- }
- }
- /*
- Command section
- This is where all the commands are
- */
- CMD:help(playerid, params[])
- {
- SendClientMessage(playerid, COLOR_BLACK, "********************************************************************************");
- SendClientMessage(playerid, COLOR_WHITE, "/help - Help commands /updates - Latest updates");
- SendClientMessage(playerid, COLOR_WHITE, "/heal - Heal another player /kill - Kill another player ");
- SendClientMessage(playerid, COLOR_WHITE, "/feedback - Rate the script!, /bugreport - Report a bug");
- SendClientMessage(playerid, COLOR_BLACK, "********************************************************************************");
- return 1;
- }
- CMD:kill(playerid, params[])
- {
- new killplayer;
- if(sscanf(params, "u", killplayer)) return SendClientMessage(playerid, COLOR_WHITE, "SYNTAX: /kill [playerid/name]");
- else if(!IsPlayerConnected(killplayer)) return SendClientMessage(playerid, COLOR_RED, "This player is not connected");
- else
- {
- if (IsPlayerAdmin(playerid)) SetPlayerHealth(killplayer, 0);
- if (IsPlayerAdmin(playerid)) SendClientMessage(killplayer, COLOR_RED, "An Admin Has Killed you!");
- else SendClientMessage(playerid, COLOR_RED, notadmin);
- }
- return 1;
- }
- CMD:heal(playerid, params[])
- {
- new healplayer;
- if(sscanf(params, "u", healplayer)) return SendClientMessage(playerid, COLOR_WHITE, "SYNTAX: /heal [playerid/name]");
- else if(!IsPlayerConnected(healplayer)) return SendClientMessage(playerid, COLOR_RED, "This player is not connected");
- else
- {
- if (IsPlayerAdmin(playerid)) SetPlayerHealth(healplayer, 100);
- if (IsPlayerAdmin(playerid)) SendClientMessage(healplayer, COLOR_RED, "An Admin Has Healed you!");
- else SendClientMessage(playerid, COLOR_RED, notadmin);
- }
- return 1;
- }
- CMD:updates(playerid,params[])
- {
- ShowPlayerDialog(playerid, DIALOG_UPDATES, DIALOG_STYLE_LIST, "Server Updates", "UPDATES: UPDATED 4/5/2013, VERSION 0.5\n- Added: MySQL Admin system\n- Added: Updates dialog\n-Added: New admin commands\n- Bug Fix: Reportbug and feedback has been officially fixed.\n- Removed: /hi command, it was only a test.", "Close", "");
- return 1;
- }
- CMD:feedback(playerid,params[])
- {
- if (isnull(params)) return SendClientMessage(playerid,-1,#"SYNTAX: /feedback <feedback> " );//This is if the player does not type any feedback and only does /feedback
- new
- _msg[ 128 ],
- _msg2[ 128 ],
- player_name[ MAX_PLAYER_NAME ];
- GetPlayerName(playerid, player_name, MAX_PLAYER_NAME);
- SendClientMessage(playerid,COLOR_WHITE,"The feedback you have given has been sent to the database, We will improve it as soon as possible.");//This is to tell the player that there feedback has gone through
- format( _msg, sizeof ( _msg ), "Name: %s\r\n", player_name);
- format( _msg2, sizeof( _msg2), "Feedback: %s\r\n",params);
- new File: fileToWrite = fopen("Feedback.txt", io_append);//Feedback.txt is were all the feedback will go from your players.(It could get spammed -_-)
- fwrite(fileToWrite,"[New Feedback]\r\n");//Everytime a player types a new suggestion it will type [New Feedback] Underneath the other feedback.
- fwrite(fileToWrite, _msg);
- fwrite(fileToWrite, _msg2);
- fclose(fileToWrite);
- return 1;
- }
- CMD:reportbug(playerid,params[])
- {
- if (isnull(params)) return SendClientMessage(playerid,-1,"SYNTAX: /bugreport <bug>" );
- new
- _bug[ 128 ],
- player_name[ MAX_PLAYER_NAME ];
- GetPlayerName(playerid, player_name, MAX_PLAYER_NAME);
- SendClientMessage(playerid,COLOR_WHITE,"The bug you reported has been sent through to the database, We will investigate it as soon as possible.");
- format( _bug, sizeof( _bug ), "Bug: %s\r\n", params);
- new File: fileToWrite = fopen("bugreports.txt", io_append);
- fwrite(fileToWrite,"[New Bug Report]\r\n");
- fwrite(fileToWrite, _bug);
- fclose(fileToWrite);
- return 1;
- }
- CMD:gmx(playerid, params[])
- {
- if (IsPlayerAdmin(playerid)) SendRconCommand("gmx");
- else SendClientMessage(playerid, COLOR_RED, notadmin);
- return 1;
- }
- CMD:kick(playerid, params[])
- {
- new id, reason[128];
- if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, COLOR_RED, notadmin);
- else if(sscanf(params, "us", id, reason))SendClientMessage(playerid, COLOR_WHITE, "Usage: /kick [id/name][reason]");
- else if(id==playerid)SendClientMessage(playerid,COLOR_RED,"Error: You can not kick yourself!");
- else if(IsPlayerAdmin(id))SendClientMessage(playerid,COLOR_RED,"Error: You can not kick another admin!");
- else if (id==INVALID_PLAYER_ID)SendClientMessage(playerid,COLOR_RED,"Error: Player is not connected!");
- else {
- new Name[MAX_PLAYER_NAME], KickMessage[128];
- new Name2[MAX_PLAYER_NAME];
- GetPlayerName(playerid, Name, sizeof(Name));
- GetPlayerName(id, Name2, sizeof(Name2));
- format(KickMessage, sizeof(KickMessage), "%s(%d) has kicked player %s(%d). Reason: %s", Name, playerid, Name2, id);
- SendClientMessageToAll(COLOR_WHITE, KickMessage);
- Kick(id);
- }
- return 1;
- }
- CMD:ban(playerid, params[])
- {
- new id, reason[128];
- if(!IsPlayerAdmin(playerid) || pInfo[playerid][pAdmin] <= SETLEVEL_ADMIN )return SendClientMessage(playerid, COLOR_RED, notadmin);
- else if(sscanf(params, "us", id, reason))SendClientMessage(playerid, COLOR_WHITE, "Usage: /ban [id/name][reason]");
- else if(id==playerid)SendClientMessage(playerid,COLOR_RED,"Error: You can not ban yourself!");
- else if(IsPlayerAdmin(id))SendClientMessage(playerid,COLOR_RED,"Error: You can not ban another admin!");
- else if (id==INVALID_PLAYER_ID)SendClientMessage(playerid,COLOR_RED,"Error: Player is not connected!");
- else {
- new Name[MAX_PLAYER_NAME], banmessage[128];
- new Name2[MAX_PLAYER_NAME];
- GetPlayerName(playerid, Name, sizeof(Name));
- GetPlayerName(id, Name2, sizeof(Name2));
- format(banmessage, sizeof(banmessage), "%s(%d) has banned player %s(%d). Reason: %s", Name, playerid, Name2, id);
- SendClientMessageToAll(COLOR_WHITE, banmessage);
- Ban(id);
- }
- return 1;
- }
- CMD:acmds(playerid,params[])
- {
- if(!IsPlayerAdmin(playerid) || pInfo[playerid][pAdmin] <= 0 )return SendClientMessage(playerid, COLOR_RED, notadmin);
- else ShowPlayerDialog(playerid, DIALOG_ACMDS, DIALOG_STYLE_MSGBOX, "Admin Commands", "/ban(playerid)(reason) - ban a player /kick(playerid)(reason) - Kick a player\n/heal(playerid) - heal a player /kill(playerid) - kill a player\n/gmx - Restart the server(rcon admins only)", "Close", "");
- return 1;
- }
- /*SendClientMessageToAll()
- ====================================
- SET LEVEL COMMAND
- ====================================
- VERY LONG
- ====================================
- ====================================
- */
- CMD:setlevel(playerid, params[])
- {
- if(IsPlayerAdmin(playerid) || pInfo[playerid][pAdmin] >= SETLEVEL_ADMIN)
- // if the player is an rcon admin or the players admin level is greater or equal
- // to the defined admin level of that command
- {//then
- new level, target;
- //here we create 2 variable which we will use
- if(sscanf(params, "ui", target, level)) return SendClientMessage(playerid, red, "[*] Usage: /setlevel [playerid/name] [level]");
- //if the player hasnt entered a target id or level then it will return that msg
- if(target == INVALID_PLAYER_ID) return SendClientMessage(playerid, red, "[*] Enter a valid player ID/name!");
- // if the target player is not a valid player then it will return that msg
- new string[128], Query[500], str2[50];
- if(level < 0 || level > MAX_ADMIN_LEVEL) // here we check if the level the player entered is more then the max admin level defined or 0
- {
- format(string, sizeof(string), "Enter a level between 0 and %d!", MAX_ADMIN_LEVEL);
- SendClientMessage(playerid, red,string); // if it is then we send the message and stop the command from processing
- return 1;
- }
- format(Query, 500, "UPDATE `playerdata` SET `admin` = '%d' WHERE `id` = '%d' LIMIT 1", level, pInfo[target][ID]); //Format the query
- mysql_query(Query);
- // here we use the UPDATE option again and tell the database to update the player we specified's admin level to what we have set.
- pInfo[target][pAdmin] = level;
- // here we set the target's var to what we defined
- format(string, 256, "You have set %s[%d]'s admin level to %d.", pInfo[target][Nick], target, level);
- SendClientMessage(playerid, green, string);
- format(string, 256, "Admin %s has set your admin level to %d.", pInfo[playerid][Nick], level);
- SendClientMessage(target, yellow, string);
- for (new i=0; i<MAX_PLAYERS; i++)
- {
- if(pInfo[i][Logged] == 1 && pInfo[i][pAdmin])
- {
- format(str2, sizeof(str2), "Admin %s[%d] has used the following command: Setlevel", pInfo[playerid][Nick], playerid);
- SendClientMessage(i,COLOR_SPRINGGREEN,string);
- }
- }
- // here we send the message to the admins
- PlayerPlaySound(target,1057,0.0,0.0,0.0);
- // play the sound for the target notifying him of this command.
- }
- else return notadmin(playerid); // if it wasnt a rcon admin or his admin level wasnt greater then the max level
- //then we send the error message
- return 1;
- }
- /*
- ======================================
- SETADMIN END
- ======================================
- */
Advertisement
Add Comment
Please, Sign In to add comment