// setting command v0.4.1
// Submitted by [NATO]Hunter
//
// setting allows you to save global or player based settings
// syntax:
// setting create <settingname> "<description>" [type]
// setting delete <settingname>
// setting clear <settingname> [time-difference]
// setting exists <return-var> <settingname>
// setting settitle <settingname> "<description>"
// setting addoption <settingname> <option-key> <option-name> "[description]"
// setting remoption <settingname> <option-key>
// setting clearoption <settingname>
// setting addsound <settingname> <sound-path>
// setting remsound <settingname>
// setting send <settingname> <userid> [locked]
// setting sendglobal <settingname> <userid> [locked]
// setting getvar <return-var> <settingname> <variable-name> [userid]
// setting setvar <settingname> <variable-name> <value> [userid]
// setting resend <settingname> <1/0>
// setting backmenu <settingname> <keymenu/popup>
// setting list
//
// For "list" settings:
// setting setdefault <settingname> <option-key> [overwrite]
// setting get <return-var> <settingname> [userid]
// setting set <settingname> <option-key> [userid]
//
// For "toggle" settings:
// setting setdefault <settingname> <option-key> <1/0> [overwrite]
// setting get <return-var> <settingname> <option-key> [userid]
// setting set <settingname> <option-key> <1/0> [userid]
// setting toggle <settingname> <option-key> [userid]
//
// Originally submitted by [NATO]Hunter

block load
{
  es_xdoblock setting/setting_register
}

// called to register the setting command
block setting_register
{
  // variables used by setting
  es_xset _setting_cmd 0
  es_xset _setting_name 0
  es_xset _setting_descr 0
  es_xset _setting_exists 0
  es_xset _setting_uniqueid 0
  es_xset _setting_userid 0
  es_xset _setting_time 0
  es_xset _setting_key 0
  es_xset _setting_value 0
  es_xset _setting_time 0
  es_xset _setting_timestamp 0
  es_xset _setting_temp 0
  es_xset _setting_keygroup 0
  es_xset _setting_type 0
  es_xset _setting_option 0
  es_xset _setting_string 0
  es_xset _setting_result 0
  es_xset _setting_toggle 0
  es_xset _setting_sound 0
  es_xset _setting_menu 0
  es_xset _setting_return 0
  es_xset _setting_helpline 0
  es_xset _setting_lock 0
  es_xset _setting_locked 0
  es_xset _setting_sendglobal 0
  es_xset _setting_query 0
  es_xset _setting_overwrite 0
  es_xset _setting_format 0
  es_xset _setting_resend 0
  es_xset _setting_backmenu 0
  es_xset _setting_dosave 0
  es_xset _setting_variable 0
  es_xset _popup_userid 0

  // setting keygroup
  es_xkeygroupload _setting_instance |setting

  // setting database
  es_xsql open _setting_database |setting
  es_xformatqv _setting_query "CREATE TABLE IF NOT EXISTS _setting_list (setting varchar(100), key varchar(100), display varchar(100), descr varchar(250), sort integer default '1' not null PRIMARY KEY,UNIQUE(setting, key));"
  es_sql query _setting_database server_var(_setting_query)
  es_xformatqv _setting_query "CREATE TABLE IF NOT EXISTS _setting_data (setting varchar(100), uniqueid varchar(100), time integer, key varchar(100) default '0', value varchar(10) default '0',PRIMARY KEY(setting, uniqueid, key));"
  es_sql query _setting_database server_var(_setting_query)
  es_xformatqv _setting_query "CREATE TABLE IF NOT EXISTS _setting_glob (setting varchar(100), key varchar(100) default '0', value varchar(10) default '0',PRIMARY KEY(setting, key));"
  es_sql query _setting_database server_var(_setting_query)
  es_xformatqv _setting_query "CREATE TABLE IF NOT EXISTS _setting_vars (setting varchar(100), uniqueid varchar(100), time integer, variable varchar(100) default '0', value varchar(100) default '0',PRIMARY KEY(setting, uniqueid, variable));"
  es_sql query _setting_database server_var(_setting_query)

  // check to see if setting exists
  es_xexists _setting_exists command setting
  ifx false(_setting_exists) do
  {
    // setting keygroup
    es_xregcmd setting setting/setting "Allows you to handle settings."
  }

  setting exists _setting_temp listtest
  ifx false(_setting_temp) do
  {
    setting create listtest "List Test" list
    setting addoption listtest option_a "Option A" "Choose one option"
    setting addoption listtest option_b "Option B"
    setting addoption listtest option_c "Option C"
    setting addoption listtest option_d "Option D"
    setting addoption listtest option_e "Option E"
    setting setdefault listtest option_c
    setting addsound listtest "ui/buttonclick.wav"
  }
  setting exists _setting_temp toggletest
  ifx false(_setting_temp) do
  {
    setting create toggletest "Toggle Test" toggle
    setting addoption toggletest toggle_a "Toggle A" "Toogle these options"
    setting addoption toggletest toggle_b "Toggle B"
    setting addoption toggletest toggle_c "Toggle C"
    setting setdefault toggletest toggle_a 1
    setting setdefault toggletest toggle_b 0
    setting addsound toggletest "ui/buttonrollover.wav"
  }

  foreach player _setting_temp #all "es_xdoblock setting/updateplayer_loadup"

  testcase qcreate corelib settingtest "Tests setting"
  testcase addtest settingtest settingtest1 setting/setting_setget_test "Test setget cases"
}

block unload
{
  // setting database
  es_xsql close _setting_database

  // setting keygroup
  es_xkeygroupsave _setting_instance |setting
  es_xkeygroupdelete _setting_instance
}

block setting
{
  // get the operation they want to do
  es_xgetargv _setting_cmd 1
  es_format _setting_cmd "setting/setting_cmd_%1" server_var(_setting_cmd)
  es_exists _setting_exists block server_var(_setting_cmd)
  ifx true(_setting_exists) do
  {
    es_doblock server_var(_setting_cmd)
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid subcommand for setting.
  }
}

block setting_cmd_create
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_xgetargv _setting_descr 3
    ifx true(_setting_descr) do
    {
      es_exists _setting_exists key _setting_instance server_var(_setting_name)
      ifx false(_setting_exists) do
      {
        es_xgetargv _setting_type 4
        if (server_var(_setting_type) != "toggle") do
        {
          es_xset _setting_type "list"
        }
        es_keycreate _setting_instance server_var(_setting_name)
        es_keysetvalue _setting_instance server_var(_setting_name) descr server_var(_setting_descr)
        es_keysetvalue _setting_instance server_var(_setting_name) type server_var(_setting_type)
        es_keysetvalue _setting_instance server_var(_setting_name) resend 1
        es_keysetvalue _setting_instance server_var(_setting_name) backmenu 0
        if (server_var(_setting_type) == "list") do
        {
          es_keysetvalue _setting_instance server_var(_setting_name) def 0
        }
        es_xkeygroupsave _setting_instance |setting
      }
      else do
      {
        es_xformatv _setting_format "setting: The setting '%1' does already exists." _setting_name
        es_xdbgmsgv 0 _setting_format
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting create. Syntax: setting create <settingname> "<description>" [type]
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting create. Syntax: setting create <settingname> "<description>" [type]
  }
}

