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,7 +24,6 @@ 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';
@ -44,11 +43,17 @@ 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 . "` (
@ -61,17 +66,14 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$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;
$loadedSettings = array();
$query = "SELECT * FROM `" . self::TABLE_SCRIPT_SETTINGS . "`;"; $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()) {
$loadedSettings[$row->settingName] = $row->settingValue;
settype($loadedSettings[$row->settingName], gettype($scriptSettings[$row->settingName]));
}
$result->close(); $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';
} }
/** /**
@ -222,7 +222,8 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$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;
} }
@ -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);
@ -272,7 +274,6 @@ 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();
@ -354,9 +355,7 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* @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);
@ -365,7 +364,6 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
// 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`,
@ -382,7 +380,6 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$serverId = $this->maniaControl->server->getServerId(); $serverId = $this->maniaControl->server->getServerId();
// Notifications // Notifications
$settingsCount = count($newSettings); $settingsCount = count($newSettings);
$settingIndex = 0; $settingIndex = 0;
@ -406,7 +403,8 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
} }
// 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++;
} }