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\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ClassUtil;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Plugins\PluginManager; use ManiaControl\Plugins\PluginManager;
@ -18,19 +19,13 @@ class SettingManager implements CallbackListener {
/* /*
* Constants * Constants
*/ */
const TABLE_SETTINGS = 'mc_settings'; const TABLE_SETTINGS = 'mc_settings';
const TYPE_STRING = 'string'; const CB_SETTING_CHANGED = 'SettingManager.SettingChanged';
const TYPE_INT = 'int';
const TYPE_REAL = 'real';
const TYPE_BOOL = 'bool';
const TYPE_ARRAY = 'array';
const CB_SETTINGS_CHANGED = 'SettingManager.SettingsChanged';
/* /*
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $arrayDelimiter = ';;';
private $storedSettings = array(); private $storedSettings = array();
/** /**
@ -46,14 +41,14 @@ class SettingManager implements CallbackListener {
} }
/** /**
* Initialize necessary Database Tables * Initialize the necessary Database Tables
* *
* @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 = "'" . Setting::TYPE_STRING . "'";
$typeSet = $defaultType . ",'" . self::TYPE_INT . "','" . self::TYPE_REAL . "','" . self::TYPE_BOOL . "','" . self::TYPE_ARRAY . "'"; $typeSet = Setting::getTypeSet();
$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(100) NOT NULL, `class` varchar(100) NOT NULL,
@ -69,7 +64,6 @@ class SettingManager implements CallbackListener {
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
} }
return $result; return $result;
} }
@ -86,23 +80,120 @@ class SettingManager implements CallbackListener {
* @return bool * @return bool
*/ */
private function deleteUnusedSettings() { private function deleteUnusedSettings() {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "` $settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `changed` < NOW() - INTERVAL 1 HOUR;"; WHERE `changed` < NOW() - INTERVAL 1 HOUR;";
$settingStatement = $mysqli->prepare($settingQuery); $result = $mysqli->query($settingQuery);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$success = $settingStatement->execute(); if ($result) {
if ($settingStatement->error) { $this->storedSettings = array();
trigger_error($settingStatement->error); return true;
$settingStatement->close();
return false;
} }
$settingStatement->close(); return false;
$this->storedSettings = array(); }
return $success;
/**
* 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 mixed $object
* @param string $settingName * @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 * @return bool
*/ */
public function initSetting($object, $settingName, $default) { public function saveSetting(Setting &$setting) {
if (is_null($default) || is_object($default)) {
return false;
}
$className = $this->getClassName($object);
$type = $this->getType($default);
$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`,
@ -132,6 +237,7 @@ class SettingManager implements CallbackListener {
@value := ?, @value := ?,
@value @value
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`),
`type` = VALUES(`type`), `type` = VALUES(`type`),
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)), `value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
`default` = VALUES(`default`), `default` = VALUES(`default`),
@ -141,248 +247,47 @@ class SettingManager implements CallbackListener {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$settingStatement->bind_param('ssss', $className, $settingName, $type, $default); $formattedValue = $setting->getFormattedValue();
$success = $settingStatement->execute(); $settingStatement->bind_param('ssss', $setting->class, $setting->setting, $setting->type, $formattedValue);
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);
$settingStatement->execute(); $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) { if ($settingStatement->error) {
trigger_error($settingStatement->error); trigger_error($settingStatement->error);
$settingStatement->close(); $settingStatement->close();
return false; return false;
} }
$setting->index = $settingStatement->insert_id;
$settingStatement->close(); $settingStatement->close();
return true;
$this->storedSettings[$className . $settingName] = $value;
// Trigger settings changed Callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SETTINGS_CHANGED, $className, $settingName, $value);
return $success;
} }
/** /**
* Cast a Setting to the given Type * Reset a Setting to its Default Value
*
* @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
* *
* @param mixed $object * @param mixed $object
* @param string $settingName * @param string $settingName
* @return bool * @return bool
*/ */
public function resetSetting($object, $settingName) { public function resetSetting($object, $settingName = null) {
$className = $this->getClassName($object); if ($object instanceof Setting) {
$mysqli = $this->maniaControl->database->mysqli; $className = $object->class;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "` $settingName = $object->setting;
} else {
$className = ClassUtil::getClass($object);
}
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = `default` SET `value` = `default`
WHERE `class` = ? WHERE `class` = '" . $mysqli->escape_string($className) . "'
AND `setting` = ?;"; AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$settingStatement = $mysqli->prepare($settingQuery); $result = $mysqli->query($settingQuery);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; 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])) { if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]); unset($this->storedSettings[$className . $settingName]);
} }
return $success; return $result;
} }
/** /**
@ -392,29 +297,29 @@ class SettingManager implements CallbackListener {
* @param string $settingName * @param string $settingName
* @return bool * @return bool
*/ */
public function deleteSetting($object, $settingName) { public function deleteSetting($object, $settingName = null) {
$className = $this->getClassName($object); if ($object instanceof Setting) {
$mysqli = $this->maniaControl->database->mysqli; $className = $object->class;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "` $settingName = $object->setting;
WHERE `class` = ? } else {
AND `setting` = ?;"; $className = ClassUtil::getClass($object);
$settingStatement = $mysqli->prepare($settingQuery); }
$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) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; 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])) { if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]); unset($this->storedSettings[$className . $settingName]);
} }
return $success;
return $result;
} }
/** /**
@ -424,9 +329,10 @@ class SettingManager implements CallbackListener {
* @return array * @return array
*/ */
public function getSettingsByClass($object) { public function getSettingsByClass($object) {
$className = $this->getClassName($object); $className = ClassUtil::getClass($object);
$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) {
@ -434,7 +340,7 @@ class SettingManager implements CallbackListener {
return null; return null;
} }
$settings = array(); $settings = array();
while ($setting = $result->fetch_object()) { while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(true))) {
$settings[$setting->index] = $setting; $settings[$setting->index] = $setting;
} }
$result->free(); $result->free();
@ -442,7 +348,7 @@ class SettingManager implements CallbackListener {
} }
/** /**
* Get all settings * Get all Settings
* *
* @return array * @return array
*/ */
@ -456,7 +362,7 @@ class SettingManager implements CallbackListener {
return null; return null;
} }
$settings = array(); $settings = array();
while ($setting = $result->fetch_object()) { while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(true))) {
$settings[$setting->index] = $setting; $settings[$setting->index] = $setting;
} }
$result->free(); $result->free();
@ -479,9 +385,9 @@ class SettingManager implements CallbackListener {
return null; return null;
} }
$settingClasses = array(); $settingClasses = array();
while ($setting = $result->fetch_object()) { while ($row = $result->fetch_object()) {
if (!$hidePluginClasses || !PluginManager::isPluginClass($setting->class)) { if (!$hidePluginClasses || !PluginManager::isPluginClass($row->class)) {
array_push($settingClasses, $setting->class); array_push($settingClasses, $row->class);
} }
} }
$result->free(); $result->free();

