load def scriptsettings on mapbegin, settings changed cb

This commit is contained in:
kremsy 2014-02-12 19:19:08 +01:00 committed by Steffen Schröder
parent e4095b82e6
commit 5ed1027eec
2 changed files with 70 additions and 52 deletions

View File

@ -12,6 +12,7 @@ use FML\Script\Script;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Maps\MapManager;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
/** /**
@ -23,11 +24,12 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
/** /**
* Constants * Constants
*/ */
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 CB_SCRIPTSETTINGS_CHANGED = 'ScriptSettings.SettingsChanged'; const CB_SCRIPTSETTINGS_CHANGED = 'ScriptSettings.SettingsChanged';
const TABLE_SCRIPT_SETTINGS = 'mc_scriptsettings'; const TABLE_SCRIPT_SETTINGS = 'mc_scriptsettings';
const SETTING_LOAD_DEFAULT_SETTINGS_MAP_BEGIN = 'Load Stored Script-Settings on Map-Begin';
/** /**
* Private Properties * Private Properties
@ -45,6 +47,8 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
// 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->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_BEGINMAP, $this, 'onBeginMap');
$this->maniaControl->settingManager->initSetting($this, self::SETTING_LOAD_DEFAULT_SETTINGS_MAP_BEGIN, true);
$this->initTables(); $this->initTables();
} }
@ -83,6 +87,18 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
*/ */
public function onInit(array $callback) { public function onInit(array $callback) {
$this->loadSettingsFromDatabase(); $this->loadSettingsFromDatabase();
}
/**
* Handle OnBegin Map Callback
*
* @param array $callback
*/
public function onBeginMap(array $callback) {
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_LOAD_DEFAULT_SETTINGS_MAP_BEGIN)) {
$this->loadSettingsFromDatabase();
}
} }
/** /**
@ -91,9 +107,9 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* @return bool * @return bool
*/ */
public function loadSettingsFromDatabase() { public function loadSettingsFromDatabase() {
try{ try {
$scriptSettings = $this->maniaControl->client->getModeScriptSettings(); $scriptSettings = $this->maniaControl->client->getModeScriptSettings();
} catch (\Exception $e){ } catch(\Exception $e) {
return false; return false;
} }

View File

@ -13,13 +13,14 @@ class SettingManager {
/** /**
* Constants * Constants
*/ */
const TABLE_SETTINGS = 'mc_settings'; const TABLE_SETTINGS = 'mc_settings';
const TYPE_STRING = 'string'; const TYPE_STRING = 'string';
const TYPE_INT = 'int'; const TYPE_INT = 'int';
const TYPE_REAL = 'real'; const TYPE_REAL = 'real';
const TYPE_BOOL = 'bool'; const TYPE_BOOL = 'bool';
const TYPE_ARRAY = 'array'; const TYPE_ARRAY = 'array';
const CB_SETTINGS_CHANGED = 'SettingManager.SettingsChanged';
/** /**
* Private properties * Private properties
*/ */
@ -42,14 +43,14 @@ class SettingManager {
* @return bool * @return bool
*/ */
private function initTables() { private function initTables() {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$defaultType = "'" . self::TYPE_STRING . "'"; $defaultType = "'" . self::TYPE_STRING . "'";
$typeSet = $defaultType; $typeSet = $defaultType;
$typeSet .= ",'" . self::TYPE_INT . "'"; $typeSet .= ",'" . self::TYPE_INT . "'";
$typeSet .= ",'" . self::TYPE_REAL . "'"; $typeSet .= ",'" . self::TYPE_REAL . "'";
$typeSet .= ",'" . self::TYPE_BOOL . "'"; $typeSet .= ",'" . self::TYPE_BOOL . "'";
$typeSet .= ",'" . self::TYPE_ARRAY . "'"; $typeSet .= ",'" . self::TYPE_ARRAY . "'";
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` ( $settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT, `index` int(11) NOT NULL AUTO_INCREMENT,
`class` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `class` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`setting` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `setting` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
@ -88,7 +89,7 @@ class SettingManager {
return $object; return $object;
} }
trigger_error('Invalid class param. ' . $object); trigger_error('Invalid class param. ' . $object);
return (string) $object; return (string)$object;
} }
/** /**
@ -121,21 +122,21 @@ class SettingManager {
* Cast a setting to the given type * Cast a setting to the given type
* *
* @param string $type * @param string $type
* @param mixed $value * @param mixed $value
* @return mixed * @return mixed
*/ */
private function castSetting($type, $value) { private function castSetting($type, $value) {
if ($type === self::TYPE_INT) { if ($type === self::TYPE_INT) {
return (int) $value; return (int)$value;
} }
if ($type === self::TYPE_REAL) { if ($type === self::TYPE_REAL) {
return (float) $value; return (float)$value;
} }
if ($type === self::TYPE_BOOL) { if ($type === self::TYPE_BOOL) {
return (bool) $value; return (bool)$value;
} }
if ($type === self::TYPE_STRING) { if ($type === self::TYPE_STRING) {
return (string) $value; return (string)$value;
} }
if ($type === self::TYPE_ARRAY) { if ($type === self::TYPE_ARRAY) {
return explode($this->arrayDelimiter, $value); return explode($this->arrayDelimiter, $value);
@ -147,7 +148,7 @@ class SettingManager {
/** /**
* Format a setting for saving it to the database * Format a setting for saving it to the database
* *
* @param mixed $value * @param mixed $value
* @param string $type * @param string $type
* @return mixed * @return mixed
*/ */
@ -169,18 +170,18 @@ class SettingManager {
* *
* @param object $object * @param object $object
* @param string $settingName * @param string $settingName
* @param mixed $default * @param mixed $default
* @return bool * @return bool
*/ */
public function initSetting($object, $settingName, $default) { public function initSetting($object, $settingName, $default) {
if ($default === null || is_object($default)) { if ($default === null || is_object($default)) {
return false; return false;
} }
$className = $this->getClassName($object); $className = $this->getClassName($object);
$type = $this->getType($default); $type = $this->getType($default);
$default = $this->formatSetting($default, $type); $default = $this->formatSetting($default, $type);
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` ( $settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
`class`, `class`,
`setting`, `setting`,
`type`, `type`,
@ -213,7 +214,7 @@ class SettingManager {
/** /**
* Get a Setting by its index * Get a Setting by its index
* *
* @param $settingIndex * @param $settingIndex
* @param bool $default * @param bool $default
* @internal param null $default * @internal param null $default
* @internal param $className * @internal param $className
@ -221,15 +222,15 @@ class SettingManager {
* @return mixed null * @return mixed null
*/ */
public function getSettingByIndex($settingIndex, $default = false) { public function getSettingByIndex($settingIndex, $default = false) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "` $settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `index` = {$settingIndex};"; WHERE `index` = {$settingIndex};";
$result = $mysqli->query($settingQuery); $result = $mysqli->query($settingQuery);
if (!$result) { if (!$result) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$row = $result->fetch_object(); $row = $result->fetch_object();
$result->close(); $result->close();
return $row; return $row;
@ -243,8 +244,8 @@ class SettingManager {
* @param $value * @param $value
*/ */
public function getSettingByClassName($className, $settingName, $default = null) { public function getSettingByClassName($className, $settingName, $default = null) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "` $settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ? WHERE `class` = ?
AND `setting` = ?;"; AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery); $settingStatement = $mysqli->prepare($settingQuery);
@ -276,7 +277,7 @@ class SettingManager {
* *
* @param object $object * @param object $object
* @param string $settingName * @param string $settingName
* @param mixed $default * @param mixed $default
* @return mixed * @return mixed
*/ */
public function getSetting($object, $settingName, $default = null) { public function getSetting($object, $settingName, $default = null) {
@ -293,8 +294,8 @@ class SettingManager {
* @return bool * @return bool
*/ */
public function updateSetting($className, $settingName, $value) { public function updateSetting($className, $settingName, $value) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "` $settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = ? SET `value` = ?
WHERE `class` = ? WHERE `class` = ?
AND `setting` = ?;"; AND `setting` = ?;";
@ -320,12 +321,15 @@ class SettingManager {
* *
* @param object $object * @param object $object
* @param string $settingName * @param string $settingName
* @param mixed $value * @param mixed $value
* @return bool * @return bool
*/ */
public function setSetting($object, $settingName, $value) { public function setSetting($object, $settingName, $value) {
$className = $this->getClassName($object); $className = $this->getClassName($object);
$this->updateSetting($className, $settingName, $value); $this->updateSetting($className, $settingName, $value);
//Trigger settings changed Callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SETTINGS_CHANGED, array(self::CB_SETTINGS_CHANGED, $className, $settingName, $value));
} }
/** /**
@ -336,9 +340,9 @@ class SettingManager {
* @return bool * @return bool
*/ */
public function resetSetting($object, $settingName) { public function resetSetting($object, $settingName) {
$className = $this->getClassName($object); $className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "` $settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = `default` SET `value` = `default`
WHERE `class` = ? WHERE `class` = ?
AND `setting` = ?;"; AND `setting` = ?;";
@ -366,9 +370,9 @@ class SettingManager {
* @return bool * @return bool
*/ */
public function deleteSetting($object, $settingName) { public function deleteSetting($object, $settingName) {
$className = $this->getClassName($object); $className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "` $settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ? WHERE `class` = ?
AND `setting` = ?;"; AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery); $settingStatement = $mysqli->prepare($settingQuery);
@ -395,7 +399,7 @@ class SettingManager {
*/ */
public function getSettingsByClass($className) { public function getSettingsByClass($className) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` WHERE `class`= '" . $mysqli->escape_string($className) . "' $query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` WHERE `class`= '" . $mysqli->escape_string($className) . "'
ORDER BY `setting` ASC;"; ORDER BY `setting` ASC;";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if ($mysqli->error) { if ($mysqli->error) {
@ -403,9 +407,8 @@ class SettingManager {
return null; return null;
} }
$settings = array(); $settings = array();
while ($setting = $result->fetch_object()) { while($setting = $result->fetch_object()) {
$settings[$setting->index] = $setting; $settings[$setting->index] = $setting;
// array_push($settings, $setting);
} }
$result->free(); $result->free();
return $settings; return $settings;
@ -418,7 +421,7 @@ class SettingManager {
*/ */
public function getSettings() { public function getSettings() {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` $query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
ORDER BY `class` ASC, `setting` ASC;"; ORDER BY `class` ASC, `setting` ASC;";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if ($mysqli->error) { if ($mysqli->error) {
@ -426,9 +429,8 @@ class SettingManager {
return null; return null;
} }
$settings = array(); $settings = array();
while ($setting = $result->fetch_object()) { while($setting = $result->fetch_object()) {
$settings[$setting->index] = $setting; $settings[$setting->index] = $setting;
// array_push($settings, $setting);
} }
$result->free(); $result->free();
return $settings; return $settings;