Author Topic: Logging time spent on server  (Read 2292 times)

I've started to work on an addon that would keep track of the amount of time a player has spent on your server. I was wondering what would be the best way of keeping track of this.

Would making a file for  each BL_ID that holds a total playtime, join time, and leave time then after a player leaves is finds the difference between the two times and then adds it to the total play time be the best way to do this?

I'm having trouble trying to phrase this question so I apologize in advance if it's not clear what I'm asking

It all depends on how you set it all up. You could create an array and hold all the information then export it upon shutdown or at intervals, then load it up when the server starts, but that method could cause latency. You can also go with your idea, or create the files and every second add time to each persons' file while they are logged on, there are multiple ways of doing this. I say pick whichever method you feel best with coding.

I dunno how you would want to store total play time, but for adding time I think the best way would be to make a variable on the player when they join and set it equal to the real time, then when they leave subtract the two and add to total playtime.

Yep I was thinking of keeping join time as a client variable and then finding the difference when a player leaves.

As for storing total play time, would keeping it stored in a file that is the player's BL_ID and it just gets updated when a player leaves or if they want to check their play time


Why would it do that?
If you create an array large enough then when loading it you can get latency. A database system that was created by Zack0Wack0 had the issue of causing my client latency when loading in an array with over 1756 different entries, each with 4-5 variables. At least that's the size of one of the files, I'm pretty sure I've had bigger ones.

Well yeah when loading it the game will lag
But you'll only load it when starting the server, and no players will be on then

function to find difference between two times
Code: [Select]
function timeDifference(%time1, %time2)
{
%hr1 = getSubStr(%time1,0,2);
%min1 = getSubStr(%time1,3,2);
%sec1 = getSubStr(%time1,6,2);

%hr2 = getSubStr(%time2,0,2);
%min2 = getSubStr(%time2,3,2);
%sec2 = getSubStr(%time2,6,2);

%sec = %sec2 - %sec1;
%min = %min2 - %min1;
%hr = %hr2 - %hr1;

//Fixes negative times
if(%sec < 0)
{
%min--;
%sec += 60;
}
if(%min < 0)
{
%hr--;
%min += 60;
}
if(%hr < 0)
{
%hr += 24;
}

return(cleanTime(%hr,%min,%sec));
}

function to add two times together
Code: [Select]
function timeAdd(%time1, %time2)
{
%hr1 = getSubStr(%time1,0,2);
%min1 = getSubStr(%time1,3,2);
%sec1 = getSubStr(%time1,6,2);

%hr2 = getSubStr(%time2,0,2);
%min2 = getSubStr(%time2,3,2);
%sec2 = getSubStr(%time2,6,2);

%sec = %sec2 + %sec1;
%min = %min2 + %min1;
%hr = %hr2 + %hr1;


//Converts time into next unit if applicable
if(%sec >= 60)
{
%min += mfloor(%sec / 60);
%sec = %sec % 60;
}
if(%min >= 60)
{
%hr += mfloor(%min / 60);
%min = %min % 60;
}

return(cleanTime(%hr,%min,%sec));
}

function to keep times properly formatted in ##:##:##
Code: [Select]
function cleanTime(%hr,%min,%sec)
{
if(strLen(%hr) == 1)
{
%hr = "0" @ %hr;
}
if(strLen(%min) == 1)
{
%min = "0" @ %min;
}
if(strLen(%sec) == 1)
{
%sec = "0" @ %sec;
}
return(%hr @ ":" @ %min @ ":" @ %sec);
}

I was wondering if anyone could tell me if I'm doing anything incredibly stupid in this. I had trouble with keeping the time properly formatted in ##:##:## and spent more time than I should have fixing it but I've tested this with a bunch of numbers and I think it works. Just need to always have two digits in each number. Though as I was typing this I realized 3 digit hours will break this, but that's a problem I'll deal with later.

I was wondering if anyone could tell me if I'm doing anything incredibly stupid in this.
Just store the time as a number of seconds, then you can just %timePlayed += %leaveTime - %joinTime; and that's it
Then convert to hh:mm:ss when needed to display
« Last Edit: December 18, 2014, 09:01:30 PM by Headcrab Zombie »

Well, time from getRealTime() and getSimTime() are just reported as a string of numbers in milliseconds so you can easily do normal math on them.
Maybe only format the time spent when requested.

EDIT: Basically what Headcrab said

Well, time from getRealTime() and getSimTime() are just reported as a string of numbers in milliseconds so you can easily do normal math on them.
I'd use mFloatLength($Sim::Time,0); instead because it reports time in seconds instead of milliseconds, and will thus take 1000 times longer to reach scientific notation.
Also you don't need accuracy down to the millisecond for this purpose. You could be fine with minutes but you'll never overflow it with seconds so you might as well just do seconds


but that's a problem I'll deal with later.
Code: [Select]
function secondsToTime(%seconds)
{
%hours = mFloor(%seconds / 3600);
%seconds = %seconds % 3600;

%minutes = mFloor(%seconds / 60);
%seconds = %seconds % 60;

%hours = %hours < 10 ? "0" @ %hours : %hours;
%minutes = %minutes < 10 ? "0" @ %minutes : %minutes;
%seconds = %seconds < 10 ? "0" @ %seconds : %seconds;

return %hours @ ":" @ %minutes @ ":" @ %seconds;
}

You could also throw some conditions into the string building before returning it, like don't add the hours if it's zero
« Last Edit: December 18, 2014, 09:37:10 PM by Headcrab Zombie »

I feel so stupid for doing all of that now. It's so much simpler with what you recommended. It's pretty much down to one line to find the total time played in seconds now.


Since I'm going to be using this on a non dedicated that goes down I was thinking of having a schedule for it that logs the total time played every 90 seconds. The schedule is cancelled if you were to check your own time played.


getTimeString(%seconds)
LOL
Yeah i had a feeling there was already a function to do that, but I couldn't think of it

Oh god, this all made me feel so stupid. Pretty much everything I've worked on already existed