View Full Version : Player Time online Per Day

14/10/2014, 02:15 AM
Sup guys,

Ok so i'm trying to measure a players activity per day but it seems i am measuring its activity per connection. Is there any way of making a measuring a player activity per day? Like: October 13 2014 - 5 hours 54 minutes. Right now i measure a players activity per connection, meaning everytime a player disconnects it inserts a new row of data but i want it to add on it every day and soon as the date changes it inserts a new row for another day.

I hope i'm clear enough

Thanks in advance

14/10/2014, 03:56 AM
Kinda confused what you're asking for, the amount of time a player has been on the server since they started playing, or just for the day?

14/10/2014, 04:49 AM
Use getdate (http://wiki.sa-mp.com/wiki/Getdate).
Check if they have anything in a field under that date. Now I don't really work with MySQL and I wouldn't be able to turn this into a code for you at this point.

What I'm trying to say is, lets say the date today was the 8th of October and I join the server at 8:00pm and I play until 10:00pm. So you create a new field called "Oct 8", "October 8", "8/10" or whatever you want it to be, and you put in 2 hours (7200 seconds or 120 minutes) to that field.

I then decide to join at 11:30pm and leave the server at 1:30am the next day (9th October). So you would then use gettime (http://wiki.sa-mp.com/wiki/Gettime) to see if I have been connected for more than gettime returns. Example:
new second, minute, hour;
gettime(hour, minute, second);
new var = (hour * 3600 + minute * 60 + second); //Seconds since midnight on the previous day
if((timeleftserver - timeconnectedtoserver) > var)
//You would make a new field for 9th October and input 'var'.
//Then you would ADD ((timeleftserver - timeconnectedtoserver) - var) to the 8th of October.

This is just an example and won't work with the code provided, but like I said before, I haven't worked with MySQL yet and I won't be able to make this one for you...

14/10/2014, 04:53 AM
Kinda confused what you're asking for, the amount of time a player has been on the server since they started playing, or just for the day?

Just for the day

@Threshold, You've provided a fair amount of help that can get me started. Though i still want someone who can show me how its done including bits of mysql.

15/10/2014, 06:30 AM

16/10/2014, 11:27 AM
I still need help on this

16/10/2014, 02:30 PM

I use a table called `sessions` for storing player session data. It logs all the relevant information: their unique ID `id`, time of connection `jointime`, time of disconnection `leavetime` (and for other purposes, also their name and IP address).

Whenever a player disconnects, an entry is added to this `sessions` table.
INSERT INTO sessions(id,jointime,leavetime) VALUES(..., [JOIN UNIX TIMESTAMP], UNIX_TIMESTAMP())
Where [JOIN UNIX TIMESTAMP] is the UNIX time at their session start.
Depending on how your system is built up and how stable your server is, you might also want to have a backup routine for adding sessions which were terminated due to server crash.

To retrieve someone's online time on 15th of October (for example), we would need 2 UNIX timestamps:
1. 00:00:00 15th Oct 2014 (1413331200 UTC)
2. 00:00:00 16th Oct 2014 (1413417600 UTC)
(retrieved from http://www.unixtimestamp.com/index.php, for this example)

Now I would run a query on the `sessions` table like this:
SELECT id,jointime,leavetime FROM sessions WHERE (jointime BETWEEN 1413331200 AND 1413417600) OR (leavetime BETWEEN 1413331200 AND 1413417600)

This query will return records of when the player either joined between midnight 15th and midnight 16th or when the player's disconnection time falls between the midnights.
This query will return rows even if the player was online
14th October 22:00 until 15th October 02:00 (4h)
or for example
15th October 23:30 until 16th October 04:00 (4h 30min)

I would then use some basic calculations to determine the exact online time of the player between the two timestamps.
// let values jointime and leavetime hold the UNIX timestamps accordingly.
// let values startOfPeriod and endOfPeriod hold the timestamps retrieved above.
new onlineDuringPeriod = leavetime-jointime;
if(jointime < startOfPeriod) {
onlineDuringPeriod -= (startOfPeriod-jointime);
if(leavetime > endOfPeriod) {
onlineDuringPeriod -= (leavetime-endOfPeriod);

The code above can be written in different ways, but I hope my version gets the point across nicely. If the player joined before the midnight of 15th, subtract the time they spent online before the midnight of 15th from the total online time. Same for leaving after the midnight of 16th.

So in the variable onlineDuringPeriod you will have the amount of seconds they spent online between 15th 00:00 and 16th 00:00.

Sorry if I'm a little rusty :D

17/10/2014, 05:09 AM
Ahh ...right, i got a little confused. I haven't really worked with UNIX timestamp. My current information in the MySQL table is stored by SA:MP's "gettime()" function. But the thing is, it counts the players activity per connection(Which i obviously dont want). The only reason i am doing this is because i am creating a panel for my server and its kinda hard for me to go through the stuff. I am not extremely experienced with MySQL functions so this tutorial got a little complex for me. Although i get what you are trying to say, its just harder for me to use it.

17/10/2014, 03:31 PM
I understand that you use the result of gettime()? The function returns the UNIX timestamp, so I assume you are storing the correct information.

I have, in my user panel, created a very similar function. Also see http://sf-se.net for the "Most online (24h)" table - this information is calculated in a way described above.

I will need to specify that you need to put that code above into a loop where you parse all MySQL results, of course.

17/10/2014, 07:34 PM
Hmm, i re-read your post and understood alot of it however, you said "SELECT id,jointime,leavetime FROM sessions WHERE (jointime BETWEEN 1413331200 AND 1413417600) OR (leavetime BETWEEN 1413331200 AND 1413417600)
", and i assume you know that i dont want that results for October 15th only... Can you tell me how would i set the above query to run on the particular day? Or am i completely getting the wrong concept?