Count the time...
#1

hi,
a question:

how to count the time a certain player has been online? (on the server)
(it would be great if it would be possible without a timer)
regards.
Reply
#2

I think you could do gettime() on OnPlayerConnect, store values into variable and when you need to get the time, you just call it again and calulate the time between connect and and the moment you called gettime again.
Reply
#3

Could u give me a link or a code example pls, cause im a noob at scripting yet, so id really appreciate that.
and thx for ur answer!!
Reply
#4

Maybe:
Код:
new hour,minute,second; // TOP
new time[MAX_PLAYERS];

OnPlayerConnect:

gettime(hour,minute,second);

OnPlayerDisconnect:
new ho,min,sec;
gettime(ho,min,sec);
time[playerid]= ho-hour && minute-min && second-sec;
Just a stupid attempt :PP
Reply
#5

Quote:
Originally Posted by veyron
Посмотреть сообщение
I think you could do gettime() on OnPlayerConnect, store values into variable and when you need to get the time, you just call it again and calulate the time between connect and and the moment you called gettime again.
gettime() will get the SA-time, not the irl time. If the time changes in any way (player dies, player changes time, etc...) this won't work anymore.
Reply
#6

https://sampwiki.blast.hk/wiki/Gettime
Quote:

Get the current server time, which will be stored in the variables &hour, &minute and &second.
Trying to make it, takes some time...
Reply
#7

so do i have to use a timer to make it accurate??
And is it ok to use a timer for this , cause if there are more players on the server it might cause lag?
Cause after ive counted the time i want to display the time with a command for each player, so theyll know for how long the ve already played on my server.
Reply
#8

i have no idea how accurate is it, also if player is online more than 24h it lies.
pawn Код:
// at top of your code
new hour[MAX_PLAYERS];
new minute[MAX_PLAYERS];
new second[MAX_PLAYERS];

public OnPlayerConnect(playerid)
{
    gettime(hour[playerid], minute[playerid], second[playerid]);
    printf("%d,%d,%d",hour[playerid],minute[playerid],second[playerid]);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    new hour2,minute2,second2,onlinehours,onlineminutes,onlineseconds;
    gettime(hour2,minute2,second2);
    if(hour2 >= hour[playerid]) onlinehours = hour2-hour[playerid];
    else onlinehours = 24-hour[playerid]+hour2;
    if(minute2 >= minute[playerid]) onlineminutes = minute2-minute[playerid];
    else
    {
        onlineminutes = 60-minute[playerid]+minute2;
        onlinehours --;
    }
    if(second2 >= second[playerid]) onlineseconds = second2-second[playerid];
    else
    {
        onlineseconds = 60-second[playerid]+second2;
        onlineminutes--;
    }
    printf("Player was olnline %d hours, %d minutes, %d seconds",onlinehours,onlineminutes,onlineseconds);
    return 1;
}
maybe there is easyer way to do it.
Reply
#9

hi,
first thx so much for ur answer, i didnt expect, that someone is going to write a whole script for me
And i dont think that there are players that will be 24h on my server
I dont want that the played time is gonna get displayed to the player with the gametext function but with a cmd.
E.g if u type in /time it will say: Player x has played x time. (with sendclientmessage)
And id like to save this.

Ive got a register system that already saves score, kills, deaths, and money and the saveable time would be great!!
I save my things like this:

pawn Код:
public OnPlayerDisconnect(playerid, reason)
{

 

    new pname[MAX_PLAYERS],accFormat[128];
    GetPlayerName(playerid,pname,sizeof pname);
    format(accFormat,sizeof accFormat,"%s.datei",pname);
    if(fexist(accFormat) && Player[playerid][loggedin])
    {

  dini_IntSet(accFormat,"Kills",kills[playerid]);
  dini_IntSet(accFormat,"Deaths",deaths[playerid]);

  dini_IntSet(accFormat,"Money",GetPlayerMoney(playerid));
  dini_IntSet(accFormat,"Score",GetPlayerScore(playerid));

//this is how the score ect. is displayed
// onplayercommandtext:

if(strcmp(cmdtext,"/stats",true)==0)
{
SendClientMessage(playerid, 0xFFFF00AA, "/////////////////////////////////////////////////////////");
new Float:ratio=floatdiv(kills[playerid], kills[playerid]);
new s[50];
format(s,sizeof(s),"Kills: %d | Deaths: %d | Ratio: %0.2f",kills[playerid],kills[playerid],ratio);
SendClientMessage(playerid,0xFFFF00AA,s);
new r[50];
format(r,sizeof(r),"Score: %d | Money: %d$",GetPlayerScore(playerid),GetPlayerMoney(playerid));
SendClientMessage(playerid,0xFFFF00AA,r);
SendClientMessage(playerid, 0xFFFF00AA, "/////////////////////////////////////////////////////////");
Do i have to save each variable (hours,minutes,seconds) seperate or is it possible to save it as "one thing".
And in the OnPLayerCommand function do i have just to put these (onlinehours,onlineminutes,onlineseconds)
variables into the brackets to get them displayed?

Sorry, my english is very bad. I hope u are able to understand me

Ps: @ powerscripters like veyron: i dont want to take ur whole time to get a script code done for me, its very helpful for beginners like me but if u dont want to post such much work im also happy about just some examples

regards and thx a lot again
Reply
#10

use this instead, saves you both resources and makes it bug-free:

pawn Код:
new PlayerOnlineTime[MAX_PLAYERS];

stock PlayedTimeToString(seconds)
{
    new
        Played = gettime()-seconds;
        Time[4],
        Result[45];
    Time[0] = Played % 60; // Seconds
    Played /= 60;
    Time[1] = Played % 60; // Minutes
    Played /= 60;
    Time[2] = Played % 24; // Hours
    Played /= 24;
    Time[3] = Played; // Days
    if(Time[3])
        format(Result, 45, "%d days, %d hours, %d minutes and %d seconds", Time[3], Time[2], Time[1], Time[0]);
    else if(Time[2])
        format(Result, 45, "%d hours, %d minutes and %d seconds", Time[2], Time[1], Time[0]);
    else if(Time[1])
        format(Result, 45, "%d minutes and %d seconds", Time[1], Time[0]);
    else
        format(Result, 45, "%d seconds", Time[0]);
    return Result;
}
OnPlayerConnect:
pawn Код:
PlayerOnlineTime[playerid] = gettime();
When telling them how long they've been online:
pawn Код:
new
    string[75];
format(string, 75, "You have been online for %s.", PlayedTimeToString(PlayerOnlineTime[playerid])
SendClientMessage(playerid, COLOR_HERE, string);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)