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,12 +24,11 @@ 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
*/ */
@ -42,36 +41,39 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$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,
UNIQUE KEY `setting` (`serverId`, `settingName`) UNIQUE KEY `setting` (`serverId`, `settingName`)
) 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() {
$mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_SCRIPT_SETTINGS . "`;";
$result = $mysqli->query($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
if ($result->num_rows) {
$result->close();
return true;
}
$this->maniaControl->client->query('GetModeScriptSettings'); $this->maniaControl->client->query('GetModeScriptSettings');
$scriptSettings = $this->maniaControl->client->getResponse(); $scriptSettings = $this->maniaControl->client->getResponse();
$mysqli = $this->maniaControl->database->mysqli;
$loadedSettings = array(); $loadedSettings = array();
while ($row = $result->fetch_object()) {
$query = "SELECT * FROM `" . self::TABLE_SCRIPT_SETTINGS . "`;"; if (!isset($scriptSettings[$row->settingName])) continue;
$result = $mysqli->query($query);
if(!$result) {
trigger_error($mysqli->error);
}
while($row = $result->fetch_object()) {
$loadedSettings[$row->settingName] = $row->settingValue; $loadedSettings[$row->settingName] = $row->settingValue;
settype($loadedSettings[$row->settingName], gettype($scriptSettings[$row->settingName])); settype($loadedSettings[$row->settingName], gettype($scriptSettings[$row->settingName]));
} }
$result->close(); $result->close();
if(count($loadedSettings) == 0) {
return true;
}
$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);
@ -148,66 +148,66 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
return $frame; return $frame;
} }
$scriptParams = $scriptInfo['ParamDescs']; $scriptParams = $scriptInfo['ParamDescs'];
$this->maniaControl->client->query('GetModeScriptSettings'); $this->maniaControl->client->query('GetModeScriptSettings');
$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();
$frame->add($pagerPrev); $frame->add($pagerPrev);
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2); $pagerPrev->setPosition($width * 0.39, $height * -0.44, 2);
$pagerPrev->setSize($pagerSize, $pagerSize); $pagerPrev->setSize($pagerSize, $pagerSize);
$pagerPrev->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowPrev); $pagerPrev->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowPrev);
$pagerNext = new Quad_Icons64x64_1(); $pagerNext = new Quad_Icons64x64_1();
$frame->add($pagerNext); $frame->add($pagerNext);
$pagerNext->setPosition($width * 0.45, $height * -0.44, 2); $pagerNext->setPosition($width * 0.45, $height * -0.44, 2);
$pagerNext->setSize($pagerSize, $pagerSize); $pagerNext->setSize($pagerSize, $pagerSize);
$pagerNext->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowNext); $pagerNext->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowNext);
$script->addPager($pagerPrev, -1, $pagesId); $script->addPager($pagerPrev, -1, $pagesId);
$script->addPager($pagerNext, 1, $pagesId); $script->addPager($pagerNext, 1, $pagesId);
$pageCountLabel = new Label(); $pageCountLabel = new Label();
$frame->add($pageCountLabel); $frame->add($pageCountLabel);
$pageCountLabel->setHAlign(Control::RIGHT); $pageCountLabel->setHAlign(Control::RIGHT);
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1); $pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1);
$pageCountLabel->setStyle('TextTitle1'); $pageCountLabel->setStyle('TextTitle1');
$pageCountLabel->setTextSize(2); $pageCountLabel->setTextSize(2);
$script->addPageLabel($pageCountLabel, $pagesId); $script->addPageLabel($pageCountLabel, $pagesId);
// 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);
$y = $height * 0.41; $y = $height * 0.41;
$script->addPage($pageFrame, count($pageFrames), $pagesId); $script->addPage($pageFrame, count($pageFrames), $pagesId);
} }
$settingFrame = new Frame(); $settingFrame = new Frame();
$pageFrame->add($settingFrame); $pageFrame->add($settingFrame);
$settingFrame->setY($y); $settingFrame->setY($y);
$nameLabel = new Label_Text(); $nameLabel = new Label_Text();
$settingFrame->add($nameLabel); $settingFrame->add($nameLabel);
$nameLabel->setHAlign(Control::LEFT); $nameLabel->setHAlign(Control::LEFT);
@ -216,17 +216,18 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall); $nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
$nameLabel->setTextSize($labelTextSize); $nameLabel->setTextSize($labelTextSize);
$nameLabel->setText($settingName); $nameLabel->setText($settingName);
$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);
@ -246,7 +248,7 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$entry->setName(self::ACTION_PREFIX_SETTING . $settingName); $entry->setName(self::ACTION_PREFIX_SETTING . $settingName);
$entry->setDefault($settingValue); $entry->setDefault($settingValue);
} }
$descriptionLabel = new Label(); $descriptionLabel = new Label();
$pageFrame->add($descriptionLabel); $pageFrame->add($descriptionLabel);
$descriptionLabel->setHAlign(Control::LEFT); $descriptionLabel->setHAlign(Control::LEFT);
@ -257,13 +259,13 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
// $descriptionLabel->setTextPrefix('Desc: '); // $descriptionLabel->setTextPrefix('Desc: ');
$descriptionLabel->setText($scriptParam['Desc']); $descriptionLabel->setText($scriptParam['Desc']);
$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);
} }
} }
return $frame; return $frame;
} }
@ -272,33 +274,32 @@ 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;
} }
$newSettings[$settingName] = $setting['Value']; $newSettings[$settingName] = $setting['Value'];
settype($newSettings[$settingName], gettype($scriptSettings[$settingName])); settype($newSettings[$settingName], gettype($scriptSettings[$settingName]));
} }
$this->applyNewScriptSettings($newSettings, $player); $this->applyNewScriptSettings($newSettings, $player);
} }
@ -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,43 +331,40 @@ 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);
} }
/** /**
* 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,43 +373,43 @@ 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++;
} }
$statement->close(); $statement->close();
$chatMessage .= '!'; $chatMessage .= '!';
$this->maniaControl->chat->sendInformation($chatMessage); $this->maniaControl->chat->sendInformation($chatMessage);
$this->maniaControl->log(Formatter::stripCodes($chatMessage)); $this->maniaControl->log(Formatter::stripCodes($chatMessage));
@ -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;
} }
} }