SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 14/10/2014, 02:15 AM   #1
ZBits
Gangsta
 
Join Date: Dec 2011
Posts: 516
Reputation: 18
Default Player Time online Per Day

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
ZBits is offline   Reply With Quote
Old 14/10/2014, 03:56 AM   #2
The__
Huge Clucker
 
Join Date: Nov 2011
Posts: 493
Reputation: 53
Default Re: Player Time online Per Day

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?
The__ is offline   Reply With Quote
Old 14/10/2014, 04:49 AM   #3
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,122
Reputation: 529
Default Re: Player Time online Per Day

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:
pawn Code:
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...
__________________


Kar's Cops and Robbers 2020
Threshold is offline   Reply With Quote
Old 14/10/2014, 04:53 AM   #4
ZBits
Gangsta
 
Join Date: Dec 2011
Posts: 516
Reputation: 18
Default Re: Player Time online Per Day

Quote:
Originally Posted by The__ View Post
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.
ZBits is offline   Reply With Quote
Old 15/10/2014, 06:30 AM   #5
ZBits
Gangsta
 
Join Date: Dec 2011
Posts: 516
Reputation: 18
Default Re: Player Time online Per Day

Bump!
ZBits is offline   Reply With Quote
Old 16/10/2014, 11:27 AM   #6
ZBits
Gangsta
 
Join Date: Dec 2011
Posts: 516
Reputation: 18
Default Re: Player Time online Per Day

I still need help on this
ZBits is offline   Reply With Quote
Old 16/10/2014, 02:30 PM   #7
AndreT
Gangsta
 
AndreT's Avatar
 
Join Date: Jul 2011
Posts: 966
Reputation: 414
Default Re: Player Time online Per Day

Hey.

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.
pawn Code:
// 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
AndreT is offline   Reply With Quote
Old 17/10/2014, 05:09 AM   #8
ZBits
Gangsta
 
Join Date: Dec 2011
Posts: 516
Reputation: 18
Default Re: Player Time online Per Day

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.
ZBits is offline   Reply With Quote
Old 17/10/2014, 03:31 PM   #9
AndreT
Gangsta
 
AndreT's Avatar
 
Join Date: Jul 2011
Posts: 966
Reputation: 414
Default Re: Player Time online Per Day

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.
AndreT is offline   Reply With Quote
Old 17/10/2014, 07:34 PM   #10
ZBits
Gangsta
 
Join Date: Dec 2011
Posts: 516
Reputation: 18
Default Re: Player Time online Per Day

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?
ZBits is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get player total online time ? Vaishnav Scripting Help 8 25/05/2014 10:53 AM
how to use dini save online player in to file every time player enter or out Chrisli520 Scripting Help 1 31/10/2013 07:04 AM
Last time when player was online Luca12 Scripting Help 5 07/07/2013 11:11 PM
Checking time a player is online Glint Scripting Help 5 29/08/2012 08:18 AM


All times are GMT. The time now is 09:06 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.