// vote command v0.4.6
// Submitted by [NATO]Hunter
//
// vote allows you to start a user vote
// syntax:
// vote create <votename> <callback-blockname> [vote-callback-blockname]
// vote delete <votename>
// vote exists <return-var> <votename>
// vote isrunning <return-var> <votename>
// vote setquestion <votename> "<question>"
// vote setanswer <votename> <#> "<answer>"
// vote addanswer <votename> "<answer>"
// vote remanswer <votename> <#>
// vote start <votename> <time>
// vote stop <votename> [cancel]
// vote send <votename> <userid> [force]
// vote submit <votename> <userid> <#> [force]
// vote getvotes <return-var> <votename> [answer]
// vote gettimeleft <return-var> <votename>
// vote list
//
// Originally submitted by [NATO]Hunter

block load
{
  es_xdoblock vote/vote_register
}

// called to register the vote command
block vote_register
{
  // variables used by vote
  es_xset _vote_cmd 0
  es_xset _vote_name 0
  es_xset _vote_exists 0
  es_xset _vote_time 0
  es_xset _vote_block 0
  es_xset _vote_question 0
  es_xset _vote_answer 0
  es_xset _vote_userid 0
  es_xset _vote_select 0
  es_xset _vote_temp 0
  es_xset _vote_count 0
  es_xset _vote_text 0
  es_xset _vote_id 0
  es_xset _vote_votes 0
  es_xset _vote_tmp 0
  es_xset _vote_keygroup 0
  es_xset _vote_silent 0
  // check to see if vote exists
  es_xexists _vote_exists command vote
  ifx false(_vote_exists) do
  {
    // vote keygroup
    es_xkeygroupcreate _vote_instance
    es_xkeygroupcreate _vote_users
    es_xregcmd vote vote/vote "Allows you to create a user vote."
  }
  
  testcase qcreate corelib votetest "Tests vote"
  testcase addtest votetest votetest1 vote/vote_test "Tests vote"
}

block vote
{
  // get the operation they want to do
  es_xexists _vote_temp script keymenu
  ifx true(_vote_temp) do
  {
    es_xgetargv _vote_cmd 1
    es_format _vote_cmd "vote/vote_cmd_%1" server_var(_vote_cmd)
    es_exists _vote_exists block server_var(_vote_cmd)
    ifx true(_vote_exists) do
    {
      es_doblock server_var(_vote_cmd)
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid subcommand for vote.
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: keymenu lib has to be loaded!
  }
}

block vote_cmd_create
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_xgetargv _vote_block 3
    ifx true(_vote_block) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx false(_vote_exists) do
      {
        es_keycreate _vote_instance server_var(_vote_name)
        es_keysetvalue _vote_instance server_var(_vote_name) running 0
        es_keysetvalue _vote_instance server_var(_vote_name) votes 0
        es_keysetvalue _vote_instance server_var(_vote_name) time 0
        es_keysetvalue _vote_instance server_var(_vote_name) block server_var(_vote_block)
        es_xgetargv _vote_block 4
        ifx true(_vote_block) do
        {
          es_keysetvalue _vote_instance server_var(_vote_name) voteblock server_var(_vote_block)
        }
        es_keycreate _vote_users server_var(_vote_name)
        es_xformatv _vote_temp "_vote__%1" _vote_name
        es_keygroupcreate server_var(_vote_temp)
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote does already exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote create. Syntax: vote create <votename> <callback-blockname> [vote-callback-blockname]
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote create. Syntax: vote create <votename> <callback-blockname> [vote-callback-blockname]
  }
}

block vote_cmd_delete
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_exists _vote_exists key _vote_instance server_var(_vote_name)
    ifx true(_vote_exists) do
    {
      es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
      ifx false(_vote_exists) do
      {
        es_keydelete _vote_instance server_var(_vote_name)
        es_keydelete _vote_users server_var(_vote_name)
        es_xformatv _vote_temp "_vote__%1" _vote_name
        es_keygroupdelete server_var(_vote_temp)
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote is still running. Use vote stop to cancel/end the vote!
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: This vote does not exists.
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote delete. Syntax: vote delete <votename>
  }
}

