TrackManiaControl/application/core/Server/UsageReporter.php

97 lines
3.0 KiB
PHP
Raw Normal View History

2014-01-17 18:58:15 +01:00
<?php
namespace ManiaControl\Server;
2014-01-31 00:04:40 +01:00
use ManiaControl\Callbacks\TimerListener;
2014-02-09 14:01:04 +01:00
use ManiaControl\Formatter;
2014-01-17 18:58:15 +01:00
use ManiaControl\ManiaControl;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
2014-01-17 18:58:15 +01:00
/**
* Class reporting ManiaControl Usage for the Server
2014-01-17 18:58:15 +01:00
*
* @author steeffeen & kremsy
*/
2014-01-31 00:04:40 +01:00
class UsageReporter implements TimerListener {
2014-01-17 18:58:15 +01:00
/**
* Constants
*/
const UPDATE_MINUTE_COUNT = 10;
const SETTING_DISABLE_USAGE_REPORTING = 'Disable Usage Reporting';
2014-02-08 16:44:40 +01:00
2014-01-17 18:58:15 +01:00
/**
* Private Properties
*/
private $maniaControl = null;
/**
* Create a new Usage Reporter Instance
2014-01-17 18:58:15 +01:00
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->maniaControl->settingManager->initSetting($this, self::SETTING_DISABLE_USAGE_REPORTING, false);
2014-01-31 00:04:40 +01:00
$this->maniaControl->timerManager->registerTimerListening($this, 'reportUsage', 1000 * 60 * self::UPDATE_MINUTE_COUNT);
2014-01-17 18:58:15 +01:00
}
2014-01-31 00:04:40 +01:00
/**
* Reports Usage every xx Minutes
*
* @param float $time
2014-01-31 00:04:40 +01:00
*/
public function reportUsage($time) {
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_DISABLE_USAGE_REPORTING)) {
2014-01-17 18:58:15 +01:00
return;
}
2014-02-22 13:05:05 +01:00
2014-01-17 18:58:15 +01:00
2014-02-08 16:44:40 +01:00
$properties = array();
$properties['ManiaControlVersion'] = ManiaControl::VERSION;
$properties['OperatingSystem'] = php_uname();
$properties['PHPVersion'] = phpversion();
$properties['ServerLogin'] = $this->maniaControl->server->login;
$properties['TitleId'] = $this->maniaControl->server->titleId;
2014-02-15 13:04:31 +01:00
$properties['ServerName'] = Formatter::stripDirtyCodes($this->maniaControl->client->getServerName());
2014-02-08 20:29:50 +01:00
$properties['PlayerCount'] = $this->maniaControl->playerManager->getPlayerCount();
2014-02-22 13:05:05 +01:00
$properties['MemoryUsage'] = memory_get_usage();
$properties['MemoryPeakUsage'] = memory_get_peak_usage();
2014-02-09 12:53:21 +01:00
$maxPlayers = $this->maniaControl->client->getMaxPlayers();
$properties['MaxPlayers'] = $maxPlayers["CurrentValue"];
2014-02-09 13:05:59 +01:00
try {
$scriptName = $this->maniaControl->client->getScriptName();
$properties['ScriptName'] = $scriptName["CurrentValue"];
} catch(Exception $e) {
if ($e->getMessage() == 'Not in script mode.') {
$properties['ScriptName'] = '';
} else {
throw $e;
}
2014-02-08 16:44:40 +01:00
}
2014-01-17 18:58:15 +01:00
2014-02-25 18:28:53 +01:00
$activePlugins = array();
if(is_array($this->maniaControl->pluginManager->getActivePlugins())) {
foreach($this->maniaControl->pluginManager->getActivePlugins() as $plugin) {
if(!is_null($plugin->getId()) && is_numeric($plugin->getId())) {
$activePlugins[] = $plugin->getId();
}
}
}
$properties['ActivePlugins'] = $activePlugins;
2014-01-31 00:04:40 +01:00
$json = json_encode($properties);
$info = base64_encode($json);
2014-01-17 18:58:15 +01:00
$this->maniaControl->fileReader->loadFile(ManiaControl::URL_WEBSERVICE . "/usagereport?info=" . urlencode($info), function ($response, $error) {
2014-02-09 12:53:21 +01:00
$response = json_decode($response);
if ($error || !$response) {
$this->maniaControl->log("Error while Sending data: " . $error);
}
});
2014-01-17 18:58:15 +01:00
}
}