TrackManiaControl/core/Server/UsageReporter.php

90 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;
use ManiaControl\Logger;
2014-01-17 18:58:15 +01:00
use ManiaControl\ManiaControl;
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>
2015-01-19 11:06:20 +01:00
* @copyright 2014-2015 ManiaControl Team
2014-05-02 17:50:30 +02:00
* @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
/*
* Private properties
2014-01-17 18:58:15 +01:00
*/
/** @var ManiaControl $maniaControl */
2014-01-17 18:58:15 +01:00
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-08-13 11:05:52 +02:00
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_REPORT_USAGE, true);
2014-05-04 15:29:01 +02:00
2014-08-13 11:05:52 +02:00
$this->maniaControl->getTimerManager()->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
/**
* Report Usage of ManiaControl on the current Server
2014-01-31 00:04:40 +01:00
*/
public function reportUsage() {
2014-08-05 02:17:41 +02:00
if (DEV_MODE
2014-08-13 11:05:52 +02:00
|| !$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_REPORT_USAGE)
2014-08-05 02:17:41 +02:00
) {
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->getServer()->login;
$properties['TitleId'] = $this->maniaControl->getServer()->titleId;
2014-08-13 11:05:52 +02:00
$properties['ServerName'] = Formatter::stripDirtyCodes($this->maniaControl->getClient()->getServerName());
$properties['UpdateChannel'] = $this->maniaControl->getUpdateManager()->getCurrentUpdateChannelSetting();
2014-04-13 19:05:54 +02:00
2014-08-13 11:05:52 +02:00
$properties['PlayerCount'] = $this->maniaControl->getPlayerManager()->getPlayerCount();
2014-05-02 17:50:30 +02:00
$properties['MemoryUsage'] = memory_get_usage();
$properties['MemoryPeakUsage'] = memory_get_peak_usage();
2014-02-09 12:53:21 +01:00
2014-08-13 11:05:52 +02:00
$maxPlayers = $this->maniaControl->getClient()->getMaxPlayers();
2014-05-04 15:29:01 +02:00
$properties['MaxPlayers'] = $maxPlayers['CurrentValue'];
2014-02-09 13:05:59 +01:00
try {
2014-08-13 11:05:52 +02:00
$scriptName = $this->maniaControl->getClient()->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-08-13 11:05:52 +02:00
$properties['ActivePlugins'] = $this->maniaControl->getPluginManager()->getActivePluginsIds();
2014-02-25 18:28:53 +01:00
2014-08-25 15:39:41 +02:00
$usageReport = json_encode($properties);
2014-01-17 18:58:15 +01:00
2014-08-25 15:39:41 +02:00
$url = ManiaControl::URL_WEBSERVICE . 'usagereport';
$this->maniaControl->getFileReader()->postData($url, function ($response, $error) {
2014-08-25 15:33:22 +02:00
$response = json_decode($response);
if ($error || !$response) {
Logger::logError('Error while Sending data: ' . print_r($error, true));
}
2014-08-25 15:39:41 +02:00
}, $usageReport);
2014-01-17 18:58:15 +01:00
}
2014-05-04 15:29:01 +02:00
}