improved server class

This commit is contained in:
Steffen Schröder 2013-11-10 20:09:08 +01:00
parent 3622266a36
commit a4a12298d4
4 changed files with 121 additions and 175 deletions

View File

@ -79,7 +79,7 @@ class Callbacks {
$this->triggerCallback(self::CB_MC_ONINIT, array(self::CB_MC_ONINIT)); $this->triggerCallback(self::CB_MC_ONINIT, array(self::CB_MC_ONINIT));
// Simulate begin map // Simulate begin map
$map = $this->maniaControl->server->getMap(); $map = $this->maniaControl->server->getCurrentMap();
if ($map) { if ($map) {
$this->triggerCallback(self::CB_MC_BEGINMAP, array(self::CB_MC_BEGINMAP, array($map))); $this->triggerCallback(self::CB_MC_BEGINMAP, array(self::CB_MC_BEGINMAP, array($map)));
} }

View File

@ -96,6 +96,8 @@ class ManiaControl {
/** /**
* Quit ManiaControl and log the given message * Quit ManiaControl and log the given message
*
* @param string $message
*/ */
public function quit($message = false) { public function quit($message = false) {
if ($this->client) { if ($this->client) {

View File

@ -12,11 +12,15 @@ class ManialinkUtil {
/** /**
* Send the given manialink to players * Send the given manialink to players
* *
* @param \IXR_ClientMulticall_Gbx $client
* @param string $manialink * @param string $manialink
* @param array $logins * @param array $logins
* @param int $timeout
* @param bool $hideOnClick
* @return bool * @return bool
*/ */
public static function sendManialinkPage($client, $manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) { public static function sendManialinkPage(\IXR_ClientMulticall_Gbx $client, $manialinkText, array $logins = null, $timeout = 0,
$hideOnClick = false) {
if (!$client || !$manialinkText) { if (!$client || !$manialinkText) {
return false; return false;
} }
@ -44,7 +48,7 @@ class ManialinkUtil {
* *
* @param string $name * @param string $name
* @param string $id * @param string $id
* @return SimpleXMLElement * @return \SimpleXMLElement
*/ */
public static function newManialinkXml($id = null) { public static function newManialinkXml($id = null) {
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><manialink/>'); $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><manialink/>');
@ -58,7 +62,7 @@ class ManialinkUtil {
/** /**
* Add alignment attributes to an xml element * Add alignment attributes to an xml element
* *
* @param SimpleXMLElement $xml * @param \SimpleXMLElement $xml
* @param string $halign * @param string $halign
* @param string $valign * @param string $valign
*/ */
@ -74,7 +78,7 @@ class ManialinkUtil {
/** /**
* Add translate attribute to an xml element * Add translate attribute to an xml element
* *
* @param SimpleXMLElement $xml * @param \SimpleXMLElement $xml
* @param bool $translate * @param bool $translate
*/ */
public static function addTranslate(\SimpleXMLElement $xml, $translate = true) { public static function addTranslate(\SimpleXMLElement $xml, $translate = true) {

View File

@ -5,7 +5,7 @@ namespace ManiaControl;
/** /**
* Class providing information and commands for the connected maniaplanet server * Class providing information and commands for the connected maniaplanet server
* *
* @author steeffeen * @author steeffeen & kremsy
*/ */
class Server { class Server {
/** /**
@ -13,160 +13,153 @@ class Server {
*/ */
const VALIDATIONREPLAYDIR = 'ValidationReplays/'; const VALIDATIONREPLAYDIR = 'ValidationReplays/';
const GHOSTREPLAYDIR = 'GhostReplays/'; const GHOSTREPLAYDIR = 'GhostReplays/';
/** /**
* Public properties * Public properties
*/ */
public $config = null; public $config = null;
/** /**
* Private properties * Private properties
*/ */
private $maniaConnect = null; private $maniaControl = null;
/** /**
* Construct server * Construct server
*
* @param ManiaControl $maniaControl
*/ */
public function __construct($maniaConnect) { public function __construct(ManiaControl $maniaControl) {
$this->maniaConnect = $maniaConnect; $this->maniaControl = $maniaControl;
// Load config // Load config
$this->config = FileUtil::loadConfig('server.xml'); $this->config = FileUtil::loadConfig('server.xml');
// Register for callbacks
$this->maniaConnect->callbacks->registerCallbackHandler(Callbacks::CB_MC_1_SECOND, $this, 'eachSecond');
} }
/** /**
* Perform actions every second * Fetch game data directory
*/
public function eachSecond() {
// Delete cached information
$this->players = null;
}
/**
* Fetch game directory of the server
* *
* @return string * @return string
*/ */
public function getDataDirectory() { public function getDataDirectory() {
if (!$this->maniaConnect->client->query('GameDataDirectory')) { if (!$this->maniaControl->client->query('GameDataDirectory')) {
trigger_error("Couldn't get data directory. " . $this->maniaConnect->getClientErrorText()); trigger_error("Couldn't get data directory. " . $this->maniaControl->getClientErrorText());
return null; return null;
} }
return $this->maniaConnect->client->getResponse(); return $this->maniaControl->client->getResponse();
} }
/** /**
* Checks if ManiaControl has access to the given directory (server data directory if no param) * Fetch maps directory
*
* @return string
*/
public function getMapsDirectory() {
$dataDirectory = $this->getDataDirectory();
if (!$dataDirectory) {
return null;
}
return $dataDirectory . 'UserData/Maps/';
}
/**
* Checks if ManiaControl has access to the given directory
* *
* @param string $directory * @param string $directory
* @return bool * @return bool
*/ */
public function checkAccess($directory = null) { public function checkAccess($directory) {
if (!$directory) { if (!$directory) {
$directory = $this->getDataDirectory(); return false;
} }
return is_dir($directory) && is_writable($directory); return (is_dir($directory) && is_writable($directory));
} }
/** /**
* Fetch server login * Fetch server login
*
* @return array
*/ */
public function getLogin($client = null) { public function getLogin() {
$systemInfo = $this->getSystemInfo(false, $client); $systemInfo = $this->getSystemInfo();
if (!$systemInfo) return null; if (!$systemInfo) {
return null;
}
return $systemInfo['ServerLogin']; return $systemInfo['ServerLogin'];
} }
/** /**
* Get detailed server info * Get server info
*
* @param bool $detailed
* @return array
*/ */
public function getInfo($detailed = false) { public function getInfo($detailed = false) {
if ($detailed) { if ($detailed) {
$login = $this->getLogin(); $login = $this->getLogin();
if (!$this->maniaConnect->client->query('GetDetailedPlayerInfo', $login)) { if (!$this->maniaControl->client->query('GetDetailedPlayerInfo', $login)) {
trigger_error("Couldn't fetch detailed server player info. " . $this->maniaConnect->getClientErrorText()); trigger_error("Couldn't fetch detailed server info. " . $this->maniaControl->getClientErrorText());
return null; return null;
} }
return $this->maniaControl->client->getResponse();
} }
else { if (!$this->maniaControl->client->query('GetMainServerPlayerInfo')) {
if (!$this->maniaConnect->client->query('GetMainServerPlayerInfo')) { trigger_error("Couldn't fetch server info. " . $this->maniaControl->getClientErrorText());
trigger_error("Couldn't fetch server player info. " . $this->maniaConnect->getClientErrorText()); return null;
return null;
}
} }
return $this->maniaConnect->client->getResponse(); return $this->maniaControl->client->getResponse();
} }
/** /**
* Get server options * Get server options
*
* @return array
*/ */
public function getOptions() { public function getOptions() {
if (!$this->maniaConnect->client->query('GetServerOptions')) { if (!$this->maniaControl->client->query('GetServerOptions')) {
trigger_error("Couldn't fetch server options. " . $this->maniaConnect->getClientErrorText()); trigger_error("Couldn't fetch server options. " . $this->maniaControl->getClientErrorText());
return null; return null;
} }
return $this->maniaConnect->client->getResponse(); return $this->maniaControl->client->getResponse();
} }
/** /**
* Fetch server name * Fetch server name
*
* @return string
*/ */
public function getName() { public function getName() {
if (!$this->maniaConnect->client->query('GetServerName')) { if (!$this->maniaControl->client->query('GetServerName')) {
trigger_error("Couldn't fetch server name. " . $this->maniaConnect->getClientErrorText()); trigger_error("Couldn't fetch server name. " . $this->maniaControl->getClientErrorText());
return null; return null;
} }
return $this->maniaConnect->client->getResponse(); return $this->maniaControl->client->getResponse();
} }
/** /**
* Fetch server version * Fetch server version
*
* @return string
*/ */
public function getVersion($forceRefresh = false) { public function getVersion() {
if (isset($this->maniaConnect->client->version) && !$forceRefresh) return $this->maniaConnect->client->version; if (!$this->maniaControl->client->query('GetVersion')) {
if (!$this->maniaConnect->client->query('GetVersion')) { trigger_error("Couldn't fetch server version. " . $this->maniaControl->getClientErrorText());
trigger_error("Couldn't fetch server version. " . $this->maniaConnect->getClientErrorText());
return null; return null;
} }
else { return $this->maniaControl->client->getResponse();
$this->maniaConnect->client->version = $this->maniaConnect->client->getResponse();
return $this->maniaConnect->client->version;
}
} }
/** /**
* Fetch server system info * Fetch server system info
*
* @return array
*/ */
public function getSystemInfo($forceRefresh = false, &$client = null) { public function getSystemInfo() {
if (!$this->maniaConnect->client && !$client) return null; if (!$this->maniaControl->client->query('GetSystemInfo')) {
if (!$client) $client = $this->maniaConnect->client; trigger_error("Couldn't fetch server system info. " . $this->maniaControl->getClientErrorText($client));
if (isset($client->systemInfo) && !$forceRefresh) return $client->systemInfo;
if (!$client->query('GetSystemInfo')) {
trigger_error("Couldn't fetch server system info. " . $this->maniaConnect->getClientErrorText($client));
return null; return null;
} }
else { return $this->maniaControl->client->getResponse();
$client->systemInfo = $client->getResponse();
return $client->systemInfo;
}
}
/**
* Fetch network status
*/
public function getNetworkStats($forceRefresh = false) {
if (isset($this->maniaConnect->client->networkStats) && !$forceRefresh) return $this->maniaConnect->client->networkStats;
if (!$this->maniaConnect->client->query('GetNetworkStats')) {
trigger_error("Couldn't fetch network stats. " . $this->maniaConnect->getClientErrorText());
return null;
}
else {
$this->maniaConnect->client->networkStats = $this->maniaConnect->client->getResponse();
return $this->maniaConnect->client->networkStats;
}
} }
/** /**
@ -181,11 +174,11 @@ class Server {
$gameMode = $parseValue; $gameMode = $parseValue;
} }
else { else {
if (!$this->maniaConnect->client->query('GetGameMode')) { if (!$this->maniaControl->client->query('GetGameMode')) {
trigger_error("Couldn't fetch current game mode. " . $this->maniaConnect->getClientErrorText()); trigger_error("Couldn't fetch current game mode. " . $this->maniaControl->getClientErrorText());
return null; return null;
} }
$gameMode = $this->maniaConnect->client->getResponse(); $gameMode = $this->maniaControl->client->getResponse();
} }
if ($stringValue) { if ($stringValue) {
switch ($gameMode) { switch ($gameMode) {
@ -225,86 +218,28 @@ class Server {
} }
return $gameMode; return $gameMode;
} }
// TODO: remove getPlayer / getPlayers -> methods now in playerHandler handeld, but should be improved more
/**
* Fetch player info
*
* @param string $login
* @return struct
*/
public function getPlayer($login, $detailed = false) {
if (!$login) return null;
$command = ($detailed ? 'GetDetailedPlayerInfo' : 'GetPlayerInfo');
if (!$this->maniaConnect->client->query($command, $login)) {
trigger_error("Couldn't player info for '" . $login . "'. " . $this->maniaConnect->getClientErrorText());
return null;
}
return $this->maniaConnect->client->getResponse();
}
/** /**
* Fetch all players * Retrieve validation replay for given player
*/
public function getPlayers(&$client = null, &$purePlayers = null, &$pureSpectators = null) {
if (!$this->maniaConnect->client && !$client) return null;
if (!$client) $client = $this->maniaConnect->client;
$fetchLength = 30;
$offset = 0;
$players = array();
if (!is_array($purePlayers)) $purePlayers = array();
if (!is_array($pureSpectators)) $pureSpectators = array();
$tries = 0;
while ($tries < 10) {
if (!$client->query('GetPlayerList', $fetchLength, $offset)) {
trigger_error("Couldn't get player list. " . $this->maniaConnect->getClientErrorText($client));
$tries++;
}
else {
$chunk = $client->getResponse();
$count = count($chunk);
$serverLogin = $this->getLogin($client);
for ($index = 0; $index < $count; $index++) {
$login = $chunk[$index]['Login'];
if ($login === $serverLogin) {
// Ignore server
unset($chunk[$index]);
}
else {
if ($chunk[$index]['SpectatorStatus'] > 0) {
// Pure spectator
array_push($pureSpectators, $chunk[$index]);
}
else {
// Pure player
array_push($purePlayers, $chunk[$index]);
}
}
}
$players = array_merge($players, $chunk);
$offset += $count;
if ($count < $fetchLength) break;
}
}
return $players;
}
/**
* Retrieve validation replay for given login
* *
* @param string $login * @param Player $player
* @return string * @return string
*/ */
public function getValidationReplay($login) { public function getValidationReplay(Player $player) {
if (!$login) return null; if (!$this->maniaControl->client->query('GetValidationReplay', $player->login)) {
if (!$this->maniaConnect->client->query('GetValidationReplay', $login)) { trigger_error("Couldn't get validation replay of '{$player->login}'. " . $this->maniaControl->getClientErrorText());
trigger_error("Couldn't get validation replay of '" . $login . "'. " . $this->maniaConnect->getClientErrorText());
return null; return null;
} }
return $this->maniaConnect->client->getResponse(); return $this->maniaControl->client->getResponse();
} }
public function getGhostReplay($login) { /**
* Retrieve ghost replay for the given player
*
* @param Player $player
* @return string
*/
public function getGhostReplay(Player $player) {
$dataDir = $this->getDataDirectory(); $dataDir = $this->getDataDirectory();
if (!$this->checkAccess($dataDir)) { if (!$this->checkAccess($dataDir)) {
return null; return null;
@ -314,11 +249,11 @@ class Server {
$map = $this->getMap(); $map = $this->getMap();
$gameMode = $this->getGameMode(); $gameMode = $this->getGameMode();
$time = time(); $time = time();
$fileName = 'Ghost.' . $login . '.' . $gameMode . '.' . $time . '.' . $map['UId'] . '.Replay.Gbx'; $fileName = "Ghost.{$login}.{$gameMode}.{$time}.{$map['UId']}.Replay.Gbx";
// Save ghost replay // Save ghost replay
if (!$this->maniaConnect->client->query('SaveBestGhostsReplay', $login, self::GHOSTREPLAYDIR . $fileName)) { if (!$this->maniaControl->client->query('SaveBestGhostsReplay', $player->login, self::GHOSTREPLAYDIR . $fileName)) {
trigger_error("Couldn't save ghost replay. " . $this->maniaConnect->getClientErrorText()); trigger_error("Couldn't save ghost replay. " . $this->maniaControl->getClientErrorText());
return null; return null;
} }
@ -331,37 +266,42 @@ class Server {
return $ghostReplay; return $ghostReplay;
} }
//TODO: remove getMap
/** /**
* Fetch current map * Fetch current map
*
* @return array
*/ */
public function getMap() { public function getCurrentMap() {
if (!$this->maniaConnect->client) return null; if (!$this->maniaControl->client->query('GetCurrentMapInfo')) {
if (!$this->maniaConnect->client->query('GetCurrentMapInfo')) { trigger_error("Couldn't fetch map info. " . $this->maniaControl->getClientErrorText());
trigger_error("Couldn't fetch map info. " . $this->maniaConnect->getClientErrorText());
return null; return null;
} }
return $this->maniaConnect->client->getResponse(); return $this->maniaControl->client->getResponse();
} }
/** /**
* Waits for the server to have the given status * Waits for the server to have the given status
*
* @param int $statusCode
* @return bool
*/ */
public function waitForStatus($client, $statusCode = 4) { public function waitForStatus($statusCode = 4) {
$client->query('GetStatus'); $this->maniaControl->client->query('GetStatus');
$response = $client->getResponse(); $response = $this->maniaControl->client->getResponse();
// Check if server reached given status // Check if server has the given status
if ($response['Code'] === 4) return true; if ($response['Code'] === 4) {
return true;
}
// Server not yet in given status -> Wait for it... // Server not yet in given status -> Wait for it...
$waitBegin = time(); $waitBegin = time();
$maxWaitTime = 20; $maxWaitTime = 20;
$lastStatus = $response['Name']; $lastStatus = $response['Name'];
error_log("Waiting for server to reach status " . $statusCode . "..."); error_log("Waiting for server to reach status {$statusCode}...");
error_log("Current Status: " . $lastStatus); error_log("Current Status: {$lastStatus}");
while ($response['Code'] !== 4) { while ($response['Code'] !== 4) {
sleep(1); sleep(1);
$client->query('GetStatus'); $this->maniaControl->client->query('GetStatus');
$response = $client->getResponse(); $response = $this->maniaControl->client->getResponse();
if ($lastStatus !== $response['Name']) { if ($lastStatus !== $response['Name']) {
error_log("New Status: " . $response['Name']); error_log("New Status: " . $response['Name']);
$lastStatus = $response['Name']; $lastStatus = $response['Name'];
@ -369,8 +309,8 @@ class Server {
if (time() - $maxWaitTime > $waitBegin) { if (time() - $maxWaitTime > $waitBegin) {
// It took too long to reach the status // It took too long to reach the status
trigger_error( trigger_error(
"Server couldn't reach status " . $statusCode . " after " . $maxWaitTime . " seconds! " . "Server couldn't reach status {$statusCode} after {$maxWaitTime} seconds! " .
$this->maniaConnect->getClientErrorText()); $this->maniaControl->getClientErrorText());
return false; return false;
} }
} }