# Entertainment, Games. (Acronym, Trivia & Scramble)
# Modified from game.tcl by kung-foo, it was edited by
# various unknown person(s).
# Edited by nukie (04 Jan 2004 - 05:01) - http://nukie.front.ru/

### Settings ###

set game(ver) 2.0
set game(deftype) 0
set game(defrounds) 20
set game(defteam) 0 
set game(scramb_type) 1
set game(src) 0
set game(cmd_help) "!gamehelp"
set game(cmd_guess) "`"
set game(cmd_giveup) "!skip"
set game(cmd_score) "!score"
set game(cmd_team) "!team"
set game(cmd_start) "!start"
set game(cmd_stop) "!stop"
set game(cmd_stat) "!database"
set game(cmd_add) "!add"
set game(cmd_rem) "!remove"

# NOTE: this should be set to the "exact" directory where the
# game data is, else it will search the root eggdrop directory.
set game(file) "scripts/game.data"
# NOTE: this can only be set to one channel only, currently it
# doesn't support multiple channel games.
set game(chan) "#HaLL"
set game(time) 5
set game(wait) 15


######### Please do not edit anything below unless you know what you are doing ;) #########

if {![info exists game(game)]} {
 set game(game) 0
 set game(type) $game(deftype)
 set game(team) $game(defteam)
 set game(rounds) $game(defrounds)
 catch {
  unset game_players
  unset game_teams
  unset game_guesses 
  unset game(question)
 } 0
}
set game(questions) ""
if {[file exists $game(file)]} {
 set fileid [open $game(file) r]
 while {![eof $fileid]} {
  gets $fileid game(temp)
  if {$game(temp) != ""} {
   lappend game(questions) $game(temp)
  }
 }
 close $fileid
}
set game(wquestions) $game(questions)
if {$game(type) == 2} {
  if {[catch {exec lynx -help}]} {
    putlog "\[4x]: ERROR this system is not equiped with lynx - aborting"
    return 0
  }
}
bind pub - $game(cmd_help) pub_game_help
bind pub - $game(cmd_guess) pub_game_guess
bind pub - $game(cmd_giveup) pub_game_giveup
bind pub - $game(cmd_score) pub_game_score
bind pub - $game(cmd_team) pub_game_team
bind pub - $game(cmd_start) pub_game_start
bind pub - $game(cmd_stop) pub_game_stop
bind pub - $game(cmd_stat) pub_game_stat
bind pub o|o $game(cmd_add) pub_game_add
bind pub o|o $game(cmd_rem) pub_game_remove
bind nick - * nick_game_update
bind pubm - * pub_game_guess

proc xrange {xr xr1 xr2} {
  return [join [lrange [split $xr] $xr1 $xr2]]
}

proc xindex {xr xr1} {
  return [join [lrange [split $xr] $xr1 $xr1]]
}

proc pub_game_help {nick uhost hand chan arg} {
  global game
  if {[string tolower $chan] == [string tolower $game(chan)]} {
    puthelp "NOTICE $nick :\[4x]: Entertainment, Games v$game(ver) Public Commands."
    puthelp "NOTICE $nick :[format "%-24s" "$game(cmd_giveup)"] - give up and skip round."
    puthelp "NOTICE $nick :[format "%-24s" "$game(cmd_score)"] - view current scores."
    puthelp "NOTICE $nick :[format "%-24s" "$game(cmd_start) \[type\] \[rounds\]"] - start a new game."
    puthelp "NOTICE $nick :[format "%-24s" "$game(cmd_stop)"] - stop the current game."
    puthelp "NOTICE $nick :\[4x]: Other Commands:"
    puthelp "NOTICE $nick :[format "%-24s" "$game(cmd_help)"] - this help."
    if {$game(type) != 2} {
      puthelp "NOTICE $nick :[format "%-24s" "$game(cmd_stat)"] - view the database statistics."
    }
    if {$game(team)} {
      puthelp "NOTICE $nick :[format "%-24s" "$game(cmd_team)"] - join a team."
    }
    if {($game(type) != 2) && ([matchattr $hand o|o $chan])} {
      puthelp "NOTICE $nick :[format "%-24s" "$game(cmd_add)"] - add a question."
      puthelp "NOTICE $nick :[format "%-24s" "$game(cmd_rem)"] - remove a question."
    }
  }
  puthelp "NOTICE $nick :\[4x]: End of Entertainment, Games v$game(ver) Help."
  putcmdlog "\[4x]: <<$nick>> !$hand! Entertainment, Games. Public Commands Help." ; return 0
}

