Jump to content

Number of Players Online


KingJ

Recommended Posts

I wrote this simple PHP script to show on our website how many players are currently playing on our servers. You must have server monitoring enabled with player counts being logged.

 

Cron Edition

 

This is the recommended version. Set this up to run on a cron or scheduled task, then simply include num-players.txt in your site.

 


<?php

$file_name = "num-players.txt";

$db_user = "tcadmin_stats";
$db_password = "yourpassword";
$db_name = "tcadmin";
$db_server = "localhost.";

$conn = mysql_connect($db_server, $db_user, $db_password) or die();

mysql_select_db($db_name, $conn);

$query = "SELECT PERF_DATE, SUM(AVERAGE_PLAYERS) FROM tc_service_performance WHERE PERF_DATE = ( SELECT PERF_DATE FROM tc_service_performance ORDER BY PERF_DATE DESC LIMIT 1 ) GROUP BY PERF_DATE" ;

$result = mysql_query($query);

$current_players = mysql_result($result,0,1);

mysql_close($conn);

$file_write = fopen($file_name, 'w');

fwrite($file_write, $current_players);

fclose($file_write);




?>

 

Live Edition

 

The cron edition should be used instead of this as this edition will query on every page load which can slow down your page loads!

 


<?php

$db_user = "tcadmin_stats";
$db_password = "yourpassword";
$db_name = "tcadmin";
$db_server = "localhost.";

$conn = mysql_connect($db_server, $db_user, $db_password) or die();

mysql_select_db($db_name, $conn);

$query = "SELECT PERF_DATE, SUM(AVERAGE_PLAYERS) FROM tc_service_performance WHERE PERF_DATE = ( SELECT PERF_DATE FROM tc_service_performance ORDER BY PERF_DATE DESC LIMIT 1 ) GROUP BY PERF_DATE" ;

$result = mysql_query($query);

$current_players = mysql_result($result,0,1);

mysql_close($conn);

echo "Current players on our network: $current_players";

?>

 

You will of course need to change the DB username, password, location and database name. You can see an example of it in action on our site, http://game.kingj.net , in the left hand navigation.

 

I have setup a separate user, tcadmin_stats that only has select privileges on tc_service_performance for PERF_DATE and AVERAGE_PLAYERS for security purposes. You can do the same, or use any user that has access to it.

Link to comment
Share on other sites

I wrote this simple PHP script to show on our website how many players are currently playing on our servers. You must have server monitoring enabled with player counts being logged.

 


<?php

$db_user = "tcadmin_stats";
$db_password = "yourpassword";
$db_name = "tcadmin";
$db_server = "localhost.";

$conn = mysql_connect($db_server, $db_user, $db_password) or die();

mysql_select_db($db_name, $conn);

$query = "SELECT PERF_DATE, SUM(AVERAGE_PLAYERS) FROM tc_service_performance WHERE PERF_DATE = ( SELECT PERF_DATE FROM tc_service_performance ORDER BY PERF_DATE DESC LIMIT 1 ) GROUP BY PERF_DATE" ;

$result = mysql_query($query);

$current_players = mysql_result($result,0,1);

mysql_close($conn);

echo "Current players on our network: $current_players";

?>

 

You will of course need to change the DB username, password, location and database name. You can see an example of it in action on our site, http://game.kingj.net , in the left hand navigation.

 

I have setup a separate user, tcadmin_stats that only has select privileges on tc_service_performance for PERF_DATE and AVERAGE_PLAYERS for security purposes. You can do the same, or use any user that has access to it.

 

Thank you very much :)

Link to comment
Share on other sites

Due to this line:

 

$conn = mysql_connect($db_server, $db_user, $db_password) or die();

 

If the connection to the server cannot be established, the script will stop running so as to prevent ugly errors appearing on your site. Remove the or die() section and you will get the error, assuming you have PHP's display errors flag on.

Link to comment
Share on other sites

no it's show only the echo message

http://www.gamesclan.it/blocks/py.php

 

don't show any connection to DB error

 

In which case, the variable isn't getting set. Try running the contents of the $query variable against your database manually using the same user and you should get returned a table with 2 columns, PERF_DATE and SUM(AVERAGE_PLAYERS) and 1 row with a value for each.

Link to comment
Share on other sites

I've not noticed a high load on the SQL server and i've made the query as fast and simple as possible. But yes, in the event of high load it would be good to echo the file to a text file and run every half hour, which is how often the game servers are sampled.

Link to comment
Share on other sites

I noticed high load lol, I just refreshed the page about 5 times to see what it would do, and the test servers i had running on my master server crashed...

 

In which case, I present the Cron Edition :)

 


<?php

$file_name = "num-players.txt";

$db_user = "tcadmin_stats";
$db_password = "yourpassword";
$db_name = "tcadmin";
$db_server = "localhost.";

$conn = mysql_connect($db_server, $db_user, $db_password) or die();

mysql_select_db($db_name, $conn);

$query = "SELECT PERF_DATE, SUM(AVERAGE_PLAYERS) FROM tc_service_performance WHERE PERF_DATE = ( SELECT PERF_DATE FROM tc_service_performance ORDER BY PERF_DATE DESC LIMIT 1 ) GROUP BY PERF_DATE" ;

$result = mysql_query($query);

$current_players = mysql_result($result,0,1);

mysql_close($conn);

$file_write = fopen($file_name, 'w');

fwrite($file_write, $current_players);

fclose($file_write);




?>

 

This is completely untested, it should work but might not. Let me know.

 

Set up a cron job, or scheduled task to run this at 1 and 31 minutes past the hour. My TCAdmin gathers server stats at every 0 and 30 minutes past the hour. Then, just include the contents of num-players.txt on your page and you now have minimum load.

Link to comment
Share on other sites

  • 2 weeks later...

It will only return player numbers for games that TCAdmin is able to query, if for some reason querying fails or TCAdmin does not know how to query that game then it won't return it. This only sums up the query results from TCAdmin, it does not query the game servers itself.

Link to comment
Share on other sites

Well I know for certain that we had more than 200 BF players on and the stats are logged for that game.

 

In that case, I don't know. I would suggest checking that all server do have an entry in tc_service_performance. Other than that, I can't help, all this does is sum all the player results for the last batch of queries in the TCAdmin database.

Link to comment
Share on other sites

So it seems that the data is stored based on the remote machines time clock or (timezone). Therefore since the UK is 6 hours ahead thats the only location that is returning results in the script.

 

That may explain why some have lower reportings, I would have though it was stored using the master servers time.

 

All of our servers run off UTC though, rather than local time. Makes much more sense when trying to coordinate event or log timings across servers. If you know where the server is you can apply local time in your head from the UTC time.

Link to comment
Share on other sites

  • 5 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Terms of Use