boolean thing working

This commit is contained in:
kremsy 2014-01-05 22:51:21 +01:00
parent 2154276a71
commit 5d4cf86f61
3 changed files with 190 additions and 95 deletions

View File

@ -14,10 +14,12 @@ use FML\Controls\Label;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\Script\Script;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
class ManiaControlSettings implements ConfiguratorMenu {
class ManiaControlSettings implements ConfiguratorMenu, CallbackListener {
/**
* Constants
*/
@ -38,6 +40,8 @@ class ManiaControlSettings implements ConfiguratorMenu {
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
}
/**
@ -225,7 +229,7 @@ class ManiaControlSettings implements ConfiguratorMenu {
foreach($configData[3] as $setting) {
$settingName = substr($setting['Name'], $prefixLength + 1);
$oldSetting = $maniaControlSettings[$settingName];
if($setting['Value'] == $oldSetting->value) {
if($setting['Value'] == $oldSetting->value || $oldSetting->type = 'bool') {
continue;
}
@ -236,4 +240,54 @@ class ManiaControlSettings implements ConfiguratorMenu {
$menuId = $this->maniaControl->configurator->getMenuId($this->getTitle());
$this->maniaControl->configurator->reopenMenu($menuId);
}
/**
* Handle ManialinkPageAnswer Callback
*
* @param array $callback
*/
public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2];
$boolSetting = (strpos($actionId, self::ACTION_SETTING_BOOL) === 0);
if(!$boolSetting) {
return;
}
$actionArray = explode(".", $actionId);
$setting = $actionArray[2];
$login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
// Toggle the Boolean Setting
$this->toggleBooleanSetting($setting, $player);
// Save all Changes
$this->saveConfigData($callback[1], $player);
}
/**
* Toggles a Boolean Value
*
* @param $setting
* @param Player $player
*/
public function toggleBooleanSetting($setting, Player $player) {
$oldSetting = $this->maniaControl->settingManager->getSettingByIndex($setting);
if(!isset($oldSetting)) {
var_dump('no setting ' . $setting);
return;
}
//Toggle value
if($oldSetting->value == "1") {
$this->maniaControl->settingManager->updateSetting($oldSetting->class, $oldSetting->setting, "0");
} else {
$this->maniaControl->settingManager->updateSetting($oldSetting->class, $oldSetting->setting, "1");
}
}
}

View File

@ -111,14 +111,16 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$loadedSettings = array();
while($row = $result->fetch_object()) {
if(!isset($scriptSettings[$row->settingName]))
if(!isset($scriptSettings[$row->settingName])) {
continue;
}
$loadedSettings[$row->settingName] = $row->settingValue;
settype($loadedSettings[$row->settingName], gettype($scriptSettings[$row->settingName]));
}
$result->close();
if(!$loadedSettings)
if(!$loadedSettings) {
return true;
}
$success = $this->maniaControl->client->query('SetModeScriptSettings', $loadedSettings);
if(!$success) {
@ -368,8 +370,9 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* @param bool
*/
private function applyNewScriptSettings(array $newSettings, Player $player) {
if(!$newSettings)
if(!$newSettings) {
return true;
}
$success = $this->maniaControl->client->query('SetModeScriptSettings', $newSettings);
if(!$success) {
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);

View File

@ -209,15 +209,39 @@ class SettingManager {
}
/**
* Get setting by name for given object
* Get a Setting by its index
*
* @param object $object
* @param string $settingName
* @param mixed $default
* @return mixed
* @param $settingIndex
* @param bool $default
* @internal param null $default
* @internal param $className
* @internal param \ManiaControl\gIndex $settin
* @return mixed|null
*/
public function getSetting($object, $settingName, $default = null) {
$className = $this->getClassName($object);
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 false;
}
$row = $result->fetch_object();
$result->close();
return $row;
}
/**
* Gets a Setting Via it's class name
*
* @param $className
* @param $settingName
* @param $value
*/
public function getSettingByClassName($className, $settingName, $default = null) {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ?
@ -235,7 +259,7 @@ class SettingManager {
}
$settingStatement->store_result();
if($settingStatement->num_rows <= 0) {
$this->initSetting($object, $settingName, $default);
$this->updateSetting($className, $settingName, $default);
return $default;
}
$settingStatement->bind_result($type, $value);
@ -246,8 +270,22 @@ class SettingManager {
return $setting;
}
/**
* Get setting by name for given object
*
* @param object $object
* @param string $settingName
* @param mixed $default
* @return mixed
*/
public function getSetting($object, $settingName, $default = null) {
$className = $this->getClassName($object);
return $this->getSettingByClassName($className, $settingName, $default);
}
/**
* Updates a Setting
*
* @param $className
* @param $settingName
* @param $value