proc pub_game_guess {nick uhost hand chan arg} {
 global game game_players game_teams game_guesses
 if {[string tolower $chan] == [string tolower $game(chan)]} {
  if {!$game(game)} {
   putserv "PRIVMSG $chan :"
  } elseif {[info exists game(question)]} {
   if {$game(team)} {
    set game(temp2) 0
    foreach game(temp) [array names game_players] {
     if {[lsearch -exact [string tolower $game_players($game(temp))] [string tolower $nick]] != -1} {
      set game(temp2) 1
     }
    }
    if {!$game(temp2)} {
     putserv "PRIVMSG $chan :\[4x]: ERROR $nick: you have not yet joined a team, type $game(cmd_team) to do so"
     return 0
    }
   }
   set guess [string tolower $arg]
   if {($game(type) == 2) && ($game(question) != "")} {
    if {[info exists game_guesses([string tolower $nick])]} {
     putserv "PRIVMSG $chan :\[4x]: ERROR $nick: you have already entered a guess"
    } else {
     set game(temp2) 0
     for {set i 0} {$i < [llength $guess]} {incr i} {
      if {[string index [xindex $guess $i] 0] != [string tolower [xindex [split $game(question) ""] $i]]} {
       set game(temp2) 1
      }
     }
     if {($game(temp2)) || ([llength $guess] != [string length $game(question)])} {
      putserv "PRIVMSG $chan :\[4x]: ERROR $nick: invalid guess"
     } else {
      set game_guesses([string tolower $nick]) $guess
      putserv "PRIVMSG $chan :\[4x]: $nick: guess entered"
     }
    }
   } else {
    foreach game(temp) $game(answers) {
     if {((!$game(type)) && ($guess == [string tolower $game(answers)])) || (($game(type) == 1) && ([string match [string tolower $game(temp)] $guess]))} {
      if {$game(team)} {
       foreach game(temp) [array names game_players] {
        if {[lsearch -exact [string tolower $game_players($game(temp))] [string tolower $nick]] != -1} {
         incr game_teams($game(temp))
         putserv "PRIVMSG $chan :\[4x]: $nick \[team $game(temp)\] is correct! ($guess)"
        }
       }
      } else {
       putserv "PRIVMSG $chan :\[4x]: $nick is correct! ($guess)"
       if {![info exists game_players([string tolower $nick])]} {
        set game_players([string tolower $nick]) 1
       } else {
        incr game_players([string tolower $nick])
       }
      }
      incr game(round)
      unset game(question)
      game_killtimers 1
      utimer 15 game_init
      game_rounds
      break
     }
    }
   }
  }
  foreach i [timers] {
   if {[xindex $i 1] == "game_timeout"} {
    killtimer [xindex $i 2]
   }
  }
  timer $game(wait) game_timeout
 }
}

proc pub_game_giveup {nick uhost hand chan arg} {
 global game game_players game_teams game_guesses
 if {[string tolower $chan] == [string tolower $game(chan)]} {
  if {!$game(game)} {
   putserv "PRIVMSG $chan :\[4x]: NO game STARTED, type $game(cmd_start) for next game"
  } elseif {[info exists game(question)]} {
   if {$game(team)} {
    set game(temp2) 0
    foreach game(temp) [array names game_players] {
     if {[lsearch -exact [string tolower $game_players($game(temp))] [string tolower $nick]] != -1} {
      set game(temp2) 1
     }
    }
    if {!$game(temp2)} {
     putserv "PRIVMSG $chan :\[4x]: ERROR $nick: you have not yet joined a team, type $game(cmd_team) to do so"
     return 0
    }
   }
   incr game(round)
   putserv "PRIVMSG $game(chan) :answer: $game(answers) (skipping)"
   if {$game(type) == 2} {
    putserv "PRIVMSG $game(chan) :\[4x]: StoPpiNg GaME - cLeaRiNg GuEsSes"
   } elseif {$game(type) == 1} {
    putserv "PRIVMSG $game(chan) :hint: $game(question)"
    if {$game(type) == 1} {
     set game(temp) ""
     foreach i $game(answers) {
      append game(temp) "$i, "
     }
     set game(temp) [string trimright $game(temp) ", "]
     putserv "PRIVMSG $game(chan) :answer(s): $game(temp)"
    } else {
     putserv "PRIVMSG $game(chan) :word: $game(scram_answer)"
     putserv "PRIVMSG $game(chan) :answer: $game(answers)"
    }
   }
   unset game(question)
   game_killtimers 1
   utimer 15 game_init
   game_rounds
  }
 }
}

