TrackManiaControl/application/core/Server/Server.php

388 lines
9.4 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\FileUtil;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
require_once __DIR__ . '/ServerCommands.php';
2013-11-09 17:24:03 +01:00
/**
2014-01-05 14:41:19 +01:00
* Class providing Information about theconnected ManiaPlanet Server
2013-11-09 17:24:03 +01:00
*
2013-11-10 20:09:08 +01:00
* @author steeffeen & kremsy
2013-11-09 17:24:03 +01:00
*/
2014-01-03 20:57:24 +01:00
class Server implements CallbackListener {
2014-01-05 14:41:19 +01:00
2014-01-03 20:57:24 +01:00
/**
* Constants
*/
const TABLE_SERVERS = 'mc_servers';
2014-01-05 14:41:19 +01:00
2013-11-09 17:24:03 +01:00
/**
2014-01-05 14:41:19 +01:00
* Private Properties
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
private $maniaControl = null;
private $serverCommands = null;
2014-01-05 14:41:19 +01:00
private $index = null;
private $login = null;
2013-11-09 17:24:03 +01:00
/**
* Construct server
2013-11-10 20:09:08 +01: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-01-03 20:57:24 +01:00
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
2014-01-05 14:41:19 +01:00
$this->serverCommands = new ServerCommands($maniaControl);
2013-11-09 17:24:03 +01:00
}
2014-01-03 20:57:24 +01:00
/**
2014-01-05 14:41:19 +01:00
* Initialize necessary Database Tables
2014-01-03 20:57:24 +01:00
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
2014-01-05 14:41:19 +01:00
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SERVERS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
2014-01-03 20:57:24 +01:00
`login` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
2014-01-05 14:41:19 +01:00
PRIMARY KEY (`index`),
2014-01-03 20:57:24 +01:00
UNIQUE KEY `login` (`login`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Servers' AUTO_INCREMENT=1;";
$statement = $mysqli->prepare($query);
2014-01-05 14:41:19 +01:00
if ($mysqli->error) {
2014-01-03 20:57:24 +01:00
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$statement->execute();
2014-01-05 14:41:19 +01:00
if ($statement->error) {
2014-01-03 20:57:24 +01:00
trigger_error($statement->error, E_USER_ERROR);
return false;
}
$statement->close();
return true;
}
/**
2014-01-05 14:41:19 +01:00
* Handle OnInit Callback
2014-01-03 20:57:24 +01:00
*
* @param array $callback
*/
public function onInit(array $callback) {
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT IGNORE INTO `" . self::TABLE_SERVERS . "` (
2014-01-05 14:41:19 +01:00
`login`
2014-01-03 20:57:24 +01:00
) VALUES (
2014-01-05 14:41:19 +01:00
?
2014-01-03 20:57:24 +01:00
);";
$statement = $mysqli->prepare($query);
2014-01-05 14:41:19 +01:00
if ($mysqli->error) {
2014-01-03 20:57:24 +01:00
trigger_error($mysqli->error);
2014-01-05 14:41:19 +01:00
return;
2014-01-03 20:57:24 +01:00
}
2014-01-05 14:41:19 +01:00
$login = $this->getLogin();
2014-01-03 20:57:24 +01:00
$statement->bind_param('s', $login);
$statement->execute();
2014-01-05 14:41:19 +01:00
if ($statement->error) {
2014-01-03 20:57:24 +01:00
trigger_error($statement->error);
$statement->close();
2014-01-05 14:41:19 +01:00
return;
2014-01-03 20:57:24 +01:00
}
$statement->close();
return true;
}
2013-11-09 17:24:03 +01:00
/**
2014-01-05 14:41:19 +01:00
* Fetch Game Data Directory
2013-11-10 20:09:08 +01:00
*
* @return string
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
public function getDataDirectory() {
2014-01-05 14:41:19 +01:00
if (!$this->maniaControl->client->query('GameDataDirectory')) {
2013-11-10 20:09:08 +01:00
trigger_error("Couldn't get data directory. " . $this->maniaControl->getClientErrorText());
return null;
}
return $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Fetch Maps Directory
2013-11-09 17:24:03 +01:00
*
* @return string
*/
2013-11-10 20:09:08 +01:00
public function getMapsDirectory() {
$dataDirectory = $this->getDataDirectory();
2014-01-05 14:41:19 +01:00
if (!$dataDirectory) {
2013-11-09 17:24:03 +01:00
return null;
}
2013-11-11 18:37:53 +01:00
return $dataDirectory . 'Maps/';
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Checks if ManiaControl has Access to the given Directory
2013-11-09 17:24:03 +01:00
*
2014-01-03 20:57:24 +01:00
* @param string $directory
2013-11-09 17:24:03 +01:00
* @return bool
*/
2013-11-10 20:09:08 +01:00
public function checkAccess($directory) {
2014-01-05 14:41:19 +01:00
if (!$directory) return false;
2013-11-10 20:09:08 +01:00
return (is_dir($directory) && is_writable($directory));
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Fetch Server Index
2013-11-10 20:09:08 +01:00
*
2014-01-05 14:41:19 +01:00
* @return int
2013-11-09 17:24:03 +01:00
*/
2014-01-05 14:41:19 +01:00
public function getIndex() {
if ($this->index) return $this->index;
$mysqli = $this->maniaControl->database->mysqli;
$login = $this->getLogin();
$query = "SELECT `index` FROM `" . self::TABLE_SERVERS . "`
WHERE `login` = '" . $login . "';";
$result = $mysqli->query($query);
if (!$result) {
trigger_error($mysqli->error);
return;
2013-11-10 20:09:08 +01:00
}
2014-01-05 14:41:19 +01:00
$row = $result->fetch_object();
$result->close();
$this->index = $row->index;
return $this->index;
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Fetch Server Login
*
* @return string
*/
public function getLogin() {
if ($this->login) return $this->login;
$systemInfo = $this->getSystemInfo();
if (!$systemInfo) return null;
$this->login = $systemInfo['ServerLogin'];
return $this->login;
}
/**
* Get the Server Info
2013-11-10 20:09:08 +01:00
*
2014-01-03 20:57:24 +01:00
* @param bool $detailed
2013-11-10 20:09:08 +01:00
* @return array
2013-11-09 17:24:03 +01:00
*/
public function getInfo($detailed = false) {
2014-01-05 14:41:19 +01:00
if ($detailed) {
2013-11-09 17:24:03 +01:00
$login = $this->getLogin();
2014-01-05 14:41:19 +01:00
if (!$this->maniaControl->client->query('GetDetailedPlayerInfo', $login)) {
2013-11-10 20:09:08 +01:00
trigger_error("Couldn't fetch detailed server info. " . $this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return null;
}
2013-11-10 20:09:08 +01:00
return $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
}
2014-01-05 14:41:19 +01:00
if (!$this->maniaControl->client->query('GetMainServerPlayerInfo')) {
2013-11-10 20:09:08 +01:00
trigger_error("Couldn't fetch server info. " . $this->maniaControl->getClientErrorText());
return null;
2013-11-09 17:24:03 +01:00
}
2013-11-10 20:09:08 +01:00
return $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Get Server Options
2013-11-10 20:09:08 +01:00
*
* @return array
2013-11-09 17:24:03 +01:00
*/
public function getOptions() {
2014-01-05 14:41:19 +01:00
if (!$this->maniaControl->client->query('GetServerOptions')) {
2013-11-10 20:09:08 +01:00
trigger_error("Couldn't fetch server options. " . $this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return null;
}
2013-11-10 20:09:08 +01:00
return $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Fetch current Server Name
2013-11-10 20:09:08 +01:00
*
* @return string
2013-11-09 17:24:03 +01:00
*/
public function getName() {
2014-01-05 14:41:19 +01:00
if (!$this->maniaControl->client->query('GetServerName')) {
2013-11-10 20:09:08 +01:00
trigger_error("Couldn't fetch server name. " . $this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return null;
}
2013-11-10 20:09:08 +01:00
return $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Fetch Server Version
2013-11-10 20:09:08 +01:00
*
* @return string
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
public function getVersion() {
2014-01-05 14:41:19 +01:00
if (!$this->maniaControl->client->query('GetVersion')) {
2013-11-10 20:09:08 +01:00
trigger_error("Couldn't fetch server version. " . $this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return null;
}
2013-11-10 20:09:08 +01:00
return $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Fetch Server System Info
2013-11-10 20:09:08 +01:00
*
* @return array
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
public function getSystemInfo() {
2014-01-05 14:41:19 +01:00
if (!$this->maniaControl->client->query('GetSystemInfo')) {
trigger_error("Couldn't fetch server system info. " . $this->maniaControl->getClientErrorText($this->maniaControl->client));
2013-11-09 17:24:03 +01:00
return null;
}
2013-11-10 20:09:08 +01:00
return $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Fetch current Game Mode
2013-11-09 17:24:03 +01:00
*
2014-01-03 20:57:24 +01:00
* @param bool $stringValue
2014-01-05 14:41:19 +01:00
* @param int $parseValue
2013-11-09 17:24:03 +01:00
* @return int | string
*/
public function getGameMode($stringValue = false, $parseValue = null) {
2014-01-05 14:41:19 +01:00
if (is_int($parseValue)) {
2013-11-09 17:24:03 +01:00
$gameMode = $parseValue;
2014-01-05 14:41:19 +01:00
}
else {
if (!$this->maniaControl->client->query('GetGameMode')) {
2013-11-10 20:09:08 +01:00
trigger_error("Couldn't fetch current game mode. " . $this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return null;
}
2013-11-10 20:09:08 +01:00
$gameMode = $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
}
2014-01-05 14:41:19 +01:00
if ($stringValue) {
switch ($gameMode) {
2013-11-09 17:24:03 +01:00
case 0:
2014-01-05 14:41:19 +01:00
{
return 'Script';
}
2013-11-09 17:24:03 +01:00
case 1:
2014-01-05 14:41:19 +01:00
{
return 'Rounds';
}
2013-11-09 17:24:03 +01:00
case 2:
2014-01-05 14:41:19 +01:00
{
return 'TimeAttack';
}
2013-11-09 17:24:03 +01:00
case 3:
2014-01-05 14:41:19 +01:00
{
return 'Team';
}
2013-11-09 17:24:03 +01:00
case 4:
2014-01-05 14:41:19 +01:00
{
return 'Laps';
}
2013-11-09 17:24:03 +01:00
case 5:
2014-01-05 14:41:19 +01:00
{
return 'Cup';
}
2013-11-09 17:24:03 +01:00
case 6:
2014-01-05 14:41:19 +01:00
{
return 'Stunts';
}
2013-11-09 17:24:03 +01:00
default:
{
2014-01-05 14:41:19 +01:00
return 'Unknown';
2013-11-09 17:24:03 +01:00
}
}
}
return $gameMode;
}
2013-11-10 20:09:08 +01:00
2013-11-09 17:24:03 +01:00
/**
2014-01-05 14:41:19 +01:00
* Retrieve Validation Replay for the given Player
2013-11-09 17:24:03 +01:00
*
2014-01-03 20:57:24 +01:00
* @param Player $player
2013-11-10 20:09:08 +01:00
* @return string
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
public function getValidationReplay(Player $player) {
2014-01-05 14:41:19 +01:00
if (!$this->maniaControl->client->query('GetValidationReplay', $player->login)) {
2013-11-10 20:09:08 +01:00
trigger_error("Couldn't get validation replay of '{$player->login}'. " . $this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return null;
}
2013-11-10 20:09:08 +01:00
return $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
}
/**
2014-01-05 14:41:19 +01:00
* Retrieve Ghost Replay for the given Player
2013-11-09 17:24:03 +01:00
*
2014-01-03 20:57:24 +01:00
* @param Player $player
2013-11-09 17:24:03 +01:00
* @return string
*/
2013-11-10 20:09:08 +01:00
public function getGhostReplay(Player $player) {
2013-11-09 17:24:03 +01:00
$dataDir = $this->getDataDirectory();
2014-01-05 14:41:19 +01:00
if (!$this->checkAccess($dataDir)) return null;
2013-11-09 17:24:03 +01:00
// Build file name
2014-01-05 14:41:19 +01:00
$map = $this->getMap();
2013-11-09 17:24:03 +01:00
$gameMode = $this->getGameMode();
2014-01-05 14:41:19 +01:00
$time = time();
$fileName = "GhostReplays/Ghost.{$player->login}.{$gameMode}.{$time}.{$map['UId']}.Replay.Gbx";
2014-01-05 14:41:19 +01:00
2013-11-09 17:24:03 +01:00
// Save ghost replay
2014-01-05 14:41:19 +01:00
if (!$this->maniaControl->client->query('SaveBestGhostsReplay', $player->login, $fileName)) {
2013-11-10 20:09:08 +01:00
trigger_error("Couldn't save ghost replay. " . $this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return null;
}
2014-01-05 14:41:19 +01:00
2013-11-09 17:24:03 +01:00
// Load replay file
$ghostReplay = file_get_contents($dataDir . 'Replays/' . $fileName);
2014-01-05 14:41:19 +01:00
if (!$ghostReplay) {
2013-11-09 17:24:03 +01:00
trigger_error("Couldn't retrieve saved ghost replay.");
return null;
}
return $ghostReplay;
}
/**
2014-01-05 14:41:19 +01:00
* Wait for the Server to have the given Status
2013-11-10 20:09:08 +01: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) {
$this->maniaControl->client->query('GetStatus');
$response = $this->maniaControl->client->getResponse();
// Check if server has the given status
2014-01-05 14:41:19 +01:00
if ($response['Code'] === 4) {
2013-11-10 20:09:08 +01:00
return true;
}
2013-11-09 17:24:03 +01:00
// Server not yet in given status -> Wait for it...
2014-01-05 14:41:19 +01:00
$waitBegin = time();
$maxWaitTime = 20;
2014-01-05 14:41:19 +01:00
$lastStatus = $response['Name'];
$this->maniaControl->log("Waiting for server to reach status {$statusCode}...");
$this->maniaControl->log("Current Status: {$lastStatus}");
2014-01-05 14:41:19 +01:00
while ($response['Code'] !== 4) {
2013-11-09 17:24:03 +01:00
sleep(1);
2013-11-10 20:09:08 +01:00
$this->maniaControl->client->query('GetStatus');
$response = $this->maniaControl->client->getResponse();
2014-01-05 14:41:19 +01:00
if ($lastStatus !== $response['Name']) {
$this->maniaControl->log("New Status: " . $response['Name']);
2013-11-09 17:24:03 +01:00
$lastStatus = $response['Name'];
}
2014-01-05 14:41:19 +01:00
if (time() - $maxWaitTime > $waitBegin) {
2013-11-09 17:24:03 +01:00
// It took too long to reach the status
2014-01-03 20:57:24 +01:00
trigger_error("Server couldn't reach status {$statusCode} after {$maxWaitTime} seconds! " . $this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return false;
}
}
return true;
}
}