# be-acro.tcl 1.0 coded by Errtu (Errtu @ irc.worldirc.org  #tavern)
# weighted letter system coded by Bringa
#
# tnx 2: - Bringa, LadyCK, Wocizon and moeffju for suggesting.
#	 - Landis, Turumbar, Luthien and above for testing & playing
#	 - ooga for not crashing during gameplay :)
#
####################################################################
# INSTALL: unzip be-acro.tgz in your scripts dir
#          move letters.acro to the eggdrop root dir
#          Configure settings below this description.
####################################################################
#
# enable with .chanset #chan +be_acro
#
# !acro <num> 	starts acro, winner is the first that reaches <num>
#		points. Defaults to 30
#
# !acrohelp	get info about the game
#
# !stopacro	allows +o users to stop the game and delete scores
#               for the current game
#
# !pauseacro	stops the game but keeps current scores
#
# !reset	resets scores for the current game
#
# !hiscores	displayes acro alltime top3
#
# !acrohelp	shows a short info about ACRO in privmsg
#
#
# Weighted letter system:
# -----------------------
# !howprob <letter>	returns the probability for one specific 
#			letter to be chosen in an acro
#
# !useless <letter>	decreases the probability for the letter
#			to be chosen in an acro
#
# !usemore <letter>	increases the probability for the letter
#			to be chosen in an acro
#
# Comments/suggestions: ooga@ooga.no-ip.org
#
# THIS IS BUGFREE CODE !!!!!!! IF YOU FIND BUGS IT IS BECAUSE I PUT
# THEM IN ON PURPOSE !! IT'S NOT A BUG! TRUST ME! 
# But um .. be so kind to mail them to me anyways :)
####################################################################
#
# You can define the nicknames (case insensitive) of bots or people
# who will never play acro. Bot checks for a minimum of 3 players and
# skips these nicks in counting (eg: X / W).  autoskips botnick.
# Seperate with spaces, eg.: "X ooga Errtu"

set noacroplayers "X Morfeas_koimatai"

# Use topics when displaying acro? (1=yes, 0=no)

set usetopics 1

##############################################
######## Stop editing after this line ########
##############################################

setudef flag be_acro 

set acrocurrent current.acro
set acropeople people.acro
set acronum counter.acro
set acrovotes votes.acro
set acroscores scores.acro
set acrohiscores hiscores.acro
set acrowinscore winscore.acro
set acrorunning 0
set acrochan dontchangethis
set acrotimerid ""
set acronewround ""

bind pub o|o !useless acro:useless
bind pub o|o !usemore acro:useful
bind pub o|o !howprob acro:howprob
bind pub - !acro acrocheck
bind pub o|o !stopacro makeitstop
bind pub - !hiscores acro:hiscores
bind pub - !acrohelp acro:help
bind pub o|o !reset acro:reset
bind pub - !pauseacro acro:pause

set acrover 1.0

set acrotopics {
  "Use a nickname"
  "Music"
  "Current events"
  "Tv/radio"
  "Try to rhyme!"
  "Make a nice insult :)"
  "Try to make sense this time :P"
  "Invent a new word!"
  "Diss the bot!"
  "Make fun of someone"
  "Use a dutch word! :)"
  "Do an acro without any abusive words --- good luck fuckers!!"
}

proc makeitstop { n u h c alltheargsIdontneed } {
  global acrorunning acrotimerid acrochan acrocurrent acropeople acronum acrovotes acronewround acrowinscore acroscores
  file delete $acroscores
  file delete $acrocurrent
  file delete $acropeople
  file delete $acronum
  file delete $acrovotes
  file delete $acrowinscore
  set acrorunning 0
  putmsg $acrochan "Okay, I'm shutting up already. Acro was \002stopped\002 by $n. Scores for this game are deleted."
  foreach t [utimers] {
    if {[regexp -all -- "acrowarningvote" $t]} {
      killutimer $acrotimerid
      unbind msgm -|- * acro:msg
    }
    if {[regexp -all -- "newround" $t]} {
      killutimer $acrotimerid
    }
    if {[regexp -all -- "acroendround" $t]} {
      killutimer $acrotimerid
      unbind msgm -|- * acro:msg
    }
    if {[regexp -all -- "acrowarningsend" $t]} { 
      killutimer $acrotimerid
      unbind msgm -|- * acro:msg
    }
    if {[regexp -all -- "acro:endvote" $t]} {
      killutimer $acrotimerid
      unbind msgm -|- * acro:msg
    }
    if {[regexp -all -- "acronewround" $t]} {
      killutimer $acrotimerid
    }
  }
}

