Setting Model Class

This commit is contained in:
Steffen Schröder 2014-05-13 14:15:00 +02:00
parent 411f23a618
commit 2fd501f2e8
5 changed files with 405 additions and 315 deletions

View File

@ -0,0 +1,153 @@
<?php
namespace ManiaControl\Settings;
use ManiaControl\ClassUtil;
/**
* Model Class for a Setting
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class Setting {
/*
* Constants
*/
const CLASS_NAME = __CLASS__;
const TYPE_STRING = 'string';
const TYPE_INT = 'int';
const TYPE_REAL = 'real';
const TYPE_BOOL = 'bool';
const TYPE_ARRAY = 'array';
const ARRAY_DELIMITER = ';;';
/*
* Public Properties
*/
public $index = null;
public $class = null;
public $setting = null;
public $type = null;
public $value = null;
public $default = null;
public $fetchTime = null;
/**
* Construct a new Setting
*
* @param bool $fetched
*/
public function __construct($fetched = false) {
if ($fetched) {
$this->value = self::castValue($this->value, $this->type);
$this->default = self::castValue($this->default, $this->type);
$this->fetchTime = time();
}
}
/**
* Cast a Setting to the given Type
*
* @param string $type
* @param mixed $value
* @return mixed
*/
public static function castValue($value, $type) {
if ($type === self::TYPE_INT) {
return (int)$value;
}
if ($type === self::TYPE_REAL) {
return (float)$value;
}
if ($type === self::TYPE_BOOL) {
return (bool)$value;
}
if ($type === self::TYPE_STRING) {
return (string)$value;
}
if ($type === self::TYPE_ARRAY) {
return explode(self::ARRAY_DELIMITER, $value);
}
trigger_error("Unsupported Setting Value Type: '" . print_r($type, true) . "'!");
return $value;
}
/**
* Get the Set String for the available Types
*
* @return string
*/
public static function getTypeSet() {
$typeSet = "'" . self::TYPE_STRING . "','" . self::TYPE_INT . "','" . self::TYPE_REAL . "','" . self::TYPE_BOOL . "','" . self::TYPE_ARRAY . "'";
return $typeSet;
}
/**
* Get the Formatted Value of the Setting
*
* @return mixed
*/
public function getFormattedValue() {
$formattedValue = self::formatValue($this->value, $this->type);
return $formattedValue;
}
/**
* Format a Value for saving it to the Database
*
* @param mixed $value
* @param string $type
* @return mixed
*/
public static function formatValue($value, $type = null) {
if ($type === null) {
$type = self::getValueType($value);
}
if ($type === self::TYPE_ARRAY) {
return implode(self::ARRAY_DELIMITER, $value);
}
if ($type === self::TYPE_BOOL) {
return ($value ? 1 : 0);
}
return $value;
}
/**
* Get Type of a Value Parameter
*
* @param mixed $value
* @return string
*/
public static function getValueType($value) {
if (is_int($value)) {
return self::TYPE_INT;
}
if (is_real($value)) {
return self::TYPE_REAL;
}
if (is_bool($value)) {
return self::TYPE_BOOL;
}
if (is_string($value)) {
return self::TYPE_STRING;
}
if (is_array($value)) {
return self::TYPE_ARRAY;
}
trigger_error("Unsupported Setting Value Type: '" . print_r($value, true) . "'!");
return null;
}
/**
* Check if the Settings belongs to the given Class
*
* @param mixed $object
* @return bool
*/
public function belongsToClass($object) {
$className = ClassUtil::getClass($object);
return ($className === $this->class);
}
}

View File