block vote_cmd_exists
{
  es_xgetargv _vote_temp 2
  ifx true(_vote_temp) do
  {
    es_xgetargv _vote_name 3
    es_xstring _vote_name replace "_" "-"
    ifx true(_vote_name) do
    {
      es_exists server_var(_vote_temp) key _vote_instance server_var(_vote_name)
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote exists. Syntax: vote exists <return-var> <votename>
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote exists. Syntax: vote exists <return-var> <votename>
  }
}

block vote_cmd_isrunning
{
  es_xgetargv _vote_temp 2
  ifx true(_vote_temp) do
  {
    es_xgetargv _vote_name 3
    es_xstring _vote_name replace "_" "-"
    ifx true(_vote_name) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx true(_vote_exists) do
      {
        es_keygetvalue server_var(_vote_temp) _vote_instance server_var(_vote_name) running
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote isrunning. Syntax: vote isrunning <return-var> <votename>
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote isrunning. Syntax: vote isrunning <return-var> <votename>
  }
}

block vote_cmd_setquestion
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_xgetargv _vote_question 3
    ifx true(_vote_question) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx true(_vote_exists) do
      {
        es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
        ifx false(_vote_exists) do
        {
          es_keysetvalue _vote_instance server_var(_vote_name) question server_var(_vote_question)
        }
        else do
        {
          es_xdbgmsg 0 vote: This vote is already running.
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote setquestion. Syntax: vote setquestion <votename> "<question>"
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote setquestion. Syntax: vote setquestion <votename> "<question>"
  }
}

block vote_cmd_setanswer
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_xgetargv _vote_temp 3
    ifx true(_vote_temp) do
    {
      es_xgetargv _vote_answer 4
      ifx true(_vote_answer) do
      {
        es_exists _vote_exists key _vote_instance server_var(_vote_name)
        ifx true(_vote_exists) do
        {
          es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
          ifx false(_vote_exists) do
          {
            es_xformatv _vote_keygroup "_vote__%1" _vote_name
            es_exists _vote_exists keygroup server_var(_vote_keygroup)
            ifx true(_vote_exists) do
            {
              es_xformatv _vote_temp "%1_%2" _vote_name _vote_temp
              es_exists _vote_exists key server_var(_vote_keygroup) server_var(_vote_temp)
              ifx false(_vote_exists) do
              {
                es_keycreate server_var(_vote_keygroup) server_var(_vote_temp)
              }
              es_keysetvalue server_var(_vote_keygroup) server_var(_vote_temp) text server_var(_vote_answer)
              es_keysetvalue server_var(_vote_keygroup) server_var(_vote_temp) count 0
            }
            else do
            {
              es_xdbgmsg 0 vote: This vote (keygroup) does not exists.
            }
          }
          else do
          {
            es_xdbgmsg 0 vote: This vote is already running.
          }
        }
        else do
        {
          es_xdbgmsg 0 vote: This vote (key) does not exists.
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: Invalid arguments for vote setanswer. Syntax: vote setanswer <votename> <#> "<answer>"
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote setanswer. Syntax: vote setanswer <votename> <#> "<answer>"
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote setanswer. Syntax: vote setanswer <votename> <#> "<answer>"
  }
}

block vote_cmd_addanswer
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_xgetargv _vote_answer 3
    ifx true(_vote_answer) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx true(_vote_exists) do
      {
        es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
        ifx false(_vote_exists) do
        {
          es_xformatv _vote_keygroup "_vote__%1" _vote_name
          es_exists _vote_exists keygroup server_var(_vote_keygroup)
          ifx true(_vote_exists) do
          {
            es_xset _vote_exists 0
            es_foreachkey _vote_temp in server_var(_vote_keygroup) "es_xdoblock vote/addanswer_loop"
            es_xmath _vote_exists + 1
            es_xformatv _vote_temp "%1_%2" _vote_name _vote_exists
            es_exists _vote_exists key server_var(_vote_keygroup) server_var(_vote_temp)
            ifx false(_vote_exists) do
            {
              es_keycreate server_var(_vote_keygroup) server_var(_vote_temp)
            }
            es_keysetvalue server_var(_vote_keygroup) server_var(_vote_temp) text server_var(_vote_answer)
            es_keysetvalue server_var(_vote_keygroup) server_var(_vote_temp) count 0
          }
          else do
          {
            es_xdbgmsg 0 vote: This vote (keygroup) does not exists.
          }
        }
        else do
        {
          es_xdbgmsg 0 vote: This vote is already running.
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote addanswer. Syntax: vote addanswer <votename> "<answer>"
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote addanswer. Syntax: vote addanswer <votename> "<answer>"
  }
}

block addanswer_loop
{
  es_xformatv _vote_select "%1_" _vote_name
  es_xstring _vote_temp replacev _vote_select
  if (server_var(_vote_temp) > server_var(_vote_exists)) do
  {
    es_xcopy _vote_exists _vote_temp
  }
}

block vote_cmd_remanswer
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_xgetargv _vote_temp 3
    ifx true(_vote_temp) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx true(_vote_exists) do
      {
        es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
        ifx false(_vote_exists) do
        {
          es_xformatv _vote_keygroup "_vote__%1" _vote_name
          es_exists _vote_exists keygroup server_var(_vote_keygroup)
          ifx true(_vote_exists) do
          {
            es_xformatv _vote_temp "%1_%2" _vote_name _vote_temp
            es_exists _vote_exists key server_var(_vote_keygroup) server_var(_vote_temp)
            ifx true(_vote_exists) do
            {
              es_keydelete server_var(_vote_keygroup) server_var(_vote_temp)
            }
            else do
            {
              es_xdbgmsg 1 vote: This answer does not exists.
            }
          }
          else do
          {
            es_xdbgmsg 0 vote: This vote (keygroup) does not exists.
          }
        }
        else do
        {
          es_xdbgmsg 0 vote: This vote is already running.
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote remanswer. Syntax: vote remanswer <votename> <#>
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote remanswer. Syntax: vote remanswer <votename> <#>
  }
}

block vote_cmd_start
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_xgetargv _vote_time 3
    ifx true(_vote_time) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx true(_vote_exists) do
      {
        es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
        ifx false(_vote_exists) do
        {
          es_xformatv _vote_keygroup "_vote__%1" _vote_name
          es_exists _vote_exists keygroup server_var(_vote_keygroup)
          ifx true(_vote_exists) do
          {
            es_keysetvalue _vote_instance server_var(_vote_name) running 1
            es_keysetvalue _vote_instance server_var(_vote_name) votes 0
            es_keysetvalue _vote_instance server_var(_vote_name) time server_var(_vote_time)
            es_keygetvalue _vote_question _vote_instance server_var(_vote_name) question
            ifx true(_vote_question) do
            {
              esnq keymenu create server_var(_vote_name) _vote_select vote/vote_submit server_var(_vote_keygroup) #keyvalue text #key server_var(_vote_question)
              foreach player _vote_userid #human "es vote send server_var(_vote_name) server_var(_vote_userid) force"
              es_delayed 1 vote __wait server_var(_vote_name)
            }
            else do
            {
              es_xdbgmsg 0 vote: This vote does not have a question. Use vote setquestion to set a question!
            }
          }
          else do
          {
            es_xdbgmsg 0 vote: This vote (keygroup) does not exists.
          }
        }
        else do
        {
          es_xdbgmsg 0 vote: This vote is already running.
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote start. Syntax: vote start <votename> <time>
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote start. Syntax: vote start <votename> <time>
  }
}
block vote_cmd___wait
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_exists _vote_exists key _vote_instance server_var(_vote_name)
    ifx true(_vote_exists) do
    {
      es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
      ifx true(_vote_exists) do
      {
        es_xformatv _vote_keygroup "_vote__%1" _vote_name
        es_exists _vote_exists keygroup server_var(_vote_keygroup)
        ifx true(_vote_exists) do
        {
          es_keygetvalue _vote_time _vote_instance server_var(_vote_name) time
          ifx true(_vote_time) do
          {
            es_xmath _vote_time - 1
            es_keysetvalue _vote_instance server_var(_vote_name) time server_var(_vote_time)
            es_delayed 1 vote __wait server_var(_vote_name)
          }
          else do
          {
            es vote stop server_var(_vote_name) 0 1
          }
        }
      }
    }
  }
}

block vote_cmd_stop
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_xgetargv _vote_silent 4
    es_exists _vote_exists key _vote_instance server_var(_vote_name)
    ifx true(_vote_exists) do
    {
      es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
      ifx true(_vote_exists) do
      {
        es_xformatv _vote_keygroup "_vote__%1" _vote_name
        es_exists _vote_exists keygroup server_var(_vote_keygroup)
        ifx true(_vote_exists) do
        {
          es_xset _vote_id 0
          es_xset _vote_text 0
          es_xset _vote_count 0
          es_xset _vote_votes 0
          es_xset _vote_percent 0
          es_xset _vote_tie 0
          es_xset _vote_canceled 0
          es_xset _vote_userid 0
          es_xgetargv _vote_temp 3
          es_keygetvalue _vote_block _vote_instance server_var(_vote_name) block
          ifx false(_vote_temp) do
          {
            es_foreachkey _vote_temp in server_var(_vote_keygroup) "es_xdoblock vote/calc_loop"
            es_keygetvalue _vote_votes _vote_instance server_var(_vote_name) votes
            es_xmathparse _vote_percent "(_vote_count / _vote_votes)*100"
            es_doblock server_var(_vote_block)
          }
          else do
          {
            es_xset _vote_id 0
            es_xset _vote_text 0
            es_xset _vote_count 0
            es_xset _vote_votes 0
            es_xset _vote_percent 0
            es_xset _vote_tie 0
            es_xset _vote_canceled 1
            es_doblock server_var(_vote_block)
          }
          es keymenu exists _vote_exists server_var(_vote_name)
          ifx true(_vote_exists) do
          {
            es keymenu delete server_var(_vote_name)
          }
          es_keysetvalue _vote_instance server_var(_vote_name) running 0
          es_keysetvalue _vote_instance server_var(_vote_name) votes 0
          es_keydelete _vote_users server_var(_vote_name)
          es_keycreate _vote_users server_var(_vote_name)
          es_foreachkey _vote_temp in server_var(_vote_keygroup) "es_xdoblock vote/stop_loop"
        }
        else do
        {
          ifx false(_vote_silent) do
          {
            es_xdbgmsg 0 vote: This vote (keygroup) does not exists.
          }
        }
      }
      else do
      {
        ifx false(_vote_silent) do
        {
          es_xdbgmsg 0 vote: This vote is not running.
        }
      }
    }
    else do
    {
      ifx false(_vote_silent) do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote stop. Syntax: vote stop <votename> [cancel]
  }
}