proc acro:pause { n u h c alltheargsIdontneed } {
  global acrorunning acrotimerid acrochan acrocurrent acropeople acronum acrovotes acronewround acrowinscore acroscores
  file delete $acrocurrent
  file delete $acropeople
  file delete $acronum
  file delete $acrovotes
  file delete $acrowinscore
  set acrorunning 0
  putmsg $acrochan "Acro was \002paused\002 by $n. Start a new round (but keep current scores) with \002!acro"
  foreach t [utimers] {
    if {[regexp -all -- "acrowarningvote" $t]} {
      killutimer $acrotimerid
      unbind msgm -|- * acro:msg
    }
    if {[regexp -all -- "newround" $t]} {
      killutimer $acrotimerid
    }
    if {[regexp -all -- "acroendround" $t]} {
      killutimer $acrotimerid
      unbind msgm -|- * acro:msg
    }
    if {[regexp -all -- "acrowarningsend" $t]} {
      killutimer $acrotimerid
      unbind msgm -|- * acro:msg
    }
    if {[regexp -all -- "acro:endvote" $t]} {
      killutimer $acrotimerid
      unbind msgm -|- * acro:msg
    }
    if {[regexp -all -- "acronewround" $t]} {
      killutimer $acrotimerid
    }
  }
}


proc parsefile {filename} {
 set s [open $filename r]
 set wholefile [read $s]
 close $s
 return [split $wholefile "\n"] }
 
proc removeshit {text} { return [string trimleft [string trimright [string trimleft [string trimright [string trimright [string trimleft [string trimright $text "\[?\]"] "\[?\]"] "\}"] "\\"] "\{"] " "] " "]}

proc acrocheck {nick uhost hand chan args} {
  global acrorunning noacroplayers botnick
  if {$acrorunning == 1} { 
    putmsg $chan "Acro is currently running.. If this is not the case then something is seriously wrong :P" 
  } else {
    set acrogo 0
    foreach i [channel info $chan] {
      if {([string match "+be_acro" $i])} {
        set acrogo 1
      }
    }
    if {$acrogo == 0} {
      putmsg $chan "Don't start acro in here please."
    } else {
      startacro $nick $uhost $hand $chan $args
    }
  }
}

