From bebd2e73530a2c7f51dd677a37119a27d5db1938 Mon Sep 17 00:00:00 2001 From: kremsy Date: Sat, 11 Jul 2015 15:18:44 +0200 Subject: [PATCH] server option communication methods --- core/Communication/CommunicationMethods.php | 15 +++++- core/Server/ServerOptionsMenu.php | 51 +++++++++++++++++++-- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/core/Communication/CommunicationMethods.php b/core/Communication/CommunicationMethods.php index f55bcff0..6451890c 100644 --- a/core/Communication/CommunicationMethods.php +++ b/core/Communication/CommunicationMethods.php @@ -16,6 +16,17 @@ interface CommunicationMethods { */ const RESTART_MANIA_CONTROL = "ManiaControl.Restart"; + /** Provides the Server Options + * no Parameters + */ + const GET_SERVER_OPTIONS = "ServerOptions.GetServerOptions"; + + /** Set Server Options + * Required Parameter + * - scriptSettings (array(optionName1 => value1, optionName2 => value2...)) + */ + const SET_SERVER_OPTIONS = "ServerOptions.SetServerOptions"; + /** Provides the ModeScriptSettings * no Parameters */ @@ -23,10 +34,10 @@ interface CommunicationMethods { /** Set ModeScriptSettings * Required Parameter - * - scriptSettings (array(settingName1 => Value1, settingName2 => value2...)) + * - scriptSettings (array(settingName1 => value1, settingName2 => value2...)) */ const SET_SCRIPT_SETTINGS = "ScriptSettings.SetScriptSettings"; - + /** Restarts the Current Map * no Parameters */ diff --git a/core/Server/ServerOptionsMenu.php b/core/Server/ServerOptionsMenu.php index a815792f..76add131 100644 --- a/core/Server/ServerOptionsMenu.php +++ b/core/Server/ServerOptionsMenu.php @@ -14,6 +14,9 @@ use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\Callbacks; use ManiaControl\Callbacks\TimerListener; +use ManiaControl\Communication\CommunicationAnswer; +use ManiaControl\Communication\CommunicationListener; +use ManiaControl\Communication\CommunicationMethods; use ManiaControl\Configurator\ConfiguratorMenu; use ManiaControl\Logger; use ManiaControl\ManiaControl; @@ -28,7 +31,7 @@ use Maniaplanet\DedicatedServer\Xmlrpc\ServerOptionsException; * @copyright 2014-2015 ManiaControl Team * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ -class ServerOptionsMenu implements CallbackListener, ConfiguratorMenu, TimerListener { +class ServerOptionsMenu implements CallbackListener, ConfiguratorMenu, TimerListener, CommunicationListener { /* * Constants */ @@ -64,6 +67,10 @@ class ServerOptionsMenu implements CallbackListener, ConfiguratorMenu, TimerList // Permissions $this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_CHANGE_SERVER_OPTIONS, AuthenticationManager::AUTH_LEVEL_SUPERADMIN); + + //TODO remove to somewhere cleaner + //Communication Listenings + $this->initalizeCommunicationListenings(); } /** @@ -322,8 +329,7 @@ class ServerOptionsMenu implements CallbackListener, ConfiguratorMenu, TimerList * @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData() */ public function saveConfigData(array $configData, Player $player) { - if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CHANGE_SERVER_OPTIONS) - ) { + if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CHANGE_SERVER_OPTIONS)) { $this->maniaControl->getAuthenticationManager()->sendNotAllowed($player); return; } @@ -376,4 +382,43 @@ class ServerOptionsMenu implements CallbackListener, ConfiguratorMenu, TimerList return true; } + + + /** + * Initializes the communication Listenings + */ + private function initalizeCommunicationListenings() { + //Communication Listenings + $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_SERVER_OPTIONS, $this, function ($data) { + return new CommunicationAnswer($this->maniaControl->getClient()->getServerOptions()); + }); + + $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::SET_SERVER_OPTIONS, $this, function ($data) { + if (!is_object($data) || !property_exists($data, "serverOptions")) { + return new CommunicationAnswer("No valid ServerOptions provided!", true); + } + + $oldServerOptions = $this->maniaControl->getClient()->getServerOptions(); + $newServerOptions = new ServerOptions(); + + foreach ($data->serverOptions as $name => $value) { + $optionName = $name; + $newServerOptions->$optionName = $value; + settype($newServerOptions->$optionName, gettype($oldServerOptions->$optionName)); + } + + $this->fillUpMandatoryOptions($newServerOptions, $oldServerOptions); + + try { + $success = $this->maniaControl->getClient()->setServerOptions($newServerOptions); + } catch (ServerOptionsException $exception) { + return new CommunicationAnswer($exception->getMessage(), true); + } + + //Trigger Server Options Changed Callback + $this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SERVER_OPTIONS_CHANGED, array(self::CB_SERVER_OPTIONS_CHANGED)); + + return new CommunicationAnswer(array("success" => $success)); + }); + } }