Best aproach to get the number of players at each hour
#1

What is the best aproach to get the number of players at each hour so I can display them on a graphic?
Reply
#2

Are you trying to do this every hour(3:23, 4:23) or when the hour changes (8:00, 7:00)?
Reply
#3

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
Reply
#4

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.
Reply
#5

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?
Reply
#6

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 ++;
}
Reply
#7

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)