@ -4,6 +4,7 @@ namespace ManiaControl\Settings;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ClassUtil;
use ManiaControl\ManiaControl;
use ManiaControl\Plugins\PluginManager;
@ -18,19 +19,13 @@ class SettingManager implements CallbackListener {
/*
* Constants
*/
const TABLE_SETTINGS = 'mc_settings';
const TYPE_STRING = 'string';
const TYPE_INT = 'int';
const TYPE_REAL = 'real';
const TYPE_BOOL = 'bool';
const TYPE_ARRAY = 'array';
const CB_SETTINGS_CHANGED = 'SettingManager.SettingsChanged';
const TABLE_SETTINGS = 'mc_settings';
const CB_SETTING_CHANGED = 'SettingManager.SettingChanged';
/*
* Private Properties
*/
private $maniaControl = null;
private $arrayDelimiter = ';;';
private $storedSettings = array();
/**
@ -46,14 +41,14 @@ class SettingManager implements CallbackListener {
}
/**
* Initialize necessary Database Tables
* Initialize the necessary Database Tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
$defaultType = "'" . self::TYPE_STRING . "'";
$typeSet = $defaultType . ",'" . self::TYPE_INT . "','" . self::TYPE_REAL . "','" . self::TYPE_BOOL . "','" . self::TYPE_ARRAY . "'";
$defaultType = "'" . Setting::TYPE_STRING . "'";
$typeSet = Setting::getTypeSet();
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`class` varchar(100) NOT NULL,
@ -69,7 +64,6 @@ class SettingManager implements CallbackListener {
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
}
return $result;
}
@ -86,23 +80,120 @@ class SettingManager implements CallbackListener {
* @return bool
*/
private function deleteUnusedSettings() {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `changed` < NOW() - INTERVAL 1 HOUR;";
$settingStatement = $mysqli->prepare($settingQuery);
$result = $mysqli->query($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$success = $settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
if ($result) {
$this->storedSettings = array();
return true;
}
$settingStatement->close();
$this->storedSettings = array();
return $success;
return false;
}
/**
* Get a Setting by its Index
*
* @param int $settingIndex
* @param mixed $defaultValue
* @return Setting
*/
public function getSettingByIndex($settingIndex, $defaultValue = null) {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `index` = {$settingIndex};";
$result = $mysqli->query($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
if ($result->num_rows <= 0) {
$result->close();
return $defaultValue;
}
/** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(true));
$result->close();
return $setting;
}
/**
* Set a Setting for the given Object
*
* @param mixed $object
* @param string $settingName
* @param mixed $value
* @return Setting
*/
public function setSetting($object, $settingName, $value) {
$className = $this->getClassName($object);
$setting = $this->getSetting($object, $settingName);
if ($setting) {
$setting->value = $value;
if (!$this->saveSetting($setting)) {
return null;
}
} else {
$setting = $this->initSetting($object, $settingName, $value);
}
$this->storedSettings[$className . $settingName] = $setting;
// Trigger Settings Changed Callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SETTING_CHANGED, $setting);
return $setting;
}
/**
* Get Setting by Name for the given Object
*
* @param mixed $object
* @param string $settingName
* @param mixed $default
* @return Setting
*/
public function getSetting($object, $settingName, $default = null) {
$settingClass = ClassUtil::getClass($object);
// Retrieve from Storage if possible
if (isset($this->storedSettings[$settingClass . $settingName])) {
return $this->storedSettings[$settingClass . $settingName];
}
// Fetch setting
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($settingClass) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
if ($result->num_rows <= 0) {
if ($default === null) {
return null;
}
$setting = $this->initSetting($object, $settingName, $default);
return $setting;
}
/** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(true));
$result->free();
// Store setting
$this->storedSettings[$settingClass . $settingName] = $setting;
return $setting;
}
/**
@ -110,16 +201,30 @@ class SettingManager implements CallbackListener {
*
* @param mixed $object
* @param string $settingName
* @param mixed $default
* @param mixed $defaultValue
* @return Setting
*/
public function initSetting($object, $settingName, $defaultValue) {
$setting = new Setting();
$setting->class = ClassUtil::getClass($object);
$setting->setting = $settingName;
$setting->type = Setting::getValueType($defaultValue);
$setting->value = $defaultValue;
$setting->default = $defaultValue;
$saved = $this->saveSetting($setting);
if ($saved) {
return $setting;
}
return null;
}
/**
* Save the given Setting in the Database
*
* @param Setting $setting
* @return bool
*/
public function initSetting($object, $settingName, $default) {
if (is_null($default) || is_object($default)) {
return false;
}
$className = $this->getClassName($object);
$type = $this->getType($default);
$default = $this->formatSetting($default, $type);
public function saveSetting(Setting &$setting) {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
`class`,
@ -132,6 +237,7 @@ class SettingManager implements CallbackListener {
@value := ?,
@value
) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`),
`type` = VALUES(`type`),
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
`default` = VALUES(`default`),
@ -141,248 +247,47 @@ class SettingManager implements CallbackListener {
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ssss', $className, $settingName, $type, $default);
$success = $settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
}
$settingStatement->close();
return $success;
}
/**
* Get Class Name of a Parameter
*
* @param mixed $param
* @return string
*/
private function getClassName($param) {
if (is_object($param)) {
return get_class($param);
}
if (is_string($param)) {
return $param;
}
trigger_error('Invalid class param. ' . $param);
return (string)$param;
}
/**
* Get Type of a Parameter
*
* @param mixed $param
* @return string
*/
private function getType($param) {
if (is_int($param)) {
return self::TYPE_INT;
}
if (is_real($param)) {
return self::TYPE_REAL;
}
if (is_bool($param)) {
return self::TYPE_BOOL;
}
if (is_string($param)) {
return self::TYPE_STRING;
}
if (is_array($param)) {
return self::TYPE_ARRAY;
}
trigger_error('Unsupported setting type. ' . print_r($param, true));
return null;
}
/**
* Format a Setting for saving it to the Database
*
* @param mixed $value
* @param string $type
* @return mixed
*/
private function formatSetting($value, $type = null) {
if ($type === null) {
$type = $this->getType($value);
}
if ($type === self::TYPE_ARRAY) {
return implode($this->arrayDelimiter, $value);
}
if ($type === self::TYPE_BOOL) {
return ($value ? 1 : 0);
}
return $value;
}
/**
* Get a Setting by its Index
*
* @param int $settingIndex
* @param mixed $default
* @return mixed
*/
public function getSettingByIndex($settingIndex, $default = false) {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `index` = {$settingIndex};";
$result = $mysqli->query($settingQuery);
if (!$result) {
trigger_error($mysqli->error);
return null;
}
if ($result->num_rows <= 0) {
$result->close();
return $default;
}
$row = $result->fetch_object();
$result->close();
return $row;
}
/**
* Get Setting by Name for the given Object
*
* @param mixed $object
* @param string $settingName
* @param mixed $default
* @return mixed
*/
public function getSetting($object, $settingName, $default = null) {
$className = $this->getClassName($object);
// Check if setting is already in the ram
if (isset($this->storedSettings[$className . $settingName])) {
return $this->storedSettings[$className . $settingName];
}
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settingStatement->bind_param('ss', $className, $settingName);
$formattedValue = $setting->getFormattedValue();
$settingStatement->bind_param('ssss', $setting->class, $setting->setting, $setting->type, $formattedValue);
$settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
return null;
}
$settingStatement->store_result();
if ($settingStatement->num_rows <= 0) {
$this->setSetting($className, $settingName, $default);
return $default;
}
$settingStatement->bind_result($type, $value);
$settingStatement->fetch();
$settingStatement->free_result();
$settingStatement->close();
$setting = $this->castSetting($type, $value);
// Store setting in the ram
$this->storedSettings[$className . $settingName] = $setting;
return $setting;
}
/**
* Set a Setting for the given Object
*
* @param mixed $object
* @param string $settingName
* @param mixed $value
* @return bool
*/
public function setSetting($object, $settingName, $value) {
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = ?
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$setting = $this->formatSetting($value);
$settingStatement->bind_param('sss', $setting, $className, $settingName);
$success = $settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
}
$setting->index = $settingStatement->insert_id;
$settingStatement->close();
$this->storedSettings[$className . $settingName] = $value;
// Trigger settings changed Callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SETTINGS_CHANGED, $className, $settingName, $value);
return $success;
return true;
}
/**
* Cast a Setting to the given Type
*
* @param string $type
* @param mixed $value
* @return mixed
*/
private function castSetting($type, $value) {
if ($type === self::TYPE_INT) {
return (int)$value;
}
if ($type === self::TYPE_REAL) {
return (float)$value;
}
if ($type === self::TYPE_BOOL) {
return (bool)$value;
}
if ($type === self::TYPE_STRING) {
return (string)$value;
}
if ($type === self::TYPE_ARRAY) {
return explode($this->arrayDelimiter, $value);
}
trigger_error('Unsupported setting type. ' . print_r($type, true));
return $value;
}
/**
* Reset a Setting to its default Value
* Reset a Setting to its Default Value
*
* @param mixed $object
* @param string $settingName
* @return bool
*/
public function resetSetting($object, $settingName) {
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
public function resetSetting($object, $settingName = null) {
if ($object instanceof Setting) {
$className = $object->class;
$settingName = $object->setting;
} else {
$className = ClassUtil::getClass($object);
}
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = `default`
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
WHERE `class` = '" . $mysqli->escape_string($className) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ss', $className, $settingName);
$success = $settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
}
$settingStatement->close();
if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]);
}
return $success;
return $result;
}
/**
@ -392,29 +297,29 @@ class SettingManager implements CallbackListener {
* @param string $settingName
* @return bool
*/
public function deleteSetting($object, $settingName) {
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
public function deleteSetting($object, $settingName = null) {
if ($object instanceof Setting) {
$className = $object->class;
$settingName = $object->setting;
} else {
$className = ClassUtil::getClass($object);
}
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($className) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ss', $className, $settingName);
$success = $settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
}
$settingStatement->close();
if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]);
}
return $success;
return $result;
}
/**
@ -424,9 +329,10 @@ class SettingManager implements CallbackListener {
* @return array
*/
public function getSettingsByClass($object) {
$className = $this->getClassName($object);
$className = ClassUtil::getClass($object);
$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;";
$result = $mysqli->query($query);
if ($mysqli->error) {
@ -434,7 +340,7 @@ class SettingManager implements CallbackListener {
return null;
}
$settings = array();
while ($setting = $result->fetch_object()) {
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(true))) {
$settings[$setting->index] = $setting;
}
$result->free();
@ -442,7 +348,7 @@ class SettingManager implements CallbackListener {
}
/**
* Get all settings
* Get all Settings
*
* @return array
*/
@ -456,7 +362,7 @@ class SettingManager implements CallbackListener {
return null;
}
$settings = array();
while ($setting = $result->fetch_object()) {
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(true))) {
$settings[$setting->index] = $setting;
}
$result->free();
@ -479,9 +385,9 @@ class SettingManager implements CallbackListener {
return null;
}
$settingClasses = array();
while ($setting = $result->fetch_object()) {
if (!$hidePluginClasses || !PluginManager::isPluginClass($setting->class)) {
array_push($settingClasses, $setting->class);
while ($row = $result->fetch_object()) {
if (!$hidePluginClasses || !PluginManager::isPluginClass($row->class)) {
array_push($settingClasses, $row->class);
}
}
$result->free();

View File

@ -92,7 +92,7 @@ class ChatlogPlugin implements CallbackListener, Plugin {
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handlePlayerChatCallback');
$this->maniaControl->callbackManager->registerCallbackListener(SettingManager::CB_SETTINGS_CHANGED, $this, 'handleSettingsChangedCallback');
$this->maniaControl->callbackManager->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'handleSettingChangedCallback');
$this->buildLogFileName();
@ -167,11 +167,11 @@ class ChatlogPlugin implements CallbackListener, Plugin {
}
/**
* Handle Settings Changed Callback
* Handle Setting Changed Callback
*
* @param string $settingClass
*/
public function handleSettingsChangedCallback($settingClass) {
public function handleSettingChangedCallback($settingClass) {
if ($settingClass !== get_class()) {
return;
}

View File

@ -18,6 +18,7 @@ use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Plugins\PluginMenu;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
/**
@ -76,15 +77,27 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* @see \ManiaControl\Plugins\Plugin::prepare()
*/
public static function prepare(ManiaControl $maniaControl) {
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_MX_KARMA_ACTIVATED, true);
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_MX_KARMA_IMPORTING, true);
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_WIDGET_DISPLAY_MX, true);
$thisClass = get_class();
$maniaControl->settingManager->initSetting($thisClass, self::SETTING_MX_KARMA_ACTIVATED, true);
$maniaControl->settingManager->initSetting($thisClass, self::SETTING_MX_KARMA_IMPORTING, true);
$maniaControl->settingManager->initSetting($thisClass, self::SETTING_WIDGET_DISPLAY_MX, true);
$servers = $maniaControl->server->getAllServers();
foreach ($servers as $server) {
$maniaControl->settingManager->initSetting(get_class(), '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $server->login . ']MX Karma Code for ' . $server->login . '$l', '');
$settingName = self::buildKarmaSettingName($server->login);
$maniaControl->settingManager->initSetting($thisClass, $settingName, '');
}
}
/**
* Build the Karma Setting Name for the given Server Login
*
* @param string $serverLogin
* @return string
*/
private static function buildKarmaSettingName($serverLogin) {
return '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $serverLogin . ']MX Karma Code for ' . $serverLogin . '$l';
}
/**
* @see \ManiaControl\Plugins\Plugin::getId()
*/
@ -146,7 +159,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->maniaControl->callbackManager->registerCallbackListener(Callbacks::ENDMAP, $this, 'sendMxKarmaVotes');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handlePlayerChat');
$this->maniaControl->callbackManager->registerCallbackListener(SettingManager::CB_SETTINGS_CHANGED, $this, 'updateSettings');
$this->maniaControl->callbackManager->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'updateSettings');
// Define player stats
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYER_MAPVOTES);
@ -161,8 +174,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->mxKarma['startTime'] = time();
//Check if Karma Code got specified, and inform admin that it would be good to specifiy one
$serverLogin = $this->maniaControl->server->login;
$mxKarmaCode = $this->maniaControl->settingManager->getSetting($this, '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $serverLogin . ']MX Karma Code for ' . $serverLogin . '$l');
$serverLogin = $this->maniaControl->server->login;
$karmaSettingName = self::buildKarmaSettingName($serverLogin);
$mxKarmaCode = $this->maniaControl->settingManager->getSetting($this, $karmaSettingName);
if ($mxKarmaCode == '') {
$permission = $this->maniaControl->settingManager->getSetting($this->maniaControl->authenticationManager, PluginMenu::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS);
@ -223,8 +237,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
return;
}
$serverLogin = $this->maniaControl->server->login;
$mxKarmaCode = $this->maniaControl->settingManager->getSetting($this, '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $serverLogin . ']MX Karma Code for ' . $serverLogin . '$l');
$serverLogin = $this->maniaControl->server->login;
$karmaSettingName = self::buildKarmaSettingName($serverLogin);
$mxKarmaCode = $this->maniaControl->settingManager->getSetting($this, $karmaSettingName);
if ($mxKarmaCode == '') {
return;
@ -708,30 +723,35 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
/**
* Update Settings
* Update Setting
*
* @param $class
* @param $settingName
* @param $value
* @param Setting $setting
*/
public function updateSettings($class, $settingName, $value) {
if (!$class = get_class()) {
public function updateSettings(Setting $setting) {
if (!$setting->belongsToClass($this)) {
return;
}
$serverLogin = $this->maniaControl->server->login;
if ($settingName == '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $serverLogin . ']MX Karma Code for ' . $serverLogin . '$l') {
$this->mxKarmaOpenSession();
}
$serverLogin = $this->maniaControl->server->login;
$karmaSettingName = self::buildKarmaSettingName($serverLogin);
if ($settingName == 'Enable Karma Widget' && $value == true) {
$this->updateManialink = true;
$this->handle1Second(time());
} elseif ($settingName == 'Enable Karma Widget' && $value == false) {
$this->updateManialink = false;
$ml = new ManiaLink(self::MLID_KARMA);
$mltext = $ml->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($mltext);
switch ($setting->setting) {
case $karmaSettingName:
{
$this->mxKarmaOpenSession();
break;
}
case self::SETTING_WIDGET_ENABLE:
{
if ($setting->value) {
$this->updateManialink = true;
$this->handle1Second(time());
} else {
$this->updateManialink = false;
$this->maniaControl->manialinkManager->hideManialink(self::MLID_KARMA);
}
break;
}
}
}

View File

@ -22,6 +22,7 @@ use ManiaControl\Maps\Map;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
/**
@ -37,8 +38,8 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
*/
const ID = 7;
const VERSION = 0.2;
const NAME = 'Local Records Plugin';
const AUTHOR = 'MCTeam';
const NAME = 'Local Records Plugin';
const AUTHOR = 'MCTeam';
const MLID_RECORDS = 'ml_local_records';
const TABLE_RECORDS = 'mc_localrecords';
const SETTING_WIDGET_TITLE = 'Widget Title';
@ -129,7 +130,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERFINISH, $this, 'handlePlayerFinish');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERCHECKPOINT, $this, 'handlePlayerCheckpoint');
$this->maniaControl->callbackManager->registerCallbackListener(SettingManager::CB_SETTINGS_CHANGED, $this, 'handleSettingsChanged');
$this->maniaControl->callbackManager->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'handleSettingChanged');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
$this->maniaControl->commandManager->registerCommandListener(array('recs', 'records'), $this, 'showRecordsList', false, 'Shows a list of Local Records on the current map.');
$this->maniaControl->commandManager->registerCommandListener('delrec', $this, 'deleteRecord', true, 'Removes a record from the database.');
@ -325,16 +326,26 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
return $records;
}
public function handleSettingsChanged($class, $settingName, $value) {
if (!$class = get_class()) {
/**
* Handle Setting Changed Callback
*
* @param Setting $setting
*/
public function handleSettingsChanged(Setting $setting) {
if (!$setting->belongsToClass($this)) {
return;
}
if ($settingName == 'Enable Local Records Widget' && $value == true) {
$this->updateManialink = true;
} elseif ($settingName == 'Enable Local Records Widget' && $value == false) {
$ml = new ManiaLink(self::MLID_RECORDS);
$mltext = $ml->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($mltext);
switch ($setting->setting) {
case self::SETTING_WIDGET_ENABLE:
{
if ($setting->value) {
$this->updateManialink = true;
} else {
$this->maniaControl->manialinkManager->hideManialink(self::MLID_RECORDS);
}
break;
}
}
}