KingJ Posted December 27, 2008 Share Posted December 27, 2008 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 More sharing options...
Dan M Posted December 27, 2008 Share Posted December 27, 2008 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 More sharing options...
iLight Posted December 27, 2008 Share Posted December 27, 2008 Very nice ! Thanks for the share. Link to comment Share on other sites More sharing options...
tangogc Posted December 27, 2008 Share Posted December 27, 2008 don't work for me blank page ;( any suggestion ? Link to comment Share on other sites More sharing options...
KingJ Posted December 27, 2008 Author Share Posted December 27, 2008 You will need to have PHP installed with the mysql extension enabled. Link to comment Share on other sites More sharing options...
tangogc Posted December 27, 2008 Share Posted December 27, 2008 yes I have I suppose problem to connect to DB not usin localhost but a remote ip. mysql is set to acept network connection but this script doesn't show any error just a blank page Link to comment Share on other sites More sharing options...
KingJ Posted December 27, 2008 Author Share Posted December 27, 2008 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 More sharing options...
tangogc Posted December 27, 2008 Share Posted December 27, 2008 no it's show only the echo message http://www.gamesclan.it/blocks/py.php don't show any connection to DB error Link to comment Share on other sites More sharing options...
KingJ Posted December 27, 2008 Author Share Posted December 27, 2008 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 More sharing options...
jcroom Posted December 31, 2008 Share Posted December 31, 2008 You sir rock thank you! Link to comment Share on other sites More sharing options...
maddanny Posted December 31, 2008 Share Posted December 31, 2008 Be careful, this table (tc_service_performance) can be very big and running your script for each visitor will slow down your Mysql server You can run your script with a cron task and store the value somewhere. Link to comment Share on other sites More sharing options...
KingJ Posted December 31, 2008 Author Share Posted December 31, 2008 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 More sharing options...
jcroom Posted December 31, 2008 Share Posted December 31, 2008 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... Link to comment Share on other sites More sharing options...
KingJ Posted December 31, 2008 Author Share Posted December 31, 2008 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 More sharing options...
jcroom Posted January 1, 2009 Share Posted January 1, 2009 The script works Too bad there is something in my php config preventing it from running "Internal Server Error" I have verified it does work though. Thanks KingJ! Edit: was just a permissions issue, fixed now. Link to comment Share on other sites More sharing options...
Brandon Posted January 13, 2009 Share Posted January 13, 2009 Not sure how accurate this script is, I tried it with our database and its returning a very low number. Based on our other stats script we have near 1200 players on game servers right now. This script is only showing 194. Any ideas? Link to comment Share on other sites More sharing options...
KingJ Posted January 14, 2009 Author Share Posted January 14, 2009 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 More sharing options...
RackGaming Posted January 14, 2009 Share Posted January 14, 2009 Thanks for this man, I was looking for tcadminstats script but seems they dont wanna sell it no more Link to comment Share on other sites More sharing options...
Brandon Posted January 14, 2009 Share Posted January 14, 2009 Well I know for certain that we had more than 200 BF players on and the stats are logged for that game. Link to comment Share on other sites More sharing options...
KingJ Posted January 14, 2009 Author Share Posted January 14, 2009 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 More sharing options...
Brandon Posted January 14, 2009 Share Posted January 14, 2009 Ah its only storing a small # of services in that table for some reason, thanks for the insight. Link to comment Share on other sites More sharing options...
Brandon Posted January 20, 2009 Share Posted January 20, 2009 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. Link to comment Share on other sites More sharing options...
KingJ Posted January 20, 2009 Author Share Posted January 20, 2009 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 More sharing options...
Brandon Posted January 21, 2009 Share Posted January 21, 2009 I suppose, however typically our customers prefer their log files to be stamped with the time code of where the server is. Ah well. Link to comment Share on other sites More sharing options...
jcroom Posted February 20, 2009 Share Posted February 20, 2009 Just a tip to anyone using the cron edition of this script: make sure you place it outside of your public directory so you don't get DoS'd Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.