proc startacro {nick uhost hand chan args} {
  global acrocurrent acropeople acronum acrovotes acrorunning acrochan acrotimerid botnick noacroplayers acrowinscore acrotopics usetopics
  regsub -all -- "\\\}" $args "" args
  regsub -all -- "\\\{" $args "" args
  regsub -all -- " " $args "" acrowinpoints
  if {[isnumber $acrowinpoints]} {
    if {$acrowinpoints < 31 && $acrowinpoints > 2} {
      set fs [open $acrowinscore w]
      puts $fs $acrowinpoints
      close $fs
    } else {
      putchan $chan "Invalid argument! Min=3 Max=30 points needed to win."
      putchan $chan "Setting score to default of 30"
      set fs [open $acrowinscore w]
      puts $fs "30"
      close $fs
      set acrowinpoints 30
    }
  } else {
    set acrowinpoints 30
    set fs [open $acrowinscore w]
    puts $fs "30"
    close $fs
  }
  if {$acrorunning == 0} {
    putmsg $chan "Points needed to win this game:\002 $acrowinpoints\002"
  }
  set nonplayers [string tolower [split $noacroplayers " "]]
  lappend nonplayers $botnick
  set usrcount 0
  foreach usr [chanlist $chan] {
    if {[lsearch -exact $nonplayers [string tolower $usr]] == -1} {
      incr usrcount
    }
  }
  if {$usrcount < 3} {
    putmsg $chan "Not enough players! Minimum is 3. Not counting: $botnick $noacroplayers"
  } else {
  set acrochan $chan
  set acrorunning 1
  set fs [open $acrocurrent w+]
  close $fs
  set fs [open $acropeople w+]
  close $fs
  set fs [open $acronum w+]
  close $fs
  set f [open $acrovotes w]
  close $f
  set letlist [parsefile "letters.acro"]
  set totprob 0
  foreach line $letlist {
    if {[regexp "\[1234567890\]" $line]} { set totprob [expr $totprob + [string range $line 2 end]] }
    }
  set acro(len) [rand 9]
  if {$acro(len) < 3} {incr acro(len) 3}
  set letnum 1
  set acron "" 
  while {$letnum <= $acro(len)} {
    regsub " " $acron "" acron
    set r [rand $totprob]
    set where 0
    foreach line $letlist {
      if {$where + [string range $line 2 end] >= $r} { 
        set doublel [concat [string index $line 0] [string index $line 0]]
        regsub " " $doublel "" doublel
        if {$letnum > 2} { set l2 [string range $acron [expr $letnum - 3] end] } else { set l2 "xz" } 
        if {$l2 == $doublel} { 
          incr letnum -1
          } else { 
          set acron [concat $acron [string index $line 0]] 
          }
        break
        }
      set where [expr $where + [string range $line 2 end]]
      }
    incr letnum
    }
  regsub -all " " $acron "" acron
  set acro(list) [split $acron ""]
  set acro(show) $acron
  putmsg $chan "A new game of acro is starting! Use the form \037/MSG $botnick ANSWER\037 to submit something."
  if {$usetopics} {
    putmsg $chan "Suggested topic: \002[lindex $acrotopics [rand [llength $acrotopics]]]\002"
  }
  putmsg $chan "Acro for this round: \0039,1 [string toupper $acro(show)] \003"
  set makeacrolength 60
  if {[llength $acro(list)] > 4} { 
    incr makeacrolength [expr 10 * ([llength $acro(list)] - 4)]
    }
  putmsg $acrochan "Length of acro is [llength $acro(list)], I give you $makeacrolength seconds to make your acro."
  bind msgm -|- * acro:msg
  set acrotimerid [utimer [expr $makeacrolength -10] "acrowarningsend $chan"]
  global acrocurrent
  set fa [open $acrocurrent w+]
  puts $fa $acro(list)
  close $fa
  }
}


proc acrowarningsend {chan} {
  global acrorunning acrochan acrotimerid
  putquick "PRIVMSG $chan :\002\0034Hurry up! Only 10 seconds left!\002\003"
  set acrotimerid [utimer 10 "acroendround $chan"]
}