block calc_loop
{
  es_keygetvalue _vote_tmp server_var(_vote_keygroup) server_var(_vote_temp) count
  if (server_var(_vote_tmp) > server_var(_vote_count)) do
  {
    es_keygetvalue _vote_text server_var(_vote_keygroup) server_var(_vote_temp) text
    es_xformatv _vote_select "%1_" _vote_name
    es_xcopy _vote_id _vote_temp
    es_xstring _vote_id replacev _vote_select
    es_xcopy _vote_count _vote_tmp
  }
  else do
  {
    ifx true(_vote_tmp) do
    {
      if (server_var(_vote_tmp) == server_var(_vote_count)) do
      {
        es_xset _vote_tie 1
      }
    }
  }
}

block stop_loop
{
  es_keysetvalue server_var(_vote_keygroup) server_var(_vote_temp) count 0
}

block vote_cmd_send
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_xgetargv _vote_userid 3
    ifx true(_vote_userid) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx true(_vote_exists) do
      {
        es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
        ifx true(_vote_exists) do
        {
          es_xgetargv _vote_temp 4
          ifx false(_vote_temp) do
          {
            es_keygetvalue _vote_exists _vote_users server_var(_vote_name) server_var(_vote_userid)
          }
          else do
          {
            es_xset _vote_exists 0
          }
          ifx false(_vote_exists) do
          {
            es keymenu exists _vote_exists server_var(_vote_name)
            ifx true(_vote_exists) do
            {
              es keymenu send server_var(_vote_name) server_var(_vote_userid)
            }
            else do
            {
              es_xdbgmsg 0 vote: The keymenu does not exists.
            }
          }
          else do
          {
            es_xdbgmsg 1 vote: This user has already voted.
          }
        }
        else do
        {
          es_xdbgmsg 0 vote: This vote is not running.
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote send. Syntax: vote send <votename> <userid> [force]
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote send. Syntax: vote send <votename> <userid> [force]
  }
}

