TrackManiaControl/application/core/Server/Server.php

311 lines
7.3 KiB
PHP
Raw Normal View History

2013-11-09 17:24:03 +01:00
<?php
namespace ManiaControl\Server;
use ManiaControl\FileUtil;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
require_once __DIR__ . '/ServerCommands.php';
2013-11-09 17:24:03 +01:00
/**
* Class providing information and commands for the connected maniaplanet server
*
2013-11-10 20:09:08 +01:00
* @author steeffeen & kremsy
2013-11-09 17:24:03 +01:00
*/
class Server {
2013-11-10 20:09:08 +01:00
2013-11-09 17:24:03 +01:00
/**
* Public properties
*/
public $config = null;
2013-11-10 20:09:08 +01:00
2013-11-09 17:24:03 +01:00
/**
* Private properties
*/
2013-11-10 20:09:08 +01:00
private $maniaControl = null;
private $serverCommands = null;
2013-11-09 17:24:03 +01:00
/**
* Construct server
2013-11-10 20:09:08 +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;
2013-11-09 17:24:03 +01:00
// Load config
$this->config = FileUtil::loadConfig('server.xml');
$this->serverCommands = new ServerCommands($maniaControl);
2013-11-09 17:24:03 +01:00
}
/**
2013-11-10 20:09:08 +01:00
* Fetch game data directory
*
* @return string
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
public function getDataDirectory() {
if (!$this->maniaControl->client->query('GameDataDirectory')) {
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
}
/**
2013-11-10 20:09:08 +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();
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
}
/**
2013-11-10 20:09:08 +01:00
* Checks if ManiaControl has access to the given directory
2013-11-09 17:24:03 +01:00
*
* @param string $directory
* @return bool
*/
2013-11-10 20:09:08 +01:00
public function checkAccess($directory) {
2013-11-09 17:24:03 +01:00
if (!$directory) {
2013-11-10 20:09:08 +01:00
return false;
2013-11-09 17:24:03 +01:00
}
2013-11-10 20:09:08 +01:00
return (is_dir($directory) && is_writable($directory));
2013-11-09 17:24:03 +01:00
}
/**
* Fetch server login
2013-11-10 20:09:08 +01:00
*
* @return array
2013-11-09 17:24:03 +01:00
*/
public function getLogin() { //TODO save the info locally
2013-11-10 20:09:08 +01:00
$systemInfo = $this->getSystemInfo();
if (!$systemInfo) {
return null;
}
2013-11-09 17:24:03 +01:00
return $systemInfo['ServerLogin'];
}
/**
2013-11-10 20:09:08 +01:00
* Get server info
*
* @param bool $detailed
* @return array
2013-11-09 17:24:03 +01:00
*/
public function getInfo($detailed = false) {
if ($detailed) {
$login = $this->getLogin();
2013-11-10 20:09:08 +01:00
if (!$this->maniaControl->client->query('GetDetailedPlayerInfo', $login)) {
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
}
2013-11-10 20:09:08 +01:00
if (!$this->maniaControl->client->query('GetMainServerPlayerInfo')) {
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
}
/**
* Get server options
2013-11-10 20:09:08 +01:00
*
* @return array
2013-11-09 17:24:03 +01:00
*/
public function getOptions() {
2013-11-10 20:09:08 +01:00
if (!$this->maniaControl->client->query('GetServerOptions')) {
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
}
/**
* Fetch server name
2013-11-10 20:09:08 +01:00
*
* @return string
2013-11-09 17:24:03 +01:00
*/
public function getName() {
2013-11-10 20:09:08 +01:00
if (!$this->maniaControl->client->query('GetServerName')) {
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
}
/**
* 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() {
if (!$this->maniaControl->client->query('GetVersion')) {
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
}
/**
* 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() {
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
}
/**
* Fetch 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 {
2013-11-10 20:09:08 +01:00
if (!$this->maniaControl->client->query('GetGameMode')) {
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
}
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-10 20:09:08 +01:00
2013-11-09 17:24:03 +01:00
/**
2013-11-10 20:09:08 +01:00
* Retrieve validation replay for given player
2013-11-09 17:24:03 +01:00
*
2013-11-10 20:09:08 +01:00
* @param Player $player
* @return string
2013-11-09 17:24:03 +01:00
*/
2013-11-10 20:09:08 +01:00
public function getValidationReplay(Player $player) {
if (!$this->maniaControl->client->query('GetValidationReplay', $player->login)) {
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
}
/**
2013-11-10 20:09:08 +01:00
* Retrieve ghost replay for the given player
2013-11-09 17:24:03 +01:00
*
2013-11-10 20:09:08 +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();
if (!$this->checkAccess($dataDir)) {
return null;
}
// Build file name
$map = $this->getMap();
$gameMode = $this->getGameMode();
$time = time();
$fileName = "GhostReplays/Ghost.{$player->login}.{$gameMode}.{$time}.{$map['UId']}.Replay.Gbx";
2013-11-09 17:24:03 +01:00
// Save ghost replay
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;
}
// Load replay file
$ghostReplay = file_get_contents($dataDir . 'Replays/' . $fileName);
2013-11-09 17:24:03 +01:00
if (!$ghostReplay) {
trigger_error("Couldn't retrieve saved ghost replay.");
return null;
}
return $ghostReplay;
}
/**
* Waits for the server to have the given status
2013-11-10 20:09:08 +01:00
*
* @param int $statusCode
* @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
if ($response['Code'] === 4) {
return true;
}
2013-11-09 17:24:03 +01:00
// Server not yet in given status -> Wait for it...
$waitBegin = time();
$maxWaitTime = 20;
2013-11-09 17:24:03 +01:00
$lastStatus = $response['Name'];
$this->maniaControl->log("Waiting for server to reach status {$statusCode}...");
$this->maniaControl->log("Current Status: {$lastStatus}");
2013-11-09 17:24:03 +01:00
while ($response['Code'] !== 4) {
sleep(1);
2013-11-10 20:09:08 +01:00
$this->maniaControl->client->query('GetStatus');
$response = $this->maniaControl->client->getResponse();
2013-11-09 17:24:03 +01:00
if ($lastStatus !== $response['Name']) {
$this->maniaControl->log("New Status: " . $response['Name']);
2013-11-09 17:24:03 +01:00
$lastStatus = $response['Name'];
}
if (time() - $maxWaitTime > $waitBegin) {
// It took too long to reach the status
trigger_error(
2013-11-10 20:09:08 +01:00
"Server couldn't reach status {$statusCode} after {$maxWaitTime} seconds! " .
$this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return false;
}
}
return true;
}
}