proc acro:msg {nick uhost hand text} {
  global acrocurrent acropeople acronum votingtime acrochan acrorunning acrotimerid
  if {[onchan $nick $acrochan]} {
  set fa [open $acrocurrent r]
  gets $fa acro(list)
  close $fa
  set sentacros [parsefile $acropeople]
  if {![isnumber $text]} {
    if {[regexp "delacro" $text]} {
      set fs [open $acropeople r]
      set acsub 0
      while {![eof $fs]} {
        set line [gets $fs]
        if {[lindex [split $line " "] 0] == $nick} {
          set acsub 1
        }
      }
      close $fs
      if {$acsub == 0} {
        puthelp "NOTICE $nick :You didn't submit an acro yet. Hurry up, the letters are \002[string toupper $acro(list)]\002"
      } else {
        set newpeople ""
        set fd [open $acropeople r]
        while {![eof $fd]} {
          set line [gets $fd]
          if {[lindex [split $line " "] 0] != $nick} {
            lappend newpeople $line
          }
        }
        close $fd
        set fs [open $acropeople w]
        set acnewlen [llength $newpeople]
        foreach newline $newpeople {
          if {$newline != ""} {
            puts $fs $newline
          }
        }
        close $fd
        puthelp "NOTICE $nick :Your acro has been deleted! Hurry up and make a new one with the letters: \002[string toupper $acro(list)]\002"
        set fs [open $acronum r]
        gets $fs acounter
        close $fs
        incr acounter -1
        puthelp "PRIVMSG $acrochan :One acro was deleted. New total: $acounter acros. Make up your mind!!!"
        set fs [open $acronum w]
        puts $fs $acounter
        close $fs
      }
    } else {
      set notsentyet 1
      foreach line $sentacros {
        if {[lindex [split $line " "] 0] == $nick} { 
          puthelp "NOTICE $nick :You already sent one acro this round. Thank you drive through." 
          set notsentyet 0
        } 
      }
      if {$notsentyet} {
        if {[llength [split $acro(list) " "]] != 0} {
          set acron($nick) [split $text " "]
          set acrolen(list) [llength [split $acro(list) " "]]
          set acrolen($nick) [llength $acron($nick)]
          if {$acrolen(list) != $acrolen($nick)} {
            puthelp "NOTICE $nick :Invalid acro! Keep in mind to use all letters. They are \002[string toupper $acro(list)]\002"
          } else {
            set acount 0
            set noacro 0
            foreach word $acron($nick) {
              set letter1 [string tolower [lindex [split $word ""] 0]]
              set org1 [string tolower [lindex $acro(list) $acount]]
              if {$org1 != $letter1} { set noacro 1 }
              incr acount 1
              }
            if {$noacro} {
              puthelp "NOTICE $nick :Invalid acro! Remember, the letters are \002[string toupper $acro(list)]\002"
            } else {
              set fs [open $acropeople a]
              puts $fs "$nick $text"
              close $fs
              set fs [open $acronum r]
              gets $fs acounter
              close $fs
              if {$acounter == ""} {
                putmsg $acrochan "1st acro received!"
                set acounter 1
                set fs [open $acronum w]
                puts $fs $acounter
                close $fs
              } else {
                incr acounter 
                putmsg $acrochan "$acounter acros received!"
                set fs [open $acronum w+]
                puts $fs $acounter
                close $fs
              }
              puthelp "NOTICE $nick :Your acro has been recorded! You are player number $acounter"
              }
            } 
          } 
        }   
      }
    } else {
    global acrovotes acropeople
    set vote($nick) [lindex [split $text " "] 0]
    set fs [open $acropeople r]
    set votefound 0
    while {![eof $fs]} {
      set votenick [lindex [split [gets $fs] " "] 0]
      if {$nick == $votenick} {
        set votefound 1
        }
      }
    close $fs
    if {$votefound < 1} { puthelp "NOTICE $nick :You did not submit an acro this round. Harmless bystanders such as you may cheer for the glorious players, but not mess with the voting." }
    set totalacros -1
    set fs [open $acropeople r]
    while {![eof $fs]} {
      gets $fs temp_
      incr totalacros 1
      }
    close $fs
    set notanacro 0
    if {$vote($nick) > $totalacros} { set notanacro 1 }
    if {$notanacro} {
      puthelp "NOTICE $nick :Invalid acro number!"
      } else {
      if {$votefound == 1} {
        set voteline "$nick $vote($nick)"
        set fs [open $acrovotes r]
        set alreadyvoted 0
        while {![eof $fs]} {
          if {[lindex [split [gets $fs] " "] 0] == $nick} {
            set alreadyvoted 1
            }
          }
        close $fs
        if {$alreadyvoted} {
          puthelp "NOTICE $nick :You already voted this round!"
        } else {
          set fp [open $acropeople r]
          set countr 0
          while {$countr < $vote($nick)} {
            set line [gets $fp]
            incr countr
          }
          close $fp
          set whorevote [lindex [split $line " "] 0]
          if {$nick != $whorevote} {
            set fs [open $acrovotes a]
            puts $fs $voteline
            close $fs
            set totvot -1
            set fp [open $acrovotes r]
            while {![eof $fp]} {
              incr totvot
              gets $fp _temp
            }
            close $fp
            if {$totvot == "1"} {
              puthelp "PRIVMSG $acrochan :1st vote received!"
            } else {
              puthelp "PRIVMSG $acrochan :$totvot votes received!"
            }
            putnotc $nick "Your vote for acro number $vote($nick) has been recorded!"
            set totalacros -1
            set fs [open $acropeople r]
            while {![eof $fs]} {
            gets $fs temp_
              incr totalacros 1
            }
            close $fs
            if {$totalacros == $totvot} {
              puthelp "PRIVMSG $acrochan :All votes are in! :)  On with the scores then!"
              killutimer $acrotimerid
              acro:endvote $acrochan
            }
          } else {
            putnotc $nick "You cannot vote for yourself, you cheating bastard!!"
          }
        }
        }
      }
    }
  }
}

