;@@@
;@@@ DreamBot IRC Bot 5.4
;@@@ File description: channel vote system
;@@@ Design & coding: DreamBot development team
;@@@ (C) Copyright 2004-2006 DreamBot development team. All rights reserved. 
;@@@

on *:text:*vote*:#: {
  if ($1 == %c $+ vote) && ($nick isop $chan || $level($address($nick,2)) >= 100) {
    if ($2 == $null) { .msg $nick $cl Command incomplete. Syntax: %c $+ vote <question>. | halt }
    if %vote [ $+ [ $chan ] ] !== $null { msg $chan A vote is currently held. | halt }
    set %vote [ $+ [ $chan ] ] $2-
    msg $chan $chan vote! Question: $2-
    msg $chan To vote please use 3 $+ %c $+ Yes or 5 $+ %c $+ No. You have 60 seconds to do so.
    .timer 1 60 _vote $chan
  }
}

on *:text:*yes:#: {
  if $1 == %c $+ yes {
    if %vote [ $+ [ $chan ] ] == $null { halt }
    if $istok($nick,%voted- [ $+ [ $chan ] ],32) { rep $nick has already voted | halt }
    inc %yes [ $+ [ $chan ] ]
    set %voted- [ $+ [ $chan ] ] %voted- [ $+ [ $chan ] ] $nick
  }
}

on *:text:*no:#: {
  if $1 == %c $+ no {
    if %vote [ $+ [ $chan ] ] == $null { halt }
    if $istok($nick,%voted- [ $+ [ $chan ] ],32) { rep $nick has already voted | halt }
    inc %no [ $+ [ $chan ] ]
    set %voted- [ $+ [ $chan ] ] %voted- [ $+ [ $chan ] ] $nick
  }
}

alias _vote {

  ;Syntax: /_vote <#channel>
  ;Action: displays vote results for <#channel>

  var %yes = %yes [ $+ [ $1 ] ]
  var %no = %no [ $+ [ $1 ] ]
  var %total = $calc(%yes + %no)
  var %yes-p = $round($calc(%yes / %total * 100),2)
  var %no-p = $round($calc(%no / %total * 100),2)

  msg $1 Vote is over! Question was: %vote [ $+ [ $1 ] ]
  msg $1 [7Total votes  $+ $iif(%total == $null,0,%total) $+ ] 3Yes  $+ $iif(%yes == $null,0,%yes) $+  ( $+ %yes-p $+ $chr(37) $+ ) 5No  $+ $iif(%no == $null,0,%no) $+  ( $+ %no-p $+ $chr(37) $+ )

  unset %vote [ $+ [ $1 ] ] %voted- [ $+ [ $1 ] ] %no [ $+ [ $1 ] ] %yes [ $+ [ $1 ] ]

}
