collapse collapse
* User Info
 
 
Welcome, Guest. Please login or register.
* Search

* Board Stats
  • stats Total Members: 989
  • stats Total Posts: 18366
  • stats Total Topics: 2501
  • stats Total Categories: 7
  • stats Total Boards: 35
  • stats Most Online: 1144

Author Topic: Module Settings  (Read 5833 times)

0 Members and 2 Guests are viewing this topic.

Offline Glarawyn

  • BeBot Hero
  • ******
  • Posts: 521
  • Karma: +0/-0
Module Settings
« on: September 25, 2006, 09:36:48 pm »
I got tired of copy/pasting functions for dealing with settings, so I made a core module to deal with it. This is written for BeBot 0.2 releases. I really don't know if it will work in 0.3 or if 0.3 implements it's own settings system.



Copy/Paste of Included Readme:
Code: [Select]
README for ModuleSettings Version 1.0.0

Prerequsites:
I've used SQL that is only available in MySQL 4.1.0 or higher. If you're
using a previous version of MySQL, this isn't going to work.

Installing ModuleSettings.php:

1: Open Bot.php in a text editor.
2: Find "var $lasttell;"
3: Add "var $settings;" after "var $lasttell;"
4: Save Bot.php.
5: Install module as normal.



Using ModuleSettings in your Custom Module:

get function:
$this -> bot -> settings -> get("ModuleName", "SettingName");
This will return an associative array if successful. $setting['SettingName']['SettingValue'];
If the setting cannot be found, the module FALSE and log an error message.

Usage Example 1:

$result = $this -> bot -> settings -> get("ModuleName", "SettingName");
if (!$result)
return "Error loading setting"
else
$somesetting = $result['SettingName'];

Usage Example 2:

$result = $this -> bot -> settings -> get("ModuleName", "SettingName");
if (!$result)
return "Error loading setting"
else
$this -> settings['SettingName'] = $result['SettingName'];



get_all function:
$this -> bot -> settings -> get_all("ModuleName");
This will return an associative array of all of ModuleName's settings.

Usage Example 1:
$settings = $this -> bot -> settings -> get_all("ModuleName");
if ($setting['BoolSettingName'])
// Do something
else
// Do something else



save function (normal use):
$this -> bot -> settings -> save("ModuleName", "SettingName", "Value");
This will save a new setting to the database, or update an existing setting
if the setting already exisits. Usage for save and update is identical.


Usage Example 1:
$this -> bot -> settings -> save("SettingsTest", "string_test", "This is my string"]); // Should create a string setting

Usage Example 2:
$this -> bot -> settings -> save("SettingsTest", "int_test", time()); // Should create an int setting

Usage Example 3:
$this -> bot -> settings -> save("SettingsTest", "bool_Test", FALSE); // Should create a bool setting

Usage Example 4:
$this -> bot -> settings -> save("SettingsTest", "NULL_test", NULL); // Should create a null setting

Usage Example 5:
$this -> bot -> settings -> save("SettingsTest", "float_test", 123456789.123); // should create a float setting

save function (Do not update if setting exisits):
$this -> bot -> settings -> save("ModuleName", "SettingName", "Value", TRUE);
This will save a new setting to the database, but will not update the setting
if the setting exists in the database already. This is equivlent to doing
$this -> bot -> db -> query(INSERT IGNORE INTO module_settigns VALUES (...));

The zip file also contains a functioning example module to show you how the functions work.

In the bot channel, do:
!save ModuleName SettingName SettingValue to save/update a setting and type !get to have the bot output all of the module_settings for SettingsTest.

Don't use the Example module on a production bot. :)

Download:
Module Settings 1.0.1[/]
« Last Edit: October 06, 2006, 09:09:12 pm by Glarawyn »

Offline Xenixa

  • Contributor
  • *******
  • Posts: 307
  • Karma: +0/-0
Re: Module Settings
« Reply #1 on: September 26, 2006, 12:30:03 pm »
I could so kiss you for this module!! Thanks.

* Xenixa runs off to jump hip deep intp some module rewroking.
<<< Hack's in Zend Studio

All my Custom Bebot files may be Found Here <-clicky

Offline Glarawyn

  • BeBot Hero
  • ******
  • Posts: 521
  • Karma: +0/-0
Re: Module Settings
« Reply #2 on: September 26, 2006, 09:22:12 pm »
Glad you like it Xenixa. I hope you don't find it too confusing. I explained my logic for Naturalistic earlier and he found it helpful, so I'm going to add the same info here.

I wanted to address a few issues I'd run into when I wrote this thing. First, if you look at my APF module you may see alot of setting mangling to get things in and out of the database with the proper datatypes. I had no shortage of issues with comparing things as the default settings table is all character (string) data. Settings just didn't behave as expected, or PHP would throw up a warning message when I tried to add integers to character data, and other similar issues. Hopefully this module will save me those headaches when I get around to updating my APF module.

Like the settings table, the module_settings table is also all string data. I have no idea what datatypes you will want for your settings, but I can identify the datatypes you pass the save() function. When save() saves a setting to the database, it determines the datatype, converts your setting value to a string, and saves the datatype and value information to the database. This way we get a single table structure for any type of setting data. The get() functions read the datatype and convert the setting value from a string back to the proper datatype before returning it to you. This is all done automatically, and the intended result is for your setting data to behave how you would expect without PHP spewing errors or warnings.

The second somewhat wierd thing is that both the get functions return arrays, even though get() is only retriving a single value. I promise, there is a reason for this. If for some reason the setting cannot be retrived from the database, get() and get_all() return FALSE. (An error will also be logged to the bot console.) With get_all() it's obvious that FALSE is an error not a setting value as you were expecting get_all() to return an array.