View File

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

View File

@ -18,6 +18,7 @@ use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin; use ManiaControl\Plugins\Plugin;
use ManiaControl\Plugins\PluginMenu; use ManiaControl\Plugins\PluginMenu;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager; use ManiaControl\Settings\SettingManager;
/** /**
@ -76,15 +77,27 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* @see \ManiaControl\Plugins\Plugin::prepare() * @see \ManiaControl\Plugins\Plugin::prepare()
*/ */
public static function prepare(ManiaControl $maniaControl) { public static function prepare(ManiaControl $maniaControl) {
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_MX_KARMA_ACTIVATED, true); $thisClass = get_class();
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_MX_KARMA_IMPORTING, true); $maniaControl->settingManager->initSetting($thisClass, self::SETTING_MX_KARMA_ACTIVATED, true);
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_WIDGET_DISPLAY_MX, 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(); $servers = $maniaControl->server->getAllServers();
foreach ($servers as $server) { 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() * @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(Callbacks::ENDMAP, $this, 'sendMxKarmaVotes');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handlePlayerChat'); $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 // Define player stats
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYER_MAPVOTES); $this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYER_MAPVOTES);
@ -161,8 +174,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->mxKarma['startTime'] = time(); $this->mxKarma['startTime'] = time();
//Check if Karma Code got specified, and inform admin that it would be good to specifiy one //Check if Karma Code got specified, and inform admin that it would be good to specifiy one
$serverLogin = $this->maniaControl->server->login; $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'); $karmaSettingName = self::buildKarmaSettingName($serverLogin);
$mxKarmaCode = $this->maniaControl->settingManager->getSetting($this, $karmaSettingName);
if ($mxKarmaCode == '') { if ($mxKarmaCode == '') {
$permission = $this->maniaControl->settingManager->getSetting($this->maniaControl->authenticationManager, PluginMenu::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS); $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; return;
} }
$serverLogin = $this->maniaControl->server->login; $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'); $karmaSettingName = self::buildKarmaSettingName($serverLogin);
$mxKarmaCode = $this->maniaControl->settingManager->getSetting($this, $karmaSettingName);
if ($mxKarmaCode == '') { if ($mxKarmaCode == '') {
return; return;
@ -708,30 +723,35 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
* Update Settings * Update Setting
* *
* @param $class * @param Setting $setting
* @param $settingName
* @param $value
*/ */
public function updateSettings($class, $settingName, $value) { public function updateSettings(Setting $setting) {
if (!$class = get_class()) { if (!$setting->belongsToClass($this)) {
return; return;
} }
$serverLogin = $this->maniaControl->server->login; $serverLogin = $this->maniaControl->server->login;
if ($settingName == '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $serverLogin . ']MX Karma Code for ' . $serverLogin . '$l') { $karmaSettingName = self::buildKarmaSettingName($serverLogin);
$this->mxKarmaOpenSession();
}
if ($settingName == 'Enable Karma Widget' && $value == true) { switch ($setting->setting) {
$this->updateManialink = true; case $karmaSettingName:
$this->handle1Second(time()); {
} elseif ($settingName == 'Enable Karma Widget' && $value == false) { $this->mxKarmaOpenSession();
$this->updateManialink = false; break;
$ml = new ManiaLink(self::MLID_KARMA); }
$mltext = $ml->render()->saveXML(); case self::SETTING_WIDGET_ENABLE:
$this->maniaControl->manialinkManager->sendManialink($mltext); {
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\Player;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin; use ManiaControl\Plugins\Plugin;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager; use ManiaControl\Settings\SettingManager;
/** /**
@ -37,8 +38,8 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
*/ */
const ID = 7; const ID = 7;
const VERSION = 0.2; const VERSION = 0.2;
const NAME = 'Local Records Plugin'; const NAME = 'Local Records Plugin';
const AUTHOR = 'MCTeam'; const AUTHOR = 'MCTeam';
const MLID_RECORDS = 'ml_local_records'; const MLID_RECORDS = 'ml_local_records';
const TABLE_RECORDS = 'mc_localrecords'; const TABLE_RECORDS = 'mc_localrecords';
const SETTING_WIDGET_TITLE = 'Widget Title'; 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(CallbackManager::CB_TM_PLAYERFINISH, $this, 'handlePlayerFinish');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERCHECKPOINT, $this, 'handlePlayerCheckpoint'); $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->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(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.'); $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; 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; return;
} }
if ($settingName == 'Enable Local Records Widget' && $value == true) {
$this->updateManialink = true; switch ($setting->setting) {
} elseif ($settingName == 'Enable Local Records Widget' && $value == false) { case self::SETTING_WIDGET_ENABLE:
$ml = new ManiaLink(self::MLID_RECORDS); {
$mltext = $ml->render()->saveXML(); if ($setting->value) {
$this->maniaControl->manialinkManager->sendManialink($mltext); $this->updateManialink = true;
} else {
$this->maniaControl->manialinkManager->hideManialink(self::MLID_RECORDS);
}
break;
}
} }
} }