PlayTime
#1

Hi!

How to make that it will calculate player playtime without timers
I want that it will be in HOURS,MINUTES,SECONDS.

Please Help!
Reply
#2

pawn Код:
new ConnectedTime[MAX_PLAYERS];
public OnPlayerConnect(playerid)
{
    ConnectedTime[playerid] = gettime();
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    new seconds = gettime() - ConnectedTime[playerid];
    new mins, hour, day;
    printf("Player's time = %s seconds", ConvertTime(seconds,mins,hour,day));
    return 1;
}
stock ConvertTime(&cts, &ctm=-1,&cth=-1,&ctd=-1,&ctw=-1,&ctmo=-1,&cty=-1) //Thanks to Kyosaur
{
    #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;
}
Reply
#3

Use gettime without arguments/parameters when a player logins and store it in a variable. When the player disconnects, use gettime again (without arguments) and subtract (-) from the stored variable. The result will be how many seconds you were in the server so you just add the previous online time and you get the total.

About the convertion: https://sampforum.blast.hk/showthread.php?tid=309778
Reply
#4

Thanks for expl and code
Reply
#5

can you please tell me how to add the previous online time and you get the total

my system pInfo[playerid][ConnectedTime]
Reply
#6

You can't get the total, In another word, You can't save a variable for a player forever, You have to store the connected time by any method ( INI file - Database ) Then load it again when he connect and add to it again and so on
pawn Код:
public OnPlayerConnect(playerid)
{
    pInfo[playerid][ConnectedTime]  = gettime();
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    new seconds = gettime() - pInfo[playerid][ConnectedTime];
    new mins, hour, day;
    printf("Player's time = %s seconds", ConvertTime(seconds,mins,hour,day));
    return 1;
}
Reply
#7

Thanks
Reply
#8

Of course you can!

You have 2 variables:
A for saving the total time.
B for storing the unix timestamp when a player logins.

When a player logins, you get the unix timestamp by using gettime() and assigning it to B. So you can save the total time when a player disconnects.

A += gettime() - B;

A was assigned the total time (previous times in seconds + seconds from the last visit).
Reply
#9

Is there any posibility to save time in like 5 Minutes 8 seconds?
Reply
#10

Quote:
Originally Posted by kalanerik99
Посмотреть сообщение
Is there any posibility to save time in like 5 Minutes 8 seconds?
It is possible and it'll look something like:

new temp;
temp = A + (gettime() - B);

with the only difference that you store the total time you've loaded and the current online time to a temporary variable and then save the time with the value of temp (it stores the total time in seconds).

PS: The A and B are supposed to be explained in my above post.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)