If get() returned just the value of the requested setting, and you happened to be dealing with a boolean setting there would be no way to determine if an error occured other than checking the log or console output. I found this to be an unaccetable situation, thus get() returns an array in the same format as get_all().

The way I do settings is to make a $settings variable available as a class wide variable. I use the get_all() function to populate the module's settings array when the module is loaded, and if changes are made I change the value of the array element and save the updated setting to the database with the save() function. I don't use the get() function at all myself, but I put it in as others may find it useful to get a single setting.

Feedback is always welcome. I've only made use of this in one module but the results so far have been great. :)

Offline Naturalistic

  • Contributor
  • *******
  • Posts: 221
  • Karma: +0/-0
Re: Module Settings
« Reply #3 on: September 27, 2006, 05:28:58 pm »
I'll have to test it out on the modules I'm working on now then. :P
220/25 Eternalist Doctor
-----------------------------
Campalot Coding Co-ordinator and Super Admin
http://www.campalot.info/index.php

Offline Alreadythere

  • BeBot Maintainer
  • BeBot Hero
  • ******
  • Posts: 1288
  • Karma: +0/-0
Re: Module Settings
« Reply #4 on: October 05, 2006, 01:29:13 pm »
Just got time to look at it, nice module.

You should add a way to predefine settings once though, as your save() function can't be used for that, it always overwrites any existing values.
Something like a define() function that does an INSERT IGNORE.

Offline Glarawyn

  • BeBot Hero
  • ******
  • Posts: 521
  • Karma: +0/-0
Re: Module Settings
« Reply #5 on: October 06, 2006, 03:58:42 am »
Just got time to look at it, nice module.

You should add a way to predefine settings once though, as your save() function can't be used for that, it always overwrites any existing values.
Something like a define() function that does an INSERT IGNORE.

Naw, a new function that does almost the same thing would be silly, just need to modify save to have two different SQL modes...

To add default settings for the first time (note the addition of the boolean default paramater):
$this -> bot -> settings -> save("name", "setting", "value", TRUE);
Code: [Select]
    /*
    Saves a setting to the database. Returns true on success, otherwise false.
    */   
    function save($module, $setting, $value, $default=FALSE)
    {
    // Figure out what type of data we have.
    if (is_bool($value))
    {
    $datatype = "bool";
    if ($value)
    $value = "TRUE";
    else
    $value = "FALSE";
    }
    elseif (is_null($value))
    {
    $datatype = "null";
    $value = "null";
    }
    elseif (is_int($value))
    $datatype = "int";
    elseif (is_float($value))
    $datatype = "float";
    elseif (is_string($value))
    $datatype = "string";
    else
    $datatype = "unknown";
    // Change $value to a string and add escape slashes if needed.
    $value = addslashes(strval($value));
    //
    $into =  "INTO module_settings (module, setting, value, datatype) ";
    $values = "VALUES ('".$module."','".$setting."','".$value."','".$datatype."') ";
    if ($default)
    {    
    $sql = "INSERT IGNORE ".$into.$values;
    }
    else
    {
    $dupkey = "ON DUPLICATE KEY UPDATE value = '".$value."', datatype = '".$datatype."'";    
    $sql = "INSERT ".$into.$values.$dupkey;
    }   
    $result = $this -> bot -> db -> returnQuery($sql);
   
    if ($result)
    $this -> bot -> log("SET", "SAVED", "Set ".$module."_".$setting." to ".$value." as datatype ".$datatype);
    else
    $this -> bot -> log("SET", "ERROR", "Could not save ".$module."_".$setting);
    return $result;
    }

If you do not pass the $default paramater of set $default to FALSE, save behaves as normal and updates any current settings. If you set $default to true, save() will insert the default setting if it does not already exisit.

I did this in the middle of class lecture, so it might not work as I was a bit distracted. I'll test it later and put up a new version with updated documentation, etc. later.

Offline Glarawyn

  • BeBot Hero
  • ******
  • Posts: 521
  • Karma: +0/-0
Re: Module Settings
« Reply #6 on: October 06, 2006, 11:41:31 pm »
Updated to v1.0.1. Only change is the one discussed with Alreadythere. Completely backwards compatible with v1.0.0. v1.0.1 adds the the ability for the save() function to be used to define default settings for your module without overwriting the saved settings every time the module loads.

Offline Glarawyn

  • BeBot Hero
  • ******
  • Posts: 521
  • Karma: +0/-0
Re: Module Settings
« Reply #7 on: December 06, 2006, 05:59:29 pm »
A new version of this module has made it into the development version of BeBot. The development version should be backwards compatible with this version, but you will have to modify your code to take advantage of the settings interface and to create new settings.

Offline Glarawyn

  • BeBot Hero
  • ******
  • Posts: 521
  • Karma: +0/-0
Re: Module Settings
« Reply #8 on: December 29, 2006, 01:17:26 am »
New Module Settings for 0.2.X

I back ported the development version of settings to BeBot 0.2.X.  ;D

 

* Recent Posts
Com bot module by bitnykk
[November 25, 2024, 05:36:11 pm ]


0.8.x updates for AO by bitnykk
[June 23, 2024, 03:19:47 pm ]


0.8.x updates for AoC by bitnykk
[June 23, 2024, 03:19:44 pm ]


[AoC] special char for items module by bitnykk
[February 09, 2024, 09:41:18 pm ]


BeBot still alive & kicking ! by bitnykk
[December 17, 2023, 12:58:44 am ]

* Who's Online
  • Dot Guests: 298
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.
* Forum Staff
bitnykk admin bitnykk
Administrator
Khalem admin Khalem
Administrator
WeZoN gmod WeZoN
Global Moderator
SimplePortal 2.3.7 © 2008-2024, SimplePortal