<?php
require_once('xaseco/includes/GbxRemote.inc.php');
$config = array(
"host" => "localhost",
"user" => "SuperAdmin",
"pass" => "SuperAdmin",
"port" => 5000,
"tmf" => true,
"dbhost" => "localhost",
"dbname" => "rcpdb",
"dbuser" => "rcpuser",
"dbpassword" => "verysecret",
"serverid" => "1"
);
ini_set('max_execution_time', 0);
ini_set('max_input_time', -1);
class Statistics {
public $client;
private $config, $serverlogin, $db;
function __construct($config) {
$this->config = $config;
$this->config['table'] = "rcp_statistics_".(string) $this->config['serverid'];
$this->connectToDatabase();
$this->connectToServer();
}
function increaseVisits() {
$date = date("Y-m-d");
$select = mysql_query("SELECT date FROM {$this->config['table']} ORDER BY id DESC Limit 1");
$row = mysql_fetch_object($select);
mysql_free_result($select);
$db_date = $row->date;
if ($date !== $db_date) {
$insert = mysql_query("INSERT INTO {$this->config['table']}(date , hits) VALUES('{$date}',1)");
}else{
$update = mysql_query("UPDATE {$this->config['table']} SET hits = hits + 1 WHERE date = '{$date}'");
}
$this->sendConsole("Player connected");
}
function connectToDatabase() {
if(!$this->db = mysql_connect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpassword'])) {
$this->sendConsole("Database connection failed: ".mysql_error(), 1);
}
mysql_select_db($this->config['dbname'], $this->db);
$query = "CREATE TABLE IF NOT EXISTS `{$this->config['table']}` (
`Id` mediumint(9) unsigned NOT NULL auto_increment,
`date` date NOT NULL,
`hits` mediumint(10) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;";
if(!mysql_query($query)) {
$this->sendConsole("Could not create '".$this->config['table']."' table: ".mysql_error(), 1);
}
}
function connectToServer() {
$this->client = new IXR_Client_Gbx();
if (!$this->client->InitWithIp(strval($this->config['host']), intval($this->config['port']))) {
$this->errorcheck();
$this->sendConsole('[Error] Could not connect to the server!', 3);
}
$this->sendConsole('Login as ' . strval($this->config['user']) . '...');
if (!$this->client->query('Authenticate', strval($this->config['user']), strval($this->config['pass']))) {
$this->errorcheck();
$this->sendConsole('[Error] Wrong username and/or password!', 3);
}
$this->sendConsole('Enabling callbacks ...');
if (!$this->client->query('EnableCallbacks', true)) {
$this->errorcheck();
$this->sendConsole('[Error] Could not activate callbacks!', 3);
}
if(!$this->config['tmf']) {
$this->sendConsole('Setting API version ...');
if(!$this->client->query('SetApiVersion', '2012-06-19')) {
$this->errorcheck();
$this->sendConsole('[Error] Could not set API version', 3);
}
}
$this->sendConsole('... Done!');
$this->client->query('GetSystemInfo');
$response = $this->client->getResponse();
$this->sendConsole(
'######################'."\n".
'# Connected with:'."\n".
'# IP: '.$response['PublishedIp'].':'.$response['Port']."\n".
'# Login: '.$response['ServerLogin']."\n".
'######################');
$this->sendConsole("Successfully started.. waiting for players!");
$this->client->query('EnableCallbacks', true);
$this->client->query('ChatSendServerMessage', '$o$aaaStatistics started!');
$this->serverlogin = $response['ServerLogin'];
}
function errorcheck() {
if ($this->client->isError()) {
echo '[Client Message ' . strval($this->client->getErrorCode()) . '] ' . strval($this->client->getErrorMessage())."\n";
$this->client->resetError();
}
}
function sendConsole($msg, $err=false) {
echo $msg."\n";
if($err) die();
}
}
$stats = new Statistics($config);
while(true) {
$stats->client->readCB();
$stats->errorcheck();
$calls = $stats->client->getCBResponses();
foreach($calls as $call) {
$name = explode(".", $call[0]);
if($name[count($name)-1] == 'PlayerConnect') {
$stats->increaseVisits();
}
}
usleep(1000);
}
?>