// vote allows you to start a user vote
// syntax:
// vote start <votename> <time> <callback-blockname> "<question>" "<answer1>" "<answer2> "[answer3]" ... "[answer9]"
// vote stop <votename>
// vote submit <votename> <userid> <answer#>
// vote menu <votename> <userid>
// vote getstatus <variable> <userid>
// vote listener
// Originally submitted by [NATO]Hunter

block load
{
  es_xdoblock corelib/vote/vote_register
}

// called to register the vote command
block vote_register
{
  // variables used by vote
  es_xsetinfo vote_answertext1 0
  es_xsetinfo vote_answertext2 0
  es_xsetinfo vote_answertext3 0
  es_xsetinfo vote_answertext4 0
  es_xsetinfo vote_answertext5 0
  es_xsetinfo vote_answertext6 0
  es_xsetinfo vote_answertext7 0
  es_xsetinfo vote_answertext8 0
  es_xsetinfo vote_answertext9 0
  es_xsetinfo vote_answer1 0
  es_xsetinfo vote_answer2 0
  es_xsetinfo vote_answer3 0
  es_xsetinfo vote_answer4 0
  es_xsetinfo vote_answer5 0
  es_xsetinfo vote_answer6 0
  es_xsetinfo vote_answer7 0
  es_xsetinfo vote_answer8 0
  es_xsetinfo vote_answer9 0
  es_xsetinfo vote_question 0
  es_xsetinfo vote_answertext 0
  es_xsetinfo vote_answer 0
  es_xsetinfo _vote_name 0
  es_xsetinfo _vote_exists 0
  es_xsetinfo _vote_time 0
  es_xsetinfo _vote_block 0
  es_xsetinfo _vote_question 0
  es_xsetinfo _vote_answer 0
  es_xsetinfo _vote_answer1 0
  es_xsetinfo _vote_answer2 0
  es_xsetinfo _vote_answer3 0
  es_xsetinfo _vote_answer4 0
  es_xsetinfo _vote_answer5 0
  es_xsetinfo _vote_answer6 0
  es_xsetinfo _vote_answer7 0
  es_xsetinfo _vote_answer8 0
  es_xsetinfo _vote_answer9 0
  es_xsetinfo _vote_userid 0
  es_xsetinfo _vote_menu 0
  es_xsetinfo _vote_answertext 0
  es_xsetinfo _vote_temp 0
  // check to see if vote exists
  es_xexists _exists command vote
  if (server_var(_exists) = 0) do
  {
    // vote keygroup
    es_xkeygroupcreate _vote_corelib
    es_xkeycreate _vote_corelib _vote_users
    es_xregcmd vote corelib/vote/vote "Allows you to create a user vote."
  }
}

block vote
{
  // get the operation they want to do
  es_xgetargv _tempcore 1
  es es_xformat _tempcore "corelib/vote/vote_cmd_%1" server_var(_tempcore)
  es es_xexists _exists block server_var(_tempcore)
  es es_xif (server_var(_exists) = 1) do
  {
    es es_xdoblock server_var(_tempcore)
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid subcommand for vote.
  }
}

// inspect es_client_command
block vote_cmd_listener
{
  es es_xif (event_var(command) = menuselect) do
  {
    es_xsetinfo _tempcore 0
    es vote getstatus _tempcore event_var(userid)
    es es_xif (server_var(_tempcore) != 0) do
    {
      es vote submit server_var(_tempcore) event_var(userid) event_var(commandstring)
    }
  }
}

