TrackManiaControl/application/core/Server/Server.php

427 lines
10 KiB
PHP
Raw Normal View History

2013-11-09 17:24:03 +01:00
<?php
namespace ManiaControl\Server;
2014-01-03 20:57:24 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use ManiaControl\Utils\CommandLineHelper;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
2013-11-09 17:24:03 +01:00
/**
* Class providing Access to the connected ManiaPlanet Server
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
2013-11-09 17:24:03 +01:00
*/
2014-01-03 20:57:24 +01:00
class Server implements CallbackListener {
2014-03-20 16:18:35 +01:00
/*
2014-01-03 20:57:24 +01:00
* Constants
*/
const TABLE_SERVERS = 'mc_servers';
const CB_TEAM_MODE_CHANGED = 'Server.TeamModeChanged';
2014-05-02 17:50:30 +02:00
2014-03-20 16:18:35 +01:00
/*
* Public Properties
2014-01-06 15:54:39 +01:00
*/
2014-05-06 10:34:57 +02:00
/** @var Config $config */
2014-03-20 16:18:35 +01:00
public $config = null;
2014-01-06 15:54:39 +01:00
public $index = -1;
public $ip = null;
public $port = -1;
public $p2pPort = -1;
public $login = null;
public $titleId = null;
2014-01-11 11:01:42 +01:00
public $dataDirectory = '';
2014-01-06 15:54:39 +01:00
public $serverCommands = null;
2014-01-17 18:58:15 +01:00
public $usageReporter = null;
2014-02-06 21:18:25 +01:00
public $rankingManager = null;
public $scriptManager = null;
2014-05-02 17:50:30 +02:00
2014-03-20 16:18:35 +01:00
/*
* Private Properties
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
private $maniaControl = null;
private $teamMode = null;
2013-11-09 17:24:03 +01:00
/**
2014-01-06 15:54:39 +01:00
* Construct a new Server
2014-05-02 17:50:30 +02:00
*
2014-01-03 20:57:24 +01:00
* @param ManiaControl $maniaControl
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-05 14:41:19 +01:00
$this->initTables();
2014-05-02 17:50:30 +02:00
$this->serverCommands = new ServerCommands($maniaControl);
$this->usageReporter = new UsageReporter($maniaControl);
$this->rankingManager = new RankingManager($maniaControl);
$this->scriptManager = new ScriptManager($maniaControl);
2014-05-02 17:50:30 +02:00
2014-01-03 20:57:24 +01:00
// Register for callbacks
2014-02-19 12:53:06 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'onInit');
2014-01-06 15:54:39 +01:00
}
2014-05-02 17:50:30 +02:00
/**
* Initialize necessary Database Tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SERVERS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`login` varchar(100) NOT NULL,
PRIMARY KEY (`index`),
UNIQUE KEY `login` (`login`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Servers' AUTO_INCREMENT=1;";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$statement->execute();
if ($statement->error) {
trigger_error($statement->error, E_USER_ERROR);
return false;
}
$statement->close();
return true;
}
2014-03-20 16:18:35 +01:00
/**
* Load the Server Configuration from the Config XML
*/
public function loadConfig() {
2014-05-01 20:20:29 +02:00
// Server id parameter
$serverId = CommandLineHelper::getParameter('-id');
2014-05-02 17:50:30 +02:00
2014-03-20 16:18:35 +01:00
// Xml server tag with given id
$serverTag = null;
2014-05-01 20:20:29 +02:00
if ($serverId) {
$serverTags = $this->maniaControl->config->xpath("server[@id='{$serverId}']");
if ($serverTags) {
2014-04-19 22:59:11 +02:00
$serverTag = $serverTags[0];
2014-05-01 20:20:29 +02:00
}
if (!$serverTag) {
trigger_error("No Server configured with the ID '{$serverId}'!", E_USER_ERROR);
}
2014-05-02 17:50:30 +02:00
} else {
2014-03-20 16:18:35 +01:00
$serverTags = $this->maniaControl->config->xpath('server');
2014-05-01 20:20:29 +02:00
if ($serverTags) {
2014-04-19 22:59:11 +02:00
$serverTag = $serverTags[0];
2014-05-01 20:20:29 +02:00
}
if (!$serverTag) {
2014-04-19 22:59:11 +02:00
trigger_error('No Server configured!', E_USER_ERROR);
2014-05-01 20:20:29 +02:00
}
2014-03-20 16:18:35 +01:00
}
2014-05-02 17:50:30 +02:00
2014-03-20 16:18:35 +01:00
// Host
$host = $serverTag->xpath('host');
2014-05-01 20:20:29 +02:00
if ($host) {
2014-05-02 17:50:30 +02:00
$host = (string)$host[0];
2014-05-01 20:20:29 +02:00
}
if (!$host) {
2014-04-19 22:59:11 +02:00
trigger_error("Invalid server configuration (host).", E_USER_ERROR);
2014-05-01 20:20:29 +02:00
}
2014-05-02 17:50:30 +02:00
2014-03-20 16:18:35 +01:00
// Port
$port = $serverTag->xpath('port');
2014-05-01 20:20:29 +02:00
if ($port) {
2014-05-02 17:50:30 +02:00
$port = (string)$port[0];
2014-05-01 20:20:29 +02:00
}
if (!$port) {
2014-04-19 22:59:11 +02:00
trigger_error("Invalid server configuration (port).", E_USER_ERROR);
2014-05-01 20:20:29 +02:00
}
2014-05-02 17:50:30 +02:00
2014-03-20 16:18:35 +01:00
// Login
$login = $serverTag->xpath('login');
2014-05-01 20:20:29 +02:00
if ($login) {
2014-05-02 17:50:30 +02:00
$login = (string)$login[0];
2014-05-01 20:20:29 +02:00
}
if (!$login) {
2014-04-19 22:59:11 +02:00
trigger_error("Invalid server configuration (login).", E_USER_ERROR);
2014-05-01 20:20:29 +02:00
}
2014-05-02 17:50:30 +02:00
2014-05-01 20:20:29 +02:00
// Password
2014-03-20 16:18:35 +01:00
$pass = $serverTag->xpath('pass');
2014-05-01 20:20:29 +02:00
if ($pass) {
2014-05-02 17:50:30 +02:00
$pass = (string)$pass[0];
2014-05-01 20:20:29 +02:00
}
if (!$pass) {
2014-04-19 22:59:11 +02:00
trigger_error("Invalid server configuration (password).", E_USER_ERROR);
2014-05-01 20:20:29 +02:00
}
2014-05-02 17:50:30 +02:00
2014-03-20 16:18:35 +01:00
// Create config object
2014-05-01 20:20:29 +02:00
$this->config = new Config($serverId, $host, $port, $login, $pass);
2014-03-20 16:18:35 +01:00
}
2014-05-02 17:50:30 +02:00
/**
* Gets all Servers from the Database
*
* @return array
*/
public function getAllServers() {
$mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_SERVERS . "`";
$result = $mysqli->query($query);
if (!$result) {
trigger_error($mysqli->error);
return array();
}
$servers = array();
while ($row = $result->fetch_object()) {
array_push($servers, $row);
}
$result->close();
return $servers;
}
/**
* Handle OnInit Callback
*/
public function onInit() {
$this->updateProperties();
}
2014-01-06 15:54:39 +01:00
/**
* Refetch the Server Properties
*/
private function updateProperties() {
// System info
2014-05-02 17:50:30 +02:00
$systemInfo = $this->maniaControl->client->getSystemInfo();
$this->ip = $systemInfo->publishedIp;
$this->port = $systemInfo->port;
2014-01-16 18:08:32 +01:00
$this->p2pPort = $systemInfo->p2PPort;
2014-05-02 17:50:30 +02:00
$this->login = $systemInfo->serverLogin;
2014-01-16 18:08:32 +01:00
$this->titleId = $systemInfo->titleId;
2014-05-02 17:50:30 +02:00
2014-01-06 15:54:39 +01:00
// Database index
2014-05-02 17:50:30 +02:00
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT INTO `" . self::TABLE_SERVERS . "` (
2014-01-06 15:54:39 +01:00
`login`
) VALUES (
?
) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`);";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
2014-01-06 15:54:39 +01:00
trigger_error($mysqli->error);
return;
}
$statement->bind_param('s', $this->login);
$statement->execute();
if ($statement->error) {
2014-01-06 15:54:39 +01:00
trigger_error($statement->error);
$statement->close();
return;
}
$this->index = $statement->insert_id;
$statement->close();
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Fetch Maps Directory
2014-05-02 17:50:30 +02:00
*
2013-11-09 17:24:03 +01:00
* @return string
*/
2013-11-10 20:09:08 +01:00
public function getMapsDirectory() {
$dataDirectory = $this->getDataDirectory();
if (!$dataDirectory) {
2014-01-08 20:11:48 +01:00
return null;
}
2014-05-06 10:34:57 +02:00
return "{$dataDirectory}Maps" . DIRECTORY_SEPARATOR;
2013-11-09 17:24:03 +01:00
}
/**
2014-05-02 17:50:30 +02:00
* Fetch Game Data Directory
*
* @return string
2013-11-09 17:24:03 +01:00
*/
2014-05-02 17:50:30 +02:00
public function getDataDirectory() {
if ($this->dataDirectory == '') {
$this->dataDirectory = $this->maniaControl->client->gameDataDirectory();
2014-01-08 20:11:48 +01:00
}
2014-05-02 17:50:30 +02:00
return $this->dataDirectory;
2013-11-09 17:24:03 +01:00
}
2014-01-05 14:41:19 +01:00
/**
* Get Server Player Info
2014-05-02 17:50:30 +02:00
*
2014-05-08 21:30:15 +02:00
* @return \Maniaplanet\DedicatedServer\Structures\PlayerDetailedInfo
2013-11-09 17:24:03 +01:00
*/
public function getInfo() {
2014-05-08 21:30:15 +02:00
return $this->maniaControl->client->getDetailedPlayerInfo($this->login);
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Retrieve Validation Replay for the given Player
2014-05-02 17:50:30 +02:00
*
* @param string $login
2013-11-10 20:09:08 +01:00
* @return string
2013-11-09 17:24:03 +01:00
*/
2014-02-20 13:49:33 +01:00
public function getValidationReplay($login) {
$login = Player::parseLogin($login);
2014-01-18 21:46:42 +01:00
try {
2014-02-20 13:49:33 +01:00
$replay = $this->maniaControl->client->getValidationReplay($login);
2014-05-02 17:50:30 +02:00
} catch (Exception $e) {
2014-05-01 20:20:29 +02:00
// TODO temp added 19.04.2014
2014-04-19 22:59:11 +02:00
$this->maniaControl->errorHandler->triggerDebugNotice("Exception line 330 Server.php" . $e->getMessage());
2014-05-02 17:50:30 +02:00
2014-02-20 13:49:33 +01:00
trigger_error("Couldn't get validation replay of '{$login}'. " . $e->getMessage());
2013-11-09 17:24:03 +01:00
return null;
}
2014-01-16 17:31:15 +01:00
return $replay;
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Retrieve Ghost Replay for the given Player
2014-05-02 17:50:30 +02:00
*
* @param string $login
2013-11-09 17:24:03 +01:00
* @return string
*/
2014-02-20 13:49:33 +01:00
public function getGhostReplay($login) {
2013-11-09 17:24:03 +01:00
$dataDir = $this->getDataDirectory();
if (!$this->checkAccess($dataDir)) {
2014-01-08 20:11:48 +01:00
return null;
}
2014-05-02 17:50:30 +02:00
2013-11-09 17:24:03 +01:00
// Build file name
$login = Player::parseLogin($login);
2014-05-02 17:50:30 +02:00
$map = $this->maniaControl->mapManager->getCurrentMap();
$gameMode = $this->getGameMode();
2014-05-02 17:50:30 +02:00
$time = time();
2014-02-20 13:49:33 +01:00
$fileName = "GhostReplays/Ghost.{$login}.{$gameMode}.{$time}.{$map->uid}.Replay.Gbx";
2014-05-02 17:50:30 +02:00
2013-11-09 17:24:03 +01:00
// Save ghost replay
2014-01-18 21:46:42 +01:00
try {
2014-02-20 13:49:33 +01:00
$this->maniaControl->client->saveBestGhostsReplay($login, $fileName);
2014-05-02 17:50:30 +02:00
} catch (Exception $e) {
2014-05-01 20:20:29 +02:00
// TODO temp added 19.04.2014
2014-04-19 22:59:11 +02:00
$this->maniaControl->errorHandler->triggerDebugNotice("Exception line 360 Server.php" . $e->getMessage());
2014-05-02 17:50:30 +02:00
2014-01-18 21:46:42 +01:00
trigger_error("Couldn't save ghost replay. " . $e->getMessage());
2013-11-09 17:24:03 +01:00
return null;
}
2014-05-02 17:50:30 +02:00
2013-11-09 17:24:03 +01:00
// Load replay file
2014-01-06 15:54:39 +01:00
$ghostReplay = file_get_contents("{$dataDir}Replays/{$fileName}");
if (!$ghostReplay) {
2013-11-09 17:24:03 +01:00
trigger_error("Couldn't retrieve saved ghost replay.");
return null;
}
return $ghostReplay;
}
2014-05-02 17:50:30 +02:00
/**
* Checks if ManiaControl has Access to the given Directory
*
* @param string $directory
* @return bool
*/
public function checkAccess($directory) {
if (!$directory) {
return false;
}
return (is_dir($directory) && is_writable($directory));
}
/**
* Fetch the current Game Mode
*
* @param bool $stringValue
* @param int $parseValue
* @return int | string
*/
public function getGameMode($stringValue = false, $parseValue = null) {
if (is_int($parseValue)) {
$gameMode = $parseValue;
} else {
$gameMode = $this->maniaControl->client->getGameMode();
}
if ($stringValue) {
switch ($gameMode) {
case 0:
return 'Script';
case 1:
return 'Rounds';
case 2:
return 'TimeAttack';
case 3:
return 'Team';
case 4:
return 'Laps';
case 5:
return 'Cup';
case 6:
return 'Stunts';
default:
return 'Unknown';
}
}
return $gameMode;
}
2013-11-09 17:24:03 +01:00
/**
2014-01-05 14:41:19 +01:00
* Wait for the Server to have the given Status
2014-05-02 17:50:30 +02:00
*
2014-01-03 20:57:24 +01:00
* @param int $statusCode
2013-11-10 20:09:08 +01:00
* @return bool
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
public function waitForStatus($statusCode = 4) {
2014-01-16 17:31:15 +01:00
$response = $this->maniaControl->client->getStatus();
2013-11-10 20:09:08 +01:00
// Check if server has the given status
if ($response->code === 4) {
2014-01-08 20:11:48 +01:00
return true;
}
2014-01-06 15:54:39 +01:00
// Server not yet in given status - Wait for it...
2014-05-02 17:50:30 +02:00
$waitBegin = time();
2014-02-27 15:03:01 +01:00
$maxWaitTime = 50;
2014-05-02 17:50:30 +02:00
$lastStatus = $response->name;
$this->maniaControl->log("Waiting for server to reach status {$statusCode}...");
$this->maniaControl->log("Current Status: {$lastStatus}");
2014-05-01 20:20:29 +02:00
while ($response->code !== 4) {
2013-11-09 17:24:03 +01:00
sleep(1);
2014-01-16 17:31:15 +01:00
$response = $this->maniaControl->client->getStatus();
2014-02-15 21:40:42 +01:00
if ($lastStatus !== $response->name) {
$this->maniaControl->log("New Status: {$response->name}");
$lastStatus = $response->name;
2013-11-09 17:24:03 +01:00
}
if (time() - $maxWaitTime > $waitBegin) {
2013-11-09 17:24:03 +01:00
// It took too long to reach the status
2014-01-20 20:51:03 +01:00
trigger_error("Server couldn't reach status {$statusCode} after {$maxWaitTime} seconds! ");
2013-11-09 17:24:03 +01:00
return false;
}
}
return true;
}
2014-05-11 15:28:53 +02:00
/**
* Set whether the Server Runs a Team-Based Mode or not
*
* @param bool $teamMode
*/
public function setTeamMode($teamMode = true) {
$oldStatus = $this->teamMode;
$this->teamMode = (bool)$teamMode;
// Trigger callback
if ($oldStatus !== $this->teamMode | $oldStatus === null) {
$this->maniaControl->callbackManager->triggerCallback(self::CB_TEAM_MODE_CHANGED, $teamMode);
}
2014-05-11 15:28:53 +02:00
}
/**
* Check if the Server Runs a Team-Based Mode
*
* @return bool
*/
public function isTeamMode() {
return $this->teamMode;
2014-05-11 15:28:53 +02:00
}
2013-11-09 17:24:03 +01:00
}