proc pub_game_score {nick uhost hand chan arg} {
 global game game_players game_teams
 if {[string tolower $chan] == [string tolower $game(chan)]} {
  if {!$game(game)} {
   putserv "PRIVMSG $chan :\[4x]: NO game STARTED, type $game(cmd_start) for next game"
  } else {
   set i 0
   if {$game(team)} {
    if {[array names game_teams] != ""} {
     putserv "PRIVMSG $chan :\[4x]: [format "%-4s" "Num"][format "%-15s" "Team"][format "%-6s" "Score"]Nicks  (Round $game(round) of $game(rounds))"
     foreach team [array names game_teams] {
      incr i
      regsub -all " " $game_players($team) ", " nick
      putserv "PRIVMSG $chan :[format "%-4s" $i.][format "%-15s" $team][format "%-6s" $game_teams($team)]$nick"
     }
     putserv "PRIVMSG $chan :\[4x]: End of Teams"
    } else {
     putserv "PRIVMSG $chan :\[4x]: there are NO teams yet "
    }
   } else {
    if {[array names game_players] != ""} {
     putserv "PRIVMSG $chan :\[4x]: [format "%-4s" "Num"][format "%-15s" "Nick"]Score  (Round $game(round) of $game(rounds))"
     foreach nick [array names game_players] {
      incr i
      putserv "PRIVMSG $chan :[format "%-4s" $i.][format "%-15s" $nick]$game_players($nick)"
     }
     putserv "PRIVMSG $chan :\[4x]: End of Score"
    } else {
     putserv "PRIVMSG $chan :\[4x]: NO players have yet to score"
    }
   }
  }
 }
}

proc pub_game_team {nick uhost hand chan arg} {
 global game game_players game_teams
 if {([string tolower $chan] == [string tolower $game(chan)]) && ($game(team))} {
  if {!$game(game)} {
   putserv "PRIVMSG $chan : \[4x]: NO game STARTED, type $game(cmd_start) for next game"
  } else {
   set team [xindex $arg 0]
   if {$team == ""} {
    putserv "PRIVMSG $chan :\[4x]: usage $game(cmd_team) <team name>"
   } else {
    foreach game(temp) [array names game_players] {
     if {[lsearch -exact [string tolower $game_players($game(temp))] [string tolower $nick]] != -1} {
      putserv "PRIVMSG $chan :\[4x]: ERROR $nick: you have already joined a team ($game(temp))"
      return 0
     }
    }
    if {[info exists game_teams($team)]} {
     lappend game_players($team) [string tolower $nick]
     putserv "PRIVMSG $chan :\[4x]: $nick joined team $team"
    } else {
     set game_teams($team) 0
     set game_players($team) [string tolower $nick]
     putserv "PRIVMSG $chan :\[4x]: $nick created team $team"
    }
   }
  }
 }
}

