Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is Simple MySQL r9-3 GameMode by JeaSon
- Do no re release without my permission
- */
- //First, of course we need to include these files first
- #include <a_samp> //Without this, we won't be able to use all samp functions/callbacks
- #include <a_mysql> //Without this, we won't be able to use all mysql functions
- #include <sscanf2> // without this we wont be able to use commands and compile code
- #include <zcmd> // without this our commands will not work
- //Let's define our mysql settings
- #define host "localhost" //This will be your mysql host. Default for xampp is localhost
- #define user "root" //This will be your mysql username. Default for xampp is root
- #define db "sami" //This is your database name. Remember we have created a database called server before.
- #define pass "" //This is your mysql password. In xampp, the password didn't set. So leave it empty.
- //dialogs
- #define dREGISTER 1 //dialog register id
- #define dLOGIN 2 // ^dialog login id
- #define dSTATS 3 //dialog stats id
- #define LIST_COMMAND true
- #define LIST_USE_DIALOG false
- #define COMMAND_BLOCKED_MSG "This command has been blocked, you cannot use it."
- #define COMMAND_BLOCKED_COLOR 0xFF0000FF
- #define MAX_COMMAND_BLOCKED 25
- #define COLOR_RED 0xFF0000FF
- #define COLOR_CMY 0xFFFF00FF
- #define COLOR_YELLOW 0xFFDD00AA
- //these are custom callbacks so they must be forwarded otherwise compiler will show error
- forward OnAccountCheck(playerid);
- forward OnAccountLoad(playerid);
- forward OnAccountRegister(playerid);
- forward UnMutedTimer(playerid);
- new BlockedCommand[MAX_COMMAND_BLOCKED][25];
- new IsPlayerRegisterd[MAX_PLAYERS];
- new bool:pMuted[MAX_PLAYERS];//Global variables. We will use them later
- 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.
- ;
- native WP_Hash(buffer[], len, const str[]); //whirlpool, for hashing our password
- //Now let's create an enumerator that holds player's information
- enum DATAX //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.
- Kills,
- Deaths,
- Score,
- Money //We will load player's money from database into this variable so we can use it anywhere later.
- }
- new pData[MAX_PLAYERS][DATAX]; //Variable that stores enumerator above
- main(){}
- public OnGameModeInit()
- {
- mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG); //Let's enable debugging so we can detect a problem(if there is)
- 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.
- 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.
- }
- else
- {
- printf("Successfully connected on DB %s",db);
- }
- SetGameModeText("MySQL R9-3");
- return 1;
- }
- //Checking player's account if they are registered or not.
- public OnPlayerConnect(playerid)
- {
- // resetting player enums so old's stats wont mix to new playerid
- for(new i; DATAX:i < DATAX; i++)
- {
- pData[playerid][DATAX:i] = 0;
- }
- IsPlayerRegisterd[playerid] = 0;
- 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 `IP`, `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);
- //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;
- }
- //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);//let's get the rows and fields from the database.
- if(rows) //if there is row
- {
- cache_get_field_content(0, "IP", IP[playerid], mysql, 16);
- new newIP[16];
- GetPlayerIp(playerid, newIP, 16);
- IsPlayerRegisterd[playerid] = 1;
- if(strlen(IP[playerid]) != 0 && !strcmp(IP[playerid], newIP, true)) //Checks that the MySQL IP has a value and that they are the same.
- {
- GameTextForPlayer(playerid, "Loading Account", 3000, 3);
- SetTimerEx("OnAccountLoad", 3000, false, "i", playerid);
- }
- else
- {
- (!strlen(IP[playerid]) || strcmp(IP[playerid], newIP, true));
- //then
- cache_get_field_content(0, "Password", pData[playerid][Password], mysql, 129);
- //we will load player's password into pData[playerid][Password] to be used in logging in
- pData[playerid][ID] = cache_get_field_content_int(0, "ID"); //now let's load player's ID into pData[playerid][ID] so we can use it later
- printf("%s", pData[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;
- }
- //Now let's response to the login and register dialog
- 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, pData[playerid][Password])) //remember we have loaded player's password into this variable, pData[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
- //We select all rows in the table that has your name and limit the result to 1
- 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(pData[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(pData[playerid][Password], 129, inputtext); //hashing inputtext
- mysql_format(mysql, query, sizeof(query), "INSERT INTO `players` (`Username`, `Password`, `IP`, `Admin`, `VIP`,`Kills`,`Deaths`,`Score`, `Money`) VALUES ('%e', '%s', '%s', 0, 0, 0, 0, 0, 50000)", Name[playerid], pData[playerid][Password], IP[playerid]);
- //Now let's create a new row and insert player's information in it
- mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid);
- //let's execute the query
- }
- }
- return 1;
- }
- //let's load player's information
- public OnAccountLoad(playerid)
- {
- new score;
- pData[playerid][Admin] = cache_get_field_content_int(0, "Admin"); //we're getting a field 4 from row 0. And since it's an integer, we use cache_get_row_int
- pData[playerid][VIP] = cache_get_field_content_int(0, "VIP"); //Above
- pData[playerid][Money] = cache_get_field_content_int(0, "Money");//Above
- pData[playerid][Kills] = cache_get_field_content_int(0,"Kills");
- pData[playerid][Deaths] = cache_get_field_content_int(0, "Deaths");
- score = cache_get_field_content_int(0, "Score");
- SetPlayerScore(playerid, score);
- GivePlayerMoney(playerid, pData[playerid][Money]);//Let's set their money
- //For player's position, set it once they spawn(OnPlayerSpawn)
- SendClientMessage(playerid, -1, "Successfully logged in"); //tell them that they have successfully logged in
- return 1;
- }
- public OnAccountRegister(playerid)
- {
- pData[playerid][ID] = cache_insert_id(); //loads the ID of the player in the variable once they registered.
- printf("New account registered. ID: %d", pData[playerid][ID]); //just for debugging.
- pData[playerid][Money] = 50000;
- GivePlayerMoney(playerid, 50000);
- return 1;
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- if(IsPlayerRegisterd[playerid] != 0)
- {
- SavePlayerData(playerid);
- }
- return 1;
- }
- public OnPlayerDeath(playerid, killerid, reason)
- {
- if(killerid != INVALID_PLAYER_ID)
- {
- SetPlayerScore(killerid, GetPlayerScore(killerid) + 1);
- pData[killerid][Kills]++;
- }
- pData[playerid][Deaths]++;
- return 1;
- }
- stock SavePlayerData(playerid)
- {
- new query[100]; //query[128] is for formatting our query and Float:pos[3] is for getting and saving player's position
- mysql_format(mysql, query, sizeof(query), "UPDATE `players` SET `IP`='%s', `Admin`=%d, `VIP`=%d, `Kills`=%d, `Deaths`=%d, `Score`=%d, `Money`=%d WHERE `ID`=%d",\
- IP[playerid], pData[playerid][Admin], pData[playerid][VIP], pData[playerid][Kills], pData[playerid][Deaths], GetPlayerScore(playerid), pData[playerid][Money], pData[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.
- }
- CMD:setlevel(playerid, params[])
- {
- new lookupid, str[128], level;
- if(pData[playerid][Admin] == 5)
- {
- if(sscanf(params,"ud",lookupid,level)) return SendClientMessage(playerid, -1, "Usage: /setlevel (UserID | UserName) (level)");
- if(pData[lookupid][Admin] > pData[playerid][Admin]) return SendClientMessage(playerid, -1, "Sorry you cant setlevel becoz his level is higher then you");
- if(level < 1 ||level > 5) return SendClientMessage(playerid, -1, "1 to 5 levels"); // you can change this to any level you want
- if(!IsPlayerConnected(lookupid)) return SendClientMessage(playerid, -1, "Sorry this player isnt connected ");
- if(level < pData[lookupid][Admin])
- {
- format(str, sizeof(str), "Admin %s (ID:%d) has demoted you to level %d",GetName(playerid), playerid, level);
- SendClientMessage(lookupid, -1, str);
- format(str, sizeof(str),"You have demoted %s (ID:%d) to level %d",GetName(lookupid),lookupid,level);
- SendClientMessage(playerid, -1, str);
- }
- if(level > pData[lookupid][Admin])
- {
- format(str, sizeof(str), "Admin %s (ID:%d) has promoted you to level %d",GetName(playerid), playerid, level);
- SendClientMessage(lookupid, -1, str);
- format(str, sizeof(str),"You have promoted %s (ID:%d) to level %d",GetName(lookupid),lookupid,level);
- SendClientMessage(playerid, -1, str);
- }
- }
- else return SendClientMessage(playerid, -1, "You are not authorized to can use this command");
- return 1;
- }
- CMD:setvip(playerid, params[])
- {
- new lookupid, str[128], level;
- if(pData[playerid][Admin] == 5)
- {
- if(sscanf(params,"ud",lookupid,level)) return SendClientMessage(playerid, -1, "Usage: /setlevel (UserID | UserName) (level)");
- if(pData[lookupid][Admin] > pData[playerid][Admin]) return SendClientMessage(playerid, -1, "Sorry you cant setlevel becoz his level is higher then you");
- if(level < 1 ||level > 5) return SendClientMessage(playerid, -1, "1 to 5 levels"); // you can change this to any level you want
- if(!IsPlayerConnected(lookupid)) return SendClientMessage(playerid, -1, "Sorry this player isnt connected ");
- if(level < pData[lookupid][Admin])
- {
- format(str, sizeof(str), "Admin %s (ID:%d) has demoted you to VIP level %d",GetName(playerid), playerid, level);
- SendClientMessage(lookupid, -1, str);
- format(str, sizeof(str),"You have demoted %s (ID:%d) to VIP level %d",GetName(lookupid),lookupid,level);
- SendClientMessage(playerid, -1, str);
- }
- if(level > pData[lookupid][Admin])
- {
- format(str, sizeof(str), "Admin %s (ID:%d) has promoted you to VIP level %d",GetName(playerid), playerid, level);
- SendClientMessage(lookupid, -1, str);
- format(str, sizeof(str),"You have promoted %s (ID:%d) to VIP level %d",GetName(lookupid),lookupid,level);
- SendClientMessage(playerid, -1, str);
- }
- }
- else return SendClientMessage(playerid, -1, "You are not authorized to can use this command");
- return 1;
- }
- CMD:stats(playerid, params[])
- {
- new str[64];
- new deaths = pData[playerid][Deaths];
- if(!deaths) deaths = 1;
- new Float:kd = floatdiv(pData[playerid][Kills], deaths);
- format(str, sizeof(str),"Your Stats\nScore: %d\nKills: %d\nDeaths: %d\nKD-Ratio: %0.2f\nMoney: %d\n Level: %d",pData[playerid][Score],pData[playerid][Kills],pData[playerid][Deaths],kd,pData[playerid][Money],pData[playerid][Admin]);
- ShowPlayerDialog(playerid, dSTATS,DIALOG_STYLE_MSGBOX,"Account stats",str, "Close","");
- return 1;
- }
- CMD:mute(playerid, params[])
- {
- if(pData[playerid][Admin] >= 3) // player is not admin
- {
- new id, mins, str[128];
- if(sscanf(params,"ui",id,mins)) return SendClientMessage(playerid, -1,"Usage: /mute <playerid> <minutes>");
- if (!IsPlayerConnected(id)) return 1; // invalid player
- if(pMuted[id] == true) return SendClientMessage(playerid, COLOR_RED,"already muted ");
- pMuted[id] = true;
- format(str, sizeof(str),"Admin %s (ID:%d) has muted %s (ID:%d) for %d min(s)",GetName(playerid), playerid, GetName(id), id, mins);
- SendClientMessageToAll(COLOR_RED, str);
- SetTimerEx("UnMutedTimer", 60*1000*mins, false, "i", id); //60*1000 = 1 minute
- }
- return 1;
- }
- CMD:unmute(playerid, params[])
- {
- if(pData[playerid][Admin] >= 3) // player is not admin
- {
- new id, str[128];
- if(sscanf(params,"u",id)) return SendClientMessage(playerid, COLOR_CMY,"Usage: /mute <playerid>");
- if(!IsPlayerConnected(id)) return SendClientMessage(playerid, COLOR_CMY,"target is not connected"); // invalid player id
- if(pMuted[id] == false) return SendClientMessage(playerid, COLOR_RED,"Player is not muted ");
- pMuted[id] = false;
- format(str, sizeof(str),"Admin %s (ID:%d) has unmuted %s (ID:%d)",GetName(playerid), playerid, GetName(id), id);
- SendClientMessageToAll(COLOR_RED, str);
- }
- return 1;
- }
- public UnMutedTimer(playerid)
- {
- new str[128];
- pMuted[playerid] = false;
- format(str,sizeof(str),"%s (ID:%d) has been auto unmuted by server ",GetName(playerid), playerid);
- SendClientMessageToAll(COLOR_RED, str);
- return 1;
- }
- CMD:setmoney(playerid, params[])
- {
- new str[128], lookupid, amount;
- if(pData[playerid][Admin] >= 3)
- {
- if(sscanf(params, "ui",lookupid, amount)) return SendClientMessage(playerid ,COLOR_YELLOW, "Usage: /setmoney <playerid/Name> <amount>");
- if(!IsPlayerConnected(lookupid)) return SendClientMessage(playerid, COLOR_YELLOW, "Player is not connected");
- format(str, sizeof(str), "Admin %s (ID:%d) has setted %s (ID:%d) money count to (%d)", GetName(playerid), playerid, GetName(lookupid),lookupid, amount);
- SendClientMessage(lookupid, COLOR_YELLOW, str);
- SetPlayerMoney(lookupid, amount);
- pData[lookupid][Money] = amount;
- } else return SendClientMessage(playerid, COLOR_YELLOW, "Only admin level 3+ can use this cmd");
- return 1;
- }
- CMD:setscore(playerid, params[])
- {
- new str[128], lookupid, amount;
- if(pData[playerid][Admin] >= 3)
- {
- if(sscanf(params, "ui",lookupid, amount)) return SendClientMessage(playerid ,COLOR_YELLOW, "Usage: /setscore <playerid/Name> <amount>");
- if(!IsPlayerConnected(lookupid)) return SendClientMessage(playerid, COLOR_YELLOW, "Player is not connected");
- format(str, sizeof(str), "Admin %s (ID:%d) has setted %s (ID:%d) score count to (%d)", GetName(playerid), playerid, GetName(lookupid),lookupid, amount);
- SendClientMessage(lookupid, COLOR_YELLOW, str);
- SetPlayerScore(lookupid, amount);
- pData[lookupid][Score] = amount;
- } else return SendClientMessage(playerid, COLOR_YELLOW, "Only admin level 3+ can use this cmd");
- return 1;
- }
- CMD:setdeaths(playerid, params[])
- {
- new str[128], lookupid, amount;
- if(pData[playerid][Admin] >= 3)
- {
- if(sscanf(params, "ui",lookupid, amount)) return SendClientMessage(playerid ,COLOR_YELLOW, "Usage: /setdeaths <playerid/Name> <amount>");
- if(!IsPlayerConnected(lookupid)) return SendClientMessage(playerid, COLOR_YELLOW, "Player is not connected");
- format(str, sizeof(str), "Admin %s (ID:%d) has setted %s (ID:%d) Death count to (%d)", GetName(playerid), playerid, GetName(lookupid),lookupid, amount);
- SendClientMessage(lookupid, COLOR_YELLOW, str);
- pData[lookupid][Deaths] = amount;
- } else return SendClientMessage(playerid, COLOR_YELLOW, "Only admin level 3+ can use this cmd");
- return 1;
- }
- CMD:setkills(playerid, params[])
- {
- new str[128], lookupid, amount;
- if(pData[playerid][Admin] >= 3)
- {
- if(sscanf(params, "ui",lookupid, amount)) return SendClientMessage(playerid ,COLOR_YELLOW, "Usage: /sekills <playerid/Name> <amount>");
- if(!IsPlayerConnected(lookupid)) return SendClientMessage(playerid, COLOR_YELLOW, "Player is not connected");
- format(str, sizeof(str), "Admin %s (ID:%d) has setted %s (ID:%d) Kill count to (%d)", GetName(playerid), playerid, GetName(lookupid),lookupid, amount);
- SendClientMessage(lookupid, COLOR_YELLOW, str);
- pData[lookupid][Kills] = amount;
- } else return SendClientMessage(playerid, COLOR_YELLOW, "Only admin level 3+ can use this cmd");
- return 1;
- }
- CMD:blockcommand(playerid, params[])
- {
- if(!IsPlayerAdmin(playerid)) return 0;
- new command[25];
- if(sscanf(params, "s[25]", command)) return SendClientMessage(playerid, -1, "USAGE: /blockcommand [command]");
- if(command[0] == '/') strdel(command, 0, 1);
- new slotfree = -1;
- for(new i = 0; i < MAX_COMMAND_BLOCKED; i++)
- {
- if(BlockedCommand[i][0] == '\0') slotfree = i;
- else if(!strcmp(BlockedCommand[i], command, true)) return SendClientMessage(playerid, -1, "This command is already blocked. Use '/unblockcommand [command]' to unblock it.");
- if(slotfree != -1) continue;
- }
- if(slotfree == -1) return SendClientMessage(playerid, -1, "You have reached the maximum limit of blocked commands. Please unblock some before proceeding.");
- format(BlockedCommand[slotfree], 25, "%s", command);
- SendClientMessage(playerid, -1, "SUCCESS: Command Blocked successfully.");
- return 1;
- }
- CMD:blockcmd(playerid, params[]) return cmd_blockcommand(playerid, params);
- CMD:unblockcommand(playerid, params[])
- {
- if(!IsPlayerAdmin(playerid)) return 0;
- new command[25];
- if(sscanf(params, "s[25]", command)) return SendClientMessage(playerid, -1, "USAGE: /unblockcommand [command]");
- if(command[0] == '/') strdel(command, 0, 1);
- new slotfree = -1;
- for(new i = 0; i < MAX_COMMAND_BLOCKED; i++)
- {
- if(!strcmp(BlockedCommand[i], command, true) && BlockedCommand[i][0] != '\0')
- {
- slotfree = i;
- break;
- }
- }
- if(slotfree == -1) return SendClientMessage(playerid, -1, "This command is not blocked. Use '/blockcommand [command]' to block it.");
- strdel(BlockedCommand[slotfree], 0, strlen(BlockedCommand[slotfree]));
- SendClientMessage(playerid, -1, "SUCCESS: Command Unblocked successfully.");
- return 1;
- }
- CMD:unblockcmd(playerid, params[]) return cmd_unblockcommand(playerid, params);
- public OnPlayerCommandPerformed(playerid, cmdtext[], success)
- {
- new bool:CMD_BLOCKED = false;
- for(new i = 0; i < MAX_COMMAND_BLOCKED; i++)
- {
- if(BlockedCommand[i][0] == '\0') continue;
- if(cmdtext[0] == '/')
- {
- if(!strcmp(BlockedCommand[i], cmdtext[1], true)) CMD_BLOCKED = true;
- }
- else if(!strcmp(BlockedCommand[i], cmdtext, true)) CMD_BLOCKED = true;
- else continue;
- }
- if(CMD_BLOCKED) return SendClientMessage(playerid, COMMAND_BLOCKED_COLOR, COMMAND_BLOCKED_MSG);
- return success;
- }
- stock GetName(playerid)
- {
- new PlayerName[MAX_PLAYER_NAME];
- GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
- return PlayerName;
- }
- stock SetPlayerMoney(playerid, money)
- {
- ResetPlayerMoney(playerid);
- GivePlayerMoney(playerid, money);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement