How do i record the amount of time player has spent in the server?
#1

Hi, everyone well i have a Freeroam server, and i put houses in it. Here i come in with the problem: when you register the server gives you 50k, well guess what players are doing now, they are making more accounts and give the money to their friends. Well i thought about making a system that wont allow you to send any money to a player, unless you have played for 5 hours in total, with all connections. Any help on how to record the time, then put it in the MySQL database, and the pull up the data again when server connects? I havent done this yet, so its hard for me. Thank you.
Reply
#2

Try this:
pawn Код:
enum pInfo
{
    pHours, //put this on your enum, this is just an example
    pMinutes, // <<
    pSeconds, // <<
}
new PlayerInfo[MAX_PLAYERS][pInfo];

public OnPlayerConnect(playerid)
{
    SetTimerEx("TimePlayed", 1000, true, "i", playerid); //set a timer to count player' time played
    return 1;
}

forward TimePlayed(playerid);
public TimePlayed(playerid)
{
    PlayerInfo[playerid][pSeconds]++; //+ player secs
    if(PlayerInfo[playerid][pSeconds] == 60) //check if their secs equal to 60
    {
        PlayerInfo[playerid][pSeconds] = 0; //reset the secs
        PlayerInfo[playerid][pMinutes]++; //convert the secs to minutes
        if(PlayerInfo[playerid][pMinutes] == 60) //check if their minutes equal to 60
        {
            PlayerInfo[playerid][pMinutes] = 0; //reset the minutes
            PlayerInfo[playerid][pHours]++; //convert the minues to hours
        }
    }
    return 1;
}

if(PlayerInfo[playerid][pHours] < 5) return SendClientMessage(playerid, -1, "ERROR: You can't give money if your time played less than 5 hours."); //put this on your give money commands
Reply
#3

Thanks man. Could i use this then, after they have played for 5hours, to just keep on recording their time, save it in database and then the players could see it in their statistics? or is there a more efficient way then creating a timer for every single player?(just wondering)
Reply
#4

Quote:
Originally Posted by Type-R
Посмотреть сообщение
Thanks man. Could i use this then, after they have played for 5hours, to just keep on recording their time, save it in database and then the players could see it in their statistics? or is there a more efficient way then creating a timer for every single player?(just wondering)
There is a way more efficient way.

pawn Код:
stock GetPlayerOnlineTime(playerid, milliseconds)
{
    new value;
    if(milliseconds == 0) {value = NetStats_GetConnectedTime(playerid) / 1000;} // Seconds.
    else {value = NetStats_GetConnectedTime(playerid);} // Milliseconds.
    return value;
}
And you just convert the time.

pawn Код:
stock ConvertTime(&cts, &ctm=-1,&cth=-1,&ctd=-1,&ctw=-1,&ctmo=-1,&cty=-1)
{
    #define PLUR(%0,%1,%2) (%0),((%0) == 1)?((#%1)):((#%2))

    #define CTM_cty 31536000
    #define CTM_ctmo 2628000
    #define CTM_ctw 604800
    #define CTM_ctd 86400
    #define CTM_cth 3600
    #define CTM_ctm 60

    #define CT(%0) %0 = cts / CTM_%0; cts %= CTM_%0

    new strii[128];

    if(cty != -1 && (cts/CTM_cty))
    {
        CT(cty); CT(ctmo); CT(ctw); CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(cty,"year","years"),PLUR(ctmo,"month","months"),PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctmo != -1 && (cts/CTM_ctmo))
    {
        cty = 0; CT(ctmo); CT(ctw); CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(ctmo,"month","months"),PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctw != -1 && (cts/CTM_ctw))
    {
        cty = 0; ctmo = 0; CT(ctw); CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctd != -1 && (cts/CTM_ctd))
    {
        cty = 0; ctmo = 0; ctw = 0; CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, and %d %s",PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(cth != -1 && (cts/CTM_cth))
    {
        cty = 0; ctmo = 0; ctw = 0; ctd = 0; CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, and %d %s",PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctm != -1 && (cts/CTM_ctm))
    {
        cty = 0; ctmo = 0; ctw = 0; ctd = 0; cth = 0; CT(ctm);
        format(strii, sizeof(strii), "%d %s, and %d %s",PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    cty = 0; ctmo = 0; ctw = 0; ctd = 0; cth = 0; ctm = 0;
    format(strii, sizeof(strii), "%d %s", PLUR(cts,"second","seconds"));
    return strii;
}
Usage:
pawn Код:
new string[128], Seconds = GetPlayerOnlineTime(playerid, 0), Minutes, Hours, Days, Weeks, Months, Years;
format(string, sizeof(string), "** You have been online for %s!", ConvertTime(Seconds, Minutes, Hours, Days, Weeks, Months, Years));
SendClientMessage(playerid, 0xFFFF00FF, string);
Reply
#5

You don't need a timer. Use unix timestamps.
Source: http://en.wikipedia.org/wiki/Unix_time

The function gettime() with no parameters returns a unix timestamp.
Source: https://sampwiki.blast.hk/wiki/Gettime

If you want, I can make a small script that records a player's time player, but it's up to you to implement it into your script.

@SickAttack - Wtf 0_o
Reply
#6

@threshold, if you could that would be great.
Reply
#7

Quote:
Originally Posted by Threshold
Посмотреть сообщение
You don't need a timer. Use unix timestamps.
Source: http://en.wikipedia.org/wiki/Unix_time

The function gettime() with no parameters returns a unix timestamp.
Source: https://sampwiki.blast.hk/wiki/Gettime

If you want, I can make a small script that records a player's time player, but it's up to you to implement it into your script.

@SickAttack - Wtf 0_o
Why would you need to use unix timestamps if SA-MP already has a function to get the time a player has been online for?

pawn Код:
NetStats_GetConnectedTime(playerid);
Source: https://sampwiki.blast.hk/wiki/NetStats_GetConnectedTime.
- Description: Gets the amount of time (in milliseconds) that a player has been connected to the server for.
Reply
#8

Thanks both of you, i will try both ways see what works the easiest.
Reply
#9

Who records milliseconds? It would just be a more wasteful calculation. Unix is in straight up seconds. Not to mention, you wouldn't be able to apply it directly to measure from the time of login to disconnect, because it starts counting at OnPlayerConnect which isn't always what people want.

@Type-R - PM me your script and I will just add it straight in to save us both time.

EDIT: By the way, Gettime() is a SA-MP function, so I'm not quite sure what you're trying to prove :l
Reply
#10

Quote:
Originally Posted by Threshold
Посмотреть сообщение
Who records milliseconds? It would just be a more wasteful calculation. Unix is in straight up seconds. Not to mention, you wouldn't be able to apply it directly to measure from the time of login to disconnect, because it starts counting at OnPlayerConnect which isn't always what people want.

@Type-R - PM me your script and I will just add it straight in to save us both time.

EDIT: By the way, Gettime() is a SA-MP function, so I'm not quite sure what you're trying to prove :l
Have you read the title? "How do i record the amount of time player has spent in the server?" It clearly says "amount of time player has spent in the server", it doesn't say "amount of time player has spent in the server after authentication", Type-R said "unless you have played for 5 hours in total" so I'm guessing he want's the total time not until the player logins.

I meant that SA-MP already has a function that is dedicated to get the time that a player has been connected to the server.

You just divide the time by 1000, and you get seconds.
pawn Код:
NetStats_GetConnectedTime(playerid) / 1000; // Seconds.
"Who records milliseconds?" I guess, SA-MP team does :P

Anyways, it's up to Type-R to choose witch method he wants to use.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)