TrackManiaControl/application/core/Server/UsageReporter.php

102 lines
3.3 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-01-17 18:58:15 +01:00
use ManiaControl\ManiaControl;
2014-02-26 11:02:19 +01:00
use ManiaControl\Plugins\Plugin;
2014-05-13 16:03:26 +02:00
use ManiaControl\Utils\Formatter;
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
2014-01-17 18:58:15 +01:00
/**
* Class reporting ManiaControl Usage for the Server
2014-01-17 18:58:15 +01:00
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-01-17 18:58:15 +01:00
*/
2014-01-31 00:04:40 +01:00
class UsageReporter implements TimerListener {
/*
2014-01-17 18:58:15 +01:00
* Constants
*/
2014-05-04 15:29:01 +02:00
const UPDATE_MINUTE_COUNT = 10;
const SETTING_REPORT_USAGE = 'Report Usage to $lManiaControl.com$l';
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;
2014-05-04 15:29:01 +02:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_REPORT_USAGE, true);
$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
/**
2014-05-04 15:29:01 +02:00
* Report Usage every xx Minutes
2014-01-31 00:04:40 +01:00
*
* @param float $time
2014-01-31 00:04:40 +01:00
*/
public function reportUsage($time) {
2014-05-24 20:56:34 +02:00
if (DEV_MODE || !$this->maniaControl->settingManager->getSettingValue($this, self::SETTING_REPORT_USAGE)) {
2014-01-17 18:58:15 +01:00
return;
}
2014-02-22 13:05:05 +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-05-04 15:29:01 +02:00
$properties['UpdateChannel'] = $this->maniaControl->updateManager->getCurrentUpdateChannelSetting();
2014-04-13 19:05:54 +02:00
2014-05-02 17:50:30 +02:00
$properties['PlayerCount'] = $this->maniaControl->playerManager->getPlayerCount();
$properties['MemoryUsage'] = memory_get_usage();
$properties['MemoryPeakUsage'] = memory_get_peak_usage();
2014-02-09 12:53:21 +01:00
$maxPlayers = $this->maniaControl->client->getMaxPlayers();
2014-05-04 15:29:01 +02:00
$properties['MaxPlayers'] = $maxPlayers['CurrentValue'];
2014-02-09 13:05:59 +01:00
try {
$scriptName = $this->maniaControl->client->getScriptName();
2014-05-04 15:29:01 +02:00
$properties['ScriptName'] = $scriptName['CurrentValue'];
} catch (GameModeException $e) {
2014-05-13 16:03:26 +02:00
$properties['ScriptName'] = '';
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();
2014-03-13 18:51:58 +01:00
if (is_array($this->maniaControl->pluginManager->getActivePlugins())) {
2014-05-02 17:50:30 +02:00
foreach ($this->maniaControl->pluginManager->getActivePlugins() as $plugin) {
2014-02-26 11:02:19 +01:00
/** @var Plugin $plugin */
2014-03-13 18:51:58 +01:00
if (!is_null($plugin::getId()) && is_numeric($plugin::getId())) {
2014-02-26 10:54:49 +01:00
$activePlugins[] = $plugin::getId();
2014-02-25 18:28:53 +01:00
}
}
}
$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
2014-05-27 15:34:37 +02:00
$url = ManiaControl::URL_WEBSERVICE . '/usagereport?info=' . urlencode($info);
2014-04-20 14:52:26 +02:00
$self = $this;
2014-05-27 15:34:37 +02:00
$this->maniaControl->fileReader->loadFile($url, function ($response, $error) use (&$self) {
2014-02-09 12:53:21 +01:00
$response = json_decode($response);
if ($error || !$response) {
2014-05-27 15:34:37 +02:00
$self->maniaControl->log('Error while Sending data: ' . print_r($error, true));
2014-02-09 12:53:21 +01:00
}
});
2014-01-17 18:58:15 +01:00
}
2014-05-04 15:29:01 +02:00
}