server settings menu coding improvements

This commit is contained in:
Steffen Schröder 2014-07-24 23:13:30 +02:00
parent f9d59c432d
commit 6138274a98

View File

@ -85,6 +85,13 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
return true; return true;
} }
/**
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
*/
public static function getTitle() {
return 'Server Settings';
}
/** /**
* Handle OnInit callback * Handle OnInit callback
*/ */
@ -145,8 +152,8 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
$script->addFeature($paging); $script->addFeature($paging);
$frame = new Frame(); $frame = new Frame();
$serverSettings = $this->maniaControl->client->getServerOptions() $serverOptions = $this->maniaControl->client->getServerOptions();
->toArray(); $serverSettings = $serverOptions->toArray();
// Config // Config
$pagerSize = 9.; $pagerSize = 9.;
@ -156,27 +163,26 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
// Pagers // Pagers
$pagerPrev = new Quad_Icons64x64_1(); $pagerPrev = new Quad_Icons64x64_1();
$frame->add($pagerPrev); $frame->add($pagerPrev);
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2); $pagerPrev->setPosition($width * 0.39, $height * -0.44, 2)
$pagerPrev->setSize($pagerSize, $pagerSize); ->setSize($pagerSize, $pagerSize)
$pagerPrev->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowPrev); ->setSubStyle($pagerPrev::SUBSTYLE_ArrowPrev);
$pagerNext = new Quad_Icons64x64_1(); $pagerNext = new Quad_Icons64x64_1();
$frame->add($pagerNext); $frame->add($pagerNext);
$pagerNext->setPosition($width * 0.45, $height * -0.44, 2); $pagerNext->setPosition($width * 0.45, $height * -0.44, 2)
$pagerNext->setSize($pagerSize, $pagerSize); ->setSize($pagerSize, $pagerSize)
$pagerNext->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowNext); ->setSubStyle($pagerNext::SUBSTYLE_ArrowNext);
$paging->addButton($pagerNext);
$paging->addButton($pagerPrev);
$pageCountLabel = new Label_Text(); $pageCountLabel = new Label_Text();
$frame->add($pageCountLabel); $frame->add($pageCountLabel);
$pageCountLabel->setHAlign($pageCountLabel::RIGHT); $pageCountLabel->setHAlign($pageCountLabel::RIGHT)
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1); ->setPosition($width * 0.35, $height * -0.44, 1)
$pageCountLabel->setStyle($pageCountLabel::STYLE_TextTitle1); ->setStyle($pageCountLabel::STYLE_TextTitle1)
$pageCountLabel->setTextSize(2); ->setTextSize(2);
$paging->setLabel($pageCountLabel); $paging->addButton($pagerNext)
->addButton($pagerPrev)
->setLabel($pageCountLabel);
// Setting pages // Setting pages
$posY = 0.; $posY = 0.;
@ -185,7 +191,7 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
foreach ($serverSettings as $name => $value) { foreach ($serverSettings as $name => $value) {
// Continue on CurrentMaxPlayers... // Continue on CurrentMaxPlayers...
$pos = strpos($name, 'Current'); // TODO maybe display current somewhere $pos = strpos($name, 'Current'); // TODO: display 'Current...' somewhere
if ($pos !== false) { if ($pos !== false) {
continue; continue;
} }
@ -203,39 +209,37 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
$nameLabel = new Label_Text(); $nameLabel = new Label_Text();
$settingFrame->add($nameLabel); $settingFrame->add($nameLabel);
$nameLabel->setHAlign($nameLabel::LEFT); $nameLabel->setHAlign($nameLabel::LEFT)
$nameLabel->setX($width * -0.46); ->setX($width * -0.46)
$nameLabel->setSize($width * 0.4, $settingHeight); ->setSize($width * 0.4, $settingHeight)
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall); ->setStyle($nameLabel::STYLE_TextCardSmall)
$nameLabel->setTextSize($labelTextSize); ->setTextSize($labelTextSize)
$nameLabel->setText($name); ->setText($name)
$nameLabel->setTextColor('fff'); ->setTextColor('fff');
if (is_bool($value)) { if (is_bool($value)) {
// Boolean checkbox // Boolean checkbox
$quad = new Quad(); $quad = new Quad();
$quad->setPosition($width * 0.23, 0, -0.01); $quad->setPosition($width * 0.23, 0, -0.01)
$quad->setSize(4, 4); ->setSize(4, 4);
$checkBox = new CheckBox(self::ACTION_PREFIX_SETTING . $name, $value, $quad); $checkBox = new CheckBox(self::ACTION_PREFIX_SETTING . $name, $value, $quad);
$settingFrame->add($checkBox); $settingFrame->add($checkBox);
} else { } else {
// Other // Other
$entry = new Entry(); $entry = new Entry();
$settingFrame->add($entry); $settingFrame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall); $entry->setStyle(Label_Text::STYLE_TextValueSmall)
$entry->setX($width * 0.23); ->setX($width * 0.23)
$entry->setTextSize(1); ->setTextSize(1)
$entry->setSize($width * 0.48, $settingHeight * 0.9); ->setSize($width * 0.48, $settingHeight * 0.9)
$entry->setName(self::ACTION_PREFIX_SETTING . $name); ->setName(self::ACTION_PREFIX_SETTING . $name)
$entry->setDefault($value); ->setDefault($value);
if ($name === 'Comment') { if ($name === 'Comment') {
$entry->setAutoNewLine(true); $entry->setSize($width * 0.48, $settingHeight * 3. + $settingHeight * 0.9)
$entry->setSize($width * 0.48, $settingHeight * 3 + $settingHeight * 0.9); ->setAutoNewLine(true);
$settingFrame->setY($posY - $settingHeight * 1.5); $settingFrame->setY($posY - $settingHeight * 1.5);
// dummy: $posY -= $settingHeight * 3.;
// TODO: "dummy:" what? remove?
$posY -= $settingHeight * 3;
$index += 3; $index += 3;
} }
} }
@ -259,8 +263,8 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
return; return;
} }
$serverSettings = $this->maniaControl->client->getServerOptions() $serverOptions = $this->maniaControl->client->getServerOptions();
->toArray(); $serverSettings = $serverOptions->toArray();
$prefixLength = strlen(self::ACTION_PREFIX_SETTING); $prefixLength = strlen(self::ACTION_PREFIX_SETTING);
@ -277,12 +281,11 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
if ($success) { if ($success) {
$this->maniaControl->chat->sendSuccess('Server Settings saved!', $player); $this->maniaControl->chat->sendSuccess('Server Settings saved!', $player);
} else { } else {
$this->maniaControl->chat->sendSuccess('Server Settings Saving failed!', $player); $this->maniaControl->chat->sendSuccess('Server Settings saving failed!', $player);
} }
// Reopen the Menu // Reopen the Menu
$menuId = $this->maniaControl->configurator->getMenuId($this->getTitle()); $this->maniaControl->configurator->showMenu($player, $this);
$this->maniaControl->configurator->reopenMenu($player, $menuId);
} }
/** /**
@ -293,20 +296,29 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
* @return bool * @return bool
*/ */
private function applyNewServerSettings(ServerOptions $newSettings, Player $player) { private function applyNewServerSettings(ServerOptions $newSettings, Player $player) {
if (!$newSettings) {
return true;
}
try { try {
$this->maniaControl->client->setServerOptions($newSettings); $this->maniaControl->client->setServerOptions($newSettings);
} catch (ServerOptionsException $e) { } catch (ServerOptionsException $exception) {
$this->maniaControl->chat->sendError($e->getMessage()); $this->maniaControl->chat->sendException($exception, $player);
return false; return false;
} }
// Save Settings into Database $this->saveServerOptions($newSettings, true);
$mysqli = $this->maniaControl->database->mysqli;
$this->maniaControl->callbackManager->triggerCallback(self::CB_SERVERSETTINGS_CHANGED, array(self::CB_SERVERSETTINGS_CHANGED));
return true;
}
/**
* Save the given server options in the database
*
* @param ServerOptions $serverOptions
* @param bool $triggerCallbacks
* @return bool
*/
private function saveServerOptions(ServerOptions $serverOptions, $triggerCallbacks = false) {
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT INTO `" . self::TABLE_SERVER_SETTINGS . "` ( $query = "INSERT INTO `" . self::TABLE_SERVER_SETTINGS . "` (
`serverIndex`, `serverIndex`,
`settingName`, `settingName`,
@ -321,13 +333,16 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
return false; return false;
} }
$settingsArray = $newSettings->toArray(); $settingName = null;
foreach ($settingsArray as $setting => $value) { $settingValue = null;
if ($value === null) { $statement->bind_param('iss', $this->maniaControl->server->index, $settingName, $settingValue);
$settingsArray = $serverOptions->toArray();
foreach ($settingsArray as $settingName => $settingValue) {
if ($settingValue === null) {
continue; continue;
} }
$statement->bind_param('iss', $this->maniaControl->server->index, $setting, $value);
$statement->execute(); $statement->execute();
if ($statement->error) { if ($statement->error) {
trigger_error($statement->error); trigger_error($statement->error);
@ -335,21 +350,12 @@ class ServerSettings implements ConfiguratorMenu, CallbackListener {
return false; return false;
} }
// Trigger own callback if ($triggerCallbacks) {
$this->maniaControl->callbackManager->triggerCallback(self::CB_SERVERSETTING_CHANGED, array(self::CB_SERVERSETTING_CHANGED, $setting, $value)); $this->maniaControl->callbackManager->triggerCallback(self::CB_SERVERSETTING_CHANGED, array(self::CB_SERVERSETTING_CHANGED, $settingName, $settingValue));
}
} }
$statement->close(); $statement->close();
$this->maniaControl->callbackManager->triggerCallback(self::CB_SERVERSETTINGS_CHANGED, array(self::CB_SERVERSETTINGS_CHANGED));
return true; return true;
} }
/**
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
*/
public function getTitle() {
return 'Server Settings';
}
} }