block setting_cmd_delete
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_exists _setting_exists key _setting_instance server_var(_setting_name)
    ifx true(_setting_exists) do
    {
      es_keydelete _setting_instance server_var(_setting_name)
      es_xkeygroupsave _setting_instance |setting
      es_xformatqv _setting_query "DELETE FROM _setting_list WHERE setting='%1';DELETE FROM _setting_data WHERE setting='%1';DELETE FROM _setting_glob WHERE setting='%1';DELETE FROM _setting_vars WHERE setting='%1'" _setting_name
      es_sql _setting_database server_var(_setting_query)
      es_xsql close _setting_database
      es_xsql open _setting_database |setting
    }
    else do
    {
      es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
      es_xdbgmsgv 0 _setting_format
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting delete. Syntax: setting delete <settingname>
  }
}

block setting_cmd_clear
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_exists _setting_exists key _setting_instance server_var(_setting_name)
    ifx true(_setting_exists) do
    {
      es_xgetargv _setting_time 3
      ifx true(_setting_time) do
      {
        es_xgettimestamp _setting_timestamp
        es_math _setting_timestamp - server_var(_setting_time)
        es_xformatqv _setting_query "DELETE FROM _setting_data WHERE setting='%1' AND time<'%2';DELETE FROM _setting_glob WHERE setting='%1';DELETE FROM _setting_vars WHERE setting='%1' AND time<'%2' AND time>1" _setting_name _setting_timestamp
        es_sql query _setting_database server_var(_setting_query)
      }
      else do
      {
        es_xformatqv _setting_query "DELETE FROM _setting_data WHERE setting='%1';DELETE FROM _setting_glob WHERE setting='%1';DELETE FROM _setting_vars WHERE setting='%1'" _setting_name
        es_sql query _setting_database server_var(_setting_query)
      }
      es_xsql close _setting_database
      es_xsql open _setting_database |setting
    }
    else do
    {
      es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
      es_xdbgmsgv 0 _setting_format
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting clear. Syntax: setting clear <settingname> [time-difference]
  }
}

block setting_cmd_exists
{
  es_xgetargv _setting_value 2
  ifx true(_setting_value) do
  {
    es_xgetargv _setting_name 3
    ifx true(_setting_name) do
    {
      es_exists server_var(_setting_value) key _setting_instance server_var(_setting_name)
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting exists. Syntax: setting exists <return-var> <settingname>
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting exists. Syntax: setting exists <return-var> <settingname>
  }
}

block setting_cmd_settitle
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_xgetargv _setting_descr 3
    ifx true(_setting_descr) do
    {
      es_exists _setting_exists key _setting_instance server_var(_setting_name)
      ifx true(_setting_exists) do
      {
        es_keysetvalue _setting_instance server_var(_setting_name) descr server_var(_setting_descr)
        es_xkeygroupsave _setting_instance |setting
      }
      else do
      {
        es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
        es_xdbgmsgv 0 _setting_format
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting settitle. Syntax: setting settitle <settingname> "<description>"
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting settitle. Syntax: setting settitle <settingname> "<description>"
  }
}

block setting_cmd_addoption
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_xgetargv _setting_key 3
    ifx true(_setting_key) do
    {
      es_xgetargv _setting_value 4
      ifx true(_setting_value) do
      {
        es_exists _setting_exists key _setting_instance server_var(_setting_name)
        ifx true(_setting_exists) do
        {
          es_xgetargv _setting_descr 5
          ifx true(_setting_descr) do
          {
            es_xformatqv _setting_query "REPLACE INTO _setting_list (setting,key,display,descr) VALUES('%1','%2','%3','%4');" _setting_name _setting_key _setting_value _setting_descr
          }
          else do
          {
            es_xformatqv _setting_query "REPLACE INTO _setting_list (setting,key,display,descr) VALUES('%1','%2','%3','0');" _setting_name _setting_key _setting_value
          }
          es_sql query _setting_database server_var(_setting_query)
          es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
          if (server_var(_setting_type) == "list") do
          {
            es_keygetvalue _setting_temp _setting_instance server_var(_setting_name) def
            ifx false(_setting_temp) do
            {
              es_keysetvalue _setting_instance server_var(_setting_name) def server_var(_setting_key)
              es_xkeygroupsave _setting_instance |setting
            }
          }
          else do
          {
            es_xformatv _setting_temp "def_%1" _setting_key
            es_exists _setting_exists keyvalue _setting_instance server_var(_setting_name) server_var(_setting_temp)
            ifx false(_setting_exists) do
            {
              es_keysetvalue _setting_instance server_var(_setting_name) server_var(_setting_temp) "on"
              es_xkeygroupsave _setting_instance |setting
            }
          }
        }
        else do
        {
          es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
          es_xdbgmsgv 0 _setting_format
        }
      }
      else do
      {
        es_xdbgmsg 0 setting: Invalid arguments for setting addoption. Syntax: setting addoption <settingname> <option-key> <option-name> "[description]"
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting addoption. Syntax: setting addoption <settingname> <option-key> <option-name> "[description]"
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting addoption. Syntax: setting addoption <settingname> <option-key> <option-name> "[description]"
  }
}

block setting_cmd_remoption
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_xgetargv _setting_key 3
    ifx true(_setting_key) do
    {
      es_exists _setting_exists key _setting_instance server_var(_setting_name)
      ifx true(_setting_exists) do
      {
        es_xformatqv _setting_query "SELECT COUNT(display) FROM _setting_list WHERE setting='%1' AND key='%2'" _setting_name _setting_key
        es_sql queryvalue _setting_database _setting_exists server_var(_setting_query)
        ifx true(_setting_exists) do
        {
          es_xformatqv _setting_query "DELETE FROM _setting_list WHERE setting='%1' AND key='%2';DELETE FROM _setting_data WHERE setting='%1' AND (key='%2' OR value='%2');DELETE FROM _setting_glob WHERE setting='%1' AND key='%2'" _setting_name _setting_key
          es_sql query _setting_database server_var(_setting_query)
          es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
          if (server_var(_setting_type) == "list") do
          {
            es_keygetvalue _setting_temp server_var(_setting_name) def
            if (server_var(_setting_temp) == server_var(_setting_key)) do
            {
              es_keysetvalue _setting_instance server_var(_setting_name) def 0
            }
          }
        }
        else do
        {
          es_xformatv _setting_format "setting: The option '%1' does not exists in setting '%2'." _setting_key _setting_name
          es_xdbgmsgv 0 _setting_format
        }
      }
      else do
      {
        es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
        es_xdbgmsgv 0 _setting_format
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting remoption. Syntax: setting remoption <settingname> <option-key>
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting remoption. Syntax: setting remoption <settingname> <option-key>
  }
}

block setting_cmd_clearoption
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_exists _setting_exists key _setting_instance server_var(_setting_name)
    ifx true(_setting_exists) do
    {
      es_xformatqv _setting_query "DELETE FROM _setting_list WHERE setting='%1'" _setting_name _setting_key
      es_sql query _setting_database server_var(_setting_query)
      es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
      if (server_var(_setting_type) == "list") do
      {
        es_keysetvalue _setting_instance server_var(_setting_name) def 0
      }
    }
    else do
    {
      es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
      es_xdbgmsgv 0 _setting_format
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting clearoption. Syntax: setting clearoption <settingname>
  }
}

