server properties

This commit is contained in:
Steffen Schröder 2014-01-06 15:54:39 +01:00
parent be0f0e409b
commit c3a2cd6122
2 changed files with 66 additions and 98 deletions

View File

@ -308,8 +308,7 @@ class PlayerManager implements CallbackListener {
$playerStatement->close(); $playerStatement->close();
// Increment the Player Join Count // Increment the Player Join Count
$serverIndex = $this->maniaControl->server->getIndex(); $this->maniaControl->statisticManager->incrementStat(self::STAT_JOIN_COUNT, $player, $this->maniaControl->server->index);
$this->maniaControl->statisticManager->incrementStat(self::STAT_JOIN_COUNT, $player, $serverIndex);
return true; return true;
} }

View File

@ -22,16 +22,24 @@ class Server implements CallbackListener {
*/ */
const TABLE_SERVERS = 'mc_servers'; const TABLE_SERVERS = 'mc_servers';
/**
* Public Properties
*/
public $index = -1;
public $ip = null;
public $port = -1;
public $p2pPort = -1;
public $login = null;
public $titleId = null;
public $serverCommands = null;
/** /**
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $serverCommands = null;
private $index = null;
private $login = null;
/** /**
* Construct server * Construct a new Server
* *
* @param ManiaControl $maniaControl * @param ManiaControl $maniaControl
*/ */
@ -39,10 +47,46 @@ class Server implements CallbackListener {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->initTables(); $this->initTables();
$this->serverCommands = new ServerCommands($maniaControl);
// Register for callbacks // Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
}
$this->serverCommands = new ServerCommands($maniaControl); /**
* Refetch the Server Properties
*/
private function updateProperties() {
// System info
$systemInfo = $this->getSystemInfo();
$this->ip = $systemInfo['PublishedIp'];
$this->port = $systemInfo['Port'];
$this->p2pPort = $systemInfo['P2PPort'];
$this->login = $systemInfo['ServerLogin'];
$this->titleId = $systemInfo['TitleId'];
// Database index
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT INTO `" . self::TABLE_SERVERS . "` (
`login`
) VALUES (
?
) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`);";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return;
}
$statement->bind_param('s', $this->login);
$statement->execute();
if ($statement->error) {
trigger_error($statement->error);
$statement->close();
return;
}
$this->index = $statement->insert_id;
$statement->close();
} }
/** /**
@ -78,27 +122,7 @@ class Server implements CallbackListener {
* @param array $callback * @param array $callback
*/ */
public function onInit(array $callback) { public function onInit(array $callback) {
$mysqli = $this->maniaControl->database->mysqli; $this->updateProperties();
$query = "INSERT IGNORE INTO `" . self::TABLE_SERVERS . "` (
`login`
) VALUES (
?
);";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return;
}
$login = $this->getLogin();
$statement->bind_param('s', $login);
$statement->execute();
if ($statement->error) {
trigger_error($statement->error);
$statement->close();
return;
}
$statement->close();
return true;
} }
/** /**
@ -121,10 +145,8 @@ class Server implements CallbackListener {
*/ */
public function getMapsDirectory() { public function getMapsDirectory() {
$dataDirectory = $this->getDataDirectory(); $dataDirectory = $this->getDataDirectory();
if (!$dataDirectory) { if (!$dataDirectory) return null;
return null; return "{$dataDirectory}Maps/";
}
return $dataDirectory . 'Maps/';
} }
/** /**
@ -138,41 +160,6 @@ class Server implements CallbackListener {
return (is_dir($directory) && is_writable($directory)); return (is_dir($directory) && is_writable($directory));
} }
/**
* Fetch Server Index
*
* @return int
*/
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;
}
$row = $result->fetch_object();
$result->close();
$this->index = $row->index;
return $this->index;
}
/**
* 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 * Get the Server Info
* *
@ -241,7 +228,7 @@ class Server implements CallbackListener {
*/ */
public function getSystemInfo() { public function getSystemInfo() {
if (!$this->maniaControl->client->query('GetSystemInfo')) { if (!$this->maniaControl->client->query('GetSystemInfo')) {
trigger_error("Couldn't fetch server system info. " . $this->maniaControl->getClientErrorText($this->maniaControl->client)); trigger_error("Couldn't fetch server system info. " . $this->maniaControl->getClientErrorText());
return null; return null;
} }
return $this->maniaControl->client->getResponse(); return $this->maniaControl->client->getResponse();
@ -268,39 +255,23 @@ class Server implements CallbackListener {
if ($stringValue) { if ($stringValue) {
switch ($gameMode) { switch ($gameMode) {
case 0: case 0:
{
return 'Script'; return 'Script';
}
case 1: case 1:
{
return 'Rounds'; return 'Rounds';
}
case 2: case 2:
{
return 'TimeAttack'; return 'TimeAttack';
}
case 3: case 3:
{
return 'Team'; return 'Team';
}
case 4: case 4:
{
return 'Laps'; return 'Laps';
}
case 5: case 5:
{
return 'Cup'; return 'Cup';
}
case 6: case 6:
{
return 'Stunts'; return 'Stunts';
}
default: default:
{
return 'Unknown'; return 'Unknown';
} }
} }
}
return $gameMode; return $gameMode;
} }
@ -341,7 +312,7 @@ class Server implements CallbackListener {
} }
// Load replay file // Load replay file
$ghostReplay = file_get_contents($dataDir . 'Replays/' . $fileName); $ghostReplay = file_get_contents("{$dataDir}Replays/{$fileName}");
if (!$ghostReplay) { if (!$ghostReplay) {
trigger_error("Couldn't retrieve saved ghost replay."); trigger_error("Couldn't retrieve saved ghost replay.");
return null; return null;
@ -359,10 +330,8 @@ class Server implements CallbackListener {
$this->maniaControl->client->query('GetStatus'); $this->maniaControl->client->query('GetStatus');
$response = $this->maniaControl->client->getResponse(); $response = $this->maniaControl->client->getResponse();
// Check if server has the given status // Check if server has the given status
if ($response['Code'] === 4) { if ($response['Code'] === 4) return true;
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'];
@ -373,7 +342,7 @@ class Server implements CallbackListener {
$this->maniaControl->client->query('GetStatus'); $this->maniaControl->client->query('GetStatus');
$response = $this->maniaControl->client->getResponse(); $response = $this->maniaControl->client->getResponse();
if ($lastStatus !== $response['Name']) { if ($lastStatus !== $response['Name']) {
$this->maniaControl->log("New Status: " . $response['Name']); $this->maniaControl->log("New Status: {$response['Name']}");
$lastStatus = $response['Name']; $lastStatus = $response['Name'];
} }
if (time() - $maxWaitTime > $waitBegin) { if (time() - $maxWaitTime > $waitBegin) {