server settings coding improvements

fixed applying of settings by filling mandatory ones with current values
This commit is contained in:
Steffen Schröder 2014-07-25 15:17:36 +02:00
parent fba9258377
commit 7a69a8ed4e

View File

@ -105,43 +105,56 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
* @return bool * @return bool
*/ */
public function loadSettingsFromDatabase() { public function loadSettingsFromDatabase() {
$serverId = $this->maniaControl->server->index;
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_SERVER_SETTINGS . "` $query = "SELECT * FROM `" . self::TABLE_SERVER_SETTINGS . "`
WHERE serverIndex = {$serverId};"; WHERE serverIndex = {$this->maniaControl->server->index};";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$serverSettings = $this->maniaControl->client->getServerOptions();
$savedSettings = array(); $oldServerOptions = $this->maniaControl->client->getServerOptions();
$newServerOptions = new ServerOptions();
while ($row = $result->fetch_object()) { while ($row = $result->fetch_object()) {
$settingName = lcfirst($row->settingName); $settingName = lcfirst($row->settingName);
if (!property_exists($serverSettings, $settingName)) { if (!property_exists($oldServerOptions, $settingName)) {
continue; continue;
} }
$settingValue = $row->settingValue; $newServerOptions->$settingName = $row->settingValue;
settype($settingValue, gettype($serverSettings->$settingName)); settype($newServerOptions->$settingName, gettype($oldServerOptions->$settingName));
$savedSettings[$settingName] = $settingValue;
} }
$result->free(); $result->free();
if (empty($savedSettings)) {
return true;
}
$serverOptions = ServerOptions::fromArray($savedSettings); $this->fillUpMandatoryOptions($newServerOptions, $oldServerOptions);
if (!$serverOptions->isValid()) {
$message = "Couldn't load server settings from database because of their invalid state."; $loaded = false;
$this->maniaControl->chat->sendErrorToAdmins($message);
return false;
}
try { try {
return $this->maniaControl->client->setServerOptions($serverOptions); $loaded = $this->maniaControl->client->setServerOptions($newServerOptions);
} catch (ServerOptionsException $exception) { } catch (ServerOptionsException $exception) {
$this->maniaControl->chat->sendExceptionToAdmins($exception); $this->maniaControl->chat->sendExceptionToAdmins($exception);
} }
return false; $message = ($loaded ? 'Server Settings successfully loaded!' : 'Error loading Server Settings!');
$this->maniaControl->chat->sendSuccessToAdmins($message);
return $loaded;
}
/**
* Fill up the new server options object with the necessary settings based on the old options object
*
* @param ServerOptions $newServerOptions
* @param ServerOptions $oldServerOptions
* @return ServerOptions
*/
private function fillUpMandatoryOptions(ServerOptions &$newServerOptions, ServerOptions $oldServerOptions) {
$mandatorySettings = array('name', 'comment', 'password', 'passwordForSpectator', 'nextCallVoteTimeOut', 'callVoteRatio');
foreach ($mandatorySettings as $settingName) {
if (!isset($newServerOptions->$settingName) && isset($oldServerOptions->$settingName)) {
$newServerOptions->$settingName = $oldServerOptions->$settingName;
}
}
return $newServerOptions;
} }
/** /**
@ -263,21 +276,20 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
return; return;
} }
$serverOptions = $this->maniaControl->client->getServerOptions();
$serverSettings = $serverOptions->toArray();
$prefixLength = strlen(self::ACTION_PREFIX_SETTING); $prefixLength = strlen(self::ACTION_PREFIX_SETTING);
$oldServerOptions = $this->maniaControl->client->getServerOptions();
$newServerOptions = new ServerOptions();
$newSettings = new ServerOptions();
foreach ($configData[3] as $setting) { foreach ($configData[3] as $setting) {
$settingName = substr($setting['Name'], $prefixLength); $settingName = lcfirst(substr($setting['Name'], $prefixLength));
$dynamicSettingName = lcfirst($settingName); $newServerOptions->$settingName = $setting['Value'];
$newSettings->$dynamicSettingName = $setting['Value']; settype($newServerOptions->$settingName, gettype($oldServerOptions->$settingName));
settype($newSettings->$dynamicSettingName, gettype($serverSettings[$settingName]));
} }
$success = $this->applyNewServerSettings($newSettings, $player); $this->fillUpMandatoryOptions($newServerOptions, $oldServerOptions);
$success = $this->applyNewServerOptions($newServerOptions, $player);
if ($success) { if ($success) {
$this->maniaControl->chat->sendSuccess('Server Settings saved!', $player); $this->maniaControl->chat->sendSuccess('Server Settings saved!', $player);
} else { } else {
@ -291,19 +303,19 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
/** /**
* Apply the Array of new Server Settings * Apply the Array of new Server Settings
* *
* @param ServerOptions $newSettings * @param ServerOptions $newServerOptions
* @param Player $player * @param Player $player
* @return bool * @return bool
*/ */
private function applyNewServerSettings(ServerOptions $newSettings, Player $player) { private function applyNewServerOptions(ServerOptions $newServerOptions, Player $player) {
try { try {
$this->maniaControl->client->setServerOptions($newSettings); $this->maniaControl->client->setServerOptions($newServerOptions);
} catch (ServerOptionsException $exception) { } catch (ServerOptionsException $exception) {
$this->maniaControl->chat->sendException($exception, $player); $this->maniaControl->chat->sendException($exception, $player);
return false; return false;
} }
$this->saveServerOptions($newSettings, true); $this->saveServerOptions($newServerOptions, true);
$this->maniaControl->callbackManager->triggerCallback(self::CB_SERVERSETTINGS_CHANGED, array(self::CB_SERVERSETTINGS_CHANGED)); $this->maniaControl->callbackManager->triggerCallback(self::CB_SERVERSETTINGS_CHANGED, array(self::CB_SERVERSETTINGS_CHANGED));