script settings improvements

This commit is contained in:
Steffen Schröder 2014-01-05 12:17:29 +01:00
parent 1091afb771
commit d28269ade6

View File

@ -24,11 +24,10 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
/**
* Constants
*/
const TITLE = 'Script Settings';
const ACTION_PREFIX_SETTING = 'ScriptSetting.';
const ACTION_SETTING_BOOL = 'ScriptSetting.ActionBoolSetting.';
const ACTION_PREFIX_SETTING = 'ScriptSetting.';
const ACTION_SETTING_BOOL = 'ScriptSetting.ActionBoolSetting.';
const CB_SCRIPTSETTING_CHANGED = 'ScriptSettings.SettingChanged';
const TABLE_SCRIPT_SETTINGS = 'mc_scriptsettings';
const TABLE_SCRIPT_SETTINGS = 'mc_scriptsettings';
/**
* Private Properties
@ -44,14 +43,20 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$this->maniaControl = $maniaControl;
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this,
'handleManialinkPageAnswer');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
$this->initTables();
}
/**
* Create all necessary Database Tables
*
* @return boolean
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SCRIPT_SETTINGS . "` (
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SCRIPT_SETTINGS . "` (
`serverId` int(11) NOT NULL AUTO_INCREMENT,
`settingName` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`settingValue` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
@ -59,19 +64,16 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Script Settings' AUTO_INCREMENT=1;";
$statement = $mysqli->prepare($query);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$statement->execute();
if($statement->error) {
if ($statement->error) {
trigger_error($statement->error, E_USER_ERROR);
return false;
}
$statement->close();
return true;
}
@ -90,33 +92,31 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* @return bool
*/
public function loadSettingsFromDatabase() {
$this->maniaControl->client->query('GetModeScriptSettings');
$scriptSettings = $this->maniaControl->client->getResponse();
$mysqli = $this->maniaControl->database->mysqli;
$loadedSettings = array();
$query = "SELECT * FROM `" . self::TABLE_SCRIPT_SETTINGS . "`;";
$query = "SELECT * FROM `" . self::TABLE_SCRIPT_SETTINGS . "`;";
$result = $mysqli->query($query);
if(!$result) {
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
while($row = $result->fetch_object()) {
$loadedSettings[$row->settingName] = $row->settingValue;
settype($loadedSettings[$row->settingName], gettype($scriptSettings[$row->settingName]));
}
$result->close();
if(count($loadedSettings) == 0) {
if ($result->num_rows) {
$result->close();
return true;
}
$this->maniaControl->client->query('GetModeScriptSettings');
$scriptSettings = $this->maniaControl->client->getResponse();
$loadedSettings = array();
while ($row = $result->fetch_object()) {
if (!isset($scriptSettings[$row->settingName])) continue;
$loadedSettings[$row->settingName] = $row->settingValue;
settype($loadedSettings[$row->settingName], gettype($scriptSettings[$row->settingName]));
}
$result->close();
$success = $this->maniaControl->client->query('SetModeScriptSettings', $loadedSettings);
if(!$success) {
$this->maniaControl->log('Error occurred: ' . $this->maniaControl->getClientErrorText());
if (!$success) {
trigger_error('Error occured: ' . $this->maniaControl->getClientErrorText());
return false;
}
return true;
@ -127,7 +127,7 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
*/
public function getTitle() {
return self::TITLE;
return 'Script Settings';
}
/**
@ -136,11 +136,11 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
*/
public function getMenu($width, $height, Script $script) {
$pagesId = 'ScriptSettingsPages';
$frame = new Frame();
$frame = new Frame();
$this->maniaControl->client->query('GetModeScriptInfo');
$scriptInfo = $this->maniaControl->client->getResponse();
if(isset($scriptInfo['faultCode'])) {
if (isset($scriptInfo['faultCode'])) {
// Not in script mode
$label = new Label();
$frame->add($label);
@ -153,10 +153,10 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$scriptSettings = $this->maniaControl->client->getResponse();
// Config
$pagerSize = 9.;
$pagerSize = 9.;
$settingHeight = 5.;
$labelTextSize = 2;
$pageMaxCount = 13;
$pageMaxCount = 13;
// Pagers
$pagerPrev = new Quad_Icons64x64_1();
@ -185,18 +185,18 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
// Setting pages
$pageFrames = array();
$y = 0.;
foreach($scriptParams as $index => $scriptParam) {
$y = 0.;
foreach ($scriptParams as $index => $scriptParam) {
$settingName = $scriptParam['Name'];
if(!isset($scriptSettings[$settingName])) {
if (!isset($scriptSettings[$settingName])) {
continue;
}
if(!isset($pageFrame)) {
if (!isset($pageFrame)) {
$pageFrame = new Frame();
$frame->add($pageFrame);
if(!empty($pageFrames)) {
if (!empty($pageFrames)) {
$pageFrame->setVisible(false);
}
array_push($pageFrames, $pageFrame);
@ -220,13 +220,14 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$settingValue = $scriptSettings[$settingName];
$substyle = '';
if($settingValue === false) {
if ($settingValue === false) {
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlRed;
} else if($settingValue === true) {
}
else if ($settingValue === true) {
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlGreen;
}
if($substyle != '') {
if ($substyle != '') {
$quad = new Quad_Icons64x64_1();
$settingFrame->add($quad);
$quad->setX($width / 2 * 0.545);
@ -235,7 +236,8 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$quad->setSize(4, 4);
$quad->setHAlign(Control::CENTER);
$quad->setAction(self::ACTION_SETTING_BOOL . $settingName);
} else {
}
else {
$entry = new Entry();
$settingFrame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
@ -259,7 +261,7 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$script->addTooltip($nameLabel, $descriptionLabel);
$y -= $settingHeight;
if($index % $pageMaxCount == $pageMaxCount - 1) {
if ($index % $pageMaxCount == $pageMaxCount - 1) {
unset($pageFrame);
}
}
@ -272,25 +274,24 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData()
*/
public function saveConfigData(array $configData, Player $player) {
$this->maniaControl->client->query('GetModeScriptSettings');
$scriptSettings = $this->maniaControl->client->getResponse();
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
$newSettings = array();
foreach($configData[3] as $setting) {
if(substr($setting['Name'], 0, $prefixLength) != self::ACTION_PREFIX_SETTING) {
foreach ($configData[3] as $setting) {
if (substr($setting['Name'], 0, $prefixLength) != self::ACTION_PREFIX_SETTING) {
continue;
}
$settingName = substr($setting['Name'], $prefixLength);
if(!isset($scriptSettings[$settingName])) {
if (!isset($scriptSettings[$settingName])) {
var_dump('no setting ' . $settingName);
continue;
}
if($setting['Value'] == $scriptSettings[$settingName]) {
if ($setting['Value'] == $scriptSettings[$settingName]) {
// Not changed
continue;
}
@ -308,21 +309,21 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* @param array $callback
*/
public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2];
$actionId = $callback[1][2];
$boolSetting = (strpos($actionId, self::ACTION_SETTING_BOOL) === 0);
if(!$boolSetting) {
if (!$boolSetting) {
return;
}
$actionArray = explode(".", $actionId);
$setting = $actionArray[2];
$setting = $actionArray[2];
$login = $callback[1][1];
$login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
//Save all Changes
// Save all Changes
$this->saveConfigData($callback[1], $player);
//Toggle the Boolean Setting
// Toggle the Boolean Setting
$this->toggleBooleanSetting($setting, $player);
}
@ -330,18 +331,18 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* Toogle a Boolean Setting
*
* @param Player $player
* @param $setting
* @param $setting
*/
public function toggleBooleanSetting($setting, Player $player) {
$this->maniaControl->client->query('GetModeScriptSettings');
$scriptSettings = $this->maniaControl->client->getResponse();
if(!isset($scriptSettings[$setting])) {
if (!isset($scriptSettings[$setting])) {
var_dump('no setting ' . $setting);
return;
}
$newSettings = array();
$newSettings = array();
$newSettings[$setting] = ($scriptSettings[$setting] ? false : true);
$this->applyNewScriptSettings($newSettings, $player);
@ -350,23 +351,20 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
/**
* Apply the Array of new Script Settings
*
* @param array $newSettings
* @param array $newSettings
* @param Player $player
*/
private function applyNewScriptSettings(array $newSettings, Player $player) {
if(!$newSettings) {
return true;
}
if (!$newSettings) return true;
$success = $this->maniaControl->client->query('SetModeScriptSettings', $newSettings);
if(!$success) {
if (!$success) {
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);
return false;
}
// Save Settings into Database
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT INTO `" . self::TABLE_SCRIPT_SETTINGS . "` (
$query = "INSERT INTO `" . self::TABLE_SCRIPT_SETTINGS . "` (
`serverId`,
`settingName`,
`settingValue`
@ -375,38 +373,38 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
) ON DUPLICATE KEY UPDATE
`settingValue` = VALUES(`settingValue`);";
$statement = $mysqli->prepare($query);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$serverId = $this->maniaControl->server->getServerId();
// Notifications
$settingsCount = count($newSettings);
$settingIndex = 0;
$title = $this->maniaControl->authenticationManager->getAuthLevelName($player->authLevel);
$chatMessage = '$ff0' . $title . ' $<' . $player->nickname . '$> set ScriptSetting' . ($settingsCount > 1 ? 's' : '') . ' ';
foreach($newSettings as $setting => $value) {
$settingIndex = 0;
$title = $this->maniaControl->authenticationManager->getAuthLevelName($player->authLevel);
$chatMessage = '$ff0' . $title . ' $<' . $player->nickname . '$> set ScriptSetting' . ($settingsCount > 1 ? 's' : '') . ' ';
foreach ($newSettings as $setting => $value) {
$chatMessage .= '$<' . '$fff' . preg_replace('/^S_/', '', $setting) . '$z$s$ff0 ';
$chatMessage .= 'to $fff' . $this->parseSettingValue($value) . '$>';
if($settingIndex <= $settingsCount - 2) {
if ($settingIndex <= $settingsCount - 2) {
$chatMessage .= ', ';
}
//Add To Database
// Add To Database
$statement->bind_param('iss', $serverId, $setting, $value);
$statement->execute();
if($statement->error) {
if ($statement->error) {
trigger_error($statement->error);
$statement->close();
return false;
}
// Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SCRIPTSETTING_CHANGED, array(self::CB_SCRIPTSETTING_CHANGED, $setting, $value));
$this->maniaControl->callbackManager->triggerCallback(self::CB_SCRIPTSETTING_CHANGED,
array(self::CB_SCRIPTSETTING_CHANGED, $setting, $value));
$settingIndex++;
}
@ -425,9 +423,9 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* @return string
*/
private function parseSettingValue($value) {
if(is_bool($value)) {
if (is_bool($value)) {
return ($value ? 'True' : 'False');
}
return (string)$value;
return (string) $value;
}
}