block vote_submit
{
  es_token _vote_name server_var(_vote_select) 1 "_"
  es_keysetvalue _vote_users server_var(_vote_name) server_var(_popup_userid) server_var(_vote_select)
  es_xformatv _vote_keygroup "_vote__%1" _vote_name
  es_xset _vote_count 0
  es_keygetvalue _vote_count server_var(_vote_keygroup) server_var(_vote_select) count
  es_xmath _vote_count + 1
  es_keysetvalue server_var(_vote_keygroup) server_var(_vote_select) count server_var(_vote_count)
  es_xset _vote_count 0
  es_keygetvalue _vote_count _vote_instance server_var(_vote_name) votes
  es_xmath _vote_count + 1
  es_keysetvalue _vote_instance server_var(_vote_name) votes server_var(_vote_count)
  es_keygetvalue _vote_block _vote_instance server_var(_vote_name) voteblock
  ifx true(_vote_block) do
  {
    es_xcopy _vote_userid _popup_userid
    es_xcopy _vote_id _vote_select
    es_xformatv _vote_temp "%1_" _vote_name
    es_xstring _vote_id replacev _vote_temp
    es_keygetvalue _vote_text server_var(_vote_keygroup) server_var(_vote_select) text
    es_doblock server_var(_vote_block)
  }
  es_xset _vote_exists 0
  foreach player _vote_userid #human "es_xmath _vote_exists + 1"
  if (server_var(_vote_count) >= server_var(_vote_exists)) do
  {
    es_soon vote stop server_var(_vote_name) 0 1
  }
}

