2013-11-09 17:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-19 20:29:37 +01:00
|
|
|
namespace ManiaControl\Server;
|
|
|
|
|
2014-01-03 20:57:24 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2013-11-19 20:29:37 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
2013-12-09 13:45:58 +01:00
|
|
|
use ManiaControl\Players\Player;
|
2014-02-15 12:53:33 +01:00
|
|
|
use Maniaplanet\DedicatedServer\Structures\ServerOptions;
|
2014-01-16 18:08:32 +01:00
|
|
|
use Maniaplanet\DedicatedServer\Structures\SystemInfos;
|
2014-02-15 12:53:33 +01:00
|
|
|
use Maniaplanet\DedicatedServer\Structures\Version;
|
2014-02-13 14:21:25 +01:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
|
2013-11-19 20:29:37 +01:00
|
|
|
|
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-08 20:11:48 +01:00
|
|
|
|
2014-01-03 20:57:24 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2014-02-01 19:06:21 +01:00
|
|
|
const TABLE_SERVERS = 'mc_servers';
|
|
|
|
const CB_TEAM_STATUS_CHANGED = 'ServerCallback.TeamStatusChanged';
|
2014-01-08 20:11:48 +01:00
|
|
|
|
2014-01-06 15:54:39 +01:00
|
|
|
/**
|
|
|
|
* Public Properties
|
|
|
|
*/
|
|
|
|
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;
|
2014-02-01 19:06:21 +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;
|
2014-02-01 19:06:21 +01:00
|
|
|
private $teamMode = false;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
2014-01-06 15:54:39 +01:00
|
|
|
* Construct a new 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-08 20:11:48 +01:00
|
|
|
|
2014-01-06 15:54:39 +01:00
|
|
|
$this->serverCommands = new ServerCommands($maniaControl);
|
2014-01-17 18:58:15 +01:00
|
|
|
$this->usageReporter = new UsageReporter($maniaControl);
|
2014-02-06 21:18:25 +01:00
|
|
|
$this->rankingManager = new RankingManager($maniaControl);
|
2014-01-08 20:11:48 +01: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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refetch the Server Properties
|
|
|
|
*/
|
|
|
|
private function updateProperties() {
|
|
|
|
// System info
|
2014-02-15 13:04:31 +01:00
|
|
|
$systemInfo = $this->maniaControl->client->getSystemInfo();
|
2014-01-16 18:08:32 +01:00
|
|
|
$this->ip = $systemInfo->publishedIp;
|
|
|
|
$this->port = $systemInfo->port;
|
|
|
|
$this->p2pPort = $systemInfo->p2PPort;
|
|
|
|
$this->login = $systemInfo->serverLogin;
|
|
|
|
$this->titleId = $systemInfo->titleId;
|
2014-01-08 20:11:48 +01:00
|
|
|
|
2014-01-06 15:54:39 +01:00
|
|
|
// Database index
|
2014-01-08 20:11:48 +01: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);
|
2014-01-31 16:11:14 +01:00
|
|
|
if ($mysqli->error) {
|
2014-01-06 15:54:39 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$statement->bind_param('s', $this->login);
|
|
|
|
$statement->execute();
|
2014-01-31 16:11:14 +01:00
|
|
|
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-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() {
|
2014-01-08 20:11:48 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SERVERS . "` (
|
2014-01-05 14:41:19 +01:00
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
2014-02-13 14:58:04 +01:00
|
|
|
`login` varchar(100) 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-31 16:11:14 +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-31 16:11:14 +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) {
|
2014-01-06 15:54:39 +01:00
|
|
|
$this->updateProperties();
|
2014-01-03 20:57:24 +01:00
|
|
|
}
|
|
|
|
|
2014-02-01 19:06:21 +01:00
|
|
|
/**
|
|
|
|
* Set if the Server Runs a Team-Mode or not
|
|
|
|
*
|
|
|
|
* @param bool $teamMode
|
|
|
|
*/
|
|
|
|
public function setTeamMode($teamMode = true) {
|
2014-02-01 20:25:20 +01:00
|
|
|
$oldStatus = $this->teamMode;
|
2014-02-01 19:06:21 +01:00
|
|
|
$this->teamMode = $teamMode;
|
|
|
|
|
|
|
|
// Trigger callback
|
2014-02-01 20:25:20 +01:00
|
|
|
if ($oldStatus != $this->teamMode) {
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_TEAM_STATUS_CHANGED, array(self::CB_TEAM_STATUS_CHANGED, $teamMode));
|
|
|
|
}
|
2014-02-01 19:06:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Server Runs a TeamMode
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isTeamMode() {
|
|
|
|
return $this->teamMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-31 16:11:14 +01:00
|
|
|
if ($this->dataDirectory == '') {
|
2014-02-13 14:21:25 +01:00
|
|
|
$this->dataDirectory = $this->maniaControl->client->gameDataDirectory();
|
2013-11-10 20:09:08 +01:00
|
|
|
}
|
2014-01-11 11:01:42 +01:00
|
|
|
return $this->dataDirectory;
|
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-31 16:11:14 +01:00
|
|
|
if (!$dataDirectory) {
|
2014-01-08 20:11:48 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-01-06 15:54:39 +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-31 16:11:14 +01:00
|
|
|
if (!$directory) {
|
2014-01-08 20:11:48 +01:00
|
|
|
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
|
|
|
/**
|
2014-02-13 14:21:25 +01:00
|
|
|
* Get Server Player Info
|
2013-11-10 20:09:08 +01:00
|
|
|
*
|
2014-02-15 12:53:33 +01:00
|
|
|
* @return \Maniaplanet\DedicatedServer\Structures\Player
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2014-02-13 14:21:25 +01:00
|
|
|
public function getInfo() {
|
|
|
|
return $this->maniaControl->client->getMainServerPlayerInfo();
|
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-08 20:11:48 +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-31 16:11:14 +01:00
|
|
|
if (is_int($parseValue)) {
|
2013-11-09 17:24:03 +01:00
|
|
|
$gameMode = $parseValue;
|
2014-01-08 20:11:48 +01:00
|
|
|
} else {
|
2014-02-13 14:21:25 +01:00
|
|
|
$gameMode = $this->maniaControl->client->getGameMode();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2014-01-31 16:11:14 +01:00
|
|
|
if ($stringValue) {
|
2014-01-08 20:11:48 +01:00
|
|
|
switch($gameMode) {
|
2013-11-09 17:24:03 +01:00
|
|
|
case 0:
|
2014-01-06 15:54:39 +01:00
|
|
|
return 'Script';
|
2013-11-09 17:24:03 +01:00
|
|
|
case 1:
|
2014-01-06 15:54:39 +01:00
|
|
|
return 'Rounds';
|
2013-11-09 17:24:03 +01:00
|
|
|
case 2:
|
2014-02-16 22:38:43 +01:00
|
|
|
return 'TimeAttack';
|
2013-11-09 17:24:03 +01:00
|
|
|
case 3:
|
2014-01-06 15:54:39 +01:00
|
|
|
return 'Team';
|
2013-11-09 17:24:03 +01:00
|
|
|
case 4:
|
2014-01-06 15:54:39 +01:00
|
|
|
return 'Laps';
|
2013-11-09 17:24:03 +01:00
|
|
|
case 5:
|
2014-01-06 15:54:39 +01:00
|
|
|
return 'Cup';
|
2013-11-09 17:24:03 +01:00
|
|
|
case 6:
|
2014-01-06 15:54:39 +01:00
|
|
|
return 'Stunts';
|
2013-11-09 17:24:03 +01:00
|
|
|
default:
|
2014-01-06 15:54:39 +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-18 21:46:42 +01:00
|
|
|
try {
|
|
|
|
$replay = $this->maniaControl->client->getValidationReplay($player->login);
|
2014-02-13 14:21:25 +01:00
|
|
|
} catch(Exception $e) {
|
2014-01-20 20:51:03 +01:00
|
|
|
trigger_error("Couldn't get validation replay of '{$player->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
|
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-31 16:11:14 +01:00
|
|
|
if (!$this->checkAccess($dataDir)) {
|
2014-01-08 20:11:48 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
// Build file name
|
2014-01-18 21:46:42 +01:00
|
|
|
$map = $this->maniaControl->mapManager->getCurrentMap();
|
2013-11-09 17:24:03 +01:00
|
|
|
$gameMode = $this->getGameMode();
|
2014-01-08 20:11:48 +01:00
|
|
|
$time = time();
|
2014-01-18 21:46:42 +01:00
|
|
|
$fileName = "GhostReplays/Ghost.{$player->login}.{$gameMode}.{$time}.{$map->uid}.Replay.Gbx";
|
2014-01-08 20:11:48 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
// Save ghost replay
|
2014-01-18 21:46:42 +01:00
|
|
|
try {
|
|
|
|
$this->maniaControl->client->saveBestGhostsReplay($player->login, $fileName);
|
2014-02-13 14:21:25 +01:00
|
|
|
} catch(Exception $e) {
|
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-01-08 20:11:48 +01: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}");
|
2014-01-31 16:11:14 +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) {
|
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
|
2014-01-31 16:11:14 +01:00
|
|
|
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-01-08 20:11:48 +01:00
|
|
|
$waitBegin = time();
|
2014-02-16 22:38:14 +01:00
|
|
|
$maxWaitTime = 30;
|
2014-02-15 21:40:42 +01:00
|
|
|
$lastStatus = $response->name;
|
2013-12-31 18:24:40 +01:00
|
|
|
$this->maniaControl->log("Waiting for server to reach status {$statusCode}...");
|
|
|
|
$this->maniaControl->log("Current Status: {$lastStatus}");
|
2014-01-16 17:31:15 +01: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
|
|
|
}
|
2014-01-31 16:11:14 +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;
|
|
|
|
}
|
|
|
|
}
|