TrackManiaControl/core/Server/ServerOptionsMenu.php

491 lines
16 KiB
PHP
Raw Permalink Normal View History

2014-01-02 23:49:21 +01:00
<?php
namespace ManiaControl\Server;
2014-01-02 23:49:21 +01:00
use FML\Components\CheckBox;
2014-01-02 23:49:21 +01:00
use FML\Controls\Entry;
use FML\Controls\Frame;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quad;
2014-01-02 23:49:21 +01:00
use FML\Controls\Quads\Quad_Icons64x64_1;
2014-04-27 16:22:12 +02:00
use FML\Script\Features\Paging;
2014-01-02 23:49:21 +01:00
use FML\Script\Script;
2014-04-13 19:05:54 +02:00
use ManiaControl\Admin\AuthenticationManager;
2014-01-02 23:49:21 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\TimerListener;
2015-07-11 15:18:44 +02:00
use ManiaControl\Communication\CommunicationAnswer;
use ManiaControl\Communication\CommunicationListener;
use ManiaControl\Communication\CommunicationMethods;
use ManiaControl\Configurator\ConfiguratorMenu;
use ManiaControl\Logger;
2014-01-02 23:49:21 +01:00
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Structures\ServerOptions;
2014-05-08 21:10:56 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\ServerOptionsException;
2014-01-02 23:49:21 +01:00
/**
* Class offering a Configurator Menu for Server Options
2014-01-02 23:49:21 +01:00
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 ManiaControl Team
2014-04-13 19:05:54 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-01-02 23:49:21 +01:00
*/
2015-07-11 15:18:44 +02:00
class ServerOptionsMenu implements CallbackListener, ConfiguratorMenu, TimerListener, CommunicationListener {
/*
2014-01-02 23:49:21 +01:00
* Constants
*/
2014-07-25 16:39:14 +02:00
const CB_SERVER_OPTION_CHANGED = 'ServerOptionsMenu.OptionChanged';
const CB_SERVER_OPTIONS_CHANGED = 'ServerOptionsMenu.OptionsChanged';
const SETTING_PERMISSION_CHANGE_SERVER_OPTIONS = 'Change Server Options';
const SETTING_LOAD_DEFAULT_SETTINGS_STARTUP = 'Load Stored Server Options on Startup';
const TABLE_SERVER_OPTIONS = 'mc_server_options';
const ACTION_PREFIX_OPTION = 'ServerOptionsMenu.';
2014-01-05 20:34:48 +01:00
2014-07-25 16:39:14 +02:00
/** @deprecated */
const CB_SERVERSETTING_CHANGED = self::CB_SERVER_OPTION_CHANGED;
/** @deprecated */
const CB_SERVERSETTINGS_CHANGED = self::CB_SERVER_OPTIONS_CHANGED;
/*
* Private properties
2014-01-02 23:49:21 +01:00
*/
/** @var ManiaControl $maniaControl */
2014-01-02 23:49:21 +01:00
private $maniaControl = null;
/**
* Construct a new server options menu instance
2014-01-02 23:49:21 +01:00
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-05 12:21:40 +01:00
$this->initTables();
2014-01-05 20:34:48 +01:00
// Callbacks
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit');
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'saveCurrentServerOptions', 6 * 3600 * 1000);
2014-04-13 19:05:54 +02:00
// Permissions
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_CHANGE_SERVER_OPTIONS, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
2015-07-11 15:18:44 +02:00
// Settings
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_LOAD_DEFAULT_SETTINGS_STARTUP, false);
2015-07-11 15:18:44 +02:00
//TODO remove to somewhere cleaner
//Communication Listenings
$this->initalizeCommunicationListenings();
2014-01-04 17:18:11 +01:00
}
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SERVER_OPTIONS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`serverIndex` int(11) NOT NULL,
`optionName` varchar(100) NOT NULL,
`optionValue` varchar(500) NOT NULL,
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`index`),
UNIQUE KEY `option` (`serverIndex`, `optionName`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Server Options' AUTO_INCREMENT=1;";
2014-01-04 17:18:11 +01:00
$statement = $mysqli->prepare($query);
2014-02-01 20:03:17 +01:00
if ($mysqli->error) {
2014-01-04 17:18:11 +01:00
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$statement->execute();
2014-02-01 20:03:17 +01:00
if ($statement->error) {
2014-01-04 17:18:11 +01:00
trigger_error($statement->error, E_USER_ERROR);
return false;
}
$statement->close();
return true;
}
/**
2017-03-25 18:40:15 +01:00
* @see \ManiaControl\Configurator\ConfiguratorMenu::getTitle()
*/
public static function getTitle() {
return 'Server Options';
}
/**
* Save the current server options in case they have been changed by an external tool
*
* @return bool
*/
public function saveCurrentServerOptions() {
2014-08-13 11:05:52 +02:00
$serverOptions = $this->maniaControl->getClient()->getServerOptions();
return $this->saveServerOptions($serverOptions);
}
/**
* Save the given server options in the database
*
* @param ServerOptions $serverOptions
* @param bool $triggerCallbacks
* @return bool
*/
private function saveServerOptions(ServerOptions $serverOptions, $triggerCallbacks = false) {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$query = "INSERT INTO `" . self::TABLE_SERVER_OPTIONS . "` (
`serverIndex`,
`optionName`,
`optionValue`
) VALUES (
?, ?, ?
) ON DUPLICATE KEY UPDATE
`optionValue` = VALUES(`optionValue`);";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$optionName = null;
$optionValue = null;
$statement->bind_param('iss', $this->maniaControl->getServer()->index, $optionName, $optionValue);
$serverOptionsArray = $serverOptions->toArray();
foreach ($serverOptionsArray as $optionName => $optionValue) {
if ($optionValue === null) {
continue;
}
$statement->execute();
if ($statement->error) {
trigger_error($statement->error);
$statement->close();
return false;
}
if ($triggerCallbacks) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SERVER_OPTION_CHANGED, array(self::CB_SERVER_OPTION_CHANGED, $optionName, $optionValue));
}
}
$statement->close();
return true;
}
2014-01-04 17:18:11 +01:00
/**
* Handle OnInit callback
*/
public function onInit() {
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_LOAD_DEFAULT_SETTINGS_STARTUP)) {
$this->loadOptionsFromDatabase();
}
2014-01-04 17:18:11 +01:00
}
/**
* Load options from database
2014-01-04 17:30:29 +01:00
*
2014-01-04 17:18:11 +01:00
* @return bool
*/
public function loadOptionsFromDatabase() {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$serverIndex = $this->maniaControl->getServer()->index;
2014-08-05 02:17:41 +02:00
$query = "SELECT * FROM `" . self::TABLE_SERVER_OPTIONS . "`
WHERE `serverIndex` = {$serverIndex};";
2014-08-05 02:17:41 +02:00
$result = $mysqli->query($query);
2014-02-01 20:03:17 +01:00
if ($mysqli->error) {
2014-01-04 17:18:11 +01:00
trigger_error($mysqli->error);
2014-01-05 12:21:40 +01:00
return false;
2014-01-04 17:18:11 +01:00
}
2014-08-13 11:05:52 +02:00
$oldServerOptions = $this->maniaControl->getClient()->getServerOptions();
$newServerOptions = new ServerOptions();
2014-05-02 17:50:30 +02:00
while ($row = $result->fetch_object()) {
$optionName = lcfirst($row->optionName);
if (!property_exists($oldServerOptions, $optionName)) {
2014-01-05 20:34:48 +01:00
continue;
}
$newServerOptions->$optionName = $row->optionValue;
settype($newServerOptions->$optionName, gettype($oldServerOptions->$optionName));
2014-01-04 17:18:11 +01:00
}
$result->free();
2014-01-05 20:34:48 +01:00
$this->fillUpMandatoryOptions($newServerOptions, $oldServerOptions);
$loaded = false;
try {
2014-08-13 11:05:52 +02:00
$loaded = $this->maniaControl->getClient()->setServerOptions($newServerOptions);
2020-06-02 17:30:30 +02:00
} catch (ServerOptionsException $e) {
$this->maniaControl->getChat()->sendExceptionToAdmins($e);
}
if ($loaded) {
Logger::logInfo('Server Options successfully loaded!');
} else {
Logger::logError('Error loading Server Options!');
}
return $loaded;
}
/**
* Fill up the new server options object with the necessary options based on the old options object
*
* @param ServerOptions $newServerOptions
* @param ServerOptions $oldServerOptions
* @return ServerOptions
*/
private function fillUpMandatoryOptions(ServerOptions &$newServerOptions, ServerOptions $oldServerOptions) {
$mandatoryOptions = array('name', 'comment', 'password', 'passwordForSpectator', 'nextCallVoteTimeOut', 'callVoteRatio');
foreach ($mandatoryOptions as $optionName) {
if (!isset($newServerOptions->$optionName) && isset($oldServerOptions->$optionName)) {
$newServerOptions->$optionName = $oldServerOptions->$optionName;
}
}
return $newServerOptions;
2014-01-02 23:49:21 +01:00
}
/**
2017-03-25 18:40:15 +01:00
* @see \ManiaControl\Configurator\ConfiguratorMenu::getMenu()
2014-01-02 23:49:21 +01:00
*/
public function getMenu($width, $height, Script $script, Player $player) {
2014-05-02 17:50:30 +02:00
$paging = new Paging();
$script->addFeature($paging);
$frame = new Frame();
2014-01-05 20:34:48 +01:00
2014-08-13 11:05:52 +02:00
$serverOptions = $this->maniaControl->getClient()->getServerOptions();
$serverOptionsArray = $serverOptions->toArray();
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
// Config
2014-01-05 20:34:48 +01:00
$pagerSize = 9.;
$optionHeight = 5.;
2014-01-02 23:49:21 +01:00
$labelTextSize = 2;
2014-01-05 20:34:48 +01:00
$tipNextValues = new Label_Text();
$frame->addChild($tipNextValues);
$tipNextValues->setPosition(- $width * 0.45, $height * -0.44, 1);
$tipNextValues->setTextSize(0.5);
$tipNextValues->setTextFont("GameFontSemiBold");
$tipNextValues->setHorizontalAlign($tipNextValues::LEFT);
$tipNextValues->setAutoNewLine(true);
$tipNextValues->setSize(45,10);
$tipNextValues->setText("All options starting by \"Next\" are applied after a map change");
2014-01-02 23:49:21 +01:00
// Pagers
$pagerPrev = new Quad_Icons64x64_1();
2017-03-25 19:15:50 +01:00
$frame->addChild($pagerPrev);
$pagerPrev->setPosition($width * 0.5 - 12, $height * -0.5 + 5, 2);
$pagerPrev->setSize($pagerSize, $pagerSize);
$pagerPrev->setSubStyle($pagerPrev::SUBSTYLE_ArrowPrev);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$pagerNext = new Quad_Icons64x64_1();
2017-03-25 19:15:50 +01:00
$frame->addChild($pagerNext);
$pagerNext->setPosition($width * 0.5 - 5, $height * -0.5 + 5, 2);
$pagerNext->setSize($pagerSize, $pagerSize);
$pagerNext->setSubStyle($pagerNext::SUBSTYLE_ArrowNext);
2014-01-05 20:34:48 +01:00
2014-06-22 19:02:18 +02:00
$pageCountLabel = new Label_Text();
2017-03-25 19:15:50 +01:00
$frame->addChild($pageCountLabel);
$pageCountLabel->setHorizontalAlign($pageCountLabel::RIGHT);
$pageCountLabel->setPosition($width * 0.5 - 16, $height * -0.5 + 5, 1);
$pageCountLabel->setStyle($pageCountLabel::STYLE_TextTitle1);
$pageCountLabel->setTextSize(2);
2014-01-05 20:34:48 +01:00
$paging->addButtonControl($pagerNext);
$paging->addButtonControl($pagerPrev);
$paging->setLabel($pageCountLabel);
$repositionnedFrame = new Frame();
$frame->addChild($repositionnedFrame);
$repositionnedFrame->setPosition($width * -0.5, $height * 0.5);
$pagesFrame = new Frame();
$repositionnedFrame->addChild($pagesFrame);
$pagesFrame->setY(-8.);
2014-01-05 20:34:48 +01:00
// Pages
2014-06-17 23:55:59 +02:00
$posY = 0.;
$index = 0;
2014-05-15 17:45:08 +02:00
$pageFrame = null;
$pageMaxCount = floor(($height - 16) / $optionHeight);
2014-05-15 17:45:08 +02:00
foreach ($serverOptionsArray as $name => $value) {
2014-01-05 12:21:40 +01:00
// Continue on CurrentMaxPlayers...
2023-05-25 13:35:18 +02:00
if (strpos($name, 'Current') !== false) continue; // TODO: display 'Current...' somewhere
2014-01-05 20:34:48 +01:00
if ($index % $pageMaxCount === 0) {
2014-01-02 23:49:21 +01:00
$pageFrame = new Frame();
$pagesFrame->addChild($pageFrame);
2017-03-25 18:40:15 +01:00
$paging->addPageControl($pageFrame);
$posY = 0.;
2014-01-02 23:49:21 +01:00
}
2014-01-05 20:34:48 +01:00
$optionsFrame = new Frame();
2017-03-25 19:15:50 +01:00
$pageFrame->addChild($optionsFrame);
$optionsFrame->setY($posY);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$nameLabel = new Label_Text();
2017-03-25 19:15:50 +01:00
$optionsFrame->addChild($nameLabel);
$nameLabel->setHorizontalAlign($nameLabel::LEFT);
$nameLabel->setX(2);
$nameLabel->setSize($width * 0.6, $optionHeight);
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
$nameLabel->setTextSize($labelTextSize)->setText($name);
$nameLabel->setTextColor('fff');
2014-01-05 20:34:48 +01:00
if (is_bool($value)) {
// Boolean checkbox
$quad = new Quad();
$quad->setPosition($width - $width * 0.3 / 2 - 2, 0, -0.01);
$quad->setSize(4, 4);
$checkBox = new CheckBox(self::ACTION_PREFIX_OPTION . $name, $value, $quad);
2017-03-25 19:15:50 +01:00
$optionsFrame->addChild($checkBox);
} else {
// Other
$entry = new Entry();
2017-03-25 19:15:50 +01:00
$optionsFrame->addChild($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
$entry->setX($width - $width * 0.3 / 2 - 2);
$entry->setTextSize(1);
$entry->setSize($width * 0.3, $optionHeight * 0.9);
$entry->setName(self::ACTION_PREFIX_OPTION . $name);
$entry->setDefault($value);
if ($name === 'Comment') {
$entry->setSize($width * 0.3, $optionHeight * 3. + $optionHeight * 0.9);
$entry->setAutoNewLine(true);
$entry->setVerticalAlign($entry::TOP);
$entry->setY($optionHeight * 1.5 + 2.5);
$optionsFrame->setY($posY - $optionHeight * 1.5);
$posY -= $optionHeight * 3.;
2014-06-17 23:55:59 +02:00
$index += 3;
2023-05-25 13:35:18 +02:00
} else if (strpos($name, 'Password') !== false) {
2023-05-25 13:03:31 +02:00
$entry->setTextFormat("Password");
$entry->setId($name);
$quad = new Quad();
$quad->setPosition($width - $width * 0.3 - 4, 0, -0.01);
$quad->setSize(4, 4);
2023-05-25 13:03:31 +02:00
$checkBox = new CheckBox(null, false, $quad);
$checkBox->setEnabledDesign("UICommon64_1", "Eye_light");
$checkBox->setDisabledDesign("UICommon64_1", "Eye_light");
$checkBox->setCustomScript("
declare CMlEntry Entry <=> (Quad_CheckBox.Parent.Parent.GetFirstChild(\"$name\") as CMlEntry);
if (Entry != Null) {
if (Quad_CheckBox.StyleSelected) {
Entry.TextFormat = CMlEntry::ETextFormat::Basic;
} else {
Entry.TextFormat = CMlEntry::ETextFormat::Password;
}
}");
$optionsFrame->addChild($checkBox);
}
2014-01-02 23:49:21 +01:00
}
2014-01-05 20:34:48 +01:00
$posY -= $optionHeight;
2014-06-17 23:55:59 +02:00
$index++;
2014-01-02 23:49:21 +01:00
}
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
return $frame;
}
/**
2017-03-25 18:40:15 +01:00
* @see \ManiaControl\Configurator\ConfiguratorMenu::saveConfigData()
2014-01-02 23:49:21 +01:00
*/
public function saveConfigData(array $configData, Player $player) {
2015-07-11 15:18:44 +02:00
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CHANGE_SERVER_OPTIONS)) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
2014-04-13 19:05:54 +02:00
return;
}
if (!$configData[3] || strpos($configData[3][0]['Name'], self::ACTION_PREFIX_OPTION) !== 0) {
2014-01-05 20:34:48 +01:00
return;
}
$prefixLength = strlen(self::ACTION_PREFIX_OPTION);
2014-01-05 20:34:48 +01:00
2014-08-13 11:05:52 +02:00
$oldServerOptions = $this->maniaControl->getClient()->getServerOptions();
$newServerOptions = new ServerOptions();
2014-05-25 09:58:52 +02:00
foreach ($configData[3] as $option) {
$optionName = lcfirst(substr($option['Name'], $prefixLength));
$newServerOptions->$optionName = $option['Value'];
settype($newServerOptions->$optionName, gettype($oldServerOptions->$optionName));
2014-01-02 23:49:21 +01:00
}
2014-01-05 20:34:48 +01:00
$this->fillUpMandatoryOptions($newServerOptions, $oldServerOptions);
$success = $this->applyNewServerOptions($newServerOptions, $player);
2014-05-16 20:42:37 +02:00
if ($success) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess('Server Options saved!', $player);
2014-05-16 20:42:37 +02:00
} else {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError('Server Options saving failed!', $player);
2014-05-16 20:42:37 +02:00
}
2014-01-05 20:34:48 +01:00
2014-05-16 20:42:37 +02:00
// Reopen the Menu
2014-08-13 11:05:52 +02:00
$this->maniaControl->getConfigurator()->showMenu($player, $this);
2014-01-02 23:49:21 +01:00
}
/**
* Apply the array of new Server Options
2014-01-02 23:49:21 +01:00
*
* @param ServerOptions $newServerOptions
* @param Player $player
2014-01-04 17:18:11 +01:00
* @return bool
2014-01-02 23:49:21 +01:00
*/
private function applyNewServerOptions(ServerOptions $newServerOptions, $player = null) {
2014-04-22 21:56:18 +02:00
try {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->setServerOptions($newServerOptions);
2020-06-02 17:30:30 +02:00
} catch (ServerOptionsException $e) {
$this->maniaControl->getChat()->sendException($e, $player);
2014-04-22 21:56:18 +02:00
return false;
}
2014-01-05 20:34:48 +01:00
$this->saveServerOptions($newServerOptions, true);
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SERVER_OPTIONS_CHANGED, array(self::CB_SERVER_OPTIONS_CHANGED));
return true;
}
2015-07-11 15:18:44 +02:00
/**
* 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);
2015-07-11 15:18:44 +02:00
try {
$success = $this->applyNewServerOptions($newServerOptions);
2015-07-11 15:18:44 +02:00
} 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));
});
}
2014-01-02 23:49:21 +01:00
}