TrackManiaControl/core/Configurator/GameModeSettings.php

675 lines
21 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\Communication\CommunicationAnswer;
use ManiaControl\Communication\CommunicationListener;
use ManiaControl\Communication\CommunicationMethods;
use ManiaControl\Logger;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
2020-04-28 17:52:17 +02:00
use Maniaplanet\DedicatedServer\Structures\GameInfos;
2017-02-11 23:26:28 +01:00
use Maniaplanet\DedicatedServer\Xmlrpc\FaultException;
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
/**
2020-04-28 17:52:17 +02:00
* Class offering a Configurator for Mode Settings
*
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-19 22:59:11 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2020-04-28 17:52:17 +02:00
class GameModeSettings implements ConfiguratorMenu, CallbackListener, CommunicationListener {
/*
* Constants
*/
2020-04-28 17:52:17 +02:00
const ACTION_PREFIX_SETTING = 'GameModeSetting.';
const CB_GAMEMODESETTING_CHANGED = 'GameModeSettings.SettingChanged';
const CB_GAMEMODESETTINGS_CHANGED = 'GameModeSettings.SettingsChanged';
/** @deprecated */
const CB_SCRIPTSETTING_CHANGED = 'GameModeSettings.SettingChanged';
/** @deprecated */
const CB_SCRIPTSETTINGS_CHANGED = 'GameModeSettings.SettingsChanged';
const TABLE_GAMEMODE_SETTINGS = 'mc_gamemodesettings';
/** @deprecated */
2014-04-19 22:59:11 +02:00
const TABLE_SCRIPT_SETTINGS = 'mc_scriptsettings';
2020-04-28 17:52:17 +02:00
const SETTING_LOAD_DEFAULT_SETTINGS_MAP_BEGIN = 'Load Stored GameMode-Settings on Map-Begin';
const SETTING_PERMISSION_CHANGE_MODE_SETTINGS = 'Change GameMode-Settings';
/** @deprecated */
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
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::BEGINMAP, $this, 'onBeginMap');
// Settings
2014-08-13 11:05:52 +02:00
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_LOAD_DEFAULT_SETTINGS_MAP_BEGIN, false);
2014-04-13 19:05:54 +02:00
// Permissions
2020-04-28 17:52:17 +02:00
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_CHANGE_MODE_SETTINGS, AuthenticationManager::AUTH_LEVEL_ADMIN);
//TODO remove to somewhere cleaner
//Communication Listenings
$this->initalizeCommunicationListenings();
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() {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2020-04-28 17:52:17 +02:00
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_GAMEMODE_SETTINGS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`serverIndex` int(11) NOT NULL,
2017-03-09 21:02:03 +01:00
`settingName` varchar(100) NOT NULL DEFAULT '',
`settingValue` varchar(500) NOT NULL DEFAULT '',
PRIMARY KEY (`index`),
2014-01-05 14:41:19 +01:00
UNIQUE KEY `setting` (`serverIndex`, `settingName`)
2020-04-28 17:52:17 +02:00
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Mode 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;
}
2017-03-09 21:02:03 +01:00
2020-04-28 17:52:17 +02:00
$dropQuery = "DROP TABLE IF EXISTS `" . self::TABLE_SCRIPT_SETTINGS . "`;";
$result = $mysqli->query($dropQuery);
2017-03-09 21:02:03 +01:00
if (!$result) {
trigger_error($mysqli->error);
return false;
}
2014-01-04 17:30:29 +01:00
$statement->close();
return true;
}
/**
2017-03-09 21:02:03 +01:00
* @see \ManiaControl\Configurator\ConfiguratorMenu::getTitle()
*/
public static function getTitle() {
2020-04-28 17:52:17 +02:00
return 'GameMode-Settings';
}
/**
* Get the settings of the GameMode into an Array
* @return array|false
*/
public function getGameModeSettingsArray() {
if ($this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
return $this->maniaControl->getClient()->getModeScriptSettings();
} else {
$gameModeSettings = $this->maniaControl->getClient()->getGameInfos();
$currentGameModeSettings = get_object_vars($gameModeSettings['CurrentGameInfos']);
unset($gameModeSettings['CurrentGameInfos']);
foreach ($currentGameModeSettings as $name => $value) {
unset($currentGameModeSettings[$name]);
$currentGameModeSettings[ucfirst($name)] = $value;
}
$gameModeSettings[0] = $currentGameModeSettings;
$nextGameModeSettings = get_object_vars($gameModeSettings['NextGameInfos']);
unset($gameModeSettings['NextGameInfos']);
foreach ($nextGameModeSettings as $name => $value) {
unset($nextGameModeSettings[$name]);
$nextGameModeSettings[ucfirst($name)] = $value;
2020-04-28 17:52:17 +02:00
}
$gameModeSettings[1] = $nextGameModeSettings;
2020-04-28 17:52:17 +02:00
return $gameModeSettings;
}
}
/**
* Set the settings of the GameMode from an Array.
* Returns true, if successful.
* @param array $settings
* @return bool
*/
public function setGameModeSettingsArray(array $settings) {
if ($this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
return $this->maniaControl->getClient()->setModeScriptSettings($settings);
} else {
$success = true;
foreach ($settings as $key => $value) {
$success &= $this->maniaControl->getClient()->execute('Set'.$key, array($value));
}
return $success;
}
}
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() {
2020-04-28 17:52:17 +02:00
$gameModeSettings = null;
try {
2020-04-28 17:52:17 +02:00
$gameModeSettings = $this->getGameModeSettingsArray();
if (!$this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
$gameModeSettings = $gameModeSettings[0];
}
2020-04-28 17:52:17 +02:00
} catch (\Exception $e) {
2014-04-19 22:59:11 +02:00
return false;
}
2014-01-17 17:09:25 +01:00
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$serverIndex = $this->maniaControl->getServer()->index;
2020-04-28 17:52:17 +02:00
$query = "SELECT * FROM `" . self::TABLE_GAMEMODE_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()) {
2020-04-28 17:52:17 +02:00
if (!isset($gameModeSettings[$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;
2020-04-28 17:52:17 +02:00
settype($loadedSettings[$row->settingName], gettype($gameModeSettings[$row->settingName]));
2014-01-04 17:30:29 +01:00
}
$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
2020-04-28 17:52:17 +02:00
try {
$this->setGameModeSettingsArray($loadedSettings);
} catch (\Exception $e) {
return false;
}
return true;
}
/**
* 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();
}
}
/**
2017-03-09 21:02:03 +01:00
* @see \ManiaControl\Configurator\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
2020-04-28 17:52:17 +02:00
$scriptParams = null;
$gameModeSettings = null;
$error = null;
2014-02-13 00:26:18 +01:00
try {
2020-04-28 17:52:17 +02:00
$gameModeSettings = $this->getGameModeSettingsArray();
if ($this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
$scriptInfo = $this->maniaControl->getClient()->getModeScriptInfo();
$scriptParams = $scriptInfo->paramDescs;
} else {
$scriptParams = $gameModeSettings[0];
2020-04-28 17:52:17 +02:00
}
} catch (\Exception $e) {
2014-04-20 15:47:35 +02:00
$label = new Label();
2017-03-25 19:15:50 +01:00
$frame->addChild($label);
2014-04-20 15:47:35 +02:00
$label->setText($e->getMessage());
return $frame;
2014-02-13 00:26:18 +01:00
}
2014-01-17 17:00:06 +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();
2017-03-25 19:15:50 +01:00
$frame->addChild($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();
2017-03-25 19:15:50 +01:00
$frame->addChild($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
2017-03-25 18:40:15 +01:00
$paging->addButtonControl($pagerNext);
$paging->addButtonControl($pagerPrev);
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.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
if (!$this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
$descriptionLabel = new Label();
$frame->addChild($descriptionLabel);
$descriptionLabel->setHorizontalAlign($descriptionLabel::LEFT);
$descriptionLabel->setPosition($width * -0.45, $height * -0.44);
$descriptionLabel->setSize($width * 0.7, $settingHeight);
$descriptionLabel->setText('Changes only apply with map skip/restart');
$descriptionLabel->setTextColor('ff0');
$descriptionLabel->setTextSize($labelTextSize);
$descriptionLabel->setTranslate(true);
}
// Setting pages
2014-05-15 17:45:08 +02:00
$pageFrame = null;
$posY = 0.;
2020-04-28 17:52:17 +02:00
$index = 0;
2014-05-15 17:45:08 +02:00
2020-04-28 17:52:17 +02:00
foreach ($scriptParams as $key => $scriptParam) {
$settingName = null;
$settingValue = null;
2020-04-28 17:52:17 +02:00
if ($this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
$settingName = $scriptParam->name;
if (!isset($gameModeSettings[$settingName])) {
continue;
}
$settingValue = $gameModeSettings[$settingName];
2020-04-28 17:52:17 +02:00
} else {
$settingName = $key;
if (!isset($gameModeSettings[0][$settingName]) && !isset($gameModeSettings[1][$settingName])) {
continue;
}
$settingValue = array(
0 => $gameModeSettings[0][$settingName],
1 => $gameModeSettings[1][$settingName]
);
2014-01-02 23:49:21 +01:00
}
2014-01-05 20:34:48 +01:00
2014-05-15 17:45:08 +02:00
if ($index % 13 === 0) {
$pageFrame = new Frame();
2017-03-25 19:15:50 +01:00
$frame->addChild($pageFrame);
$posY = 0.41 * $height;
2017-03-25 18:40:15 +01:00
$paging->addPageControl($pageFrame);
}
2014-01-05 20:34:48 +01:00
2013-12-09 09:07:55 +01:00
$settingFrame = new Frame();
2017-03-25 19:15:50 +01:00
$pageFrame->addChild($settingFrame);
$settingFrame->setY($posY);
2014-01-05 20:34:48 +01:00
$nameLabel = new Label_Text();
2017-03-25 19:15:50 +01:00
$settingFrame->addChild($nameLabel);
$nameLabel->setHorizontalAlign($nameLabel::LEFT);
$nameLabel->setSize(0.4 * $width, $settingHeight);
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
$nameLabel->setText($settingName);
2020-04-28 17:52:17 +02:00
$nameLabel->setTextSize($labelTextSize);
$nameLabel->setX(-0.46 * $width);
if (!$this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
if (is_bool($settingValue[0])) {
$activeQuad = new Quad_Icons64x64_1();
$settingFrame->addChild($activeQuad);
$activeQuad->setSize(0.9 * $settingHeight, 0.9 * $settingHeight);
if ($settingValue[0]) {
$activeQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_LvlGreen);
} else {
$activeQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_LvlRed);
}
$activeQuad->setX(0.1 * $width);
} else {
$currentLabel = new Label_Text();
$settingFrame->addChild($currentLabel);
$currentLabel->setHorizontalAlign(Label_Text::RIGHT);
$currentLabel->setSize(0.2 * $width, 0.9 * $settingHeight);
$currentLabel->setStyle(Label_Text::STYLE_TextValueSmall);
$currentLabel->setText($settingValue[0]);
$currentLabel->setTextColor('aaa');
$currentLabel->setTextPrefix('$i');
$currentLabel->setTextSize(1);
$currentLabel->setX(0.11 * $width);
}
2014-01-05 20:34:48 +01:00
$settingValue = $settingValue[1];
}
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->setSize(4, 4);
$quad->setX(0.27 * $width);
2014-06-15 02:26:50 +02:00
$checkBox = new CheckBox(self::ACTION_PREFIX_SETTING . $settingName, $settingValue, $quad);
2017-03-25 19:15:50 +01:00
$settingFrame->addChild($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();
2017-03-25 19:15:50 +01:00
$settingFrame->addChild($entry);
2020-04-28 17:52:17 +02:00
$entry->setDefault($settingValue);
$entry->setName(self::ACTION_PREFIX_SETTING . $settingName);
$entry->setSize(0.3 * $width, 0.9 * $settingHeight);
2013-12-30 20:12:53 +01:00
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
$entry->setTextSize(1);
$entry->setX(0.275 * $width);
2013-12-09 09:07:55 +01:00
}
2014-01-05 20:34:48 +01:00
2020-04-28 17:52:17 +02:00
if ($this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
$descriptionLabel = new Label();
$pageFrame->addChild($descriptionLabel);
$descriptionLabel->setHorizontalAlign($descriptionLabel::LEFT);
$descriptionLabel->setPosition(-0.45 * $width, -0.44 * $height);
$descriptionLabel->setSize(0.7 * $width, $settingHeight);
2020-04-28 17:52:17 +02:00
$descriptionLabel->setTextSize($labelTextSize);
$descriptionLabel->setTranslate(true);
$nameLabel->addTooltipLabelFeature($descriptionLabel, $scriptParam->desc);
}
2014-01-05 20:34:48 +01:00
$posY -= $settingHeight;
2020-04-28 17:52:17 +02:00
$index++;
}
2014-01-05 20:34:48 +01:00
return $frame;
}
2013-12-30 20:12:53 +01:00
/**
2017-03-09 21:02:03 +01:00
* @see \ManiaControl\Configurator\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) {
2020-04-28 17:52:17 +02:00
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CHANGE_MODE_SETTINGS)) {
2014-08-13 11:05:52 +02:00
$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
}
2020-04-28 17:52:17 +02:00
$gameModeSettings = null;
2014-02-13 00:26:18 +01:00
try {
2020-04-28 17:52:17 +02:00
$gameModeSettings = $this->getGameModeSettingsArray();
if (!$this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
$gameModeSettings = $gameModeSettings[0];
}
2020-04-28 17:52:17 +02:00
} catch (\Exception $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);
2020-04-28 17:52:17 +02:00
if (!isset($gameModeSettings[$settingName])) {
2014-06-15 02:26:50 +02:00
var_dump('no setting ' . $settingName);
continue;
}
2020-04-28 17:52:17 +02:00
if ($setting['Value'] == $gameModeSettings[$settingName]) {
2014-06-15 02:26:50 +02:00
// Not changed
continue;
}
$newSettings[$settingName] = $setting['Value'];
2020-04-28 17:52:17 +02:00
settype($newSettings[$settingName], gettype($gameModeSettings[$settingName]));
2013-12-30 20:12:53 +01:00
}
2014-01-05 20:34:48 +01:00
2020-04-28 17:52:17 +02:00
$success = $this->applyNewModeSettings($newSettings, $player);
2014-06-15 02:26:50 +02:00
if ($success) {
2020-04-28 17:52:17 +02:00
$this->maniaControl->getChat()->sendSuccess('GameMode-Settings saved!', $player);
2014-06-15 02:26:50 +02:00
} else {
2020-04-28 17:52:17 +02:00
$this->maniaControl->getChat()->sendError('GameMode-Settings Saving failed!', $player);
2014-06-15 02:26:50 +02:00
}
2014-01-05 20:34:48 +01:00
// Reopen the Menu
2014-08-13 11:05:52 +02:00
$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
*/
2020-04-28 17:52:17 +02:00
private function applyNewModeSettings(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
2017-02-11 23:26:28 +01:00
try {
2020-04-28 17:52:17 +02:00
$success = $this->setGameModeSettingsArray($newSettings);
if (!$success) {
return false;
}
} catch (\Exception $e) {
$this->maniaControl->getChat()->sendException($e, $player);
2017-02-11 23:26:28 +01:00
return false;
}
2014-01-04 17:30:29 +01:00
// Save Settings into Database
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2020-04-28 17:52:17 +02:00
$query = "INSERT INTO `" . self::TABLE_GAMEMODE_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;
2014-08-13 11:05:52 +02:00
$title = $this->maniaControl->getAuthenticationManager()->getAuthLevelName($player);
2020-04-28 17:52:17 +02:00
$chatMessage = '$ff0' . $title . ' ' . $player->getEscapedNickname() . ' set GameMode-Setting' . ($settingsCount > 1 ? 's' : '') . ' ';
foreach ($newSettings as $settingName => $settingValue) {
$chatMessage .= '$<' . '$fff' . preg_replace('/^S_/', '', $settingName) . '$z$ff0 ';
$chatMessage .= 'to $fff' . $this->parseSettingValue($settingValue) . '$>';
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
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
2020-04-28 17:52:17 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_GAMEMODESETTING_CHANGED, $settingName, $settingValue);
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
2020-04-28 17:52:17 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_GAMEMODESETTINGS_CHANGED);
2014-01-05 20:34:48 +01:00
2013-12-31 16:12:23 +01:00
$chatMessage .= '!';
2014-08-13 11:05:52 +02:00
$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');
}
return (string) $value;
}
/**
* Initializes the communication Listenings
*/
private function initalizeCommunicationListenings() {
2020-04-28 17:52:17 +02:00
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_GAMEMODE_SETTINGS, $this, function ($data) {
try {
$gameModeSettings = $this->getGameModeSettingsArray();
} catch (\Exception $e) {
return new CommunicationAnswer($e->getMessage(), true);
}
return new CommunicationAnswer($gameModeSettings);
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::SET_GAMEMODE_SETTINGS, $this, function ($data) {
if (!is_object($data) || !property_exists($data, "gameModeSettings")) {
return new CommunicationAnswer("No valid GameMode-Settings provided!", true);
}
$gameModeSettings = null;
try {
$gameModeSettings = $this->getGameModeSettingsArray();
if (!$this->maniaControl->getServer()->getScriptManager()->isScriptMode()) {
$gameModeSettings = $gameModeSettings[0];
}
2020-04-28 17:52:17 +02:00
} catch (\Exception $e) {
return new CommunicationAnswer($e->getMessage(), true);
}
$newSettings = array();
foreach ($data->gameModeSettings as $name => $value) {
if (!isset($gameModeSettings[$name])) {
var_dump('no setting ' . $name);
continue;
}
if ($value == $gameModeSettings[$name]) {
// unchanged
continue;
}
$newSettings[$name] = $value;
settype($newSettings[$name], gettype($gameModeSettings[$name]));
}
// No new Settings
if (empty($newSettings)) {
return new CommunicationAnswer(array("success" => true));
}
// Trigger GameModeSettings Changed Callback
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_GAMEMODESETTINGS_CHANGED);
// Set the Settings
try {
$success = $this->setGameModeSettingsArray($newSettings);
return new CommunicationAnswer(array("success" => $success));
} catch (\Exception $e) {
return new CommunicationAnswer($e->getMessage(), true);
}
});
/** @deprecated */
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_SCRIPT_SETTINGS, $this, function ($data) {
try {
$scriptSettings = $this->maniaControl->getClient()->getModeScriptSettings();
2020-04-28 17:52:17 +02:00
} catch (\Exception $e) {
return new CommunicationAnswer($e->getMessage(), true);
}
return new CommunicationAnswer($scriptSettings);
});
2020-04-28 17:52:17 +02:00
/** @deprecated */
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::SET_SCRIPT_SETTINGS, $this, function ($data) {
if (!is_object($data) || !property_exists($data, "scriptSettings")) {
return new CommunicationAnswer("No valid ScriptSettings provided!", true);
}
try {
$scriptSettings = $this->maniaControl->getClient()->getModeScriptSettings();
2020-04-28 17:52:17 +02:00
} catch (\Exception $e) {
return new CommunicationAnswer($e->getMessage(), true);
}
$newSettings = array();
foreach ($data->scriptSettings as $name => $value) {
if (!isset($scriptSettings[$name])) {
var_dump('no setting ' . $name);
continue;
}
if ($value == $scriptSettings[$name]) {
// Not changed
continue;
}
$newSettings[$name] = $value;
settype($newSettings[$name], gettype($scriptSettings[$name]));
}
//No new Settings
if (empty($newSettings)) {
return new CommunicationAnswer(array("success" => true));
}
//Trigger Scriptsettings Changed Callback
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SCRIPTSETTINGS_CHANGED);
//Set the Settings
$success = $this->maniaControl->getClient()->setModeScriptSettings($newSettings);
return new CommunicationAnswer(array("success" => $success));
});
}
}