Temporary bans can be more easily scriptable?
#1

Well, by some months I assume that could not understand perfectly how the 'self ban machine' works, is too much calculations that confuse me. But I'm not creating this thread to understand this.

I was studying about this and the function gettime caught my eye, since this get the seconds count from the date 01/01/1970 to the actual date.

I'll show a fast and non-working example:

From LuxAdmin temp ban system based on Self Ban Machine:

pawn Код:
if(strcmp(cmd, "/tempban", true) == 0)
{
    new name[MAX_PLAYER_NAME];
    new giveplayer[MAX_PLAYER_NAME];
    new giveplayerid;

    if(AccInfo[playerid][Level] >= 4)
    {
        tmp = strtok(cmdtext,idx);
        if(!strlen(tmp))
        {
            SendClientMessage(playerid, LIGHTBLUE2, "Usage: /tempban [PlayerID] [Day(s)] [Reason]");
            SendClientMessage(playerid, orange, "Function: Temporarily bans a player for specified Days");
            return 1;
        }
        giveplayerid = ReturnUser(tmp);
        if(IsPlayerConnected(giveplayerid))
        {
            tmp = strtok(cmdtext, idx);
            if (!strlen(tmp))
            {
            SendClientMessage(playerid, LIGHTBLUE2, "Usage: /tempban [PlayerID] [Day(s)] [Reason]");
            SendClientMessage(playerid, orange, "Function: Temporarily bans a player for specified Days");
            return 1;
            }
            new days = strval(tmp);
            if(!IsNumeric(tmp))
            return SendClientMessage(playerid, lightred, "ERROR: Invalid Day! Only Numbers!");

            if(strval(tmp) <= 0 || strval(tmp) > 1000)
            return SendClientMessage(playerid, lightred, "ERROR: Invalid Day! (1-1000)");

            new reason[128];
            reason = strtok2(cmdtext,idx);
            if (!strlen(reason))
            return SendClientMessage(playerid, lightred, "ERROR: Reason not Specified!");

            if (strlen(reason) <= 0 || strlen(reason) > 100)
            return SendClientMessage(playerid, lightred, "ERROR: Invalid Reason length!");

            new ip[15];
            GetPlayerIp(giveplayerid,ip,15);
            GetPlayerName(playerid, name, sizeof name);
            GetPlayerName(giveplayerid, giveplayer, sizeof(giveplayer));
            new File:tempban = fopen("LuxAdmin/Config/TempBans.ban", io_append);
            if (tempban)
            {
                new year,month,day;
                getdate(year, month, day);
                day += days;
                if (IsMonth31(month))
                {
                    if (day > 31)
                    {
                        month += 1;
                        if (month > 12)
                        {
                            year += 1;
                            while(day > 31) day -= 31;
                        }
                        else while(day > 31) day -= 31;
                    }
                }
                else if (!IsMonth31(month))
                {
                    if (day > 30)
                    {
                        month += 1;
                        if (month > 12)
                        {
                            year += 1;
                            while(day > 30) day -= 30;
                        }
                        else while(day > 30) day -= 30;
                    }
                }
                else if (!IsMonth31(month) && IsMonth29(year) && month == 2)
                {
                    if (day > 29)
                    {
                        month += 1;
                        if (month > 12)
                        {
                            year += 1;
                            while(day > 29) day -= 29;
                        }
                        else while(day > 29) day -= 29;
                    }
                }
                else if (!IsMonth31(month) && !IsMonth29(year) && month == 2)
                {
                    if (day > 28)
                    {
                        month += 1;
                        if (month > 12)
                        {
                            year += 1;
                            while(day > 28) day -= 28;
                        }
                        else while(day > 28) day -= 28;
                    }
                }
                format(string, sizeof string, "%d|%d|%d|%s\n", day, month, year, ip);
                fwrite(tempban, string);
                fclose(tempban);
            }
            format(string,128,"|- Administrator %s Temporarily Banned %s for %d Day(s) | Reason: %s -|",name,giveplayer,days,reason);
            SendClientMessageToAll(lightred,string);
            Kick(giveplayerid);

            format(string, sizeof string, "Admin %s Temporarily Banned %s for %d Day(s) | Reason: %s",name,giveplayer,days,reason);
            SaveIn("TempBansLog",string);
        }
        else
        {
        ErrorMessages(playerid, 2);
        }
    }
    else return ErrorMessages(playerid, 1);
    return true;
}

stock TempBanCheck(playerid)
{
    new ip[15];
    new str[128];
    new load[4][32];
    new ban_day, ban_month, ban_year, ban_ip[15];
    GetPlayerIp(playerid, ip, sizeof ip);
    new year, month, day;
    getdate(year, month, day);
    new File:file = fopen("LuxAdmin/Config/TempBans.ban",io_read);
    if (file)
    {
        while (fread(file, str, sizeof str))
        {
            split(str, load, '|');

            ban_day = strval(load[0]);
            ban_month = strval(load[1]);
            ban_year = strval(load[2]);
            strmid(ban_ip, load[3], 0, strlen(load[3])-1, 15);
            if (!(year >= ban_year && month >= ban_month && day >= ban_day) && !strcmp(ban_ip, ip, true))
            {
                format(str, sizeof str, "|- You are temporarily banned from this server until: %d/%d/%d -|", ban_day, ban_month, ban_year);
                SendClientMessage(playerid, RED, str);
                return Kick(playerid);
            }
        }
    }
    return true;
}