proc pub_game_start {nick uhost hand chan arg} {
 global game game_players game_teams game_guesses
 if {[string tolower $chan] == [string tolower $game(chan)]} {
  if {$game(game)} {
   putserv "PRIVMSG $chan :\[4x]: game already in progress!"
  } else {
   set game(type) $game(deftype)
   set game(team) $game(defteam)
   set game(rounds) $game(defrounds)
   foreach game(temp) [string tolower $arg] {
    if {$game(temp) == "scrambler"} {
     set game(type) 0
    } elseif {$game(temp) == "trivia"} {
     set game(type) 1
    } elseif {$game(temp) == "acro"} {
     set game(type) 2
    } elseif {$game(temp) == "-team"} {
     set game(team) 1
    } elseif {$game(temp) == "-noteam"} {
     set game(team) 0
    } elseif {![catch {expr $game(temp)}]} {
     set game(rounds) $game(temp)
    }
   }
   if {([catch {expr $game(rounds)}]) || ($game(rounds) < 1)} {
    putserv "PRIVMSG $chan :\[4x]: ERROR invalid number of rounds "
   } else {
    set game(game) 1
    set game(round) 1
    catch {
     unset game_players
     unset game_teams
     unset game_guesses
     unset game(question)
    } 0
    game_killtimers 1
    if {$game(type) == 2} {
     putserv "PRIVMSG $chan :\[4x]: aCRo GaME STaRTeD with $game(rounds) Rounds, type $game(cmd_guess)<answer> to guess/answer the game "
     putlog "\[4x]: <<$nick>> !$hand! Entertainment, Games. aCRo GaME STaRTeD."
    } elseif {$game(type) == 1} {
     putserv "PRIVMSG $chan :\[4x]: TRiVia GaME STaRTeD with $game(rounds) Rounds, type $game(cmd_guess)<answer> to guess/answer the game "
     putlog "\[4x]: <<$nick>> !$hand! Entertainment, Games. TRiVia GaME STaRTeD."
    } else {
     putserv "PRIVMSG $chan :\[4x]: ScRaMbLe GaME STaRTeD with $game(rounds) Rounds"
     putlog "\[4x]: <<$nick>> !$hand! Entertainment, Games. ScRaMbLe GaME STaRTeD."
    }
    if {$game(team)} {
     putserv "PRIVMSG $chan :\[4x]: type $game(cmd_team) <team> to join a team "
    }
    timer $game(wait) game_timeout
    utimer 15 game_init
   }
  }
 }
}

proc pub_game_stop {nick uhost hand chan arg} {
 global game game_players game_teams game_guesses
 if {[string tolower $chan] == [string tolower $game(chan)]} {
  if {!$game(game)} {
   putserv "PRIVMSG $chan :\[4x]: NO game STARTED, type $game(cmd_start)  for next game"
  } else {
   set game(game) 0
   catch {
    unset game_players
    unset game_teams
    unset game_guesses
    unset game(question)
   } 0
   game_killtimers 1
   putserv "PRIVMSG $chan :\[4x]: StoPpiNg GaME - cLeaRiNg sCoRe"
   putlog "\[4x]: <<$nick>> !$hand! Entertainment, Games. StoPpeD."
  }
 }
}

proc pub_game_stat {nick uhost hand chan arg} {
 global game
 if {([string tolower $chan] == [string tolower $game(chan)]) && ($game(type) != 2)} {
  putserv "PRIVMSG $chan :\[4x]: there's a total of [llength $game(questions)] questions ([llength $game(wquestions)] unused) in [string length $game(questions)] bytes of database"
 }
}

proc pub_game_add {nick uhost hand chan arg} {
 global game
 if {([string tolower $chan] == [string tolower $game(chan)]) && ($game(type) != 2)} {
  if {[xindex $arg 1] == ""} {
   if {$game(type) == 1} {
    putserv "NOTICE $nick :\[4x]: usage $game(cmd_add) \"<question>\" \"<answer 1>\" \"<answer 2...>\""
   } else {
    putserv "NOTICE $nick :\[4x]: usage $game(cmd_add) \"<question>\" \"<phrase to scramble>\""
   }
  } else {
   foreach i $game(questions) {
    if {[string tolower [lindex $i 0]] == [string tolower [lindex $arg 0]]} {
     putserv "NOTICE $nick :\[4x]: ERROR, already exists!"
     return 0
    } 
   }
   lappend game(questions) $arg
   set game(wquestions) $game(questions)
   game_save
   putserv "NOTICE $nick :\[4x]: question added"
   putlog "\[4x]: <<$nick>> !$hand! added question"
  }
 }
}

proc pub_game_remove {nick uhost hand chan arg} {
 global game
 if {([string tolower $chan] == [string tolower $game(chan)]) && ($game(type) != 2)} {
  if {[xindex $arg 0] == ""} {
   putserv "NOTICE $nick :\[4x]: usage $game(cmd_rem) <question>"
  } else {
   set game(temp) 0
   foreach i $game(questions) {
    if {[string tolower [lindex $i 0]] == [string tolower $arg]} {
     set game(questions) [lreplace $game(questions) $game(temp) $game(temp)]
     set game(wquestions) $game(questions)
     game_save
     putserv "NOTICE $nick :\[4x]: question removed"
     putlog "\[4x]: <<$nick>> !$hand! removed question"
     return 0
    } else {
     incr game(temp)
    }
   }
   putserv "NOTICE $nick :\[4x]: ERROR doesn't exist"
  }
 }
}