block setting_cmd_addsound
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_xgetargv _setting_sound 3
    ifx true(_setting_sound) do
    {
      es_exists _setting_exists key _setting_instance server_var(_setting_name)
      ifx true(_setting_exists) do
      {
        es_keysetvalue _setting_instance server_var(_setting_name) sound server_var(_setting_sound)
        es_xkeygroupsave _setting_instance |setting
      }
      else do
      {
        es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
        es_xdbgmsgv 0 _setting_format
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting addsound. Syntax: setting addsound <settingname> <sound-path>
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting addsound. Syntax: setting addsound <settingname> <sound-path>
  }
}

block setting_cmd_remsound
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_exists _setting_exists key _setting_instance server_var(_setting_name)
    ifx true(_setting_exists) do
    {
      es_keysetvalue _setting_instance server_var(_setting_name) sound 0
      es_xkeygroupsave _setting_instance |setting
    }
    else do
    {
      es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
      es_xdbgmsgv 0 _setting_format
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting remsound. Syntax: setting remsound <settingname>
  }
}

block setting_cmd_send
{
  es_xexists _setting_temp script keymenu
  ifx true(_setting_temp) do
  {
    es_xgetargv _setting_name 2
    ifx true(_setting_name) do
    {
      es_xgetargv _setting_userid 3
      ifx true(_setting_userid) do
      {
        es_exists _setting_exists key _setting_instance server_var(_setting_name)
        ifx true(_setting_exists) do
        {
          es_getuserid _setting_userid server_var(_setting_userid)
          ifx true(_setting_userid) do
          {
            es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
            es_xformatv _setting_keygroup "_settingresult_%1_%2" _setting_name _setting_userid
            es keygroupremove server_var(_setting_keygroup)
            if (server_var(_setting_type) == "list") do
            {
              es uniqueid _setting_uniqueid server_var(_setting_userid) 1
              es_xformatqv _setting_query "SELECT _setting_list.key, _setting_list.display, _setting_list.descr, _setting_data.value AS data_value FROM _setting_list LEFT OUTER JOIN _setting_data ON (_setting_list.setting=_setting_data.setting AND _setting_data.uniqueid='%2') WHERE _setting_list.setting='%1' ORDER BY _setting_list.sort ASC" _setting_name _setting_uniqueid
              es_sql query _setting_database server_var(_setting_keygroup) server_var(_setting_query)
              es_foreachkey _setting_key in server_var(_setting_keygroup) "es_xdoblock setting/setting_createstring_list"
              es_xset _setting_helpline "You can choose one of these options"
            }
            else do
            {
              es uniqueid _setting_uniqueid server_var(_setting_userid) 1
              es_xformatqv _setting_query "SELECT _setting_list.key, _setting_list.display, _setting_list.descr, _setting_data.key AS data_key, _setting_data.value AS data_value FROM _setting_list LEFT OUTER JOIN _setting_data ON (_setting_list.setting=_setting_data.setting AND _setting_list.key=_setting_data.key AND _setting_data.uniqueid='%2') WHERE _setting_list.setting='%1' ORDER BY _setting_list.sort ASC" _setting_name _setting_uniqueid
              es_sql query _setting_database server_var(_setting_keygroup) server_var(_setting_query)
              es_foreachkey _setting_key in server_var(_setting_keygroup) "es_xdoblock setting/setting_createstring_toggle"
              es_xset _setting_helpline "You can toggle each option on or off"
            }
            es_keygetvalue _setting_descr _setting_instance server_var(_setting_name) descr
            es_keygetvalue _setting_backmenu _setting_instance server_var(_setting_name) backmenu
            es_xformatv _setting_menu "_settingmenu_%1_%2" _setting_name _setting_userid
            es_xformatv _setting_descr "%1\n%2" _setting_descr _setting_helpline
            es keymenu exists _setting_exists server_var(_setting_menu)
            ifx true(_setting_exists) do
            {
              es keymenu delete server_var(_setting_menu)
            }
            es_xgetargv _setting_locked 4
            ifx false(_setting_locked) do
            {
              esnq keymenu create server_var(_setting_menu) _setting_key setting/setting_submit server_var(_setting_keygroup) #keyvalue string #key server_var(_setting_descr)
            }
            else do
            {
              esnq keymenu create server_var(_setting_menu) _setting_key setting/setting_dummy server_var(_setting_keygroup) #keyvalue string #key server_var(_setting_descr)
            }
            es keymenu setvar server_var(_setting_menu) _setting_name server_var(_setting_name)
            ifx true(_setting_backmenu) do
            {
              es keymenu backmenu server_var(_setting_menu) server_var(_setting_backmenu)
            }
            es keymenu send server_var(_setting_menu) server_var(_setting_userid)
          }
          else do
          {
            es_xformatv _setting_format "setting: The userid '%1' does not exists." _setting_userid
            es_xdbgmsgv 0 _setting_format
          }
        }
        else do
        {
          es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
          es_xdbgmsgv 0 _setting_format
        }
      }
      else do
      {
        es_xdbgmsg 0 setting: Invalid arguments for setting send. Syntax: setting send <settingname> <userid>
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting send. Syntax: setting send <settingname> <userid>
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: keymenu lib has to be loaded!
  }
}

block setting_cmd_sendglobal
{
  es_xexists _setting_temp script keymenu
  ifx true(_setting_temp) do
  {
    es_xgetargv _setting_name 2
    ifx true(_setting_name) do
    {
      es_xgetargv _setting_userid 3
      ifx true(_setting_userid) do
      {
        es_exists _setting_exists key _setting_instance server_var(_setting_name)
        ifx true(_setting_exists) do
        {
          es_getuserid _setting_userid server_var(_setting_userid)
          ifx true(_setting_userid) do
          {
            es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
            es_xformatv _setting_keygroup "_settingresultglob_%1_%2" _setting_name _setting_userid
            es keygroupremove server_var(_setting_keygroup)
            if (server_var(_setting_type) == "list") do
            {
              es_xformatqv _setting_query "SELECT _setting_list.key, _setting_list.display, _setting_list.descr, _setting_glob.value AS data_value FROM _setting_list LEFT OUTER JOIN _setting_glob ON (_setting_list.setting=_setting_data.setting) WHERE _setting_list.setting='%1' ORDER BY _setting_list.sort ASC" _setting_name
              es_sql query _setting_database server_var(_setting_keygroup) server_var(_setting_query)
              es_foreachkey _setting_key in server_var(_setting_keygroup) "es_xdoblock setting/setting_createstring_list"
              es_xset _setting_helpline "You can choose one of these options"
            }
            else do
            {
              es_xformatqv _setting_query "SELECT _setting_list.key, _setting_list.display, _setting_list.descr, _setting_glob.key AS data_key, _setting_glob.value AS data_value FROM _setting_list LEFT OUTER JOIN _setting_glob ON (_setting_list.setting=_setting_data.setting AND _setting_list.key=_setting_data.key) WHERE _setting_list.setting='%1' ORDER BY _setting_list.sort ASC" _setting_name
              es_sql query _setting_database server_var(_setting_keygroup) server_var(_setting_query)
              es_foreachkey _setting_key in server_var(_setting_keygroup) "es_xdoblock setting/setting_createstring_toggle"
              es_xset _setting_helpline "You can toggle each option on or off"
            }
            es_keygetvalue _setting_descr _setting_instance server_var(_setting_name) descr
            es_keygetvalue _setting_backmenu _setting_instance server_var(_setting_name) backmenu
            es_xformatv _setting_menu "_settingmenuglob_%1_%2" _setting_name _setting_userid
            es_xformatv _setting_descr "%1\n%2" _setting_descr _setting_helpline
            es keymenu exists _setting_exists server_var(_setting_menu)
            ifx true(_setting_exists) do
            {
              es keymenu delete server_var(_setting_menu)
            }
            es_xgetargv _setting_locked 4
            ifx false(_setting_locked)
            {
              esnq keymenu create server_var(_setting_menu) _setting_key setting/setting_submitglobal server_var(_setting_keygroup) #keyvalue string #key server_var(_setting_descr)
            }
            else do
            {
              esnq keymenu create server_var(_setting_menu) _setting_key setting/setting_dummy server_var(_setting_keygroup) #keyvalue string #key server_var(_setting_descr)
            }
            es keymenu setvar server_var(_setting_menu) _setting_name server_var(_setting_name)
            ifx true(_setting_backmenu) do
            {
              es keymenu backmenu server_var(_setting_menu) server_var(_setting_backmenu)
            }
            es keymenu send server_var(_setting_menu) server_var(_setting_userid)
          }
          else do
          {
            es_xformatv _setting_format "setting: The userid '%1' does not exists." _setting_userid
            es_xdbgmsgv 0 _setting_format
          }
        }
        else do
        {
          es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
          es_xdbgmsgv 0 _setting_format
        }
      }
      else do
      {
        es_xdbgmsg 0 setting: Invalid arguments for setting sendglobal. Syntax: setting sendglobal <settingname> <userid>
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting sendglobal. Syntax: setting sendglobal <settingname> <userid>
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: keymenu lib has to be loaded!
  }
}

block setting_submit
{
  es keymenu getmenuname _setting_menu server_var(_popup_name)
  es keymenu getvar server_var(_setting_menu) _setting_name _setting_name
  es_exists _setting_exists key _setting_instance server_var(_setting_name)
  ifx true(_setting_exists) do
  {
    es_xcopy _setting_userid _popup_userid
    es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
    es_keygetvalue _setting_sound _setting_instance server_var(_setting_name) sound
    es_keygetvalue _setting_resend _setting_instance server_var(_setting_name) resend
    ifx true(_setting_sound) do
    {
      es_playsound server_var(_setting_userid) server_var(_setting_sound) 1.0
    }
    es_xformatv _setting_keygroup "_settingresult_%1_%2" _setting_name _setting_userid
    if (server_var(_setting_type) == "list") do
    {
      es_xgettimestamp _setting_timestamp
      es uniqueid _setting_uniqueid server_var(_setting_userid) 1
      es_keygetvalue _setting_temp server_var(_setting_keygroup) server_var(_setting_key) key
      es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,uniqueid,time,key,value) VALUES('%1','%2','%3','%1','%4')" _setting_name _setting_uniqueid _setting_timestamp _setting_temp
      es_sql query _setting_database server_var(_setting_query)
      ifx true(_setting_resend) do
      {
        es_xformatv _setting_keygroup "_settingresult_%1_%2" _setting_name _setting_userid
        es keygroupremove server_var(_setting_keygroup)
        es_xformatqv _setting_query "SELECT _setting_list.key, _setting_list.display, _setting_list.descr, _setting_data.value AS data_value FROM _setting_list LEFT OUTER JOIN _setting_data ON (_setting_list.setting=_setting_data.setting AND _setting_data.uniqueid='%2') WHERE _setting_list.setting='%1' ORDER BY _setting_list.sort ASC" _setting_name _setting_uniqueid
        es_sql query _setting_database server_var(_setting_keygroup) server_var(_setting_query)
        es_foreachkey _setting_key in server_var(_setting_keygroup) "es_xdoblock setting/setting_createstring_list"
        es_xset _setting_helpline "You can choose one of these options"
      }
    }
    else do
    {
      es_xgettimestamp _setting_timestamp
      es uniqueid _setting_uniqueid server_var(_setting_userid) 1
      es_keygetvalue _setting_temp server_var(_setting_keygroup) server_var(_setting_key) key
      es_keygetvalue _setting_string server_var(_setting_keygroup) server_var(_setting_key) data_value
      ifx false(_setting_string) do
      {
        es_xformatv _setting_string "def_%1" _setting_option
        es_keygetvalue _setting_string _setting_instance server_var(_setting_name) server_var(_setting_string)
      }
      if (server_var(_setting_string) == "on") do
      {
        es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,uniqueid,time,key,value) VALUES('%1','%2','%3','%4','off')" _setting_name _setting_uniqueid _setting_timestamp _setting_temp
      }
      else do
      {
        es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,uniqueid,time,key,value) VALUES('%1','%2','%3','%4','on')" _setting_name _setting_uniqueid _setting_timestamp _setting_temp
      }
      es_sql query _setting_database server_var(_setting_query)
      ifx true(_setting_resend) do
      {
        es_keygroupdelete server_var(_setting_keygroup)
        es_xformatqv _setting_query "SELECT _setting_list.key, _setting_list.display, _setting_list.descr, _setting_data.key AS data_key, _setting_data.value AS data_value FROM _setting_list LEFT OUTER JOIN _setting_data ON (_setting_list.setting=_setting_data.setting AND _setting_list.key=_setting_data.key AND _setting_data.uniqueid='%2') WHERE _setting_list.setting='%1' ORDER BY _setting_list.sort ASC" _setting_name _setting_uniqueid
        es_sql query _setting_database server_var(_setting_keygroup) server_var(_setting_query)
        es_foreachkey _setting_key in server_var(_setting_keygroup) "es_xdoblock setting/setting_createstring_toggle"
        es_xset _setting_helpline "You can toggle each option on or off"
      }
    }
    ifx true(_setting_resend) do
    {
      es_keygetvalue _setting_descr _setting_instance server_var(_setting_name) descr
      es_keygetvalue _setting_backmenu _setting_instance server_var(_setting_name) backmenu
      es_xformatv _setting_descr "%1\n%2" _setting_descr _setting_helpline
      es keymenu update server_var(_setting_menu)
      es keymenu send server_var(_setting_menu) server_var(_setting_userid)
    }
    es_xset _setting_dosave 1
  }
  else do
  {
    es_xdbgmsg 0 setting: menu: Submitted setting does not exists!
  }
}

block setting_submitglobal
{
  es keymenu getmenuname _setting_menu server_var(_popup_name)
  es keymenu getvar server_var(_setting_menu) _setting_name _setting_name
  es_exists _setting_exists key _setting_instance server_var(_setting_name)
  ifx true(_setting_exists) do
  {
    es_xcopy _setting_userid _popup_userid
    es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
    es_keygetvalue _setting_sound _setting_instance server_var(_setting_name) sound
    es_keygetvalue _setting_resend _setting_instance server_var(_setting_name) resend
    ifx true(_setting_sound) do
    {
      es_playsound server_var(_setting_userid) server_var(_setting_sound) 1.0
    }
    es_xformatv _setting_keygroup "_settingresultglob_%1_%2" _setting_name _setting_userid
    if (server_var(_setting_type) == "list") do
    {
      es_keygetvalue _setting_temp server_var(_setting_keygroup) server_var(_setting_key) key
      es_xformatqv _setting_query "REPLACE INTO _setting_glob (setting,key,value) VALUES('%1','%1','%2')" _setting_name _setting_temp
      es_sql query _setting_database server_var(_setting_query)
      ifx true(_setting_resend) do
      {
        es_xformatv _setting_keygroup "_settingresultglob_%1_%2" _setting_name _setting_userid
        es keygroupremove server_var(_setting_keygroup)
        es_xformatqv _setting_query "SELECT _setting_list.key, _setting_list.display, _setting_list.descr, _setting_glob.value AS data_value FROM _setting_list LEFT OUTER JOIN _setting_glob ON (_setting_list.setting=_setting_data.setting) WHERE _setting_list.setting='%1' ORDER BY _setting_list.sort ASC" _setting_name
        es_sql query _setting_database server_var(_setting_keygroup) server_var(_setting_query)
        es_foreachkey _setting_key in server_var(_setting_keygroup) "es_xdoblock setting/setting_createstring_list"
        es_xset _setting_helpline "You can choose one of these options"
      }
    }
    else do
    {
      es_keygetvalue _setting_temp server_var(_setting_keygroup) server_var(_setting_key) key
      es_keygetvalue _setting_string server_var(_setting_keygroup) server_var(_setting_key) data_value
      ifx false(_setting_string) do
      {
        es_xformatv _setting_string "def_%1" _setting_option
        es_keygetvalue _setting_string _setting_instance server_var(_setting_name) server_var(_setting_string)
      }
      if (server_var(_setting_string) == "on") do
      {
        es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,key,value) VALUES('%1','%2','off')" _setting_name _setting_temp
      }
      else do
      {
        es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,key,value) VALUES('%1','%2','on')" _setting_name _setting_temp
      }
      es_sql query _setting_database server_var(_setting_query)
      ifx true(_setting_resend) do
      {
        es_keygroupdelete server_var(_setting_keygroup)
        es_xformatqv _setting_query "SELECT _setting_list.key, _setting_list.display, _setting_list.descr, _setting_glob.key AS data_key, _setting_glob.value AS data_value FROM _setting_list LEFT OUTER JOIN _setting_glob ON (_setting_list.setting=_setting_data.setting AND _setting_list.key=_setting_data.key) WHERE _setting_list.setting='%1' ORDER BY _setting_list.sort ASC" _setting_name
        es_sql query _setting_database server_var(_setting_keygroup) server_var(_setting_query)
        es_foreachkey _setting_key in server_var(_setting_keygroup) "es_xdoblock setting/setting_createstring_toggle"
        es_xset _setting_helpline "You can toggle each option on or off"
      }
    }
    ifx true(_setting_resend) do
    {
      es_keygetvalue _setting_descr _setting_instance server_var(_setting_name) descr
      es_keygetvalue _setting_backmenu _setting_instance server_var(_setting_name) backmenu
      es_xformatv _setting_descr "%1\n%2" _setting_descr _setting_helpline
      es keymenu update server_var(_setting_menu)
      es keymenu send server_var(_setting_menu) server_var(_setting_userid)
    }
    es_xset _setting_dosave 1
  }
  else do
  {
    es_xdbgmsg 0 setting: menu: Submitted setting does not exists!
  }
}

