Question about Temp Banning (MySQL)
#1

Alright, I know it involves using timestamp etc, but how exactly does it work? What does it involve and how do I get there? TimeStamp confuses me for dates/hours etc. Can someone explain?
Reply
#2

Unix timestamps are in seconds, if you want to ban someone for lets say a day, every time they login detect if the current Unix time stamp using gettime is 60*60*24*2 seconds (172800) seconds away from the one you saved into the database.

pawn Код:
new query[100], pname[24];
GetPlayerName(playerid, pname, 24);
format(query, sizeof(query), "UPDATE usertable SET bantime = UNIX_TIMESTAMP() WHERE name = '%s', pname);
mysql_query(query);
Then just load the timestamp and compare it to the current one .
Reply
#3

I'm a little confused although that cleared it up a little, On the database do I select the type to be Unix Timestamp? Also, I'm confused about the seconds part, could you explain it, in perhaps an example?
Reply
#4

I usually select INT on the database and then setup a value to about 20 cells. That's a bit unnecessary, but it works fine.
Reply
#5

So do I use a timer to count down the seconds or? Does it do it automatically? I.e Say, I banned them for a day and after 24 hours... It unbans them?
Reply
#6

Anyone?
Reply
#7

When you ban them, you'll want to obtain the timestamp using gettime(). Just like that, gettime(), nothing else. Here's some pseudo code again:

pawn Код:
new iTimestamp = gettime();
Then, you'll want to do some calculations. If you want to ban them for 24 hours, then you'll need to add 24 hours to the timestamp. Keep in mind that a second is 1, a minute is 60, an hour is 3600 (try to keep up here), and there are 24 hours in a day (last time I checked), so 3600 * 24 = 86400. More pseudo code:

pawn Код:
iTimestamp = iTimestamp+86400;
Then you'll want to insert that value into a MySQL DB. When a player connects, check to make sure that if you do gettime() again, that the value of gettime() is greater than (>) the value you stored.

I can't make it much clearer than that!
Reply
#8

Ahh I see! Thank you, that is very clear! So, Say when I ban them for like 3 days.... How could I tell the player what date they will be unbanned? i.e You have been banned for 3 days, You will be unbanned on the 23/01/12 - Is this possible and how would I go about it?
Reply
#9

You'll need this function:

pawn Код:
stock date(timestamp, _form = 0) // originally by pen_TheGun, edited by RealCop228
{
    /*
        ~ 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
        * by RealCop228: date( 1247182451, 4) will print >> July 09, 2009
        * by RealCop228: date( 1247182451, 5) will print >> 23:34
    */


    new year = 1970, day = 0, month = 0, hour = 0, mins = 00, 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];

    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=00, 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, "%s %02d, %d", names_of_month[month],day+1,year); // by RealCop228
        case 5: format(returnstring, 31, "%02d:%02d", hour,mins); // by RealCop228
        default: format(returnstring, 31, "%02d.%02d.%d-%02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
    }
    return returnstring;
}
For your use, you could probably do:

pawn Код:
date(iTimestamp, 2);
That's going to return a string, so probably use that in a format or something.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)