proc nick_game_update {nick uhost hand chan newnick} {
 global game game_players game_teams game_guesses
 if {[string tolower $chan] == [string tolower $game(chan)]} {
  set nick [string tolower $nick]
  set newnick [string tolower $newnick]
  if {($game(type) == 2) && ([info exists game_guesses($nick)])} {
   set game_guesses($newnick) $game_guesses($nick)
   unset game_guesses($nick)
  }
  if {$game(team)} {
   foreach game(temp) [array names game_players] {
    set i [lsearch -exact [string tolower $game_players($game(temp))] [string tolower $nick]]
    if {$i != -1} {
     set game_players($game(temp)) [lreplace $game_players($game(temp)) $i $i $newnick]
     return 0
    }
   }
  } else {
   if {[info exists game_players($nick)]} {
    set game_players($newnick) $game_players($nick)
    unset game_players($nick)
   }
  }
 } 
}

proc game_save {} {
 global game
 set fileid [open $game(file) w]
 foreach game(temp) $game(questions) {
  puts $fileid $game(temp)
 }
 close $fileid 
}

proc game_init {} {
 global game game_players game_teams game_guesses
 if {$game(game)} {
  if {$game(type) == 2} {
   catch {unset game_guesses}
   set game(question) [getacro]
   putserv "PRIVMSG $game(chan) :\[4x]: Round $game(round) of $game(rounds)"
   putserv "PRIVMSG $game(chan) :acronym: $game(question)"
   set game(time_elasped) 1
   game_killtimers 0
   timer 1 game_update
  } else {
   if {$game(questions) != ""} {
    set game(temp) [getquestion]
    set game(question) [lindex $game(temp) 0]
    set game(answers) [lrange $game(temp) 1 end]
    if {!$game(type)} {
     set game(answers) [lindex $game(answers) 0]
     set game(scram_answer) [scramble $game(answers)]
    }
    putserv "PRIVMSG $game(chan) :\[4x]: Round $game(round) of $game(rounds)"
    putserv "PRIVMSG $game(chan) :hint: $game(question)"
    if {!$game(type)} {
     putserv "PRIVMSG $game(chan) :word: $game(scram_answer)"
    }
    set game(time_elasped) 1
    game_killtimers 0
    timer 1 game_update
   } else {
    putserv "PRIVMSG $game(chan) :\[4x]: ERROR, NO phrases have been saved, please contact my admin"
    set game(game) 0
    catch {
     unset game_players
     unset game_teams
     unset game_guesses
     unset game(question)
    } 0
    game_killtimers 1
    putserv "PRIVMSG $game(chan) :\[4x]: StoPpiNg GaME - cLeaRiNg sCoRe"
   }
  }
 }
}

