[Tool/Web/Other] Showing playerbase with ****** Charts
#1

Intro:
I made this very simple php-script and i want to share it. It's using javascript, php and 2 API's (from ****** and SACNRMonitor). With ****** graphs you can customize much, much, much, muuuch more !

Preview
If the preview doesn't work it mostly mean SACNR monitor is down or the api is having problems.
On the preview it's only the graph and the tables - not the live map.

Since 17/04/2015 you can dynamicly choose weither show the tables or not:

Example 1 (with tables)
Example 2 (without tables and a knob showing current players)


Download
Available on Github

Extra
Info
Available options for the line chart
Available options for the table
Required file (SACNRMonitor.php)
Required file 2 (knob library)

Have fun with it, play around with it.
Reply
#2

Nice, I will give this a try.
Reply
#3

Its nice to see you active again Michael
Reply
#4

Preview doesn't work.

"Not Found

The requested URL /test.php was not found on this server."
Reply
#5

Quote:
Originally Posted by Nubik
View Post
Preview doesn't work.

"Not Found

The requested URL /test.php was not found on this server."
Fixed
Reply
#6

don't work on localhost... I don't know why...
Reply
#7

Quote:
Originally Posted by xStunt
View Post
don't work on localhost... I don't know why...
You could check out your browser console to see javascript errors and see the problem
Reply
#8

Nice...
Reply
#9

Preview not work
Reply
#10

Hey, My server whatever bla bla I cant see the map..
Everything works fine, I have PHP 5.4 or more, I forgot but it was latest, mysql latest everything updated..
http://5.175.173.213/zfrmap/
to test it..
Please tell the problem
My graph.php
Code:
<?php
    /* UPDATE 24/12/2014:
     * Fixed the using_table part
     * Added another table using more server info from SACNR monitor
     * 
     * UPDATE 13/02/2014:
     * Improved variable usage
     * */

	include "SACNRMonitor.php";

	$serverid = 1695416;    // SACNR Server ID | http://monitor.sacnr.com/server-<here is an id>.html
	$hours = 1;            // how much past hours should the graph show the playerbase. Max value = 44 !
	$using_table = false;    //Display a detailed table under it ? true or false

	$monitor = new SACNRMonitor;
    $json_query = json_encode((array)$monitor->get_query_by_id($serverid));
    $json_info = json_encode((array)$monitor->get_info_by_id($serverid));

	if($hours > 44 || $hours < 1) die("Invalid value: $hours");
?>

<!DOCTYPE html>
<html>
	<head>
		<title>Zees Freeroam - Online Players Info</title>
		<script src="http://ajax.******apis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
		<script type="text/javascript" src="https://www.******.com/jsapi"></script>
		<script type="text/javascript">
		******.load('visualization', '1.0', {'packages':['corechart','table']});
		******.setOnLoadCallback(drawChart);

		function drawChart()
		{
			var data = [], enabled =  <? echo empty($using_table) ? 0 : 1 ?>, datavalues = <? echo $json_query ?>, date = [];

            data[0] = new ******.visualization.DataTable();
            data[0].addColumn('string', 'Date');
            data[0].addColumn('number', 'Playercount');

			for (var i = datavalues.length-(<? echo $hours ?>* 4), j = datavalues.length; i < j; i++)
			{
				date[0] = new Date(parseInt(datavalues[i]["Timestamp"])*1000);
				date[1] = ((date[0].getDate() < 10) ? "0"+date[0].getDate() : date[0].getDate()) + "/" + (date[0].getMonth()+1) + "/" + date[0].getFullYear();
				date[2] = date[0].getHours() + ":" + ((date[0].getMinutes() < 10) ? "0"+date[0].getMinutes() : date[0].getMinutes());

				data[0].addRow([date[1] + " " + date[2], parseInt(datavalues[i]["PlayersOnline"])]);
			}

            data[1] = new ******.visualization.DataTable(), datavalues = <? echo $json_info ?>;

            data[1].addColumn('string','Info');
            data[1].addColumn('string','Value');

            data[1].addRows([
                ['Map', datavalues["Map"]],
                ['Players', datavalues["Players"] + "/" + datavalues["MaxPlayers"]],
                ['Average', datavalues["AvgPlayers"]],
                ['Time', datavalues["Time"]],
                ['Version', datavalues["Version"]],
                ['Website', '<a href="http://'+ datavalues["WebURL"] +'">Click</a>'],
                ['Password', (datavalues["Password"] === '0') ? 'No' : 'Yes'],
                ['Host-tab', (datavalues["HostedTab"] === '0') ? 'No' : 'Yes']
            ]);

			if(enabled === 1)
			{
                var table = new ******.visualization.Table(document.getElementById('table'));
                table.draw(data[0], {showRowNumber: true, width: 600});
                table = new ******.visualization.Table(document.getElementById('info'));
                table.draw(data[1], {showRowNumber: false, width: 400, allowHtml: true});
			}

			var options = {
				title:  datavalues["Hostname"] + " (" + datavalues["Gamemode"] + ")",
				height: 400,
				legend: {position: 'bottom'},
				orientation: 'horizontal'
			};

			var chart = new ******.visualization.LineChart(document.getElementById('chart'));
			chart.draw(data[0], options);
		}
	    </script>
        <style type="text/css">
            #table, #info { display: inline-block;  vertical-align: top;}
            #chart { margin-bottom: 10px; }
        </style>
	</head>
	<body>
		<div id="chart"></div>
		<div id="table"></div>
        <div id="info"></div>
	</body>
