SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 05/06/2017, 12:45 PM   #1
AndreiWow
High-roller
 
AndreiWow's Avatar
 
Join Date: Nov 2014
Location: Romania, Bucharest
Posts: 1,050
Reputation: 48
Default Best aproach to get the number of players at each hour

What is the best aproach to get the number of players at each hour so I can display them on a graphic?
AndreiWow is offline   Reply With Quote
Old 05/06/2017, 12:47 PM   #2
Abagail
High-roller
 
Join Date: Dec 2013
Location: GetPlayerPos(playerid, pos); Scripter/Mapper.
Posts: 3,133
Reputation: 445
Default Re: Best aproach to get the number of players at each hour

Are you trying to do this every hour(3:23, 4:23) or when the hour changes (8:00, 7:00)?
Abagail is offline   Reply With Quote
Old 05/06/2017, 12:49 PM   #3
AndreiWow
High-roller
 
AndreiWow's Avatar
 
Join Date: Nov 2014
Location: Romania, Bucharest
Posts: 1,050
Reputation: 48
Default Re: Best aproach to get the number of players at each hour

Quote:
Originally Posted by Abagail View Post
Are you trying to do this every hour(3:23, 4:23) or when the hour changes (8:00, 7:00)?
When the hour changes.

For example

at 6:00, then at 7:00, then at 8:00 and so on

if I manage to get the players at each hour, I'll then save it in database and use php to display them on the graphic, but I do not know how to do this at each hour
AndreiWow is offline   Reply With Quote
Old 05/06/2017, 01:15 PM   #4
Abagail
High-roller
 
Join Date: Dec 2013
Location: GetPlayerPos(playerid, pos); Scripter/Mapper.
Posts: 3,133
Reputation: 445
Default Re: Best aproach to get the number of players at each hour

You could do something such as:

pawn Code:
new server_hour;

// ongamemodeinit
new minute, second;
gettime(server_hour, minute, second);
HourTimer = SetTimer("HourlyUpdate", (60 - minute) * 1000, true);

//
forward HourlyUpdate();
public HourlyUpdate()
{
      new gPlayers, local_query[128];
      for(new i; i <= GetPlayerPoolSize(); i++)
      {
            if(IsPlayerConnected(i)) gPlayers++;
      }

      mysql_format(gSQLConnection, local_query, sizeof local_query, "INSERT..."
      mysql_pquery(gSQLConnection, local_query);
      return true;
}

I assume this time calculation is correct and will work, although I haven't tested it.
Abagail is offline   Reply With Quote
Old 05/06/2017, 01:21 PM   #5
AndreiWow
High-roller
 
AndreiWow's Avatar
 
Join Date: Nov 2014
Location: Romania, Bucharest
Posts: 1,050
Reputation: 48
Default Re: Best aproach to get the number of players at each hour

Quote:
Originally Posted by Abagail View Post
You could do something such as:

pawn Code:
HourTimer = SetTimer("HourlyUpdate", (60 - hour) * 1000, true);

I assume this time calculation is correct and will work, although I haven't tested it.
Can you please explain a bit your calculation?
AndreiWow is offline   Reply With Quote
Old 05/06/2017, 01:24 PM   #6
Abagail
High-roller
 
Join Date: Dec 2013
Location: GetPlayerPos(playerid, pos); Scripter/Mapper.
Posts: 3,133
Reputation: 445
Default Re: Best aproach to get the number of players at each hour

I haven't personally tested it, but the logic seems right. Hour should actually be minute in the timer call, so:
pawn Code:
SetTimerEx("HourlyUpdate", (60 - minute) * 1000*60, false, "i", true);
The logic is:

Let's say the current minute is 20. If we take 60 and subtract 20, we get 40 minutes. There are 40 minutes left in the hour, so call the timer in 40 minutes. We multiply the result by 1000 afterwards to get the proper amount of time, since timer functions don't use seconds.

Current time: 3:40.
(60 - 40) = 20
20*(1000*60)
20 * 60000
= 20 minutes left until next hour

Edit: Sorry, another mistake. It needs to be 1000*60, not just 60, since its minutes we want and not seconds. Also, after the first timer call, you'll need to redo the timer for a straight hour. So,

pawn Code:
public HourlyUpdate(bool: firstcall)
{
      if(firstcall == true)
      {
             KillTimer(HourTimer);
             HourTimer = SetTimerEx("HourlyUpdate", 3600000, true, "i", false);

             firstcall = false;
      }

      // after insert
      if(server_hour == 23) server_hour = 0;
      else server_hour ++;
}
Abagail is offline   Reply With Quote
Old 05/06/2017, 01:31 PM   #7
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,068
Reputation: 2658
Default Re: Best aproach to get the number of players at each hour

Timers are inaccurate. Why not just use OnPlayerConnect/OnPlayerDisconnect? Save the timestamp with it so you have more data points. Then when you go to compose your graph, for each data point: round down to the nearest hour if minutes are smaller than 30 and round up to to the nearest hour if minutes are equal to or greater than 30. Then make averages.
__________________
Vince 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
Players Number IceSKull Scripting Help 16 08/04/2017 01:04 PM
Number of players in a world ? GeekSiMo Scripting Help 5 23/05/2014 06:53 PM
Let admins read players' PM's selectively without players number limitation WellDone Scripting Help 6 15/05/2013 09:34 AM
[HELP] Players press key number 0 .Skool_. Scripting Help 1 19/04/2012 12:00 AM
How to make players score rise every 1 hour? Feel Scripting Help 5 29/10/2011 12:57 AM


All times are GMT. The time now is 03:32 PM.


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