proc acro:howprob {n u h c letter} {
  global acrochan
  set letter [string tolower $letter]
  set l [removeshit $letter]
  set letlist [parsefile "letters.acro"]
  set totprob 0
  set thislet 0
  foreach line $letlist {
    if {[string index $line 0] == $l} { 
      set thislet [string range $line 2 end] 
      }
    if {[regexp "\[1234567890\]" $line]} { set totprob [expr $totprob + [string range $line 2 end]] }
    }
  set percent [expr (100.0 / $totprob) * $thislet]
  set pp1 [lindex [split $percent .] 0]
  set pp2 [lindex [split $percent .] 1]
  regsub " " [lrange [split $pp2 ""] 0 1] "" pp2s
  set percent $pp1.$pp2s
  putmsg $c "The probabilty for the letter $l to appear in any acro is $thislet in $totprob, which is roughly $percent percent."
} 

proc changeprob { letter amount } {
  global acrochan
  set l [removeshit $letter]
  set letlist [parsefile "letters.acro"]
  set w [lsearch -regexp $letlist "$l.*"]
  set prob [string range [lindex $letlist $w] 2 end]
  if {[expr $prob + $amount] < 1} { putmsg $acrochan "Can't do that, that would result in a probability smaller than 1." } else { 
    incr prob $amount
    set newline [concat $l $prob]
    set newlist [lreplace $letlist $w $w $newline]
    set st [open "letters.acro" w]
    set newlist [string trimright $newlist "\n"]
    foreach line $newlist { 
      if {[regexp "\[1234567890\]+" $line]} { puts $st $line } }
    close $st
    putmsg $acrochan "Probability for $l changed by $amount to $prob."
    }
  }
  
proc acro:useful {n u h c arg} { 
  set letter [removeshit [string index $arg 0]]
  set amount [removeshit [string range $arg 1 end]]
  if {$amount == ""} {set amount 1}
  changeprob $letter $amount }
proc acro:useless {n u h c arg} { 
  set letter [removeshit [string index $arg 0]]
  set amount [removeshit [string range $arg 1 end]]
  if {$amount == ""} {set amount 1}
  changeprob $letter [expr 0 - $amount] }



proc acroendround {chan} {
  global acrovotes acrocurrent acropeople acrorunning botnick acrotimerid
  set fo [open $acropeople r]
  set totacro -1
  while {![eof $fo]} {
    gets $fo _temp
    incr totacro
  }
  putmsg $chan "Time is up!"
  if {$totacro > 2} {
    set fs [open $acrovotes w+]
    close $fs
    set fa [open $acrocurrent w+]
    close $fa
    putmsg $chan "\002Current Acros:\002"
    set fs [open $acropeople r]
    set acnt 1
    while {![eof $fs]} {
      set acro(line) [lrange [split [gets $fs] " "] 1 end]
      if {$acro(line) != ""} {
        set numbah "\002\[$acnt\]\002 \0039,1"
        putmsg $chan "$numbah $acro(line) "
      }
      incr acnt 1
    }
    set totalacros -1
    set fs [open $acropeople r]
    while {![eof $fs]} {
      gets $fs temp_
      incr totalacros 1
    }
    close $fs
    set votesecs [expr ((30 + (($totalacros -3) * 5)) + ($totalacros * 2)) + 4]
    putmsg $chan "Start voting !! You have \002[expr ($votesecs - ($totalacros * 2)) -4]\002 seconds. Use the form \037/MSG $botnick NUMBER\037 to submit a vote."
    bind msgm -|- * acro:msg
    set acrotimerid [utimer [expr $votesecs -10] "acrowarningvote $chan"]
  } else {
    putmsg $chan "Sorry, not enough players this round! New round starts in 15 seconds."
    unbind msgm -|- * acro:msg
    set fp [open $acrocurrent w+]
    close $fp
    set acrotimerid [utimer 15 "startacro ooga ooga ooga $chan newround"]
    set acrorunning 0
  }
}

proc acrowarningvote {chan} {
  global acrorunning acrotimerid acrochan
  putquick "PRIVMSG $chan :\002\0034Hurry up! Only 10 seconds left!\002\003"
  set acrotimerid [utimer 10 "acro:endvote $chan"]
}


