TCL Vote

Vote

##########################################
# Vote.tcl 0.1 by KuNgFo0
#
# Set the next line as the question you want to vote for
set vote_question "Which School Is The Best ??"
# Set the next line as all the possible options to vote for
set vote_options "Set your questions here!"
# Set the next line as the file to use to store the vote
set vote_file "vote.data"
# Set the next line as the command you want to initiate the vote counter
set vote_command "!vote"

bind msg - $vote_command msg_vote
if {[file exists $vote_file]} {
 source $vote_file
}

putlog "*** Vote.tcl 0.1 by #egghelp@efnet loaded"

proc msg_vote {nick uhost hand arg} {
 global vote_question vote_options vote_file vote_data
 if {![validuser $hand]} {
  putserv "PRIVMSG $nick :Error: You are not allowed to vote"
 } else {
  set vote [string tolower [lindex $arg 0]]
  if {$vote == ""} {
   foreach i $vote_options {
    set vote_temp($i) 0
   }
   foreach i [array names vote_data] {
    if {[lsearch -exact [string tolower $vote_options] [string tolower $vote_data($i)]] != -1} {
     incr vote_temp($vote_data($i))
    } else {
     unset vote_data($i)
    }
   }
   putserv "PRIVMSG $nick :Vote question: $vote_question"
   putserv "PRIVMSG $nick :Vote status:"
   foreach i $vote_options {
    if {!$vote_temp($i)} {
     putserv "PRIVMSG $nick :$i - \0020\002 (0%)"
    } else {
     putserv "PRIVMSG $nick :$i - \002$vote_temp($i)\002 ([expr round($vote_temp($i).0 / [llength [array names vote_data]].0 * 100)]%)"
    }
   } 
   if {![info exists vote_data($hand)]} {
    putserv "PRIVMSG $nick :You have not voted yet"
   } else {
    putserv "PRIVMSG $nick :Your vote: $vote_data($hand)"
   }
  } elseif {$vote == "unset"} {
   if {![info exists vote_data($hand)]} {
    putserv "PRIVMSG $nick :Error: You have not voted"
   } else {
    putserv "PRIVMSG $nick :Vote removed ($vote_data($hand))"
    unset vote_data($hand)
   }
  } else {
   if {[info exists vote_data($hand)]} {
    putserv "PRIVMSG $nick :Error: You have already voted ($vote_data($hand))"
   } else {
    if {[lsearch -exact [string tolower $vote_options] $vote] != -1} {
     set vote_data($hand) $vote
     putserv "PRIVMSG $nick :Vote added ($vote)"
    } else {
     putserv "PRIVMSG $nick :Error: Invalid vote ($vote)"
    }
   }
  }
  set fileid [open $vote_file w]
  foreach i [array names vote_data] {
   if {[lsearch -exact [string tolower $vote_options] $vote_data($i)] != -1} {
    puts $fileid "set vote_data($i) $vote_data($i)"
   }
  }
  close $fileid
 }
}