proc game_update {} {
 global game game_players game_teams game_guesses
 if {$game(game)} {
  if {$game(time_elasped) < $game(time)} {
   if {$game(type) == 2} {
    putserv "PRIVMSG $game(chan) :acronym: $game(question)"
   } else {
    putserv "PRIVMSG $game(chan) :hint: $game(question)"
    if {!$game(type)} {
     putserv "PRIVMSG $game(chan) :word: $game(scram_answer)"
    }
   }
   incr game(time_elasped)
   game_killtimers 0
   timer 1 game_update
  } else {
   incr game(round)
   putserv "PRIVMSG $game(chan) :\[4x]: Time's Up!"
   if {$game(type) == 2} {
    if {[array names game_guesses] == ""} {
     putserv "PRIVMSG $game(chan) :\[4x]: no guesses to calculate!"
    } else {
     putserv "PRIVMSG $game(chan) :\[4x]: calculating results, please wait"
     foreach nick [array names game_guesses] {
      if {$game_guesses($nick) != ""} {
       regsub -all " " $game_guesses($nick) "+" guess
       if {$game(src)} {
        catch {exec lynx -source http://search.yahoo.com/bin/search?p=$guess | grep "Yahoo! Category Matches"} game(temp)
        set game(temp2) [lsearch -exact $game(temp) "of"]
        if {$game(temp2) != -1} {
         incr game(temp2)
         set game(temp3) [xindex [split [xindex $game(temp) $game(temp2)] ")"] 0]
        } else {
         set game(temp3) 0
        }
       } else {
        catch {exec lynx -source http://www.altavista.com/cgi-bin/query?pg=q\&kl=XX\&q=$guess | grep "AltaVista found"} game(temp)
        set game(temp2) [lsearch -exact $game(temp) "found"]
        if {$game(temp2) != -1} {
         incr game(temp2)
         if {[xindex $game(temp) $game(temp2)] == "about"} {
          incr game(temp2)
         }
         regsub -all "," [xindex $game(temp) $game(temp2)] "" game(temp3)
        } else {
         set game(temp3) 0
        }
       }
       if {![catch {expr $game(temp3)}]} {
        set game_hits($nick) $game(temp3)
       } else {
        set game_hits($nick) 0
       }
      } else {
       set game_hits($nick) 0
      } 
     }
     if {$game(src)} {
      putserv "PRIVMSG $game(chan) :\[4x]: Results: (Yahoo)"
     } else {
      putserv "PRIVMSG $game(chan) :\[4x]: Results: (Altavista)"
     }
     if {[array names game_hits] != ""} {
      putserv "PRIVMSG $game(chan) :\[4x]: [format "%-4s" "Num"][format "%-15s" "Nick"]Hits"
      set winners ""
      set score 0
      set i 0
      foreach nick [array names game_hits] {
       if {$game_hits($nick) > $score} {
        set winners $nick
        set score $game_hits($nick)
       } elseif {$game_hits($nick) == $score} {
        lappend winners $nick
       }
      }
      foreach nick [array names game_hits] {
       incr i
       if {([lsearch -exact $winners $nick] != -1) && ([llength $winners] == 1)} {
        set game(temp) "(winner)"
       } elseif {([lsearch -exact $winners $nick] != -1) && ([llength $winners] > 1)} {
        set game(temp) "(tie)"
       } else {
        set game(temp) ""
       }
       if {($game(temp) == "(tie)") || ($game(temp) == "(winner)")} {
        if {$game(team)} {
         foreach game(temp2) [array names game_players] {
          if {[lsearch -exact [string tolower $game_players($game(temp2))] $nick] != -1} {
           incr game_teams($game(temp2))
          }
         }
        } else {
         if {![info exists game_players($nick)]} {
          set game_players($nick) 1
         } else {
          incr game_players($nick)
         }
        }
       }
       putserv "PRIVMSG $game(chan) :\[4x]: [format "%-4s" $i.][format "%-15s" $nick]$game_hits($nick) $game(temp)"
      }
      putserv "PRIVMSG $game(chan) :\[4x]: End of Score"
     } else {
      putserv "PRIVMSG $game(chan) :\[4x]: NO players scored"
     }
     game_killtimers 1
     timer $game(wait) game_timeout
    }
   } else {
    putserv "PRIVMSG $game(chan) :hint: $game(question)"
    if {$game(type) == 1} {
     set game(temp) ""
     foreach i $game(answers) {
      append game(temp) "$i, "
     }
     set game(temp) [string trimright $game(temp) ", "]
     putserv "PRIVMSG $game(chan) :answer(s): $game(temp)"
    } else {
     putserv "PRIVMSG $game(chan) :word: $game(scram_answer)"
     putserv "PRIVMSG $game(chan) :answer: $game(answers)"
    }
   }
   unset game(question)
   game_killtimers 0
   utimer 15 game_init
   game_rounds
  }
 }
}

proc game_timeout {} {
 global game game_players game_teams game_guesses
 if {$game(game)} {
  set game(game) 0
  catch {
   unset game_players
   unset game_teams
   unset game_guesses
   unset game(question)
  } 0
  game_killtimers 1
  putserv "PRIVMSG $game(chan) :\[4x]: halting the game after $game(wait) minutes of inactivity"
 }
}

proc game_rounds {} {
 global game game_players game_teams game_guesses
 if {$game(round) > $game(rounds)} {
  putserv "PRIVMSG $game(chan) :\[4x]:  - tHe eNd -"
  set winners ""
  set score 0
  set i 0
  if {$game(team)} {
   foreach team [array names game_teams] {
    if {$game_teams($team) > $score} {
     set winners $team
     set score $game_teams($team)
    } elseif {$game_teams($team) == $score} {
     lappend winners $team
    }
   }
   putserv "PRIVMSG $game(chan) :\[4x]: [format "%-4s" "Num"][format "%-15s" "Team"][format "%-6s" "Score"]Nicks"
   foreach team [array names game_teams] {
    incr i
    if {([lsearch -exact $winners $team] != -1) && ([llength $winners] == 1)} {
     set game(temp) "(winner)"
    } elseif {([lsearch -exact $winners $team] != -1) && ([llength $winners] > 1)} {
     set game(temp) "(tie)"
    } else {
     set game(temp) ""
    }
    regsub -all " " $game_players($team) ", " nick
    putserv "PRIVMSG $game(chan) :[format "%-4s" $i.][format "%-15s" $team][format "%-6s" $game_teams($team)]$nick $game(temp)"
   }
   putserv "PRIVMSG $game(chan) :\[4x]: End of Teams"
  } else {
   putserv "PRIVMSG $game(chan) :\[4x]: [format "%-4s" "Num"][format "%-15s" "Nick"]Score"
   foreach nick [array names game_players] {
    if {$game_players($nick) > $score} {
     set winners $nick
     set score $game_players($nick)
    } elseif {$game_players($nick) == $score} {
     lappend winners $nick
    }
   }
   foreach nick [array names game_players] {
    incr i
    if {([lsearch -exact $winners $nick] != -1) && ([llength $winners] == 1)} {
     set game(temp) "(winner)"
    } elseif {([lsearch -exact $winners $nick] != -1) && ([llength $winners] > 1)} {
     set game(temp) "(tie)"
    } else {
     set game(temp) ""
    }
    putserv "PRIVMSG $game(chan) :[format "%-4s" $i.][format "%-15s" $nick]$game_players($nick) $game(temp)"
   }
   putserv "PRIVMSG $game(chan) :\[4x]: End of Score"
  }
  set game(game) 0
  catch {
   unset game_players
   unset game_teams
   unset game_guesses 
   unset game(question)
  } 0
  game_killtimers 1
 }
}

proc getquestion {} {
 global game
 if {$game(wquestions) == ""} {
  set game(wquestions) $game(questions)
 }
 set i [rand [llength $game(wquestions)]]
 set game(temp) [lindex $game(wquestions) $i]
 set game(wquestions) [lreplace $game(wquestions) $i $i]
 if {$game(temp) == ""} {
  getquestion
 }
 return $game(temp)
}

proc scramble {arg} {
 global game
 set scrambled ""
 if {$game(scramb_type)} {
  foreach game(temp) $arg {
   set game(temp2) ""
   while {$game(temp) != ""} {
    set i [rand [string length $game(temp)]]
    append game(temp2) [string index $game(temp) $i]
    set game(temp) [string range $game(temp) 0 [expr $i - 1]][string range $game(temp) [expr $i + 1] end]
   }
   lappend scrambled $game(temp2)
  } 
 } else {
  while {$arg != ""} {
   set i [rand [string length $arg]]
   append scrambled [string index $arg $i]
   set arg [string range $arg 0 [expr $i - 1]][string range $arg [expr $i + 1] end]
  }
 }
 return $scrambled
}

proc getacro {} {
 set acro ""
 for {set i [rand 4]} {$i < 4} {incr i} {
  append acro [string index "ABCDEFGHIJKLMNOPQRSTUVWXYZ" [rand 26]]
 }
 return $acro
}

proc game_killtimers {arg} {
 foreach i [utimers] {
  if {[xindex $i 1] == "game_init"} {
   killutimer [xindex $i 2]
  }
 }
 foreach i [timers] {
  if {[xindex $i 1] == "game_update"} {
   killtimer [xindex $i 2]
  }
 }
 if {$arg} {
  foreach i [timers] {
   if {[xindex $i 1] == "game_timeout"} {
    killtimer [xindex $i 2]
   }
  }
 }
}
putlog "\[4x]: Entertainment, Games. Loaded."