stock IsMonth31(month)
{
    switch (month)
    {
        case 1: return 1;
        case 3: return 1;
        case 5: return 1;
        case 7: return 1;
        case 8: return 1;
        case 10: return 1;
        case 12: return 1;
        default: return 0;
    }
    return 0;
}

stock IsMonth29(year)
{
    new y = 2000;
    for(new i = 4; i < 3000; i += 4) if ((y+i) == year) return 1;
    return 0;
}
Supposed temp ban system with gettime(only an fast example, doesn't works):

pawn Код:
TempBanCheck(playerid)
{
    new PlayerUnbanDay = dini_Int("playeracoount", "unbanday");

    return (gettime() < PlayerUnbanDay);
}

if(!strcmp("/tempban", maincmd, true))
{
    param = strtok(cmdtext, index);
    player = strval(param);
    param = strtok(cmdtext, index);
    new days = strval(param);
    Dini_SetInt("playeraccount", "unbanday", gettime() + (days * 1440));
}
Supossedly, the same system can be used to mute or anything else one player for minutes/seconds/days.

The main question is: Why Self and anyone else not used it in your code when made your temporary ban system?
Reply
#2

Hmm That seems more easy, i was trying to compress it, but here you are, you did it, and you was faster then me.
Reply
#3

I just want to know if there is any problem with doing it that way.
Reply
#4

I think these functions should help you BOTH understanding how that "self ban" system works (I don't understand why it's called like that anyway...), and to code your own system:
Код:
stock mktime(hour,minute,second,day,month,year)
{
	new timestamp2;

	timestamp2 = second + (minute * 60) + (hour * 3600);

	new days_of_month[12];

	if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) {
			days_of_month = {31,29,31,30,31,30,31,31,30,31,30,31}; // Schaltjahr
		} else {
			days_of_month = {31,28,31,30,31,30,31,31,30,31,30,31}; // keins
		}
	new days_this_year = 0;
	days_this_year = day;
	if(month > 1) { // No January Calculation, because its always the 0 past months
		for(new i=0; i<month-1;i++) {
			days_this_year += days_of_month[i];
		}
	}
	timestamp2 += days_this_year * 86400;

	for(new j=1970;j<year;j++) {
		timestamp2 += 31536000;
		if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )  timestamp2 += 86400; // Schaltjahr + 1 Tag
	}

	return timestamp2;
}

stock timestamp()
{
	new h,m,s,d,n,y;
	gettime(h,m,s);
	getdate(y,n,d);
	return mktime(h,m,s,d,n,y);
}

stock date( timestamp, _form=0 )
{
    /*
        ~ convert a Timestamp to a Date.
        ~ 10.07.2009

        date( 1247182451 )  will print >> 09.07.2009-23:34:11
        date( 1247182451, 1) will print >> 09/07/2009, 23:34:11
        date( 1247182451, 2) will print >> July 09, 2009, 23:34:11
        date( 1247182451, 3) will print >> 9 Jul 2009, 23:34
    */
    new year=1970, day=0, month=0, hour=0, mins=0, sec=0;

    new days_of_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    new names_of_month[12][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    new returnstring[32];
    
    timestamp -= 86400;
	// 31622400: seconds in 366 days, 31536000: seconds in 365 days
    while(timestamp>31622400){
        timestamp -= 31536000;
        // if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) timestamp -= 86400;
        year++;
    }

    if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )
        days_of_month[1] = 29;
    else
        days_of_month[1] = 28;


    while(timestamp>86400){
        timestamp -= 86400, day++;
        if(day==days_of_month[month]) day=0, month++;
    }

    while(timestamp>60){
        timestamp -= 60, mins++;
        if( mins == 60) mins=0, hour++;
    }

    sec=timestamp;

    switch( _form ){
        case 1: format(returnstring, 31, "%02d/%02d/%d %02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
        case 2: format(returnstring, 31, "%s %02d, %d, %02d:%02d:%02d", names_of_month[month],day+1,year, hour, mins, sec);
        case 3: format(returnstring, 31, "%d %c%c%c %d, %02d:%02d", day+1,names_of_month[month][0],names_of_month[month][1],names_of_month[month][2], year,hour,mins);
        case 4: format(returnstring, 31, "%d-%02d-%02d %02d:%02d:%02d", year, month+1, day+1, hour, mins, sec);
        case 5: format(returnstring, 31, "%02d:%02d", hour, mins);

        default: format(returnstring, 31, "%02d.%02d.%d-%02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
    }

    return returnstring;
}
The "timestamp" parameter is what you get with "GetDate".

However, if you have a MySQL server, you'd better use it with the helpf of its unix clock and a table for the temp bans: it's much simpler to use if you code the proper SQL queries. MySQL provides sme functions like UNIX_TIMESTAMP() and UTC_TIMESTAMP() that are very handy to manage temp bans.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)