block setting_createstring_list
{
  es_keygetvalue _setting_option server_var(_setting_keygroup) server_var(_setting_key) key
  es_keygetvalue _setting_value server_var(_setting_keygroup) server_var(_setting_key) display
  es_keygetvalue _setting_descr server_var(_setting_keygroup) server_var(_setting_key) descr
  es_keygetvalue _setting_temp server_var(_setting_keygroup) server_var(_setting_key) data_value
  ifx false(_setting_temp) do
  {
    es_keygetvalue _setting_temp _setting_instance server_var(_setting_name) def
  }
  if (server_var(_setting_option) == server_var(_setting_temp)) do
  {
    ifx true(_setting_descr) do
    {
      es_xformatv _setting_value "%1 (active) - %2" _setting_value _setting_descr
    }
    else do
    {
      es_xformatv _setting_value "%1 (active)" _setting_value
    }
    es_keysetvalue server_var(_setting_keygroup) server_var(_setting_key) string server_var(_setting_value)
  }
  else do
  {
    ifx true(_setting_descr) do
    {
      es_xformatv _setting_value "%1 - %2" _setting_value _setting_descr
    }
    es_keysetvalue server_var(_setting_keygroup) server_var(_setting_key) string server_var(_setting_value)
  }
}

block setting_createstring_toggle
{
  es_keygetvalue _setting_option server_var(_setting_keygroup) server_var(_setting_key) key
  es_keygetvalue _setting_value server_var(_setting_keygroup) server_var(_setting_key) display
  es_keygetvalue _setting_descr server_var(_setting_keygroup) server_var(_setting_key) descr
  es_keygetvalue _setting_temp server_var(_setting_keygroup) server_var(_setting_key) data_value
  ifx false(_setting_temp) do
  {
    es_xformatv _setting_string "def_%1" _setting_option
    es_keygetvalue _setting_temp _setting_instance server_var(_setting_name) server_var(_setting_string)
  }
  ifx true(_setting_descr) do
  {
    es_xformatv _setting_value "%1 (%2) - %3" _setting_value _setting_temp _setting_descr
  }
  else do
  {
    es_xformatv _setting_value "%1 (%2)" _setting_value _setting_temp
  }
  es_keysetvalue server_var(_setting_keygroup) server_var(_setting_key) string server_var(_setting_value)
}

