TrackManiaControl/application/core/Configurators/ScriptSettings.php

481 lines
14 KiB
PHP
Raw Normal View History

<?php
namespace ManiaControl\Configurators;
2014-01-02 23:49:21 +01:00
use FML\Controls\Control;
use FML\Controls\Entry;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Labels\Label_Text;
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;
use ManiaControl\Callbacks\CallbackManager;
2014-04-28 20:50:38 +02:00
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\ManiaControl;
2014-04-19 22:59:11 +02:00
use ManiaControl\Maps\Map;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
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-04-19 22:59:11 +02:00
const ACTION_PREFIX_SETTING = 'ScriptSetting';
const ACTION_SETTING_BOOL = 'ScriptSetting.ActionBoolSetting.';
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
*/
private $maniaControl = null;
/**
* Create 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-01-05 20:34:48 +01:00
2013-12-31 13:51:30 +01:00
// Register for callbacks
2014-01-05 20:34:48 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
2014-02-19 12:53:06 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'onInit');
2014-04-28 20:50:38 +02:00
$this->maniaControl->callbackManager->registerCallbackListener(Callbacks::BEGINMAP, $this, 'onBeginMap');
$this->maniaControl->settingManager->initSetting($this, self::SETTING_LOAD_DEFAULT_SETTINGS_MAP_BEGIN, true);
2014-01-04 17:30:29 +01:00
$this->initTables();
2014-04-13 19:05:54 +02:00
//Permission for Change Script-Settings
$this->maniaControl->authenticationManager->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
*
* @return boolean
*/
2014-01-04 17:30:29 +01:00
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
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;
}
/**
* 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->client->getModeScriptSettings();
} catch (GameModeException $e) {
2014-04-19 22:59:11 +02:00
return false;
}
2014-01-17 17:09:25 +01:00
2014-01-13 18:24:03 +01:00
$mysqli = $this->maniaControl->database->mysqli;
$serverId = $this->maniaControl->server->index;
$query = "SELECT * FROM `" . self::TABLE_SCRIPT_SETTINGS . "` WHERE serverIndex = " . $serverId . ";";
$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->close();
2014-02-01 20:03:17 +01:00
if (!$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
2014-01-20 20:51:03 +01:00
try {
$this->maniaControl->client->setModeScriptSettings($loadedSettings);
2014-05-02 17:50:30 +02:00
} catch (Exception $e) {
2014-05-02 15:26:07 +02:00
trigger_error('Error occurred: ' . $e->getMessage());
2014-01-04 17:30:29 +01:00
return false;
}
return true;
}
/**
2014-05-02 17:50:30 +02:00
* Handle OnBegin Map Callback
*
* @param Map $map
*/
2014-05-02 17:50:30 +02:00
public function onBeginMap(Map $map) {
2014-05-13 16:03:26 +02:00
if ($this->maniaControl->settingManager->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->client->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->client->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
$pageMaxCount = 13;
// Pagers
$pagerPrev = new Quad_Icons64x64_1();
$frame->add($pagerPrev);
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2);
$pagerPrev->setSize($pagerSize, $pagerSize);
$pagerPrev->setSubStyle(Quad_Icons64x64_1::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);
$pagerNext->setSubStyle(Quad_Icons64x64_1::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
$pageCountLabel = new Label();
$frame->add($pageCountLabel);
$pageCountLabel->setHAlign(Control::RIGHT);
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1);
$pageCountLabel->setStyle('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
$pageFrames = array();
2014-01-05 20:34:48 +01:00
$y = 0.;
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-02-01 20:03:17 +01:00
if (!isset($pageFrame)) {
$pageFrame = new Frame();
$frame->add($pageFrame);
2014-02-01 20:03:17 +01:00
if (!empty($pageFrames)) {
2013-12-31 13:51:30 +01:00
$pageFrame->setVisible(false);
}
array_push($pageFrames, $pageFrame);
2013-12-09 09:07:55 +01:00
$y = $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($y);
2014-01-05 20:34:48 +01:00
$nameLabel = new Label_Text();
2013-12-09 09:07:55 +01:00
$settingFrame->add($nameLabel);
$nameLabel->setHAlign(Control::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
2013-12-30 20:12:53 +01:00
$substyle = '';
2014-02-01 20:03:17 +01:00
if ($settingValue === false) {
2013-12-30 20:12:53 +01:00
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlRed;
2014-02-01 20:03:17 +01:00
} else if ($settingValue === true) {
2013-12-30 20:12:53 +01:00
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlGreen;
}
2014-01-05 20:34:48 +01:00
2014-02-01 20:03:17 +01:00
if ($substyle != '') {
2013-12-30 20:12:53 +01:00
$quad = new Quad_Icons64x64_1();
$settingFrame->add($quad);
$quad->setX($width / 2 * 0.545);
$quad->setZ(-0.01);
$quad->setSubStyle($substyle);
$quad->setSize(4, 4);
$quad->setHAlign(Control::CENTER);
$quad->setAction(self::ACTION_SETTING_BOOL . $settingName);
2014-01-05 20:34:48 +01:00
} else {
2013-12-30 20:12:53 +01:00
$entry = new Entry();
$settingFrame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
$entry->setHAlign(Control::CENTER);
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-01-05 20:34:48 +01: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);
$descriptionLabel->setHAlign(Control::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
$y -= $settingHeight;
2014-02-01 20:03:17 +01:00
if ($index % $pageMaxCount == $pageMaxCount - 1) {
2013-12-31 11:05:14 +01:00
unset($pageFrame);
}
}
2014-01-05 20:34:48 +01:00
return $frame;
}
2013-12-30 20:12:53 +01:00
/**
* Handle ManialinkPageAnswer Callback
2013-12-31 13:51:30 +01:00
*
2013-12-30 20:12:53 +01:00
* @param array $callback
*/
2013-12-31 13:51:30 +01:00
public function handleManialinkPageAnswer(array $callback) {
2014-01-05 20:34:48 +01:00
$actionId = $callback[1][2];
2013-12-30 20:12:53 +01:00
$boolSetting = (strpos($actionId, self::ACTION_SETTING_BOOL) === 0);
2014-02-01 20:03:17 +01:00
if (!$boolSetting) {
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
2013-12-30 20:12:53 +01:00
$actionArray = explode(".", $actionId);
2014-01-05 20:34:48 +01:00
$setting = $actionArray[2];
$login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
2014-01-05 20:34:48 +01:00
2014-01-05 12:17:29 +01:00
// Toggle the Boolean Setting
$this->toggleBooleanSetting($setting, $player);
2014-01-05 20:37:01 +01:00
// Save all Changes
$this->saveConfigData($callback[1], $player);
2013-12-30 20:12:53 +01:00
}
/**
* Toggle a Boolean Setting
2013-12-31 13:51:30 +01:00
*
2013-12-30 20:12:53 +01:00
* @param Player $player
2014-01-05 20:34:48 +01:00
* @param $setting
2013-12-30 20:12:53 +01:00
*/
public function toggleBooleanSetting($setting, Player $player) {
2014-02-13 00:26:18 +01:00
try {
$scriptSettings = $this->maniaControl->client->getModeScriptSettings();
} catch (GameModeException $e) {
2014-04-19 22:59:11 +02:00
return;
2014-02-13 00:26:18 +01:00
}
2014-02-01 20:03:17 +01:00
if (!isset($scriptSettings[$setting])) {
var_dump('no setting ' . $setting);
return;
2013-12-30 20:12:53 +01:00
}
2014-01-05 20:34:48 +01:00
$newSettings = array();
$newSettings[$setting] = ($scriptSettings[$setting] ? false : true);
2014-01-05 20:34:48 +01:00
$this->applyNewScriptSettings($newSettings, $player);
}
/**
* Apply the Array of new Script Settings
*
2014-01-05 20:34:48 +01:00
* @param array $newSettings
* @param Player $player
* @return bool
*/
private function applyNewScriptSettings(array $newSettings, Player $player) {
2014-04-19 22:59:11 +02:00
if (!$newSettings) {
return true;
}
2014-01-20 20:51:03 +01:00
try {
2014-02-01 20:03:17 +01:00
$this->maniaControl->client->setModeScriptSettings($newSettings);
2014-05-02 17:50:30 +02:00
} catch (Exception $e) {
2014-04-19 22:59:11 +02:00
//TODO temp added 19.04.2014
$this->maniaControl->errorHandler->triggerDebugNotice("Exception line 416 ScriptSettings.php" . $e->getMessage());
2014-01-20 20:51:03 +01:00
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
2014-01-04 17:30:29 +01:00
return false;
2013-12-30 20:12:53 +01:00
}
2014-01-05 20:34:48 +01:00
2014-01-04 17:30:29 +01:00
// Save Settings into Database
2014-01-05 20:34:48 +01:00
$mysqli = $this->maniaControl->database->mysqli;
$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;
}
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->authenticationManager->getAuthLevelName($player->authLevel);
$chatMessage = '$ff0' . $title . ' $<' . $player->nickname . '$> 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
$statement->bind_param('iss', $this->maniaControl->server->index, $setting, $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->callbackManager->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->callbackManager->triggerCallback(self::CB_SCRIPTSETTINGS_CHANGED);
2014-01-05 20:34:48 +01:00
2013-12-31 16:12:23 +01:00
$chatMessage .= '!';
$this->maniaControl->chat->sendInformation($chatMessage);
2014-01-05 19:15:58 +01:00
$this->maniaControl->log($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;
}
2014-05-02 17:50:30 +02:00
/**
* @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData()
*/
public function saveConfigData(array $configData, Player $player) {
if (!$this->maniaControl->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_CHANGE_SCRIPT_SETTINGS)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
if (!$configData[3] || strpos($configData[3][0]['Name'], self::ACTION_PREFIX_SETTING) !== 0) {
return;
}
try {
$scriptSettings = $this->maniaControl->client->getModeScriptSettings();
} catch (GameModeException $e) {
2014-05-02 17:50:30 +02:00
return;
}
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
$newSettings = array();
foreach ($configData[3] as $setting) {
$settingName = substr($setting['Name'], $prefixLength + 1);
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]));
}
$this->applyNewScriptSettings($newSettings, $player);
//Reopen the Menu
$menuId = $this->maniaControl->configurator->getMenuId($this->getTitle());
$this->maniaControl->configurator->reopenMenu($player, $menuId);
}
/**
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
*/
public function getTitle() {
return 'Script Settings';
}
}