// start the vote
block vote_cmd_start
{
  es_xgetargv _vote_name 2
  es es_xif (server_var(_vote_name) != 0) do
  {
    es_xgetargv _vote_time 3
    es es_xif (server_var(_vote_time) > 0) do
    {
      es_xgetargv _vote_block 4
      es es_xif (server_var(_vote_block) != 0) do
      {
        es_xgetargv _vote_question 5
        es es_xif (server_var(_vote_question) != 0) do
        {
          es_xgetargv _vote_answer1 6
          es es_xif (server_var(_vote_answer1) != 0) do
          {
            es_xgetargv _vote_answer2 7
            es es_xif (server_var(_vote_answer2) != 0) do
            {
              es_xgetargv _vote_answer3 8
              es_xgetargv _vote_answer4 9
              es_xgetargv _vote_answer5 10
              es_xgetargv _vote_answer6 11
              es_xgetargv _vote_answer7 12
              es_xgetargv _vote_answer8 13
              es_xgetargv _vote_answer9 14
              es_xsetinfo _vote_userid 0
              es_xsetinfo _vote_menu 0
              es es_xformat _vote_menu "%1\n->1 %2\n->2 %3" server_var(_vote_question) server_var(_vote_answer1) server_var(_vote_answer2)
              es es_xif (server_var(_vote_answer3) != 0) do
              {
                es es_xformat _vote_menu "%1\n->3 %2" server_var(_vote_menu) server_var(_vote_answer3)
              }
              es es_xif (server_var(_vote_answer4) != 0) do
              {
                es es_xformat _vote_menu "%1\n->4 %2" server_var(_vote_menu) server_var(_vote_answer4)
              }
              es es_xif (server_var(_vote_answer5) != 0) do
              {
                es es_xformat _vote_menu "%1\n->5 %2" server_var(_vote_menu) server_var(_vote_answer5)
              }
              es es_xif (server_var(_vote_answer6) != 0) do
              {
                es es_xformat _vote_menu "%1\n->6 %2" server_var(_vote_menu) server_var(_vote_answer6)
              }
              es es_xif (server_var(_vote_answer7) != 0) do
              {
                es es_xformat _vote_menu "%1\n->7 %2" server_var(_vote_menu) server_var(_vote_answer7)
              }
              es es_xif (server_var(_vote_answer8) != 0) do
              {
                es es_xformat _vote_menu "%1\n->8 %2" server_var(_vote_menu) server_var(_vote_answer8)
              }
              es es_xif (server_var(_vote_answer9) != 0) do
              {
                es es_xformat _vote_menu "%1\n->9 %2" server_var(_vote_menu) server_var(_vote_answer9)
              }
              es es_xformat _vote_menu "%1\n->0 Exit" server_var(_vote_menu)
              es es_xkeycreate _vote_corelib server_var(_vote_name)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) block server_var(_vote_block)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) menu server_var(_vote_menu)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) question server_var(_vote_question)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) answertext_1 server_var(_vote_answer1)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) answertext_2 server_var(_vote_answer2)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) answertext_3 server_var(_vote_answer3)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) answertext_4 server_var(_vote_answer4)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) answertext_5 server_var(_vote_answer5)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) answertext_6 server_var(_vote_answer6)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) answertext_7 server_var(_vote_answer7)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) answertext_8 server_var(_vote_answer8)
              es es_xkeysetvalue _vote_corelib server_var(_vote_name) answertext_9 server_var(_vote_answer9)
              foreach player _vote_userid #human "es es_xmenu server_var(_vote_time) server_var(_vote_userid) server_var(_vote_menu);es es_xkeysetvalue _vote_corelib _vote_users server_var(_vote_userid) server_var(_vote_name)"
              es es_xdelayed server_var(_vote_time) vote stop server_var(_vote_name)
            }
            else do
            {
              es_xdbgmsg 0 vote: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
            }
          }
          else do
          {
            es_xdbgmsg 0 vote: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
          }
        }
        else do
        {
          es_xdbgmsg 0 vote: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
      }
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
  }
}