block setting_cmd_getvar
{
  es_xgetargv _setting_value 2
  ifx true(_setting_value) do
  {
    es_xgetargv _setting_name 3
    ifx true(_setting_name) do
    {
      es_xgetargv _setting_variable 4
      ifx true(_setting_variable) do
      {
        es_exists _setting_exists key _setting_instance server_var(_setting_name)
        ifx true(_setting_exists) do
        {
          es_xgetargv _setting_temp 5
          ifx true(_setting_temp) do
          {
            es uniqueid _setting_uniqueid server_var(_setting_temp) 1
            es_xformatqv _setting_query "SELECT value FROM _setting_vars WHERE setting='%1' AND variable='%2' AND uniqueid='%3'" _setting_name _setting_variable _setting_uniqueid
            es_sql queryvalue _setting_database server_var(_setting_value) server_var(_setting_query)
          }
          else do
          {
            es_xformatqv _setting_query "SELECT value FROM _setting_vars WHERE setting='%1' AND variable='%2' AND uniqueid='glob'" _setting_name _setting_variable
            es_sql queryvalue _setting_database server_var(_setting_value) server_var(_setting_query)
          }
        }
        else do
        {
          es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
          es_xdbgmsgv 0 _setting_format
        }
      }
      else do
      {
        es_xdbgmsg 0 setting: Invalid arguments for setting getvar. Syntax: setting getvar <return-var> <settingname> <variable-name> [userid]
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting getvar. Syntax: setting getvar <return-var> <settingname> <variable-name> [userid]
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting getvar. Syntax: setting getvar <return-var> <settingname> <variable-name> [userid]
  }
}

block setting_cmd_setvar
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_xgetargv _setting_variable 3
    ifx true(_setting_variable) do
    {
      es_xgetargv _setting_value 4
      es_exists _setting_exists key _setting_instance server_var(_setting_name)
      ifx true(_setting_exists) do
      {
        es_xgetargv _setting_temp 5
        ifx true(_setting_temp) do
        {
          es_xgettimestamp _setting_timestamp
          es uniqueid _setting_uniqueid server_var(_setting_temp) 1
          es_xformatqv _setting_query "REPLACE INTO _setting_vars (setting,uniqueid,time,variable,value) VALUES('%1','%2','%3','%4','%5')" _setting_name _setting_uniqueid _setting_timestamp _setting_variable _setting_value
          es_sql query _setting_database server_var(_setting_query)
        }
        else do
        {
          es_xformatqv _setting_query "REPLACE INTO _setting_vars (setting,uniqueid,time,variable,value) VALUES('%1','glob',0,'%2','%3')" _setting_name _setting_variable _setting_value
          es_sql query _setting_database server_var(_setting_query)
        }
        es_xset _setting_dosave 1
      }
      else do
      {
        es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
        es_xdbgmsgv 0 _setting_format
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting setvar. Syntax: setting setvar <settingname> <variable-name> <value> [userid]
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting setvar. Syntax: setting setvar <settingname> <variable-name> <value> [userid]
  }
}

block setting_cmd_resend
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_exists _setting_exists key _setting_instance server_var(_setting_name)
    ifx true(_setting_exists) do
    {
      es_xgetargv _setting_resend 3
      ifx true(_setting_resend) do
      {
        es_keysetvalue _setting_instance server_var(_setting_name) resend 1
      }
      else do
      {
        es_keysetvalue _setting_instance server_var(_setting_name) resend 0
      }
      es_xkeygroupsave _setting_instance |setting
    }
    else do
    {
      es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
      es_xdbgmsgv 0 _setting_format
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting resend. Syntax: setting resend <settingname> <1/0>
  }
}

block setting_cmd_backmenu
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_exists _setting_exists key _setting_instance server_var(_setting_name)
    ifx true(_setting_exists) do
    {
      es_xgetargv _setting_backmenu 3
      ifx true(_setting_backmenu) do
      {
        es popup exists server_var(_setting_backmenu) _setting_exists
        ifx true(_setting_exists) do
        {
          es_keysetvalue _setting_instance server_var(_setting_name) backmenu server_var(_setting_backmenu)
        }
        else do
        {
          es keymenu exists _setting_exists server_var(_setting_backmenu)
          ifx true(_setting_exists) do
          {
            es_keysetvalue _setting_instance server_var(_setting_name) backmenu server_var(_setting_backmenu)
          }
          else do
          {
            es_xformatv _setting_format "setting: The specified popup or keymenu '%1' does not exist." _setting_backmenu
            es_xdbgmsgv 0 _setting_format
          }
        }
      }
      else do
      {
        es_keysetvalue _setting_instance server_var(_setting_name) backmenu 0
      }
      es_xkeygroupsave _setting_instance |setting
    }
    else do
    {
      es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
      es_xdbgmsgv 0 _setting_format
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting backmenu. Syntax: setting backmenu <settingname> <keymenu/popup>
  }
}