proc acro:endvote {chan} {
  global acrovotes acropeople acroscores acrocurrent acrohiscores acrorunning acrowinscore acrotimerid
  putmsg $chan "\002Time's up! Let's see the result of the voting..."
  unbind msgm -|- * acro:msg
  set totalacros -1
  set fs [open $acropeople r]
  while {![eof $fs]} {
    gets $fs temp_
    incr totalacros 1
  }
  close $fs
  set votecur 1
  while {$votecur < [expr $totalacros +1]} {
    set fb [open $acrovotes r]
    while {![eof $fb]} {
      set line [gets $fb]
      if {$line != ""} {
        if {[lindex [split $line " "] 1] == $votecur} {
          lappend acronum($votecur) [lindex [split $line " "] 0]
        }
      }
    }
    close $fb
    incr votecur
  }
  set count 1
  set arrayc 1
  while {$arrayc < [expr $totalacros +1]} {
    if {[array get acronum $arrayc] == ""} {
      lappend acronum($arrayc) "0"
    }
    incr arrayc
  }
  set acrohighscore 0
  set acrohi(who) ""
  while {$count < [expr [array size acronum] +1]} {
    set tempc 0
    set fa [open $acropeople r]
    while {$tempc < $count} {
      set line [gets $fa]
      set whos [lindex [split $line " "] 0]
      set theacro [lrange [split $line " "] 1 end]
      incr tempc
    }
    close $fa
    set acrount [llength $acronum($count)]
    if {$acronum($count) != 0} {
      if {$acrount > $acrohighscore} {
        set acrohi(who) $whos
        set acrohighscore $acrount
      } elseif {$acrount == $acrohighscore} {
        lappend acrohi(who) $whos
      }
      putmsg $chan "\0039,1$whos's\003\0030,1 acro \"\00311,1$theacro\003\0030,1\" got \003\0038,1 $acrount \003\0030,1vote(s) from: \0038,1$acronum($count)"
    } else {
      putmsg $chan "\0039,1$whos's\003\0030,1 acro \"\00311,1$theacro\003\0030,1\" got no votes this round :("
    }
    incr count
  }
  set totwinners [llength $acrohi(who)]
  set fastvoter ""
  set fastchk ""
  foreach winner $acrohi(who) {
    set countr 1
    set fp [open $acropeople r]
    while {![eof $fp]} {
      set line [gets $fp]
      if {$winner == [lindex [split $line " "] 0]} {
        lappend fastvotelines $countr
      }
      incr countr 1
    }
    close $fp
    set fs [open $acrovotes r]
    set fastchk ""
    while {![eof $fs] && $fastchk == ""} {
      set line [gets $fs]
      foreach l $fastvotelines {
        if {$l == [lindex [split $line " "] 1]} {
          set fastchk [lindex [split $line " "] 0]
        }
      }
    }
    close $fs
  }
  set fastvoter $fastchk
  if {$fastvoter != ""} {
    putmsg $chan "\002$fastvoter\002 was the first one to vote for a winning acro and receives 1 bonus point!"
  }
  if {$totwinners < 2} {
    if {$acrohi(who) != ""} {
      putmsg $chan "\002$acrohi(who)\002 got the most votes and receives 3 points!"
    }
  } else {
    putmsg $chan "\002$acrohi(who)\002 tied for the most votes and receive 3 points each!"
  }
  if {![file exists $acroscores]} {
    set fs [open $acroscores w+]
    close $fs
  }
  foreach winner $acrohi(who) {
    set fp [open $acrovotes r]
    set validw 0
    while {![eof $fp]} {
      set line [gets $fp]
      if {$winner == [lindex [split $line " "] 0]} {
        set validw 1
      }
    }
    if {$validw == 0} {
      putmsg $chan "Aw.. \002$winner\002 didn't vote! Now he/she gets no points! :P"
      set pos [lsearch -exact $acrohi(who) $winner]
      set acrohi(who) [lreplace $acrohi(who) $pos $pos]
    }
  }
  foreach winner $acrohi(who) {
  set fc [open $acroscores r+]
  set foundw 0
  set foundf 0
  while {![eof $fc]} {
    set line [gets $fc]
    if {$winner == [lindex [split $line " "] 0]} {
      set foundw 1
      set winscore [lindex [split $line " "] 1]
    }
  }
  close $fc
  if {$foundw} {
    set fp [open $acroscores r]
    set newscores ""
    lappend newscores "$winner [expr $winscore +3]"
    while {![eof $fp]} {
      set line [gets $fp]
      if {$winner != [lindex [split $line " "] 0]} {
        lappend newscores $line
      }
    }
    close $fp
    set fs [open $acroscores w]
    foreach newline $newscores {
      if {$newline != ""} {
        puts $fs $newline
      }
    }
    close $fs
  } else {
    set fp [open $acroscores a]
    puts $fp "$winner 3"
    close $fp
  }
  }
  set fc [open $acroscores r+]
  set foundf 0
  while {![eof $fc]} {
    set line [gets $fc]
    if {$fastvoter == [lindex [split $line " "] 0]} {
      set foundf 1
      set fastscore [lindex [split $line " "] 1]
    }
  }
  close $fc
  if {$foundf} {
    set fp [open $acroscores r]
    set newscores ""
    lappend newscores "$fastvoter [expr $fastscore +1]"
    while {![eof $fp]} {
      set line [gets $fp]
      if {$fastvoter != [lindex [split $line " "] 0]} {
        lappend newscores $line
      }
    }
    close $fp
    set fs [open $acroscores w]
    foreach newline $newscores {
      if {$newline != ""} {
        puts $fs $newline
      }
    }
    close $fs
  } else {
    set fp [open $acroscores a]
    puts $fp "$fastvoter 1"
    close $fp
  }
  set acrorunning 0
  acro:scores $chan
  set fp [open $acrocurrent w+]
  close $fp
  set fc [open $acrowinscore r]
  set pointchk [gets $fc]
  close $fc
  set fp [open $acroscores r]
  set acrowinner ""
  while {![eof $fp]} {
    set line [gets $fp]
    if {[lindex [split $line " "] 1] >= $pointchk} {
      lappend acrowinner [lindex [split $line " "] 0]
    }
  }
  if {$acrowinner != ""} {
    if {[llength $acrowinner] == 1} {
      putmsg $chan "We have a WINNER! Congratulations to \002$acrowinner\002!!! He/she gets 1 point in the total acro hiscore."
      putmsg $chan "Type \002!acro\002 to start a new game or \002!hiscores\002 to show the current top 10"
      set acrorunning 0
      file delete $acroscores
      file delete $acrowinscore
      if {![file exists $acrohiscores]} {
        set fs [open $acrohiscores w+]
        close $fs
      }
      set fs [open $acrohiscores r]
      set wfound 0
      while {![eof $fs]} {
        set line [gets $fs]
        if {$acrowinner == [lindex [split $line " "] 0]} {
          set wfound 1
          set wscore [lindex [split $line " "] 1]
          putlog "found $acrowinner with $wscore points!"
        }
      }
      if {$wfound == 1} {
        set fs [open $acrohiscores r]
        set newhifile ""
        lappend newhifile "$acrowinner [expr $wscore +1]"
        while {![eof $fs]} {
          set line [gets $fs]
          if {[lindex [split $line " "] 0] != $acrowinner} {
            lappend newhifile $line
          }
        }
        close $fs
        set fp [open $acrohiscores w]
        foreach newline $newhifile {
          if {$newline != ""} {
            puts $fp $newline
          }
        }
        close $fp
      } else {
        set fd [open $acrohiscores a]
        puts $fd "$acrowinner 1"
        close $fd
      }

    } else {
      putmsg $chan "We have \002[llength $acrowinner]\002 WINNERS! Congratulations to \002$acrowinner\002!!! Each gets 1 point in the total acro hiscore."
      putmsg $chan "Type \002!acro\002 to start a new game or \002!hiscores\002 to show the current top 10"
      set acrorunning 0
      file delete $acroscores
      file delete $acrowinscore
      if {![file exists $acrohiscores]} {
        set fs [open $acrohiscores w+]
        close $fs
      }
      foreach awin $acrowinner {
        set fs [open $acrohiscores r]
        set wfound 0
        while {![eof $fs]} {
          set line [gets $fs]
          if {$awin == [lindex [split $line " "] 0]} {
            set wfound 1
            set wscore [lindex [split $line " "] 1]
          }
        }

        if {$wfound == 1} {
          set fs [open $acrohiscores r]
          set newhifile ""
          lappend newhifile "$awin [expr $wscore +1]"
          while {![eof $fs]} {
            set line [gets $fs]
            if {[lindex [split $line " "] 0] != $awin} {
              lappend newhifile $line
            }
          }
          close $fs
          set fp [open $acrohiscores w]
          foreach newline $newhifile {
            if {$newline != ""} {
              puts $fp $newline
            }
          }
          close $fp
        } else {
          set fd [open $acrohiscores a]
          puts $fd "$awin 1"
          close $fd
        }
      }
    }
  } else {
    putmsg $chan "Next round starts in\002 15\002 seconds!"
### this isn't exactly true as the timer gets started before the putmsg gets displayed.
### we'll 'correct' this by increasing the timer a little bit :P
    set acrotimerid [utimer 25 "startacro ooga localhost ooga $chan acronewround"]
  }
}