// stop a running vote if the time is over
block vote_cmd_stop
{
  es_xgetargv _vote_name 2
  es es_xif (server_var(_vote_name) != 0) do
  {
    es es_xexists _tempcore key _vote_corelib server_var(_vote_name)
    es es_xif (server_var(_tempcore) = 1) do
    {
      es es_xkeygetvalue _vote_block _vote_corelib server_var(_vote_name) block
      es es_xkeygetvalue vote_question _vote_corelib server_var(_vote_name) question
      es es_xkeygetvalue vote_answertext1 _vote_corelib server_var(_vote_name) answertext_1
      es es_xkeygetvalue vote_answertext2 _vote_corelib server_var(_vote_name) answertext_2
      es es_xkeygetvalue vote_answertext3 _vote_corelib server_var(_vote_name) answertext_3
      es es_xkeygetvalue vote_answertext4 _vote_corelib server_var(_vote_name) answertext_4
      es es_xkeygetvalue vote_answertext5 _vote_corelib server_var(_vote_name) answertext_5
      es es_xkeygetvalue vote_answertext6 _vote_corelib server_var(_vote_name) answertext_6
      es es_xkeygetvalue vote_answertext7 _vote_corelib server_var(_vote_name) answertext_7
      es es_xkeygetvalue vote_answertext8 _vote_corelib server_var(_vote_name) answertext_8
      es es_xkeygetvalue vote_answertext9 _vote_corelib server_var(_vote_name) answertext_9
      es es_xkeygetvalue vote_answer1 _vote_corelib server_var(_vote_name) answer_1
      es es_xkeygetvalue vote_answer2 _vote_corelib server_var(_vote_name) answer_2
      es es_xkeygetvalue vote_answer3 _vote_corelib server_var(_vote_name) answer_3
      es es_xkeygetvalue vote_answer4 _vote_corelib server_var(_vote_name) answer_4
      es es_xkeygetvalue vote_answer5 _vote_corelib server_var(_vote_name) answer_5
      es es_xkeygetvalue vote_answer6 _vote_corelib server_var(_vote_name) answer_6
      es es_xkeygetvalue vote_answer7 _vote_corelib server_var(_vote_name) answer_7
      es es_xkeygetvalue vote_answer8 _vote_corelib server_var(_vote_name) answer_8
      es es_xkeygetvalue vote_answer9 _vote_corelib server_var(_vote_name) answer_9
      es es_xif (server_var(vote_answer1) > server_var(_vote_temp)) do
      {
        es_xcopy _vote_temp vote_answer1
        es_xsetinfo vote_answer 1
      }
      es es_xif (server_var(vote_answer2) > server_var(_vote_temp)) do
      {
        es_xcopy _vote_temp vote_answer2
        es_xsetinfo vote_answer 2
      }
      es es_xif (server_var(vote_answer3) > server_var(_vote_temp)) do
      {
        es_xcopy _vote_temp vote_answer3
        es_xsetinfo vote_answer 3
      }
      es es_xif (server_var(vote_answer4) > server_var(_vote_temp)) do
      {
        es_xcopy _vote_temp vote_answer4
        es_xsetinfo vote_answer 4
      }
      es es_xif (server_var(vote_answer5) > server_var(_vote_temp)) do
      {
        es_xcopy _vote_temp vote_answer5
        es_xsetinfo vote_answer 5
      }
      es es_xif (server_var(vote_answer6) > server_var(_vote_temp)) do
      {
        es_xcopy _vote_temp vote_answer6
        es_xsetinfo vote_answer 6
      }
      es es_xif (server_var(vote_answer7) > server_var(_vote_temp)) do
      {
        es_xcopy _vote_temp vote_answer7
        es_xsetinfo vote_answer 7
      }
      es es_xif (server_var(vote_answer8) > server_var(_vote_temp)) do
      {
        es_xcopy _vote_temp vote_answer8
        es_xsetinfo vote_answer 8
      }
      es es_xif (server_var(vote_answer9) > server_var(_vote_temp)) do
      {
        es_xcopy _vote_temp vote_answer9
        es_xsetinfo vote_answer 9
      }
      es es_xformat _vote_answertext "vote_answertext%1" server_var(vote_answer)
      es es_xcopy vote_answertext server_var(_vote_answertext)
      es es_xdoblock server_var(_vote_block)
      es es_xkeydelete _vote_corelib server_var(_vote_name)
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments to vote stop. Syntax: vote stop <name>
  }
}

// vote
block vote_cmd_add
{
  es_xgetargv _vote_name 2
  es es_xif (server_var(_vote_name) != 0) do
  {
    es es_xexists _vote_exists key _vote_corelib server_var(_vote_name)
    es es_xif (server_var(_vote_exists) = 1) do
    {
      es_xgetargv _vote_userid 3
      es es_xif (server_var(_vote_userid) > 0) do
      {
        es_xgetargv _vote_answer 4
        es es_xif (server_var(_vote_answer) > 0) do
        {
          es es_xif (server_var(_vote_answer) < 10) do
          {
            es_xsetinfo _vote_count 0
            es_xsetinfo _vote_userok 0
            es_xsetinfo _vote_answertext 0
            es es_xformat _vote_userid "userid_%1" server_var(_vote_userid)
            es es_xformat _vote_answertext "answertext_%1" server_var(_vote_answer)
            es es_xformat _vote_answer "answer_%1" server_var(_vote_answer)
            es es_xkeygetvalue _vote_userok _vote_corelib server_var(_vote_name) server_var(_vote_userid)
            es es_xif (server_var(_vote_userok) != 1) do
            {
              es es_xkeygetvalue _vote_answertext _vote_corelib server_var(_vote_name) server_var(_vote_answertext)
              es es_xif (server_var(_vote_answertext) != 0) do
              {
                es es_xkeysetvalue _vote_corelib server_var(_vote_name) server_var(_vote_userid) 1
                es es_xkeygetvalue _vote_count _vote_corelib server_var(_vote_name) server_var(_vote_answer)
                es es_xif (server_var(_vote_count) <= 0) then es_xsetinfo _vote_count 0
                es_xmath _vote_count + 1
                es es_xkeysetvalue _vote_corelib server_var(_vote_name) server_var(_vote_answer) server_var(_vote_count)
              }
              else do
              {
                es_xdbgmsg 0 vote: This answer doesn't exist
              }
            }
            else do
            {
              es_xgetargv _vote_userid 3
              es es_xdbgmsg 0 vote: The user server_var(_vote_userid) voted already
            }
          }
        }
      }
      else do
      {
        es_xdbgmsg 0 vote: Invalid arguments to vote add. Syntax: vote add <name> <userid> <answer#>
      }
    }
    else do
    {
      es es_xdbgmsg 0 vote: The vote server_var(_vote_name) isn't existing
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments to vote add. Syntax: vote add <name> <userid> <answer#>
  }
}

// re-enter the menu
block vote_cmd_menu
{
  es_xgetargv _vote_name 2
  es es_xif (server_var(_vote_name) != 0) do
  {
    es es_xexists _tempcore key _vote_corelib server_var(_vote_name)
    es es_xif (server_var(_tempcore) = 1) do
    {
      es_xgetargv _vote_userid 3
      es es_xgetuserid _vote_userid server_var(_vote_userid)
      es es_xif (server_var(_vote_userid) > 0) do
      {
        es es_xkeygetvalue _vote_menu _vote_corelib server_var(_vote_name) menu
        es es_xkeysetvalue _vote_corelib _vote_users server_var(_vote_userid) server_var(_vote_name)
        es es_xmenu 20 server_var(_vote_userid) server_var(_vote_menu)
      }
      else do
      {
        es_xdbgmsg 0 vote: Invalid arguments to vote menu. Syntax: vote menu <name> <userid>
      }
    }
    else do
    {
      es es_xdbgmsg 0 vote: The vote server_var(_vote_name) isn't existing
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments to vote menu. Syntax: vote menu <name> <userid>
  }
}

// handle menuselect
block vote_cmd_getstatus
{
  es_xgetargv _vote_name 2
  es es_xif (server_var(_vote_name) != 0) do
  {
    es_xgetargv _vote_userid 3
    es es_xif (server_var(_vote_userid) > 0) do
    {
      es es_xsetinfo server_var(_vote_name) 0
      es es_xkeygetvalue server_var(_vote_name) _vote_corelib _vote_users server_var(_vote_userid)
    }
    else do
    {
      es_xdbgmsg 0 vote: Invalid arguments to vote getstatus. Syntax: vote getstatus <variable> <userid>
    }
  }
  else do
  {
    es_xdbgmsg 0 vote: Invalid arguments to vote getstatus. Syntax: vote getstatus <variable> <userid>
  }
}

// -----------------------------------------------------------------------------
block vote_test
{
  log on
  vote start testvote 10 "corelib/vote_testb" "Is this vote working?" "Yes" "No" "Don't know" "Not important"
  vote submit testvote 25 1
  vote submit testvote 26 1
  vote submit testvote 27 1
  vote submit testvote 28 2
  vote submit testvote 29 4
  vote submit testvote 30 4
  es_xgetuserid _tempcore
  es vote getstatus testvotename server_var(_tempcore)
  es es_xif (server_var(testvotename) != "testvote") then es_xmsg FAILURE: vote test1 failed.
}

block vote_testb
{
  es_msg vote_question = server_var(vote_question)
  es_msg vote_answertext1 = server_var(vote_answertext1)
  es_msg vote_answertext2 = server_var(vote_answertext2)
  es_msg vote_answertext3 = server_var(vote_answertext3)
  es_msg vote_answertext4 = server_var(vote_answertext4)
  es_msg vote_answertext5 = server_var(vote_answertext5)
  es_msg vote_answertext6 = server_var(vote_answertext6)
  es_msg vote_answertext7 = server_var(vote_answertext7)
  es_msg vote_answertext8 = server_var(vote_answertext8)
  es_msg vote_answertext9 = server_var(vote_answertext9)
  es_msg vote_answer1 = server_var(vote_answer1)
  es_msg vote_answer2 = server_var(vote_answer2)
  es_msg vote_answer3 = server_var(vote_answer3)
  es_msg vote_answer4 = server_var(vote_answer4)
  es_msg vote_answer5 = server_var(vote_answer5)
  es_msg vote_answer6 = server_var(vote_answer6)
  es_msg vote_answer7 = server_var(vote_answer7)
  es_msg vote_answer8 = server_var(vote_answer8)
  es_msg vote_answer9 = server_var(vote_answer9)
  es_msg vote_answertext = server_var(vote_answertext)
  es_msg vote_answer = server_var(vote_answer)
  es es_xif (server_var(vote_answer) != 1) then es_xmsg FAILURE: vote test1 failed.
}