block setting_cmd_setdefault
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_xgetargv _setting_key 3
    ifx true(_setting_key) do
    {
      es_exists _setting_exists key _setting_instance server_var(_setting_name)
      ifx true(_setting_exists) do
      {
        es_xformatqv _setting_query "SELECT COUNT(key) FROM _setting_list WHERE setting='%1' AND key='%2'" _setting_name _setting_key
        es_sql queryvalue _setting_database _setting_exists server_var(_setting_query)
        ifx true(_setting_exists) do
        {
          es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
          if (server_var(_setting_type) == "list") do
          {
            es_keysetvalue _setting_instance server_var(_setting_name) def server_var(_setting_key)
            es_xgetargv _setting_overwrite 4
            ifx true(_setting_overwrite) do
            {
              es_xformatqv _setting_query "UPDATE _setting_data SET value='%1', key='%2' WHERE setting='%2';UPDATE _setting_glob SET value='%1', key='%2' WHERE setting='%2'" _setting_key _setting_name
              es_sql query _setting_database server_var(_setting_query)
              es_xsql close _setting_database
              es_xsql open _setting_database |setting
            }
          }
          else do
          {
            es_xformatv _setting_temp "def_%1" _setting_key
            es_xgetargv _setting_value 4
            ifx true(_setting_value) do
            {
              es_keysetvalue _setting_instance server_var(_setting_name) server_var(_setting_temp) on
            }
            else do
            {
              es_keysetvalue _setting_instance server_var(_setting_name) server_var(_setting_temp) off
            }
            es_xgetargv _setting_overwrite 5
            ifx true(_setting_overwrite) do
            {
              ifx true(_setting_value) do
              {
                es_xformatqv _setting_query "UPDATE _setting_data SET value='on' WHERE setting='%1' AND key='%2';UPDATE _setting_glob SET value='on' WHERE setting='%1' AND key='%2'" _setting_key _setting_name
                es_sql query _setting_database server_var(_setting_query)
              }
              else do
              {
                es_xformatqv _setting_query "UPDATE _setting_data SET value='off' WHERE setting='%1' AND key='%2';UPDATE _setting_glob SET value='off' WHERE setting='%1' AND key='%2'" _setting_key _setting_name
                es_sql query _setting_database server_var(_setting_query)
              }
              es_xsql close _setting_database
              es_xsql open _setting_database |setting
            }
          }
          es_xkeygroupsave _setting_instance |setting
        }
        else do
        {
          es_xformatv _setting_format "setting: The option '%1' does not exists in setting '%2'." _setting_key _setting_name
          es_xdbgmsgv 0 _setting_format
        }
      }
      else do
      {
        es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
        es_xdbgmsgv 0 _setting_format
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting setdefault. Syntax: setting setdefault <settingname> <option-key> [1/0] [overwrite]
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting setdefault. Syntax: setting setdefault <settingname> <option-key> [1/0] [overwrite]
  }
}