</html>
My SACNRMonitor.php

Code:
<?php
/*

	SACNRMonitor.php

	Title:		SACNR Monitor PHP API

	Purpose:	Enables easy access to SACNR Monitor's API using
			cURL or file_get_contents (fopen).

			This API is not necessary to access the API, it
			simply makes it easier.

			As of today, 16th of November 2010, there are no
			usage limits for the API. You may make unlimited
			requests to the API. This may be changed at any
			time.

	Last updated:	11th Nov 2010 @ 1:43 AM

	Author:		Blacklite - blacklite@sacnr.com

	License:	GPL - http://www.gnu.org/licenses/gpl-3.0.html...5.61827378 
if (!class_exists('SACNRMonitor')):
 
class SACNRMonitor {
	
	private $use_curl;
	private $ch;
	private $query_url = 'http://monitor.sacnr.com/api/?';
	
	/*
		Syntax:
			public SACNRMonitor::__construct( bool $use_curl = true )
		
		Description:
			Initializes SACNRMonitor, called automatically by PHP. Set to
			use cURL by default, can be changed by setting $use_curl to false.
			Will issue a notice if the requested method is not available but
			the other one is. Will issue a fatal error if neither method is
			available on your system.
	*/
	public function __construct($use_curl = true) {
		if ($use_curl && !$this->curl()) {
			if ($this->fopen()) {
				$this->error(1);
				$use_curl = false;
			} else {
				$this->error(0);
				return false;
			}
		} else if (!$use_curl && !$this->fopen()) {
			if ($this->curl()) {
				$this->error(2);
				$use_curl = true;
			} else {
				$this->error(0);
				return false;
			}
		}
		$this->use_curl = $use_curl;
	}
	
	/*
		Syntax:
			public SACNRMonitor::__destruct( )
		
		Description:
			Unloads SACNRMonitor. Called automatically by PHP. Will check if
			the cURL handle is initialized and close it if necessary.
	*/
	public function __destruct() {
		if ($this->ch) {
			curl_close($this->ch);
		}
	}
	
	/*
		Functions to get data by ID for actions:
			info
			players
			query
			ad
	*/
	
	public function get_info_by_id($server_id) {
		return $this->get_data_by_id($server_id, 'info');
	}
	
	public function get_players_by_id($server_id) {
		return $this->get_data_by_id($server_id, 'players');
	}
	
	public function get_query_by_id($server_id) {
		return $this->get_data_by_id($server_id, 'query');
	}
	
	public function get_ad_by_id($server_id) {
		return $this->get_data_by_id($server_id, 'ad');
	}
	
	/*
		Functions to get data by IP for actions:
			info
			players
			query
			ad
	*/
	
	public function get_info_by_ip($ip, $port) {
		return $this->get_data_by_ip($ip, $port, 'info');
	}
	
	public function get_players_by_ip($ip, $port) {
		return $this->get_data_by_ip($ip, $port, 'players');
	}
	
	public function get_query_by_ip($ip, $port) {
		return $this->get_data_by_ip($ip, $port, 'query');
	}
	
	public function get_ad_by_ip($ip, $port) {
		return $this->get_data_by_ip($ip, $port, 'ad');
	}
	
	// Private functions (can not be used outside the SACNRMonitor class)
	
	/*
		Syntax:
			private SACNRMonitor::get_data_by_id( string $server_id, string $action )
		
		Description:
			Gets the requested $action for the $server_id.
	*/
	private function get_data_by_id($server_id, $action) {
		return $this->get(array('ServerID' => $server_id, 'Action' => $action));
	}
	
	/*
		Syntax:
			private SACNRMonitor::get_data_by_ip( string $ip, string $port, string $action )
		
		Description:
			Gets the requested $action for the $ip and $port.
	*/
	private function get_data_by_ip($ip, $port, $action) {
		return $this->get(array('IP' => $ip, 'Port' => $port, 'Action' => $action));
	}
	
	/*
		Syntax:
			private SACNRMonitor::get( array $options )
		
		Description:
			Builds the final query URL, and handles errors. $options contains a list
			of querystrings to put into the URL. Eg, array('ServerID' => 123) will
			append &ServerID=123 onto the request URL. Uses SACNRMonitor::get_url()
			to fetch the URL.
	*/
	private function get($options) {
		$urlopt = array();
		foreach($options as $k => $v) {
			$urlopt[] = urlencode($k) . '=' . urlencode($v);
		}
		$url = $this->query_url . implode('&', $urlopt);
		$text = null;
		$response = $this->get_url($url, $text);
		if ($response === false) {
			$this->error(3, $text);
		}
		return $response;
	}
	
	/*
		Syntax:
			private SACNRMonitor::fopen( )
		
		Description:
			Returns true if the file_get_contents method is supported on this system.
	*/
	private function fopen() {
		return ini_get('allow_url_fopen') ? true : false;
	}
	
	/*
		Syntax:
			private SACNRMonitor::curl( )
		
		Description:
			Returns true if the cURL method is supported on this system.
	*/
	private function curl() {
		return function_exists('curl_init');
	}
	
	/*
		Syntax:
			private SACNRMonitor::error( int $e_num, string $ad_text = null )
		
		Description:
			All errors are triggered through this function. Makes it easy to disable
			them if required. $ad_text can be used to display extra information on an
			error.
	*/
	private function error($e_num, $ad_text = null) {
		$error = array(
			array(
				'You must install and enable the cURL module, or enable the PHP option \'allow_url_fopen\' to use this API.',
				E_USER_ERROR
			),
			array(
				'Tried to use cURL, but it was not enabled. Reverting to fopen...',
				E_USER_NOTICE
			),
			array(
				'Tried to use fopen, but it was not enabled. Reverting to cURL...',
				E_USER_NOTICE
			),
			array(
				'The server returned an error while fetching the requested data: '.htmlentities($ad_text),
				E_USER_NOTICE
			)
		);
		call_user_func_array('trigger_error', $error[$e_num]);
	}
	
	/*
		Syntax:
			private SACNRMonitor::get_url( string $url, string &$text )
		
		Description:
			Chooses cURL or file_get_contents to fetch the URL, and returns the
			result, unserialized. &$text will contain the raw result. If the result
			doesn't unserialize correctly (e.g. is boolean FALSE), then $text will
			probably contain the error message.
	*/
	private function get_url($url, &$text) {
		if ($this->use_curl) {
			if (!$this->ch) {
				$this->ch = curl_init();
				curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
			}
			curl_setopt($this->ch, CURLOPT_URL, $url);
			$result = curl_exec($this->ch);
		} else {
			$result = @file_get_contents($url);
		}
		return unserialize($result);
	}
}
 
endif;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)