TrackManiaControl/application/core/Configurator/ScriptSettings.php

410 lines
12 KiB
PHP
Raw Normal View History

<?php
namespace ManiaControl\Configurator;
2014-06-15 02:55:50 +02:00
use FML\Components\CheckBox;
2014-01-02 23:49:21 +01:00
use FML\Controls\Entry;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Labels\Label_Text;
2014-06-15 02:26:50 +02:00
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;
2013-12-31 10:40:03 +01:00
use FML\Script\Script;
2014-04-13 19:05:54 +02:00
use ManiaControl\Admin\AuthenticationManager;
2013-12-30 20:12:53 +01:00
use ManiaControl\Callbacks\CallbackListener;
2014-04-28 20:50:38 +02:00
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Logger;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
/**
* Class offering a Configurator for Script Settings
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
2014-04-19 22:59:11 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2013-12-31 13:51:30 +01:00
class ScriptSettings implements ConfiguratorMenu, CallbackListener {
/*
* Constants
*/
2014-06-15 02:26:50 +02:00
const ACTION_PREFIX_SETTING = 'ScriptSetting.';
2014-04-19 22:59:11 +02:00
const CB_SCRIPTSETTING_CHANGED = 'ScriptSettings.SettingChanged';
const CB_SCRIPTSETTINGS_CHANGED = 'ScriptSettings.SettingsChanged';
const TABLE_SCRIPT_SETTINGS = 'mc_scriptsettings';
const SETTING_LOAD_DEFAULT_SETTINGS_MAP_BEGIN = 'Load Stored Script-Settings on Map-Begin';
2014-04-13 19:05:54 +02:00
const SETTING_PERMISSION_CHANGE_SCRIPT_SETTINGS = 'Change Script-Settings';
2014-01-05 20:34:48 +01:00
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
/**
* Construct a new script settings instance
*
2013-12-31 13:51:30 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-06-15 02:26:50 +02:00
$this->initTables();
2014-01-05 20:34:48 +01:00
// Callbacks
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::BEGINMAP, $this, 'onBeginMap');
// Settings
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_LOAD_DEFAULT_SETTINGS_MAP_BEGIN, false);
2014-04-13 19:05:54 +02:00
// Permissions
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_CHANGE_SCRIPT_SETTINGS, AuthenticationManager::AUTH_LEVEL_ADMIN);
2014-01-04 17:30:29 +01:00
}
2014-01-05 12:17:29 +01:00
/**
* Create all necessary database tables
2014-01-05 12:17:29 +01:00
*
* @return boolean
*/
2014-01-04 17:30:29 +01:00
private function initTables() {
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-01-05 20:34:48 +01:00
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SCRIPT_SETTINGS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`serverIndex` int(11) NOT NULL,
`settingName` varchar(100) NOT NULL,
`settingValue` varchar(500) NOT NULL,
PRIMARY KEY (`index`),
2014-01-05 14:41:19 +01:00
UNIQUE KEY `setting` (`serverIndex`, `settingName`)
2014-01-04 17:30:29 +01:00
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Script Settings' AUTO_INCREMENT=1;";
2014-01-05 20:34:48 +01:00
2014-01-04 17:30:29 +01:00
$statement = $mysqli->prepare($query);
2014-02-01 20:03:17 +01:00
if ($mysqli->error) {
2014-01-04 17:30:29 +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:30:29 +01:00
trigger_error($statement->error, E_USER_ERROR);
return false;
}
$statement->close();
return true;
}
/**
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
*/
public static function getTitle() {
return 'Script Settings';
}
2014-01-04 17:30:29 +01:00
/**
* Handle OnInit callback
*/
public function onInit() {
2014-01-04 17:30:29 +01:00
$this->loadSettingsFromDatabase();
}
2014-01-04 17:30:29 +01:00
/**
* Load Settings from Database
*
* @return bool
*/
public function loadSettingsFromDatabase() {
try {
$scriptSettings = $this->maniaControl->getClient()->getModeScriptSettings();
} catch (GameModeException $e) {
2014-04-19 22:59:11 +02:00
return false;
}
2014-01-17 17:09:25 +01:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$serverIndex = $this->maniaControl->getServer()->index;
$query = "SELECT * FROM `" . self::TABLE_SCRIPT_SETTINGS . "`
WHERE serverIndex = {$serverIndex};";
$result = $mysqli->query($query);
2014-02-01 20:03:17 +01:00
if ($mysqli->error) {
2014-01-04 17:30:29 +01:00
trigger_error($mysqli->error);
2014-01-05 12:17:29 +01:00
return false;
2014-01-04 17:30:29 +01:00
}
2014-01-05 20:34:48 +01:00
2014-01-05 12:17:29 +01:00
$loadedSettings = array();
2014-05-02 17:50:30 +02:00
while ($row = $result->fetch_object()) {
2014-02-01 20:03:17 +01:00
if (!isset($scriptSettings[$row->settingName])) {
2014-01-05 20:34:48 +01:00
continue;
2014-01-05 22:51:21 +01:00
}
2014-01-04 17:30:29 +01:00
$loadedSettings[$row->settingName] = $row->settingValue;
settype($loadedSettings[$row->settingName], gettype($scriptSettings[$row->settingName]));
}
$result->free();
if (empty($loadedSettings)) {
2014-01-05 20:34:48 +01:00
return true;
2014-01-05 22:51:21 +01:00
}
2014-01-05 20:34:48 +01:00
return $this->maniaControl->getClient()->setModeScriptSettings($loadedSettings);
}
/**
* Handle Begin Map Callback
*/
public function onBeginMap() {
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_LOAD_DEFAULT_SETTINGS_MAP_BEGIN)) {
2014-05-02 17:50:30 +02:00
$this->loadSettingsFromDatabase();
}
}
/**
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
*/
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-02-13 00:26:18 +01:00
try {
$scriptInfo = $this->maniaControl->getClient()->getModeScriptInfo();
} catch (GameModeException $e) {
2014-04-20 15:47:35 +02:00
$label = new Label();
$frame->add($label);
$label->setText($e->getMessage());
return $frame;
2014-02-13 00:26:18 +01:00
}
2014-01-17 17:00:06 +01:00
$scriptParams = $scriptInfo->paramDescs;
2014-01-05 20:34:48 +01:00
2014-02-13 00:26:18 +01:00
try {
$scriptSettings = $this->maniaControl->getClient()->getModeScriptSettings();
} catch (GameModeException $e) {
2014-02-13 00:26:18 +01:00
}
2014-01-05 20:34:48 +01:00
// Config
2014-01-05 20:34:48 +01:00
$pagerSize = 9.;
$settingHeight = 5.;
$labelTextSize = 2;
2014-01-05 20:34:48 +01:00
// Pagers
$pagerPrev = new Quad_Icons64x64_1();
$frame->add($pagerPrev);
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2);
$pagerPrev->setSize($pagerSize, $pagerSize);
2014-06-22 19:02:18 +02:00
$pagerPrev->setSubStyle($pagerPrev::SUBSTYLE_ArrowPrev);
2014-01-05 20:34:48 +01:00
$pagerNext = new Quad_Icons64x64_1();
$frame->add($pagerNext);
$pagerNext->setPosition($width * 0.45, $height * -0.44, 2);
$pagerNext->setSize($pagerSize, $pagerSize);
2014-06-22 19:02:18 +02:00
$pagerNext->setSubStyle($pagerNext::SUBSTYLE_ArrowNext);
2014-01-05 20:34:48 +01:00
2014-05-02 17:50:30 +02:00
$paging->addButton($pagerNext);
$paging->addButton($pagerPrev);
2014-01-05 20:34:48 +01:00
2014-06-22 19:02:18 +02:00
$pageCountLabel = new Label_Text();
$frame->add($pageCountLabel);
2014-06-22 19:02:18 +02:00
$pageCountLabel->setHAlign($pageCountLabel::RIGHT);
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1);
2014-06-22 19:02:18 +02:00
$pageCountLabel->setStyle($pageCountLabel::STYLE_TextTitle1);
$pageCountLabel->setTextSize(2);
2014-01-05 20:34:48 +01:00
2014-05-02 17:50:30 +02:00
$paging->setLabel($pageCountLabel);
2014-01-05 20:34:48 +01:00
// Setting pages
2014-05-15 17:45:08 +02:00
$pageFrame = null;
$posY = 0.;
2014-05-15 17:45:08 +02:00
2014-05-02 17:50:30 +02:00
foreach ($scriptParams as $index => $scriptParam) {
2014-01-17 17:00:06 +01:00
/** @var \Maniaplanet\DedicatedServer\Structures\ScriptSettings $scriptParam */
$settingName = $scriptParam->name;
2014-01-05 20:34:48 +01:00
2014-02-01 20:03:17 +01:00
if (!isset($scriptSettings[$settingName])) {
2014-01-02 23:49:21 +01:00
continue;
}
2014-01-05 20:34:48 +01:00
2014-05-15 17:45:08 +02:00
if ($index % 13 === 0) {
$pageFrame = new Frame();
$frame->add($pageFrame);
$posY = $height * 0.41;
2014-05-02 17:50:30 +02:00
$paging->addPage($pageFrame);
}
2014-01-05 20:34:48 +01:00
2013-12-09 09:07:55 +01:00
$settingFrame = new Frame();
$pageFrame->add($settingFrame);
$settingFrame->setY($posY);
2014-01-05 20:34:48 +01:00
$nameLabel = new Label_Text();
2013-12-09 09:07:55 +01:00
$settingFrame->add($nameLabel);
2014-06-15 02:26:50 +02:00
$nameLabel->setHAlign($nameLabel::LEFT);
2013-12-09 09:07:55 +01:00
$nameLabel->setX($width * -0.46);
$nameLabel->setSize($width * 0.4, $settingHeight);
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
$nameLabel->setTextSize($labelTextSize);
$nameLabel->setText($settingName);
2014-01-05 20:34:48 +01:00
2013-12-09 09:07:55 +01:00
$settingValue = $scriptSettings[$settingName];
2014-01-05 20:34:48 +01:00
2014-06-15 02:26:50 +02:00
if (is_bool($settingValue)) {
// Boolean checkbox
$quad = new Quad();
2013-12-30 20:12:53 +01:00
$quad->setX($width / 2 * 0.545);
$quad->setSize(4, 4);
2014-06-15 02:26:50 +02:00
$checkBox = new CheckBox(self::ACTION_PREFIX_SETTING . $settingName, $settingValue, $quad);
$settingFrame->add($checkBox);
2014-01-05 20:34:48 +01:00
} else {
2014-06-15 02:26:50 +02:00
// Value entry
2013-12-30 20:12:53 +01:00
$entry = new Entry();
$settingFrame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
2013-12-31 13:51:30 +01:00
$entry->setX($width / 2 * 0.55);
2013-12-30 20:12:53 +01:00
$entry->setTextSize(1);
$entry->setSize($width * 0.3, $settingHeight * 0.9);
2014-06-15 02:26:50 +02:00
$entry->setName(self::ACTION_PREFIX_SETTING . $settingName);
2013-12-30 20:12:53 +01:00
$entry->setDefault($settingValue);
2013-12-09 09:07:55 +01:00
}
2014-01-05 20:34:48 +01:00
$descriptionLabel = new Label();
$pageFrame->add($descriptionLabel);
2014-06-22 19:02:18 +02:00
$descriptionLabel->setHAlign($descriptionLabel::LEFT);
$descriptionLabel->setPosition($width * -0.45, $height * -0.44);
$descriptionLabel->setSize($width * 0.7, $settingHeight);
2013-12-30 23:53:42 +01:00
$descriptionLabel->setTextSize($labelTextSize);
$descriptionLabel->setTranslate(true);
2014-01-17 17:00:06 +01:00
$descriptionLabel->setText($scriptParam->desc);
2014-05-02 17:50:30 +02:00
$nameLabel->addTooltipFeature($descriptionLabel);
2014-01-05 20:34:48 +01:00
$posY -= $settingHeight;
}
2014-01-05 20:34:48 +01:00
return $frame;
}
2013-12-30 20:12:53 +01:00
/**
2014-06-15 02:26:50 +02:00
* @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData()
2013-12-30 20:12:53 +01:00
*/
2014-06-15 02:26:50 +02:00
public function saveConfigData(array $configData, Player $player) {
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CHANGE_SCRIPT_SETTINGS)) {
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
2014-06-15 02:26:50 +02:00
return;
}
if (!$configData[3] || strpos($configData[3][0]['Name'], self::ACTION_PREFIX_SETTING) !== 0) {
2014-01-02 23:49:21 +01:00
return;
2014-01-04 17:30:29 +01:00
}
2014-01-05 20:34:48 +01:00
2014-02-13 00:26:18 +01:00
try {
$scriptSettings = $this->maniaControl->getClient()->getModeScriptSettings();
} catch (GameModeException $e) {
2014-04-19 22:59:11 +02:00
return;
2014-02-13 00:26:18 +01:00
}
2014-06-15 02:26:50 +02:00
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
$newSettings = array();
foreach ($configData[3] as $setting) {
$settingName = substr($setting['Name'], $prefixLength);
if (!isset($scriptSettings[$settingName])) {
var_dump('no setting ' . $settingName);
continue;
}
if ($setting['Value'] == $scriptSettings[$settingName]) {
// Not changed
continue;
}
$newSettings[$settingName] = $setting['Value'];
settype($newSettings[$settingName], gettype($scriptSettings[$settingName]));
2013-12-30 20:12:53 +01:00
}
2014-01-05 20:34:48 +01:00
2014-06-15 02:26:50 +02:00
$success = $this->applyNewScriptSettings($newSettings, $player);
if ($success) {
$this->maniaControl->getChat()->sendSuccess('Script Settings saved!', $player);
2014-06-15 02:26:50 +02:00
} else {
$this->maniaControl->getChat()->sendError('Script Settings Saving failed!', $player);
2014-06-15 02:26:50 +02:00
}
2014-01-05 20:34:48 +01:00
// Reopen the Menu
$this->maniaControl->getConfigurator()->showMenu($player, $this);
}
/**
* Apply the Array of new Script Settings
*
2014-01-05 20:34:48 +01:00
* @param array $newSettings
* @param Player $player
2014-06-15 02:26:50 +02:00
* @return bool
*/
private function applyNewScriptSettings(array $newSettings, Player $player) {
if (empty($newSettings)) {
2014-04-19 22:59:11 +02:00
return true;
}
2014-01-20 20:51:03 +01:00
$this->maniaControl->getClient()->setModeScriptSettings($newSettings);
2014-01-05 20:34:48 +01:00
2014-01-04 17:30:29 +01:00
// Save Settings into Database
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-01-05 20:34:48 +01:00
$query = "INSERT INTO `" . self::TABLE_SCRIPT_SETTINGS . "` (
2014-01-05 14:41:19 +01:00
`serverIndex`,
2014-01-04 17:30:29 +01:00
`settingName`,
`settingValue`
) VALUES (
?, ?, ?
) ON DUPLICATE KEY UPDATE
`settingValue` = VALUES(`settingValue`);";
$statement = $mysqli->prepare($query);
2014-02-01 20:03:17 +01:00
if ($mysqli->error) {
2014-01-04 17:30:29 +01:00
trigger_error($mysqli->error);
return false;
}
$settingName = null;
$settingValue = null;
$statement->bind_param('iss', $this->maniaControl->getServer()->index, $settingName, $settingValue);
2014-01-05 20:34:48 +01:00
// Notifications
2013-12-31 16:12:23 +01:00
$settingsCount = count($newSettings);
2014-01-05 20:34:48 +01:00
$settingIndex = 0;
$title = $this->maniaControl->getAuthenticationManager()->getAuthLevelName($player);
$chatMessage = '$ff0' . $title . ' '. $player->getEscapedNickname() . ' set ScriptSetting' . ($settingsCount > 1 ? 's' : '') . ' ';
2014-05-02 17:50:30 +02:00
foreach ($newSettings as $setting => $value) {
2013-12-31 15:09:08 +01:00
$chatMessage .= '$<' . '$fff' . preg_replace('/^S_/', '', $setting) . '$z$s$ff0 ';
2013-12-31 16:12:23 +01:00
$chatMessage .= 'to $fff' . $this->parseSettingValue($value) . '$>';
2014-01-05 20:34:48 +01:00
2014-02-01 20:03:17 +01:00
if ($settingIndex <= $settingsCount - 2) {
2013-12-31 16:12:23 +01:00
$chatMessage .= ', ';
}
2014-01-05 20:34:48 +01:00
2014-01-05 12:17:29 +01:00
// Add To Database
$settingName = $setting;
$settingValue = $value;
2014-01-04 17:30:29 +01:00
$statement->execute();
2014-02-01 20:03:17 +01:00
if ($statement->error) {
2014-01-04 17:30:29 +01:00
trigger_error($statement->error);
}
2014-01-05 20:34:48 +01:00
// Trigger own callback
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SCRIPTSETTING_CHANGED, $setting, $value);
2014-01-05 20:34:48 +01:00
2013-12-31 16:12:23 +01:00
$settingIndex++;
}
2014-01-04 17:30:29 +01:00
$statement->close();
2014-01-05 20:34:48 +01:00
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SCRIPTSETTINGS_CHANGED);
2014-01-05 20:34:48 +01:00
2013-12-31 16:12:23 +01:00
$chatMessage .= '!';
$this->maniaControl->getChat()->sendInformation($chatMessage);
Logger::logInfo($chatMessage, true);
2014-01-04 17:30:29 +01:00
return true;
}
/**
* Parse the Setting Value to a String Representation
*
* @param mixed $value
* @return string
*/
private function parseSettingValue($value) {
2014-02-01 20:03:17 +01:00
if (is_bool($value)) {
return ($value ? 'True' : 'False');
}
2014-01-05 20:34:48 +01:00
return (string)$value;
}
}