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