block setting_cmd_get
{
  es_xgetargv _setting_value 2
  ifx true(_setting_value) do
  {
    es_xgetargv _setting_name 3
    ifx true(_setting_name) do
    {
      es_exists _setting_exists key _setting_instance server_var(_setting_name)
      ifx true(_setting_exists) do
      {
        es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
        if (server_var(_setting_type) == "list") do
        {
          es_xgetargv _setting_temp 4
          ifx true(_setting_temp) do
          {
            es uniqueid _setting_uniqueid server_var(_setting_temp) 1
            es_xformatqv _setting_query "SELECT value FROM _setting_data WHERE setting='%1' AND key='%1' AND uniqueid='%2'" _setting_name _setting_uniqueid
            es_sql queryvalue _setting_database _setting_return server_var(_setting_query)
          }
          else do
          {
            es_xformatqv _setting_query "SELECT value FROM _setting_glob WHERE setting='%1' AND key='%1'" _setting_name
            es_sql queryvalue _setting_database _setting_return server_var(_setting_query)
          }
          ifx false(_setting_return) do
          {
            es_keygetvalue server_var(_setting_value) _setting_instance server_var(_setting_name) def
          }
          else do
          {
            es_set server_var(_setting_value) server_var(_setting_return)
          }
        }
        else do
        {
          es_xgetargv _setting_option 4
          ifx true(_setting_option) do
          {
            es_xgetargv _setting_temp 5
            ifx true(_setting_temp) do
            {
              es uniqueid _setting_uniqueid server_var(_setting_temp) 1
              es_xformatqv _setting_query "SELECT value FROM _setting_data WHERE setting='%1' AND key='%2' AND uniqueid='%3'" _setting_name _setting_option _setting_uniqueid
              es_sql queryvalue _setting_database _setting_return server_var(_setting_query)
            }
            else do
            {
              es_xformatqv _setting_query "SELECT value FROM _setting_glob WHERE setting='%1' AND key='%2'" _setting_name _setting_option
              es_sql queryvalue _setting_database _setting_return server_var(_setting_query)
            }
            ifx false(_setting_return) do
            {
              es_xformatv _setting_string "def_%1" _setting_option
              es_keygetvalue _setting_return _setting_instance server_var(_setting_name) server_var(_setting_string)
            }
            if (server_var(_setting_return) == "on") do
            {
              es_set server_var(_setting_value) 1
            }
            else do
            {
              es_set server_var(_setting_value) 0
            }
          }
          else do
          {
            es_xdbgmsg 0 setting: Invalid arguments for setting get. No [option-key] specified.
          }
        }
      }
      else do
      {
        es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
        es_xdbgmsgv 0 _setting_format
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting get. Syntax: setting get <return-var> <settingname> [option-key] [userid]
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting get. Syntax: setting get <return-var> <settingname> [option-key] [userid]
  }
}

block setting_cmd_set
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_xgetargv _setting_key 3
    ifx true(_setting_key) do
    {
      es_exists _setting_exists key _setting_instance server_var(_setting_name)
      ifx true(_setting_exists) do
      {
        es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
        if (server_var(_setting_type) == "list") do
        {
          es_xgetargv _setting_temp 4
          ifx true(_setting_temp) do
          {
            es_xgettimestamp _setting_timestamp
            es uniqueid _setting_uniqueid server_var(_setting_temp) 1
            es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,uniqueid,time,key,value) VALUES('%1','%2','%3','%1','%4')" _setting_name _setting_uniqueid _setting_timestamp _setting_key
            es_sql query _setting_database server_var(_setting_query)
          }
          else do
          {
            es_xformatqv _setting_query "REPLACE INTO _setting_glob (setting,key,value) VALUES('%1','%1','%2')" _setting_name _setting_key
            es_sql query _setting_database server_var(_setting_query)
          }
          es_xset _setting_dosave 1
        }
        else do
        {
          es_xgetargv _setting_string 4
          es_xgetargv _setting_temp 5
          ifx true(_setting_temp) do
          {
            es_xgettimestamp _setting_timestamp
            es uniqueid _setting_uniqueid server_var(_setting_temp) 1
            ifx true(_setting_string) do
            {
              es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,uniqueid,time,key,value) VALUES('%1','%2','%3','%4','on')" _setting_name _setting_uniqueid _setting_timestamp _setting_key
            }
            else do
            {
              es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,uniqueid,time,key,value) VALUES('%1','%2','%3','%4','off')" _setting_name _setting_uniqueid _setting_timestamp _setting_key
            }
            es_sql query _setting_database server_var(_setting_query)
          }
          else do
          {
            ifx true(_setting_string) do
            {
              es_xformatqv _setting_query "REPLACE INTO _setting_glob (setting,key,value) VALUES('%1','%2','on')" _setting_name _setting_key
            }
            else do
            {
              es_xformatqv _setting_query "REPLACE INTO _setting_glob (setting,key,value) VALUES('%1','%2','off')" _setting_name _setting_key
            }
            es_sql query _setting_database server_var(_setting_query)
          }
          es_xset _setting_dosave 1
        }
      }
      else do
      {
        es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
        es_xdbgmsgv 0 _setting_format
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting set. Syntax: setting set <settingname> <option-key> [1/0] [userid]
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting set. Syntax: setting set <settingname> <option-key> [1/0] [userid]
  }
}

block setting_cmd_toggle
{
  es_xgetargv _setting_name 2
  ifx true(_setting_name) do
  {
    es_xgetargv _setting_key 3
    ifx true(_setting_key) do
    {
      es_exists _setting_exists key _setting_instance server_var(_setting_name)
      ifx true(_setting_exists) do
      {
        es_keygetvalue _setting_type _setting_instance server_var(_setting_name) type
        if (server_var(_setting_type) == "toggle") do
        {
          es_xgetargv _setting_temp 4
          ifx true(_setting_temp) do
          {
            es_xgettimestamp _setting_timestamp
            es uniqueid _setting_uniqueid server_var(_setting_temp) 1
            es_xformatqv _setting_query "SELECT value FROM _setting_data WHERE setting='%1' AND uniqueid='%2' AND key='%3'" _setting_name _setting_uniqueid _setting_key
            es_sql queryvalue _setting_database _setting_string server_var(_setting_query)
            ifx false(_setting_string) do
            {
              es_xformatv _setting_string "def_%1" _setting_key
              es_keygetvalue _setting_string _setting_instance server_var(_setting_name) server_var(_setting_string)
            }
            if (server_var(_setting_string) == "on") do
            {
              es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,uniqueid,time,key,value) VALUES('%1','%2','%3','%4','off')" _setting_name _setting_uniqueid _setting_timestamp _setting_key
            }
            else do
            {
              es_xformatqv _setting_query "REPLACE INTO _setting_data (setting,uniqueid,time,key,value) VALUES('%1','%2','%3','%4','on')" _setting_name _setting_uniqueid _setting_timestamp _setting_key
            }
            es_sql query _setting_database server_var(_setting_query)
            es_xset _setting_dosave 1
          }
          else do
          {
            es_xformatqv _setting_query "SELECT value FROM _setting_glob WHERE setting='%1' AND key='%3'" _setting_name _setting_key
            es_sql queryvalue _setting_database _setting_string server_var(_setting_query)
            ifx false(_setting_string) do
            {
              es_xformatv _setting_string "def_%1" _setting_key
              es_keygetvalue _setting_string _setting_instance server_var(_setting_name) server_var(_setting_string)
            }
            if (server_var(_setting_string) == "on") do
            {
              es_xformatqv _setting_query "REPLACE INTO _setting_glob (setting,key,value) VALUES('%1','%2','off')" _setting_name _setting_key
            }
            else do
            {
              es_xformatqv _setting_query "REPLACE INTO _setting_glob (setting,key,value) VALUES('%1','%2','on')" _setting_name _setting_key
            }
            es_sql query _setting_database server_var(_setting_query)
            es_xset _setting_dosave 1
          }
        }
        else do
        {
          es_xformatv _setting_format "setting: The setting '%1' does not have the toggle type." _setting_name
          es_xdbgmsgv 0 _setting_format
        }
      }
      else do
      {
        es_xformatv _setting_format "setting: The setting '%1' does not exists." _setting_name
        es_xdbgmsgv 0 _setting_format
      }
    }
    else do
    {
      es_xdbgmsg 0 setting: Invalid arguments for setting toggle. Syntax: setting toggle <settingname> <option-key> [userid]
    }
  }
  else do
  {
    es_xdbgmsg 0 setting: Invalid arguments for setting toggle. Syntax: setting toggle <settingname> <option-key> [userid]
  }
}