block vote_cmd_submit
{
  es_xgetargv _vote_name 2
  es_xstring _vote_name replace "_" "-"
  ifx true(_vote_name) do
  {
    es_xgetargv _vote_userid 3
    ifx true(_vote_userid) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx true(_vote_exists) do
      {
        es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
        ifx true(_vote_exists) do
        {
          es_xformatv _vote_keygroup "_vote__%1" _vote_name
          es_exists _vote_exists keygroup server_var(_vote_keygroup)
          ifx true(_vote_exists) do
          {
            es_xgetargv _vote_temp 5
            ifx false(_vote_temp) do
            {
              es_keygetvalue _vote_exists _vote_users server_var(_vote_name) server_var(_vote_userid)
            }
            else do
            {
              es_xset _vote_exists 0
            }
            ifx false(_vote_exists) do
            {
              es_xgetargv _vote_select 4
              es_xformatv _vote_select "%1_%2" _vote_name _vote_select
              es_exists _vote_exists key server_var(_vote_keygroup) server_var(_vote_select)
              ifx true(_vote_exists) do
              {
                es_keysetvalue _vote_users server_var(_vote_name) server_var(_vote_userid) server_var(_vote_select)
                es_xset _vote_temp 0
                es_keygetvalue _vote_temp server_var(_vote_keygroup) server_var(_vote_select) count
                es_xmath _vote_temp + 1
                es_keysetvalue server_var(_vote_keygroup) server_var(_vote_select) count server_var(_vote_temp)
                es_xset _vote_temp 0
                es_keygetvalue _vote_temp _vote_instance server_var(_vote_name) votes
                es_xmath _vote_temp + 1
                es_keysetvalue _vote_instance server_var(_vote_name) votes server_var(_vote_temp)
                es_xset _vote_userid 0
                foreach player _tempcore #human "es_xmath _vote_userid + 1"
                if (server_var(_vote_temp) >= server_var(_vote_userid)) do
                {
                  es_soon vote stop server_var(_vote_name) 0 1
                }
              }
              else do
              {
                es_xdbgmsg 0 vote: This answer does not exists.
              }
            }
            else do
            {
              es_xdbgmsg 1 vote: This user has already voted.
            }
          }
          else do
          {
            es_xdbgmsg 0 vote: This vote (keygroup) does not exists.
          }
        }
        else do
        {
          es_xdbgmsg 0 vote: This vote is not running.
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote submit. Syntax: vote submit <votename> <userid> <#> [force]
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote submit. Syntax: vote submit <votename> <userid> <#> [force]
  }
}

block vote_cmd_getvotes
{
  es_xgetargv _vote_temp 2
  ifx true(_vote_temp) do
  {
    es_set server_var(_vote_temp) 0
    es_xgetargv _vote_name 3
    es_xstring _vote_name replace "_" "-"
    ifx true(_vote_name) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx true(_vote_exists) do
      {
        es_xgetargv _vote_select 4
        ifx true(_vote_select) do
        {
          es_xformatv _vote_keygroup "_vote__%1" _vote_name
          es_exists _vote_exists keygroup server_var(_vote_keygroup)
          ifx true(_vote_exists) do
          {
            es_xformatv _vote_select "%1_%2" _vote_name _vote_select
            es_exists _vote_exists key server_var(_vote_keygroup) server_var(_vote_select)
            ifx true(_vote_exists) do
            {
              es_keygetvalue server_var(_vote_temp) server_var(_vote_keygroup) server_var(_vote_select) count
            }
            else do
            {
              es_xdbgmsg 0 vote: This answer does not exists.
            }
          }
          else do
          {
            es_xdbgmsg 0 vote: This vote (keygroup) does not exists.
          }
        }
        else do
        {
          es_keygetvalue server_var(_vote_temp) _vote_instance server_var(_vote_name) votes
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote getvotes. Syntax: vote getvotes <return-var> <votename> [answer]
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote getvotes. Syntax: vote getvotes <return-var> <votename> [answer]
  }
}

block vote_cmd_gettimeleft
{
  es_xgetargv _vote_temp 2
  ifx true(_vote_temp) do
  {
    es_set server_var(_vote_temp) 0
    es_xgetargv _vote_name 3
    es_xstring _vote_name replace "_" "-"
    ifx true(_vote_name) do
    {
      es_exists _vote_exists key _vote_instance server_var(_vote_name)
      ifx true(_vote_exists) do
      {
        es_keygetvalue server_var(_vote_temp) _vote_instance server_var(_vote_name) time
      }
      else do
      {
        es_xdbgmsg 0 vote: This vote (key) does not exists.
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments for vote gettimeleft. Syntax: vote gettimeleft <return-var> <votename>
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments for vote gettimeleft. Syntax: vote gettimeleft <return-var> <votename>
  }
}

block vote_cmd_list
{
  es_xdbgmsg 0 Registered vote names:
  es_xdbgmsg 0 -------------------------
  es_xset _vote_temp 0
  es_xforeachkey _vote_name in _vote_instance "es_xdoblock vote/vote_listloop"
  es_xdbgmsg 0 -------------------------
}

block vote_listloop
{
  es_xmath _vote_temp + 1
  es_xformatv _vote_string "%1: %2" _vote_temp _vote_name
  es_xdbgmsgv 0 _vote_string
}

event player_activate
{
  es_xforeachkey _vote_name in _vote_instance "es_xdoblock vote/sendplayer"
}

block sendplayer
{
  es_keygetvalue _vote_exists _vote_instance server_var(_vote_name) running
  ifx true(_vote_exists) do
  {
    es keymenu exists _vote_exists server_var(_vote_name)
    ifx true(_vote_exists) do
    {
      es_delayed 5 keymenu send server_var(_vote_name) event_var(userid)
    }
  }
}

event player_disconnect
{
  es_xforeachkey _vote_name in _vote_instance "es_xdoblock vote/clearplayer"
}

block clearplayer
{
  es_exists _vote_exists key _vote_users server_var(_vote_name)
  ifx true(_vote_exists) do
  {
    es_keygetvalue _vote_select _vote_users server_var(_vote_name) event_var(userid)
    ifx true(_vote_select) do
    {
      if (server_var(_vote_name) in server_var(_vote_select)) do
      {
        es_xformatv _vote_keygroup "_vote__%1" _vote_name
        es_exists _vote_exists keygroup server_var(_vote_keygroup)
        ifx true(_vote_exists) do
        {
          es_xset _vote_temp 0
          es_keygetvalue _vote_temp server_var(_vote_keygroup) server_var(_vote_select) count
          es_xmath _vote_temp - 1
          es_keysetvalue server_var(_vote_keygroup) server_var(_vote_select) count server_var(_vote_temp)
        }
        es_exists _vote_exists key _vote_instance server_var(_vote_name)
        ifx true(_vote_exists) do
        {
          es_xset _vote_temp 0
          es_keygetvalue _vote_temp _vote_instance server_var(_vote_name) votes
          es_xmath _vote_temp - 1
          es_keysetvalue _vote_instance server_var(_vote_name) votes server_var(_vote_temp)
        }
      }
    }
  }
}

event es_map_start
{
  es_xforeachkey _vote_name in _vote_instance "es_xdoblock vote/clearvotes"
}

block clearvotes
{
  es_keysetvalue _vote_instance server_var(_vote_name) running 0
  es_keysetvalue _vote_instance server_var(_vote_name) votes 0
  es_keysetvalue _vote_instance server_var(_vote_name) time 0
  es_exists _vote_exists key _vote_users server_var(_vote_name)
  ifx true(_vote_exists) do
  {
    es_keydelete _vote_users server_var(_vote_name)
  }
}

block vote_test
{
  es_xset myvar 0
  
  testlib begin create1 "Testing whether or not create/exists works 1"
  es_xset myvar 0
  vote create testvote "vote/vote_test2"
  vote exists myvar testvote
  testlib fail_unless myvar equalto 1
  testlib end

  testlib begin delete1 "Testing whether or not delete/exists works 1"
  es_xset myvar 0
  vote delete testvote
  vote exists myvar testvote
  testlib fail_unless myvar equalto 0
  testlib end

  testlib begin create2 "Testing whether or not create/exists works 2"
  es_xset myvar 0
  vote create testvote "vote/vote_test2"
  vote setquestion testvote "Does vote work?"
  vote addanswer testvote "Yes"
  vote addanswer testvote "Seems so"
  vote addanswer testvote "No"
  vote exists myvar testvote
  testlib fail_unless myvar equalto 1
  testlib end
  
  testlib begin running1 "Testing whether or not isrunning works 1"
  es_xset myvar 0
  vote isrunning myvar testvote
  testlib fail_unless myvar equalto 0
  testlib end

  testlib begin running2 "Testing whether or not start/isrunning works 2"
  es_xset myvar 0
  vote start testvote 10
  vote isrunning myvar testvote
  testlib fail_unless myvar equalto 1
  testlib end
  
  testlib begin running3 "Testing whether or not stop/isrunning works 3"
  es_xset myvar 0
  vote stop testvote cancel
  vote isrunning myvar testvote
  testlib fail_unless myvar equalto 0
  testlib end

  testlib begin exists1 "Testing whether or not exists works 1"
  es_xset myvar 0
  vote exists myvar testvote
  testlib fail_unless myvar equalto 1
  testlib end

  testlib begin delete2 "Testing whether or not delete/exists works 2"
  es_xset myvar 0
  vote delete testvote
  vote exists myvar testvote
  testlib fail_unless myvar equalto 0
  testlib end
}

block vote_test2
{
  // empty block
}
