- refactored many try-catch clauses

- added todos for validating catching
This commit is contained in:
Steffen Schröder
2014-02-13 14:21:25 +01:00
parent 10dfd6b0cb
commit 4197dc82ff
23 changed files with 213 additions and 293 deletions

View File

@ -7,6 +7,7 @@ use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Structures\SystemInfos;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
/**
* Class providing Information about theconnected ManiaPlanet Server
@ -162,12 +163,7 @@ class Server implements CallbackListener {
*/
public function getDataDirectory() {
if ($this->dataDirectory == '') {
try {
$this->dataDirectory = $this->maniaControl->client->gameDataDirectory();
} catch(\Exception $e) {
trigger_error("Couldn't get data directory. " . $e->getMessage());
return null;
}
$this->dataDirectory = $this->maniaControl->client->gameDataDirectory();
}
return $this->dataDirectory;
}
@ -199,29 +195,12 @@ class Server implements CallbackListener {
}
/**
* Get the Server Info
* Get Server Player Info
*
* @param bool $detailed
* @return array
*/
public function getInfo($detailed = false) {
if ($detailed) {
$login = $this->login;
try {
$info = $this->maniaControl->client->getDetailedPlayerInfo($login);
} catch(\Exception $e) {
trigger_error("Couldn't fetch detailed server info. " . $e->getMessage());
return null;
}
return $info;
}
try {
$info = $this->maniaControl->client->getMainServerPlayerInfo();
} catch(\Exception $e) {
trigger_error("Couldn't fetch server info. " . $e->getMessage());
return null;
}
return $info;
public function getInfo() {
return $this->maniaControl->client->getMainServerPlayerInfo();
}
/**
@ -230,14 +209,7 @@ class Server implements CallbackListener {
* @return array
*/
public function getOptions() {
try {
$options = $this->maniaControl->client->getServerOptions();
} catch(\Exception $e) {
trigger_error("Couldn't fetch server options. " . $e->getMessage());
return null;
}
return $options;
return $this->maniaControl->client->getServerOptions();
}
/**
@ -246,13 +218,7 @@ class Server implements CallbackListener {
* @return string
*/
public function getName() {
try {
$name = $this->maniaControl->client->getServerName();
} catch(\Exception $e) {
trigger_error("Couldn't fetch server name. " . $e->getMessage());
return null;
}
return $name;
return $this->maniaControl->client->getServerName();
}
@ -262,13 +228,7 @@ class Server implements CallbackListener {
* @return string
*/
public function getVersion() {
try {
$version = $this->maniaControl->client->getVersion();
} catch(\Exception $e) {
trigger_error("Couldn't fetch server version. " . $e->getMessage());
return null;
}
return $version;
return $this->maniaControl->client->getVersion();
}
/**
@ -277,14 +237,7 @@ class Server implements CallbackListener {
* @return SystemInfos
*/
public function getSystemInfo() {
try {
$systemInfo = $this->maniaControl->client->getSystemInfo();
} catch(\Exception $e) {
trigger_error("Couldn't fetch server system info. " . $e->getMessage());
return null;
}
return $systemInfo;
return $this->maniaControl->client->getSystemInfo();
}
/**
@ -298,12 +251,7 @@ class Server implements CallbackListener {
if (is_int($parseValue)) {
$gameMode = $parseValue;
} else {
try {
$gameMode = $this->maniaControl->client->getGameMode();
} catch(\Exception $e) {
trigger_error("Couldn't fetch current game mode. " . $e->getMessage());
return null;
}
$gameMode = $this->maniaControl->client->getGameMode();
}
if ($stringValue) {
switch($gameMode) {
@ -337,7 +285,7 @@ class Server implements CallbackListener {
public function getValidationReplay(Player $player) {
try {
$replay = $this->maniaControl->client->getValidationReplay($player->login);
} catch(\Exception $e) {
} catch(Exception $e) {
trigger_error("Couldn't get validation replay of '{$player->login}'. " . $e->getMessage());
return null;
}
@ -365,7 +313,7 @@ class Server implements CallbackListener {
// Save ghost replay
try {
$this->maniaControl->client->saveBestGhostsReplay($player->login, $fileName);
} catch(\Exception $e) {
} catch(Exception $e) {
trigger_error("Couldn't save ghost replay. " . $e->getMessage());
return null;
}

View File

@ -13,6 +13,7 @@ use ManiaControl\Commands\CommandListener;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
/**
* Class offering various commands related to the dedicated server
@ -105,8 +106,11 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
//Check if Pause exists in current GameMode
try {
$scriptInfos = $this->maniaControl->client->getModeScriptInfo();
} catch(\Exception $e) {
return;
} catch(Exception $e) {
if ($e->getMessage() == 'Not in script mode.') {
return;
}
throw $e;
}
$pauseExists = false;
foreach($scriptInfos->commandDescs as $param) {
@ -160,9 +164,12 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
try {
$this->maniaControl->client->triggerModeScriptEvent('WarmUp_Extend', '10');
} catch(\Exception $e) {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
} catch(Exception $e) {
if ($e->getMessage() == 'Not in script mode.') {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
}
throw $e;
}
$this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> extended the WarmUp by 10 seconds!');
@ -182,9 +189,12 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
try {
$this->maniaControl->client->triggerModeScriptEvent('WarmUp_Stop', '');
} catch(\Exception $e) {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
} catch(Exception $e) {
if ($e->getMessage() == 'Not in script mode.') {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
}
throw $e;
}
$this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> stopped the WarmUp!');
@ -202,9 +212,12 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
}
try {
$this->maniaControl->client->sendModeScriptCommands(array('Command_ForceWarmUp' => True));
} catch(\Exception $e) {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
} catch(Exception $e) {
if ($e->getMessage() == 'Not in script mode.') {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
}
throw $e;
}
$this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> paused the Game!');
@ -305,12 +318,7 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
return;
}
$serverName = $params[1];
try {
$this->maniaControl->client->setServerName($serverName);
} catch(\Exception $e) {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
}
$this->maniaControl->client->setServerName($serverName);
$this->maniaControl->chat->sendSuccess("Server name changed to: '{$serverName}'!", $player->login);
}
@ -332,12 +340,7 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
$password = $messageParts[1];
$successMessage = "Password changed to: '{$password}'!";
}
try {
$this->maniaControl->client->setServerPassword($password);
} catch(\Exception $e) {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
}
$this->maniaControl->client->setServerPassword($password);
$this->maniaControl->chat->sendSuccess($successMessage, $player->login);
}
@ -359,12 +362,7 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
$password = $messageParts[1];
$successMessage = "Spectator password changed to: '{$password}'!";
}
try {
$this->maniaControl->client->setServerPasswordForSpectator($password);
} catch(\Exception $e) {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
}
$this->maniaControl->client->setServerPasswordForSpectator($password);
$this->maniaControl->chat->sendSuccess($successMessage, $player->login);
}
@ -394,13 +392,7 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
$amount = 0;
}
try {
$this->maniaControl->client->setMaxPlayers($amount);
} catch(\Exception $e) {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
}
$this->maniaControl->client->setMaxPlayers($amount);
$this->maniaControl->chat->sendSuccess("Changed max players to: {$amount}", $player->login);
}
@ -430,12 +422,7 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
$amount = 0;
}
try {
$this->maniaControl->client->setMaxSpectators($amount);
} catch(\Exception $e) {
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
return;
}
$this->maniaControl->client->setMaxSpectators($amount);
$this->maniaControl->chat->sendSuccess("Changed max spectators to: {$amount}", $player->login);
}
@ -443,16 +430,9 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage
* Perform server shutdown
*
* @param string $login
* @return bool
*/
private function shutdownServer($login = '#') {
try {
$this->maniaControl->client->stopServer();
} catch(\Exception $e) {
trigger_error("Server shutdown command from '{login}' failed. " . $e->getMessage());
return false;
}
$this->maniaControl->client->stopServer();
$this->maniaControl->quit("Server shutdown requested by '{$login}'");
return true;
}
}

View File

@ -6,6 +6,7 @@ use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Formatter;
use ManiaControl\ManiaControl;
use ManiaControl\Update\UpdateManager;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
/**
* Class reports Usage
@ -56,18 +57,18 @@ class UsageReporter implements TimerListener {
$properties['ServerName'] = Formatter::stripDirtyCodes($this->maniaControl->server->getName());
$properties['PlayerCount'] = $this->maniaControl->playerManager->getPlayerCount();
try {
$maxPlayers = $this->maniaControl->client->getMaxPlayers();
$properties['MaxPlayers'] = $maxPlayers["CurrentValue"];
} catch(\Exception $e) {
$properties['MaxPlayers'] = -1;
}
$maxPlayers = $this->maniaControl->client->getMaxPlayers();
$properties['MaxPlayers'] = $maxPlayers["CurrentValue"];
try {
$scriptName = $this->maniaControl->client->getScriptName();
$properties['ScriptName'] = $scriptName["CurrentValue"];
} catch(\Exception $e) {
$properties['ScriptName'] = '';
} catch(Exception $e) {
if ($e->getMessage() == 'Not in script mode.') {
$properties['ScriptName'] = '';
} else {
throw $e;
}
}
$json = json_encode($properties);