block setting_cmd_list
{
  es_xdbgmsg 0 Registered setting names:
  es_xdbgmsg 0 -------------------------
  es_xset _setting_temp 0
  es_xforeachkey _setting_key in _setting_instance "es_xdoblock setting/setting_listloop"
  es_xdbgmsg 0 -------------------------
}

block setting_listloop
{
  es_xmath _setting_temp + 1
  es_xformatv _setting_string "%1: %2" _setting_temp _setting_key
  es_xdbgmsgv 0 _setting_string
  keygroupremove _setting_result
  es_xformatqv _setting_query "SELECT key FROM _setting_list WHERE setting='%1' ORDER BY sort ASC" _setting_key
  es_sql query _setting_database _setting_result server_var(_setting_query)
  es_xforeachkey _setting_option in _setting_result "es_xdoblock setting/setting_listloop_sub"
}

block setting_listloop_sub
{
  es_keygetvalue _setting_option _setting_result server_var(_setting_option) key
  es_xformatv _setting_string "    - %1" _setting_option
  es_xdbgmsgv 0 _setting_string
}

event es_map_start
{
  es_xset _setting_lock 0
  es_xdelayed 60 es_xdoblock setting/savealldata 
  // first time save after 1 minute
}

event player_activate
{
  es uniqueid _setting_uniqueid event_var(userid) 1
  es_xdoblock setting/updateplayer
  ifx false(_setting_lock) do
  {
    ifx true(_setting_dosave) do
    {
      es_xset _setting_dosave 0
      es_xset _setting_lock 1
      es_xdelayed 60 es_xset _setting_lock 0
      es_xsql close _setting_database
      es_xsql open _setting_database |setting
    }
  }
}

event player_team
{
  if (event_var(disconnect) != 1) do
  {
    es uniqueid _setting_uniqueid event_var(userid) 1
    es_xdoblock setting/updateplayer
    ifx false(_setting_lock) do
    {
      ifx true(_setting_dosave) do
      {
        es_xset _setting_dosave 0
        es_xset _setting_lock 1
        es_xdelayed 60 es_xset _setting_lock 0
        es_xsql close _setting_database
        es_xsql open _setting_database |setting
      }
    }
  }
}

block savealldata
{
  ifx true(_setting_dosave) do
  {
    es_xset _setting_dosave 0
    es_xkeygroupsave _setting_instance |setting
    es_xsql close _setting_database
    es_xsql open _setting_database |setting
    es_xdelayed 300 es_xdoblock setting/savealldata 
  }
  // save every 5 minutes
}

block updateplayer
{
  es_xgettimestamp _setting_timestamp
  es_xformatqv _setting_query "UPDATE _setting_data SET time='%1' WHERE uniqueid='%2'" _setting_timestamp _setting_uniqueid
  es_sql query _setting_database server_var(_setting_query)
}

block updateplayer_loadup
{
  es uniqueid _setting_uniqueid server_var(_setting_temp) 1
  es_xdoblock setting/updateplayer
}

block setting_dummy
{
  // dummy block
}

// Test stuff
block setting_setget_test
{
  es_xset myvar 0

  testlib begin setvar1 "Testing whether or not setvar/getvar works for a list setting 1"
  es_xset myvar 0
  setting setvar listtest testvar 1
  setting getvar myvar listtest testvar
  testlib fail_unless myvar equalto 1
  testlib end

  testlib begin setvar2 "Testing whether or not setvar/getvar works for a list setting 2"
  es_xset myvar 0
  setting setvar listtest testvar blah
  setting getvar myvar listtest testvar
  testlib fail_unless myvar equalto blah
  testlib end

  testlib begin setvar3 "Testing whether or not setvar/getvar works for a list setting 3"
  es_xset myvar 0
  setting setvar listtest testvar 0
  setting getvar myvar listtest testvar
  testlib fail_unless myvar equalto 0
  testlib end

  testlib begin setvar4 "Testing whether or not setvar/getvar works for a list setting 4"
  es_xset myvar 0
  setting setvar listtest testvar -1234
  setting getvar myvar listtest testvar
  testlib fail_unless myvar equalto -1234
  testlib end

  testlib begin set1 "Testing whether or not set/get works for a list setting 1"
  es_xset myvar 0
  setting set listtest option_a
  setting get myvar listtest
  testlib fail_unless myvar equalto option_a
  testlib end

  testlib begin set2 "Testing whether or not set/get works for a list setting 2"
  es_xset myvar 0
  setting set listtest option_c
  setting get myvar listtest
  testlib fail_unless myvar equalto option_c
  testlib end

  testlib begin set3 "Testing whether or not set/get works for a list setting 3"
  es_xset myvar 0
  setting set listtest option_e
  setting get myvar listtest
  testlib fail_unless myvar equalto option_e
  testlib end

  testlib begin set4 "Testing whether or not set/get works for a toggle setting 4"
  es_xset myvar 0
  setting set toggletest toggle_a 1
  setting get myvar toggletest toggle_a
  testlib fail_unless myvar equalto 1
  testlib end

  testlib begin set5 "Testing whether or not set/get works for a toggle setting 5"
  es_xset myvar 0
  setting set toggletest toggle_a 0
  setting get myvar toggletest toggle_a
  testlib fail_unless myvar equalto 0
  testlib end

  testlib begin set6 "Testing whether or not set/get works for a toggle setting 6"
  es_xset myvar 0
  setting set toggletest toggle_c 1
  setting get myvar toggletest toggle_c
  testlib fail_unless myvar equalto 1
  testlib end

  testlib begin set7 "Testing whether or not set/get works for a toggle setting 7"
  es_xset myvar 0
  setting set toggletest toggle_c 0
  setting get myvar toggletest toggle_c
  testlib fail_unless myvar equalto 0
  testlib end
}
