// keyrand command v0.1
// Submitted by [NATO]Hunter
//
//  keyrand <var> <keygroup> [key]
//
// Originally submitted by [NATO]Hunter
//   Idea using SQLite for this by Don and XE_ManUp (keygroupsort)

block load
{
  es_xsql open _keyrand_database ":memory:"
  es_xset _keyrand_exists 0
  es_xset _keyrand_temp 0
  es_xset _keyrand_query 0
  es_xset _keyrand_keygroup 0
  es_xset _keyrand_argc 0
  es_xset _keyrand_count 0
  es_xset _keyrand_rand_max 0
  es_xset _keyrand_key 0
  es_xset _keyrand_val 0
  es_xset _keyrand_return 0
  es_xset _keyrand_format 0
  es_xexists _keyrand_exists command keyrand
  ifx false(_keyrand_exists) do
  {
    es_xregcmd keyrand corelib/keyrand/keyrand "Gets a random key or keyvalue (corelib command)"
  }
}

block unload
{
  es_xsql close _keyrand_database
}

block keyrand
{
  es_xgetargc _keyrand_argc
  if (server_var(_keyrand_argc) >= 3) do
  {
    if (server_var(_keyrand_argc) < 5) do
    {
      es_xgetargv _keyrand_return 1
      es_exists _keyrand_exists variable server_var(_keyrand_return)
      ifx false(_keyrand_exists) do
      {
        es_set server_var(_keyrand_return) 0
      }
      es_xgetargv _keyrand_keygroup 2
      es_exists _keyrand_exists keygroup server_var(_keyrand_keygroup)
      ifx true(_keyrand_exists) do
      {
        es_xgetargv _keyrand_key 3
        ifx true(_keyrand_key) do
        {
          es_exists _keyrand_exists key server_var(_keyrand_keygroup) server_var(_keyrand_key)
          ifx true(_keyrand_exists) do
          {
            es_xformatqv _keyrand_query "CREATE TABLE %1 (key varchar(50), keyindex integer);" _keyrand_keygroup
            es_sql query _keyrand_database server_var(_keyrand_query)
            es_xgettimestamp _keyrand_rand_max
            es_foreachval _keyrand_val in server_var(_keyrand_keygroup) server_var(_keyrand_key) "es_xdoblock corelib/keyrand/rand_values"
            es_xformatqv _keyrand_query "SELECT key FROM %1 ORDER BY keyindex ASC LIMIT 1" _keyrand_keygroup
            es_sql queryvalue _keyrand_database server_var(_keyrand_return) server_var(_keyrand_query)
            es_xformatqv _keyrand_query "DELETE FROM %1;DROP TABLE %1;" _keyrand_keygroup
            es_sql query _keyrand_database server_var(_keyrand_query)
          }
          else do
          {
            es_xformatv _keyrand_format "keyrand: The key '%1' does not exists in keygroup '%2'." _keyrand_key _keyrand_keygroup
            es_xdbgmsgv 0 _keyrand_format
          }
        }
        else do
        {
          es_xformatqv _keyrand_query "CREATE TABLE %1 (key varchar(50), keyindex integer);" _keyrand_keygroup
          es_sql query _keyrand_database server_var(_keyrand_query)
          es_xgettimestamp _keyrand_rand_max
          es_foreachkey _keyrand_key in server_var(_keyrand_keygroup) "es_xdoblock corelib/keyrand/rand_keys"
          es_xformatqv _keyrand_query "SELECT key FROM %1 ORDER BY keyindex ASC LIMIT 1" _keyrand_keygroup
          es_sql queryvalue _keyrand_database server_var(_keyrand_return) server_var(_keyrand_query)
          es_xformatqv _keyrand_query "DELETE FROM %1;DROP TABLE %1;" _keyrand_keygroup
          es_sql query _keyrand_database server_var(_keyrand_query)
        }
      }
      else do
      {
        es_xformatv _keyrand_format "keyrand: The keygroup '%1' does not exist." _keyrand_keygroup
        es_xdbgmsgv 0 _keyrand_format
      }
    }
    else do
    {
      es_xdbgmsg 0 keyrand: (Wrong # of arguments) keyrand <var> <keygroup> [key]
    }
  }
  else do
  {
    es_xdbgmsg 0 keyrand: Syntax: keyrand <var> <keygroup> [key]
  }
}

block rand_keys
{
  es_rand _keyrand_count 1 server_var(_keyrand_rand_max)
  es_xformatqv _keyrand_query "INSERT INTO %1 (key,keyindex) VALUES('%2','%3')" _keyrand_keygroup _keyrand_key _keyrand_count
  es_sql query _keyrand_database server_var(_keyrand_query)
}

block rand_values
{
  es_rand _keyrand_count 1 server_var(_keyrand_rand_max)
  es_xformatqv _keyrand_query "INSERT INTO %1 (key,keyindex) VALUES('%2','%3')" _keyrand_keygroup _keyrand_val _keyrand_count
  es_sql query _keyrand_database server_var(_keyrand_query)
}
