Logic is right. This aint a charity. This is a place where you could get assistance with your code. If everybody would be feeding the spoon to you, what would be the point of all this if you wont learn from it.
Kovac is right as wel. Do not check for a part of IP. I even recommend not using IP at all.
The thing is, when you are going to ban ip addresses. you might be banning internet cafes or school buildings. Imagine 5 guys playing on your server when they are at school. Ban one of them and you ban the other 4 too.
Also, many people use a dynamic changing IP address so you can't even ban them using a ip address.
Let me create a step plan for you on how to create this:
note: You will be needing a saving system for this and i will explain it in sqlite to you.
I am explaining this in SQLite instead of MySQL because it does not require you to have a sql server. This just uses a file in your scriptfiles folder.
I have created a database for you using db browser for SQLite and you can download it here:
test_database.db
Put test_database inside scriptfiles folder.
This database is empty and does require step 2 below to get the table and fields or you could manually add them.
1. At the very top of your script place this:
Код:
#include <zcmd>
#include <sscanf2>
new DB:TEST_DATABASE; // you can rename TEST_DATABASE to whatever you want
2. Create a table name called BANS with the fields Name, Admin and reason:
So under OnGameModeInit() we place the following code:
PHP код:
TEST_DATABASE = db_open("test_database.db"); // Opens the path to the database, this file should be in your scriptfiles folder
db_query(TEST_DATABASE, "CREATE TABLE IF NOT EXISTS `BANS` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT, `NAME` TEXT NOT NULL, `ADMIN` TEXT NOT NULL, `REASON` TEXT NOT NULL)");
3. The following thing we want to do is to check if the player is banned. So the following code should be placed under OnPlayerConnect or OnplayerRequestClass.
PHP код:
new DBResult:BANISHMENTS_RESULTS;
new szQuery[128], name[MAX_PLAYER_NAME];
GetPlayerName(playerid, name, sizeof(name));
format(szQuery, sizeof(szQuery), "select * from `BANS` where `NAME` = '%s'", DB_Escape(name)); // format the query into a string which selects rows that contain the players name.
BANISHMENTS_RESULTS = db_query(TEST_DATABASE, szQuery); // Execute the query
if(db_num_rows(BANISHMENTS_RESULTS))
{
// Tell the player he is banned and kick him.
}
db_free_result(BANISHMENTS_RESULTS);
4. The following thing we do is create a command to ban a player. For now i use ZCMD.
PHP код:
CMD:ban(playerid,params[])
{
new szQuery[128], szQuery2[128], reason[128] , str[256], bannerstr[128], reasonstr[256], finalstr[128];
if(sscanf(params,"ss[128]",params,reason)) return SendClientMessage(playerid, -1, "USAGE: /ban [name] [reason]");
new DBResult:BANISHMENTS_RESULTS;
format(szQuery, sizeof(szQuery), "select * from `BANS` where `NAME` = '%s'", DB_Escape(params)); // format the query into a string which selects rows that contain the players name.
BANISHMENTS_RESULTS = db_query(TEST_DATABASE, szQuery); // Execute the query
if(db_num_rows(BANISHMENTS_RESULTS)) // when player is already banned
{
db_get_field_assoc(BANISHMENTS_RESULTS, "ADMIN", bannerstr, sizeof(bannerstr)); // get the admin who banned the player
db_get_field_assoc(BANISHMENTS_RESULTS, "REASON", reasonstr, sizeof(reasonstr)); // get the reason of banishment.
format(str, sizeof(str), "This player is already banned. Admin: %s Reason: %s", bannerstr, reasonstr);
SendClientMessage(playerid, -1, str);
db_free_result(BANISHMENTS_RESULTS);
} else { // player is not banned so he can be banned
new DBResult:INSERT_BAN_RESULT;
new AdminName[MAX_PLAYER_NAME];
GetPlayerName(playerid, AdminName, MAX_PLAYER_NAME);
format(szQuery2, sizeof(szQuery2), "INSERT INTO BANS (ID, NAME, ADMIN, REASON) VALUES('NULL', '%s', '%s', %s)", DB_Escape(params), DB_Escape(AdminName), DB_Escape(reason)); // format the query to insert the ban into the database
INSERT_BAN_RESULT = db_query(TEST_DATABASE, szQuery2); // Execute the query
new getplayerid = GetPlayerID(params); //get the player id of the banned player
SendClientMessage(getplayerid, -1 "You have been banned from the server");
format(finalstr, sizeof(finalstr), "%s has been banned by %s. reason: %s", params, AdminName, reason);
SendClientMessageToAll(-1, finalstr);
}
return 1;
}
5. I have used DB_escape to prevent sql injection so you need to add the function to your gamemode:
PHP код:
stock DB_Escape(text[]) {
/* credits to ****** according to the wiki */
new
ret[80 * 2],
ch,
i,
j;
while ((ch = text[i++]) && j < sizeof (ret))
{
if (ch == '\'')
{
if (j < sizeof (ret) - 2)
{
ret[j++] = '\'';
ret[j++] = '\'';
}
}
else if (j < sizeof (ret))
{
ret[j++] = ch;
}
else
{
j++;
}
}
ret[sizeof (ret) - 1] = '\0';
return ret;
}
Note: I have typed the code above in this thread and i haven't compiled it or used a scripting program to write it and thats why the code above may not work of may have bugs but the point is that you'll get an idea of how it can be done.