proc acro:scores {chan} {
  global acroscores acrorunning
  set fc [open $acroscores r]
  while {![eof $fc]} {
    set line [gets $fc]
    if {$line != ""} {
      set anick [lindex [split $line " "] 0]
      set ascore [lindex [split $line " "] 1]
      lappend atotals "\0039,1$anick:\00311,1 $ascore "
    }
  }
  close $fc
  set atotals [lsort -index 1 -integer -decreasing $atotals]
  regsub -all -- "\\\}" $atotals "" atotals
  regsub -all -- "\\\{" $atotals "" atotals
  putmsg $chan "\0038,1Scores:  \003$atotals"
}

proc acro:hiscores {nick uhost hand chan args} {
  global acrohiscores
  if {![file exists $acrohiscores]} {
    putmsg $chan "Sorry, the hiscore list is empty! Why? Because people don't play, that's why!! :P"
  } else {
    set fc [open $acrohiscores r]
    while {![eof $fc]} {
      set line [gets $fc]
      if {$line != ""} {
        set anick [lindex [split $line " "] 0]
        set ascore [lindex [split $line " "] 1]
        lappend atotals "\0039,1$anick:\00311,1 $ascore "
      }
    }
    close $fc
    if {[llength $atotals] > 10} {
      set atotals [lsort -index 1 -integer -decreasing $atotals]
      set atotals [lreplace $atotals 10 end]
      putmsg $chan "\002ACRO HISCORES TOP 10:\002"
      regsub -all -- "\\\}" $atotals "" atotals
      regsub -all -- "\\\{" $atotals "" atotals
      putmsg $chan "$atotals"
    } else {
      set atotals [lsort -index 1 -integer -decreasing $atotals]
      putmsg $chan "\002ACRO HISCORES TOP 10:\002"
      regsub -all -- "\\\}" $atotals "" atotals
      regsub -all -- "\\\{" $atotals "" atotals
      putmsg $chan "$atotals"
    }
  }
}

proc acro:help {nick uhost hand chan arg} {
  global botnick
  puthelp "PRIVMSG $nick :\002ACRO\002 is a nice game where people are given a mix of characters and make a sentence out of these."
  puthelp "PRIVMSG $nick :For example: i give you the letters \002LOL\002. Then you can type /msg $botnick \002L\002aughing \002O\002ut \002L\002oud"
  puthelp "PRIVMSG $nick :If you didn't like the acro you submitted, simply type /msg $botnick delacro    and start all over again."
  puthelp "PRIVMSG $nick :At the end of the round, all those who submitted an acro can vote for one by typing /msg $botnick number"
  puthelp "PRIVMSG $nick :The person who gets the most votes wins the round and gets 3 points. With 30 points you win the game!"
  puthelp "PRIVMSG $nick :Start the game with !acro <nr>  where <nr> is the points needed to win. If <nr> is omitted, the default is 30. \002Have fun!\002 :)"
}

proc acro:reset {nick uhost hand chan arg} {
  global acroscores
  if {[file exists $acroscores]} {
    file delete $acroscores
    putmsg $chan "All points for this game have been deleted!"
  } else {
    putmsg $chan "Error: no points were giving yet this round."
  }
}

putlog "be-acro v$acrover -- loaded!"
