2013-11-09 17:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class providing information and commands for the connected maniaplanet server
|
|
|
|
*
|
|
|
|
* @author steeffeen
|
|
|
|
*/
|
|
|
|
class Server {
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const VALIDATIONREPLAYDIR = 'ValidationReplays/';
|
|
|
|
const GHOSTREPLAYDIR = 'GhostReplays/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public properties
|
|
|
|
*/
|
|
|
|
public $config = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
2013-11-09 19:26:57 +01:00
|
|
|
private $mc = null;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct server
|
|
|
|
*/
|
2013-11-09 19:26:57 +01:00
|
|
|
public function __construct($mc) {
|
|
|
|
$this->mc = $mc;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
// Load config
|
|
|
|
$this->config = Tools::loadConfig('server.ManiaControl.xml');
|
2013-11-09 19:26:57 +01:00
|
|
|
$this->mc->checkConfig($this->config, array('host', 'port', 'login', 'pass'), 'server');
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
// Register for callbacks
|
2013-11-09 19:26:57 +01:00
|
|
|
$this->mc->callbacks->registerCallbackHandler(Callbacks::CB_IC_1_SECOND, $this, 'eachSecond');
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform actions every second
|
|
|
|
*/
|
|
|
|
public function eachSecond() {
|
|
|
|
// Delete cached information
|
|
|
|
$this->players = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch game directory of the server
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDataDirectory() {
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client->query('GameDataDirectory')) {
|
|
|
|
trigger_error("Couldn't get data directory. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-09 19:26:57 +01:00
|
|
|
return $this->mc->client->getResponse();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if ManiaControl has access to the given directory (server data directory if no param)
|
|
|
|
*
|
|
|
|
* @param string $directory
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkAccess($directory = null) {
|
|
|
|
if (!$directory) {
|
|
|
|
$directory = $this->getDataDirectory();
|
|
|
|
}
|
|
|
|
return is_dir($directory) && is_writable($directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch server login
|
|
|
|
*/
|
|
|
|
public function getLogin($client = null) {
|
|
|
|
$systemInfo = $this->getSystemInfo(false, $client);
|
|
|
|
if (!$systemInfo) return null;
|
|
|
|
return $systemInfo['ServerLogin'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get detailed server info
|
|
|
|
*/
|
|
|
|
public function getInfo($detailed = false) {
|
|
|
|
if ($detailed) {
|
|
|
|
$login = $this->getLogin();
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client->query('GetDetailedPlayerInfo', $login)) {
|
|
|
|
trigger_error("Couldn't fetch detailed server player info. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client->query('GetMainServerPlayerInfo')) {
|
|
|
|
trigger_error("Couldn't fetch server player info. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2013-11-09 19:26:57 +01:00
|
|
|
return $this->mc->client->getResponse();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get server options
|
|
|
|
*/
|
|
|
|
public function getOptions() {
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client->query('GetServerOptions')) {
|
|
|
|
trigger_error("Couldn't fetch server options. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-09 19:26:57 +01:00
|
|
|
return $this->mc->client->getResponse();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch server name
|
|
|
|
*/
|
|
|
|
public function getName() {
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client->query('GetServerName')) {
|
|
|
|
trigger_error("Couldn't fetch server name. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-09 19:26:57 +01:00
|
|
|
return $this->mc->client->getResponse();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch server version
|
|
|
|
*/
|
|
|
|
public function getVersion($forceRefresh = false) {
|
2013-11-09 19:26:57 +01:00
|
|
|
if (isset($this->mc->client->version) && !$forceRefresh) return $this->mc->client->version;
|
|
|
|
if (!$this->mc->client->query('GetVersion')) {
|
|
|
|
trigger_error("Couldn't fetch server version. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
2013-11-09 19:26:57 +01:00
|
|
|
$this->mc->client->version = $this->mc->client->getResponse();
|
|
|
|
return $this->mc->client->version;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch server system info
|
|
|
|
*/
|
|
|
|
public function getSystemInfo($forceRefresh = false, &$client = null) {
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client && !$client) return null;
|
|
|
|
if (!$client) $client = $this->mc->client;
|
2013-11-09 17:24:03 +01:00
|
|
|
if (isset($client->systemInfo) && !$forceRefresh) return $client->systemInfo;
|
|
|
|
if (!$client->query('GetSystemInfo')) {
|
2013-11-09 19:26:57 +01:00
|
|
|
trigger_error("Couldn't fetch server system info. " . $this->mc->getClientErrorText($client));
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$client->systemInfo = $client->getResponse();
|
|
|
|
return $client->systemInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch network status
|
|
|
|
*/
|
|
|
|
public function getNetworkStats($forceRefresh = false) {
|
2013-11-09 19:26:57 +01:00
|
|
|
if (isset($this->mc->client->networkStats) && !$forceRefresh) return $this->mc->client->networkStats;
|
|
|
|
if (!$this->mc->client->query('GetNetworkStats')) {
|
|
|
|
trigger_error("Couldn't fetch network stats. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
2013-11-09 19:26:57 +01:00
|
|
|
$this->mc->client->networkStats = $this->mc->client->getResponse();
|
|
|
|
return $this->mc->client->networkStats;
|
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-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client->query('GetGameMode')) {
|
|
|
|
trigger_error("Couldn't fetch current game mode. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-09 19:26:57 +01:00
|
|
|
$gameMode = $this->mc->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-09 23:01:54 +01:00
|
|
|
//TODO: remove getPlayer / getPlayers -> methods now in playerHandler handeld, but should be improved more
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
|
|
|
* Fetch player info
|
|
|
|
*
|
|
|
|
* @param string $login
|
|
|
|
* @return struct
|
|
|
|
*/
|
|
|
|
public function getPlayer($login, $detailed = false) {
|
|
|
|
if (!$login) return null;
|
|
|
|
$command = ($detailed ? 'GetDetailedPlayerInfo' : 'GetPlayerInfo');
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client->query($command, $login)) {
|
|
|
|
trigger_error("Couldn't player info for '" . $login . "'. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-09 19:26:57 +01:00
|
|
|
return $this->mc->client->getResponse();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all players
|
|
|
|
*/
|
|
|
|
public function getPlayers(&$client = null, &$purePlayers = null, &$pureSpectators = null) {
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client && !$client) return null;
|
|
|
|
if (!$client) $client = $this->mc->client;
|
2013-11-09 17:24:03 +01:00
|
|
|
$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)) {
|
2013-11-09 19:26:57 +01:00
|
|
|
trigger_error("Couldn't get player list. " . $this->mc->getClientErrorText($client));
|
2013-11-09 17:24:03 +01:00
|
|
|
$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
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getValidationReplay($login) {
|
|
|
|
if (!$login) return null;
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client->query('GetValidationReplay', $login)) {
|
|
|
|
trigger_error("Couldn't get validation replay of '" . $login . "'. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-09 19:26:57 +01:00
|
|
|
return $this->mc->client->getResponse();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getGhostReplay($login) {
|
|
|
|
$dataDir = $this->getDataDirectory();
|
|
|
|
if (!$this->checkAccess($dataDir)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build file name
|
|
|
|
$map = $this->getMap();
|
|
|
|
$gameMode = $this->getGameMode();
|
|
|
|
$time = time();
|
|
|
|
$fileName = 'Ghost.' . $login . '.' . $gameMode . '.' . $time . '.' . $map['UId'] . '.Replay.Gbx';
|
|
|
|
|
|
|
|
// Save ghost replay
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client->query('SaveBestGhostsReplay', $login, self::GHOSTREPLAYDIR . $fileName)) {
|
|
|
|
trigger_error("Couldn't save ghost replay. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load replay file
|
|
|
|
$ghostReplay = file_get_contents($dataDir . 'Replays/' . self::GHOSTREPLAYDIR . $fileName);
|
|
|
|
if (!$ghostReplay) {
|
|
|
|
trigger_error("Couldn't retrieve saved ghost replay.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $ghostReplay;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch current map
|
|
|
|
*/
|
|
|
|
public function getMap() {
|
2013-11-09 19:26:57 +01:00
|
|
|
if (!$this->mc->client) return null;
|
|
|
|
if (!$this->mc->client->query('GetCurrentMapInfo')) {
|
|
|
|
trigger_error("Couldn't fetch map info. " . $this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-09 19:26:57 +01:00
|
|
|
return $this->mc->client->getResponse();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for the server to have the given status
|
|
|
|
*/
|
|
|
|
public function waitForStatus($client, $statusCode = 4) {
|
|
|
|
$client->query('GetStatus');
|
|
|
|
$response = $client->getResponse();
|
|
|
|
// Check if server reached given status
|
|
|
|
if ($response['Code'] === 4) return true;
|
|
|
|
// Server not yet in given status -> Wait for it...
|
|
|
|
$waitBegin = time();
|
2013-11-09 19:26:57 +01:00
|
|
|
$timeoutTags = $this->mc->config->xpath('timeout');
|
2013-11-09 17:24:03 +01:00
|
|
|
$maxWaitTime = (!empty($timeoutTags) ? (int) $timeoutTags[0] : 20);
|
|
|
|
$lastStatus = $response['Name'];
|
|
|
|
error_log("Waiting for server to reach status " . $statusCode . "...");
|
|
|
|
error_log("Current Status: " . $lastStatus);
|
|
|
|
while ($response['Code'] !== 4) {
|
|
|
|
sleep(1);
|
|
|
|
$client->query('GetStatus');
|
|
|
|
$response = $client->getResponse();
|
|
|
|
if ($lastStatus !== $response['Name']) {
|
|
|
|
error_log("New Status: " . $response['Name']);
|
|
|
|
$lastStatus = $response['Name'];
|
|
|
|
}
|
|
|
|
if (time() - $maxWaitTime > $waitBegin) {
|
|
|
|
// It took too long to reach the status
|
|
|
|
trigger_error(
|
|
|
|
"Server couldn't reach status " . $statusCode . " after " . $maxWaitTime . " seconds! " .
|
2013-11-09 19:26:57 +01:00
|
|
|
$this->mc->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|