set tcqdb "eggdrop/scripts/ccnaquestions.txt"

set questionfiles "$tcqdb&default
eggdrop/scripts/demoquestions.txt&demo
eggdrop/scripts//ccnaquestions.txt&CCNA"

set tcchoosecmd "!choosedsss"
set tccommandlist $tcchoosecmd$
set tcflagschoose "-|-"
set tcqdbsep "|"
set tcmultiplesep "$"
set tcmultifact 2
set tccheat "ON"
set tcqdbquestionfirst 0
set tcscf "eggdrop/scripts/trivia.scores"
set tcerrmethod 0
set tcerrfil "eggdrop/scripts/trivia.errors"
set tcerremail "Krishna.82@hotmail.com"
set tcerrmailtmp "/tmp"
set tchtmlfile "apn/SSN/mychan.html"
set tchtmlrefresh 0
set tchtmlfont "verdana,helvetica,arial"
set tcchan "#Lombok"
set tcpointsperanswer 40
set tcmaxhint 3
set tcalwaysshowq 0
set tccapsquestion 0
set tccapsanswer 0
set tccapshint 0
set tcstreakmin 0
set tcmaxmissed 3
set tchintchar "*"
set tctimehint 20
set tctimenext 5
set tccongrats [list "Congratulations" "Well done" "Nice going" "Way to go" "You got it" "That's the way" "Show 'em how it's done" "Check out the big brain on"]
set tcnobodygotit [list "Nobody got it right." "Hello? Anybody home?" "You're going to have to try harder!" "Are these too tough for you?" "Am I alone here or what?" "You're not going to score any points this way!"]
set tctrythenextone [list "Let's see if you can get the next one..." "Get ready for the next one..." "Maybe you'll get the next one..." "Try and get the next one..." "Here comes the next one..."]
set tctimeanswer 1
set tcshowanswer 1
set tcshowallscores 0
set tcusebold 0
set tcpriv2msg 0
set tccmdhelp "?"
set tccmdstart "!startTTTTTTTTTTTT"
set tcflagsstart "-|-"
set tccmdnostop "!nostopppppppp"
set tcflagsnostop "o|o"
set tccmdhint "!hintsssss"
set tcflagshint "-|-"
set tctempnohint 10
set tccmdskip "!skip"
set tcflagsskip "o|o"
set tccmdtop10 "!top10ssssssssss"
set tcflagnostop10 "-|-"
set tccmdreset "resetssssss"
set tcflagsreset "m|m"
set tcresetreqpw 1
set tcrules "No advertising, no profanity, no harassing of users, no active scripts and no flooding. Break the rules and expect to be banned. Have fun. :-)"
set tcerrremindtime 15

#  COLOURS
#  The colour codes used are the same as those used by mIRC:
#  00:white        01:black        02:dark blue    03:dark green
#  04:red          05:brown        06:purple       07:orange
#  08:yellow       09:light green  10:turquoise    11:cyan
#  12:light blue   13:magenta      14:dark grey    15:light grey
#
#  Always specify colour codes as two digits, i.e. use "01" for
#  black, not "1".
#  You can specify a background colour using "00,04" (white text
#  on red background).
#  To disable a colour, use "".
#  Note that disabling some colours but not others may yield
#  unexpected results.

set tccolourstart "03"          ;#Game has started.
set tccolournostop "04"         ;#Game has nostopped.
set tccolourskip "10"           ;#Question has been skipped.
set tccolourerr "04"            ;#How to report errors.
set tccolourmiss "10"           ;#Nobody answered the question.
set tccolourqhead "04"          ;#Question heading.
set tccolourqbody "12"          ;#Question text
set tccolourhint "03"           ;#Hint.
set tccolourstrk "12"           ;#Person is on a winning streak.
set tccolourscr1 "04"           ;#Score of person in first place.
set tccolourscr2 "12"           ;#Score of person in second place.
set tccolourscr3 "03"           ;#Score of person in third place.
set tccolourrset "04"           ;#Scores have been reset.
set tccolourstend "12"          ;#Winning streak ended.
set tccolourmisc1 "06"          ;#Miscellaneous colour #1.
set tccolourmisc2 "04"          ;#Miscellaneous colour #2.
set tccolourmpca "00,04"        ;#Color of multiplechoice answer code

set tcwrngansw " "


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#                                                                   #
#    Any editing done beyond this point is done at your own risk!   #
#                                                                   #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#Misc checks & var initialisations
set tcver "1.3.4"
set tcrel "release"
if {[info tclversion]<8.2} {
        putlog "\002[file tail [info script]]\002 failed to load: in order to use this script, eggdrop needs to be compiled to use tcl 8.2 or higher (recommended: latest stable version)."
        return
}
if {$tctimeanswer==1&&[info tclversion]<8.3} {
        putlog "\002[file tail [info script]]\002 warning: timing of answers has been automatically disabled. this feature requires tcl 8.3 or higher."
        set tctimeanswer 0
}
if {![info exists alltools_loaded]||$allt_version<205} {
        putlog "\002[file tail [info script]]\002 failed to load: please load alltools.tcl v1.14 or higher (available with eggdrop 1.6.13 or higher) before attempting to load this script."
        return
}
if {[utimerexists tchtml]!=""} {killutimer $tchtmlrefreshtimer}
if {$tchtmlrefresh>0} {
        set tchtmlrefreshtimer [utimer $tchtmlrefresh tchtml]
}
if {![file exists $tcqdb]} {
        putlog "\002[file tail [info script]]\002 failed to load: $tcqdb does not exist."
        return
}
if {[llength [split $tcchan]]!=1} {
        putlog "\002[file tail [info script]]\002 failed to load: too many channels specified."
        return
}
if {![info exists tcplaying]} {
        set ctcp-version "${ctcp-version} Triviaplus.tcl M.C.-questions and categories. Based upon $tcver ($tcrel) from www.eggdrop.za.net)"
        set tcplaying 0
}
if {![info exists tchintnum]} {set tchintnum 0}
if {![info exists tcmissed]} {set tcmissed 0}

#Binds
bind pubm $tcflagsstart "$tcchan %$tccmdstart" tcstart
bind pubm $tcflagsnostop "$tcchan %$tccmdnostop" tcnostop
proc tcbindhintcmd {} {
        global tcflagshint tccmdhint
        bind pubm $tcflagshint "$::tcchan %$tccmdhint" tcforcehint
}
proc tcunbindhintcmd {} {
        global tcflagshint tccmdhint
        unbind pubm $tcflagshint "$::tcchan %$tccmdhint" tcforcehint
}
tcbindhintcmd
bind pubm $tcflagsskip "$tcchan %$tccmdskip" tcskip
bind pubm $tcflagnostop10 "$tcchan %$tccmdtop10" tcshowtop10
bind join -|- "$tcchan *" tcjoinmsg
bind msg - $tccmdhelp tcgivehelp
bind kick - "$tcchan $botnick" tcbotgotkicked
bind evnt - disconnect-server tcbotgotdisconnected


#this is the new choose command
bind pubm $tcflagschoose "$tcchan %$tcchoosecmd" give_choicenew
proc give_choicenew {nick uhost handle channel args} {
 global questions tcplaying tcqdb quest usedq questionno questionfiles gameplaying gameonoff chan tcquestionstotal tctotalquestfiles tcquestionfiles tcset_catgentrig tcchoosecmd
  if {$tcplaying != 0} {
  putserv "PRIVMSG $channel :Game is already running, please either wait for it to finish or enter !nostop (if ur autorized) then $tcchoosecmd again to set category"
        return
}
        set questionno 0
        set quest ""
        set usedq ""
        set tcset_catgentrig ""
set tcquestionfiles [split $questionfiles]
set tctotalquestfiles [llength $questionfiles]
                set i 0
                        while {$i<$tctotalquestfiles} {
                        set tcfilename [lindex [split [lindex $tcquestionfiles $i] &] 0]
                        set tcdesc [lindex [split [lindex $tcquestionfiles $i] &] 1]
                        if {$tcqdb ==$tcfilename} {tcgamemsg "[tccolqbody] Current category:\00304$tcdesc[tccolqbody] with \00304$tcquestionstotal [tccolqbody]questions"}
                        incr i
                        }
                        tcgamemsg "[tccolqbody] Current \00304$tctotalquestfiles\003 [tccolqbody]categories available."
                        set i 0
                        set qfile ""
                        while {$i<$tctotalquestfiles} {
                        set tcfilename [lindex [split [lindex $tcquestionfiles $i] &] 0]
                        set tcdesc [lindex [split [lindex $tcquestionfiles $i] &] 1]
                        set qfile [open $tcfilename r]
                        set tclist [split [read -nonewline $qfile] "\n"]
                        set tcquestioncount [llength $tclist]
                        close $qfile
                        incr i
                        tcgamemsg "\00304$tcdesc[tccolqbody] with \00304$tcquestioncount\003 [tccolqbody]questions."
                                bind pub -|- $tcdesc set_catgen
                                lappend tcset_catgentrig $tcdesc
                        }
                        tcgamemsg "[tccolqbody] Which category u want to use?"
}

#set category
proc set_catgen {nick uhost handle channel text} {
 global tcqdb tctotalquestfiles tcquestionfiles tcdesc lastbind tcset_catgentrig tccmdstart
                        set i 0
                        while {$i<$tctotalquestfiles} {
                        set tcfilename [lindex [split [lindex $tcquestionfiles $i] &] 0]
                        set tcdesc [lindex [split [lindex $tcquestionfiles $i] &] 1]
                        if {[strlwr $tcdesc] != [tcstripcodes [strlwr [string trim $lastbind]]]} {
                        incr i
                        }
                        if {[strlwr $tcdesc] == [tcstripcodes [strlwr [string trim $lastbind]]]} {
                        tcgamemsg "[tccolqbody] Current category set to: \00304$lastbind"
                        tcgamemsg "[tccolqbody] Use \00304$tccmdstart[tccolqbody] to start the game!"
                        set tcqdb $tcfilename
                                                set i 0
                                                while {$i<$tctotalquestfiles} {
                                                unbind pub -|- [lindex $tcset_catgentrig $i] set_catgen
                                                incr i
                                                }
                        return
                        }
        }
}
###############################################################################
#starts the game if it isn't running.
proc tcstart {nick host hand chan text} {
        global tcplaying tcstreak tcchan tcerrremindtime tcerrremindtimer tcmissed tcchoosecmd tccheat
        if {[strlwr $tcchan]==[strlwr $chan]} {
                if {$tcplaying==0} {
                if {$tccheat != "ON"} {set tccheat "OFF"}
                        tcgamemsg "[tccolstart]Multple game started by $nick! For an other category use: $tcchoosecmd [tccolstart]The cheat protection is \00304$tccheat"
                        tcnextq
                        set tcplaying 1
                        set tcstreak 0
                        set tcmissed 0
                        set tcerrremindtimer [timer $tcerrremindtime tcerrremind]
                }
        }
}


#nostops the game if it's running.
proc tcnostop {nick host hand chan text} {
        global tchinttimer tcnextqtimer tcplaying tcchan tccurrentanswer tcstreak tcstreakmin
        global tcerrremindtimer tcrebindhinttimer
        if {[strlwr $tcchan]==[strlwr $chan]} {
                if {$tcplaying==1} {
                        tcgamemsg "[tccolnostop]Multiple game nostopped by $nick!"
                        if {$tcstreakmin>0&&[lindex [split $tcstreak ,] 1]>=$tcstreakmin} { tcstreakend }
                        set tcstreak 0
                        set tcplaying 0
                        catch {unbind pubm -|- "$tcchan *" tccheckanswer}
                        if {[utimerexists tchint]!=""} {killutimer $tchinttimer}
                        if {[utimerexists tcnextq]!=""} {killutimer $tcnextqtimer}
                        if {[timerexists tcerrremind]!=""} {killtimer $tcerrremindtimer}
                        if {[utimerexists tcrebindhinttimer]!=""} {killtimer $tcrebindhinttimer}
                }
        }
}

#gives a hint if there is currently a question to answer.
proc tcforcehint {nick host hand chan text} {
        global tchinttimer tcnextqtimer tcplaying tcchan tccurrentanswer tcstreak tcstreakmin
        global tctempnohint tcmaxhintcurrent tchintnum tcrebindhinttimer tctempnohint
        if {[strlwr $tcchan]==[strlwr $chan]} {
                if {$tcplaying==1&&[utimerexists tchint]!=""} {
                        killutimer $tchinttimer
                        tchint
                        tcunbindhintcmd
                        if {$tchintnum<$tcmaxhintcurrent} {
                                set tcrebindhinttimer [utimer $tctempnohint tcbindhintcmd]
                        }
                }
        }
}

#skips the current question if one has been asked.
proc tcskip {nick host hand chan text} {
        global tchinttimer tcnextqtimer tcplaying tcchan tccurrentanswer tcstreak
        global tcstreakmin tctimenext tcrebindhinttimer
        global KAOSAnswers KAOSNumAnswered KAOSQuestionTimer KAOSAdTimer
        if {[strlwr $tcchan]==[strlwr $chan]} {
                if {$tcplaying==1&&[utimerexists tchint]!=""} {
                        tcgamemsg "\00306Skipping to next question by \00302$nick's\00306 request..."
                        if {$tcstreakmin>0&&[lindex [split $tcstreak ,] 1]>=$tcstreakmin&&[strlwr [lindex [split $tcstreak ,] 0]]==[strlwr $nick]} {
                                tcstreakend
                                set tcstreak 0
                        }
                        catch {unbind pubm -|- "$tcchan *" tccheckanswer}
                        killutimer $tchinttimer
                        if {[utimerexists tcrebindhinttimer]!=""} {killtimer $tcrebindhinttimer}
                        KAOSAskQuestion
                }
        }
}

#reminds channel how to report errors in questions/answers
proc tcerrremind {} {
        global tcerrremindtimer tcerrremindtime botnick tccmderror
        tcgamemsg "[tccolerr]Remember: to report errors in questions/answers, type /msg $botnick $tccmderror <number> \[description\]"
        set tcerrremindtimer [timer $tcerrremindtime tcerrremind]
}

#bot got kicked. nostop the game.
proc tcbotgotkicked {nick host hand chan targ text} {
        tcquietnostop
}

#bot got disconnected. nostop the game.
proc tcbotgotdisconnected {disconnect-server} {
        tcquietnostop
}

#nostops the game without telling the channel.
proc tcquietnostop {} {
        global tcplaying tcstreak tcchan tccurrentanswer tchinttimer tcnextqtimer tcerrremindtimer
        global tcrebindhinttimer
        if {$tcplaying==1} {
                set tcstreak 0
                set tcplaying 0
                catch {unbind pubm -|- "$tcchan *" tccheckanswer}
                if {[utimerexists tchint]!=""} {killutimer $tchinttimer}
                if {[utimerexists tcnextq]!=""} {killutimer $tcnextqtimer}
                if {[timerexists tcerrremind]!=""} {killtimer $tcerrremindtimer}
                if {[utimerexists tcrebindhinttimer]!=""} {killtimer $tcrebindhinttimer}
        }
}

#reads the question database.
proc tcreadqdb {} {
        global tcqdb tcquestionstotal tcquestionslist
        set tcquestionstotal 0
        set tcquestionslist ""
        set qfile [open $tcqdb r]
        set tcquestionslist [split [read -nonewline $qfile] "\n"]
        set tcquestionstotal [llength $tcquestionslist]
        close $qfile
}

#selects the next question.
proc tcnextq {} {
        global tcqdb tccurrentquestion tccurrentanswer tcquestionnumber
        global tcquestionstotal tchintnum tcchan tcquestionslist tcqdbsep tcqdbquestionfirst
        global tccapsquestion tccapsanswer
        tcreadqdb
        set tccurrentquestion ""
        set tccurrentanswer ""
        while {$tccurrentquestion == ""} {
                set tcquestionnumber [rand [llength $tcquestionslist]]
                set tcquestionselected [lindex $tcquestionslist $tcquestionnumber]
#Striping all the *mess* leaving alphanumeric.
                set tcquestionselected [tcstripcodes $tcquestionselected]

                set tccurrentquestion [lindex [split $tcquestionselected $tcqdbsep] [expr $tcqdbquestionfirst^1]]
                if {$tccapsquestion==1} {
                        set tccurrentquestion [strupr $tccurrentquestion]
                }
                set tccurrentanswer [string trim [lindex [split $tcquestionselected $tcqdbsep] $tcqdbquestionfirst]]
                if {$tccapsanswer==1} {
                        set tccurrentanswer [strupr $tccurrentanswer]
                }
        }
        unset tchintnum
        tchint
        bind pubm -|- "$tcchan *" tccheckanswer
        return
}

#shows timed hints.
proc tchint {} {
        global tcmaxhint tchintnum tccurrentanswer tchinttimer tcchan tcmultiplesep tcmultifact
        global tctimehint tchintchar tcquestionnumber tcquestionstotal
        global tccurrentquestion tchintcharsused tcnextqtimer tctimenext tcstreak tcstreakmin
        global tcnobodygotit tctrythenextone tcmissed tcmaxmissed tccmdstart tcshowanswer
        global tctimestart tctimeanswer tcalwaysshowq tcmaxhintcurrent tctempnohint tccapshint tcwrngansw
        global KAOSAnswers KAOSNumAnswered KAOSQuestionTimer KAOSAdTimer


        if {[catch {incr tchintnum}]!=0} {
                set tchintnum 0
                regsub -all -- "\[^A-Za-z0-9\]" $tccurrentanswer "" _hintchars
                set tcmaxhintcurrent [expr [strlen $_hintchars]<=$tcmaxhint?[expr [strlen $_hintchars]-1]:$tcmaxhint]
                catch {tcunbindhintcmd}
                if {$tcmaxhintcurrent>0} {
                        set tcrebindhinttimer [utimer $tctempnohint tcbindhintcmd]
                }
        }
        if {$tchintnum >= [expr $tcmaxhintcurrent+1]} {
                incr tcmissed
                set _msg ""
                append _msg "\0032[lindex $tcnobodygotit [rand [llength $tcnobodygotit]]]"
                if {$tcshowanswer==1} {
                        append _msg " The answer was \0036$tccurrentanswer"
                }
                if {$tcmaxmissed>0&&$tcmissed>=$tcmaxmissed} {
                        append _msg " That's $tcmissed questions gone by unanswered! The game is now automatically disabled. To start the game again, type $tccmdstart"
                        tcquietnostop
                } else {
                        append _msg " \0032[lindex $tctrythenextone [rand [llength $tctrythenextone]]]"
                }
                tcgamemsg "[tccolmiss]$_msg"
                if {$tcstreakmin>0&&[lindex [split $tcstreak ,] 1]>=$tcstreakmin} { tcstreakend }
                set tcstreak 0
                catch {unbind pubm -|- "$tcchan *" tccheckanswer}
                if {$tcmaxmissed==0||$tcmissed<$tcmaxmissed} {
                        KAOSAskQuestion
                }
                return
        } elseif {$tchintnum == 0} {
                set i 0
                set _hint {}
                set tchintcharsused {}
                foreach word [split $tccurrentanswer] {
                        regsub -all -- "\[A-Za-z0-9\]" $word $tchintchar _current
                        lappend _hint $_current
                }
                if {$tctimeanswer==1} {
                        set tctimestart [clock clicks -milliseconds]
                }
        } elseif {$tchintnum == 1} {
                set i 0
                set _hint {}
                while {$i<[llength [split $tccurrentanswer]]} {
                        set _word [lindex [split $tccurrentanswer] $i]
                        set j 0
                        set _newword {}
                        while {$j<[strlen $_word]} {
                                if {$j==0} {
                                        append _newword [stridx $_word $j]
                                        lappend tchintcharsused $i,$j
                                } else {
                                        if {[string is alnum [stridx $_word $j]]} {
                                                append _newword $tchintchar
                                        } else {
                                                append _newword [stridx $_word $j]
                                                lappend tchintcharsused $i,$j
                                        }
                                }
                                incr j
                        }
                        lappend _hint $_newword
                        incr i
                }
                } else {
                        set i 0
                        set _hint {}
                        while {$i<[llength [split $tccurrentanswer]]} {
                                set _word [lindex [split $tccurrentanswer] $i]
                                set j 0
                                set _newword {}
                                set _selected [rand [strlen $_word]]
                                regsub -all -- "\[^A-Za-z0-9\]" $_word "" _wordalnum
                                if {[strlen $_wordalnum]>=$tchintnum} {
                                        while {[lsearch $tchintcharsused $i,$_selected]!=-1||[string is alnum [stridx $_word $_selected]]==0} {
                                         set _selected [rand [strlen $_word]]
                                        }
                                }
                                lappend tchintcharsused $i,$_selected
                                while {$j<[strlen $_word]} {
                                        if {[lsearch $tchintcharsused $i,$j]!=-1||[string is alnum [stridx $_word $j]]==0} {
                                                append _newword [stridx $_word $j]
                                        } else {
                                                if {[string is alnum [stridx $_word $j]]} {
                                                        append _newword $tchintchar
                                                }
                                }
                                incr j
                        }
                        lappend _hint $_newword
                        incr i
                }
        }
        if {$tccapshint==1} {
                set _hint [strupr $_hint]
        }
        tcgamemsg "\0030,2 :: Question no. $tcquestionnumber\/$tcquestionstotal \0038\[\00311 ŽMultiple ChoiseŽ\0038 \] \0030tpi@DALnet :: \003"
# reseting wronganswer counter
# tcgamemsg "resetting wrong answer counter for the hint"
        set tcwrngansw ""
#Detection of a multiplechoice question
set tcmultiple 0
set tcmultiquestion [split $tccurrentquestion $tcmultiplesep]
set tcmultiple [llength $tcmultiquestion]
#Normal question
if {$tcmultiple==0} {
if {$tcalwaysshowq==1||$tchintnum==0} {

                tcgamemsg "\0036 $tccurrentquestion geen multiple"
        }
}
#Detected an multiplechoice
if {$tcmultiple!=0} {
if {$tcalwaysshowq==1||$tchintnum==0} {
                set i 0
                        while {$i<$tcmultiple} {
                        if {$i==0} {
                        tcgamemsg "\0032[lindex $tcmultiquestion $i]"
                        }
                        if {$i!=0} {
                        set tcanswername [strupr [string range [lindex $tcmultiquestion $i] 0 0]]
                        set tcanswertxt [string range [lindex $tcmultiquestion $i] 2 end]
                        tcgamemsg "\0032\037\002 $tcanswername \002\037 : $tcanswertxt\003"
                        }
                        incr i
                        }
        }
}
        tcgamemsg "\0030,2 :: \00311+40 \0030Points Bila Anda Menjawab Dengan Benar :: \037-= GudLuck =-\037 :: "
        if {$tcmultiple ==0 } { set tchinttimer [utimer $tctimehint tchint]}
        if {$tcmultiple !=0 } { set tchinttimer [utimer [expr $tctimehint+$i] tchint]}
}

#checks if anyone has said the correct answer on channel.
proc tccheckanswer {nick host hand chan text} {
        global tccurrentanswer tcwrngansw tccommandlist tccheat
#cheat protection on
                if {$tccheat == "ON"} {
                if {[strlwr $tccurrentanswer] != [tcstripcodes [strlwr [string trim $text]]]} {

                if { ([string length $text] == [string length $tccurrentanswer]) || [string first $text $tccommandlist] != -1} {
                set firstone [lsearch $tcwrngansw $nick]
                if {$firstone ==-1} {lappend tcwrngansw $nick}
           if {$firstone !=-1} {tcgamemsg "\0036 $nick, \0032ga bisa jawab dua kali. tunggu soal berikutnya ok!"}
                }
        }

        if {[strlwr $tccurrentanswer] == [tcstripcodes [strlwr [string trim $text]]]} {
                        set firstone [lsearch $tcwrngansw $nick]
                if {$firstone ==-1} {
                tccorrectanswer $nick
                        }
                if {$firstone !=-1} {tcgamemsg "\0036 $nick, \0032jangan asal nebak jawabannya, Tadi kan udah salah jawab"}
                }
                return
        }

#cheatprotection is not ON
                if {[strlwr $tccurrentanswer] == [tcstripcodes [strlwr [string trim $text]]]} {
                tccorrectanswer $nick
                }
}

#triggered when someone says the correct answer.
proc tccorrectanswer {nick} {
        global tccurrentanswer tchinttimer tctimenext tcchan tcnextqtimer tcstreak tcstreakmin
        global tccongrats tcmissed
        global tctimestart tcrealnames tcscoresbyrank tctimeanswer tcpointsperanswer tcwrngansw
        global KAOSAnswers KAOSNumAnswered KAOSQuestionTimer KAOSAdTimer
        global lastwinner lastwinnercount botnick userlist quizconf rankfile timerankreset

    variable bestscore 0 lastbestscore 0 lastbest ""
    variable userarray
    variable waitforrank 0 gameend 0

    mx_getcreate_userentry $nick $nick
    array set userarray $userlist($nick)

            set lastbest [lindex [lsort -command mx_sortrank [array names userlist]] 0]
            if {$lastbest == ""} {
                set lastbestscore 0
            } else {
                array set aa $userlist($lastbest)
                set lastbestscore $aa(score)
            }
            incr userarray(score) $tcpointsperanswer
            if {$userarray(score) == 1} {
                set userarray(started) [unixtime]
            }
            set userlist($nick) [array get userarray]

        set tcwrngansw ""
        set _timetoanswer ""
        if {$tctimeanswer==1} {
                set _timetoanswer [expr [expr [clock clicks -milliseconds]-$tctimestart]/1000.00]
        }
        set _msg "\0032[lindex $tccongrats [rand [llength $tccongrats]]] :\0036 $tccurrentanswer \0032Oleh\0036 $nick \0032setelah\0036 $_timetoanswer \0032Detik - Score: \0036+30 \0032Points. Total Score:\0036 $userarray(score) \0032Points - Rank:\0036 [mx_get_rank_pos $nick]"
        tcgamemsg "$_msg"
        pushmode $tcchan +v $nick
        if {$tcstreak!=0} {
                if {[lindex [split $tcstreak ,] 0]==[strlwr $nick]} {
                        set tcstreak [strlwr $nick],[expr [lindex [split $tcstreak ,] 1]+1]
                        if {$tcstreakmin>0&&[lindex [split $tcstreak ,] 1]>=$tcstreakmin} {
                                tcgamemsg "[tccolstrk][tccolmisc2]$nick[tccolstrk] is on a winning streak! [tccolmisc2][lindex [split $tcstreak ,] 1] [tccolstrk]in a row so far!"
                        }
                } else {
                        if {$tcstreakmin>0&&[lindex [split $tcstreak ,] 1]>=$tcstreakmin} { tcstreakend }
                        set tcstreak [strlwr $nick],1
                }
        } else {
                set tcstreak [strlwr $nick],1
        }
        set tcmissed 0
        catch {unbind pubm -|- "$tcchan *" tccheckanswer}
        killutimer $tchinttimer
        tmcquiz_rank_save {} {} {}
        KAOSAskQuestion
}

#triggered when someone joins trivia chan.
proc tcjoinmsg {nick host hand chan} {
        global botnick tcplaying tccmdhelp tccmdstart tcflagsstart tccmdnostop tcflagsnostop tcchan
        global KAOSRunning
        if {$nick != $botnick} {
                set _msg ""
                append _msg ""
                if {$KAOSRunning==1} {
                        append _msg ""
                } else {
                        append _msg ""
                }
                if {[matchattr $hand $tcflagsstart $tcchan]&&$KAOSRunning==0} {
                        append _msg " "
                }
                append _msg ""
                [tcpriv] $nick "$_msg"
        }
}

# Returns text without colour, bold, etc. control codes.
# This is a stripped down version of the proc in MC_8's mc.moretools.tcl.
proc tcstripcodes {text} {
        regsub -all -- "\003(\[0-9\]\[0-9\]?(,\[0-9\]\[0-9\]?)?)?" $text "" text
#stripping tabs as well
        regsub -all -- "\t" $text " " text
        set text "[string map -nocase [list \002 "" \017 "" \026 "" \037 ""] $text]"
        return $text
}

proc tcgamemsg {what} {
        global tcchan

        putquick "PRIVMSG $tcchan :[tcbold]$what"
}

proc tcbold {} {
        global tcusebold
        if {$tcusebold==1} { return "\002" }
}
proc tccolstart {} {
        global tccolourstart
        if {$tccolourstart!=""} { return "\003$tccolourstart" }
}
proc tccolnostop {} {
        global tccolournostop
        if {$tccolournostop!=""} { return "\003$tccolournostop" }
}
proc tccolskip {} {
        global tccolourskip
        if {$tccolourskip!=""} { return "\003$tccolourskip" }
}
proc tccolerr {} {
        global tccolourerr
        if {$tccolourerr!=""} { return "\003$tccolourerr" }
}
proc tccolmiss {} {
        global tccolourmiss
        if {$tccolourmiss!=""} { return "\003$tccolourmiss" }
}
proc tccolqhead {} {
        global tccolourqhead
        if {$tccolourqhead!=""} { return "\003$tccolourqhead" }
}
proc tccolqbody {} {
        global tccolourqbody
        if {$tccolourqbody!=""} { return "\003$tccolourqbody" }
}
proc tccolhint {} {
        global tccolourhint
        if {$tccolourhint!=""} { return "\003$tccolourhint" }
}
proc tccolstrk {} {
        global tccolourstrk
        if {$tccolourstrk!=""} { return "\003$tccolourstrk" }
}
proc tccolscr1 {} {
        global tccolourscr1
        if {$tccolourscr1!=""} { return "\003$tccolourscr1" }
}
proc tccolscr2 {} {
        global tccolourscr2
        if {$tccolourscr2!=""} { return "\003$tccolourscr2" }
}
proc tccolscr3 {} {
        global tccolourscr3
        if {$tccolourscr3!=""} { return "\003$tccolourscr3" }
}
proc tccolrset {} {
        global tccolourrset
        if {$tccolourrset!=""} { return "\003$tccolourrset" }
}
proc tccolstend {} {
        global tccolourstend
        if {$tccolourstend!=""} { return "\003$tccolourstend" }
}
proc tccolmisc1 {} {
        global tccolourmisc1
        if {$tccolourmisc1!=""} { return "\003$tccolourmisc1" }
}
proc tccolmisc2 {} {
        global tccolourmisc2
        if {$tccolourmisc2!=""} { return "\003$tccolourmisc2" }
}
proc tcpriv {} {
        global tcpriv2msg
        if {$tcpriv2msg==1} { return "putmsg" } else { return "putnotc" }
}
proc tccolmpca {} {
        global tccolourmpca
        if {$tccolourmpca!=""} {return "\003$tccolourmpca" }
}

#this generates an html file with all the people on the chan with
#their score, as well as a list of all scores, sorted by rank
proc tchtml {} {
        global tcchan botnick tchtmlfile tchtmlrefresh server tcscoresbyname tcranksbyname
        global tcscorestotal tcranksbyname tcrealnames tcscoresbyrank tcranksbynum tcplaying
        global tcquestionstotal tchtmlrefreshtimer tchtmlfont
        tcgetscores
        tcreadqdb
        set _file [open $tchtmlfile~new w]
        puts $_file "<!DOCTYPE HTML PUBLIC \"-/W3C/DTD HTML 4.01 Transitional/EN\">"
        puts $_file "<html>"
        puts $_file " <head>"
        puts $_file "  <title>$botnick's trivia channel: $tcchan on [lindex [split $server :] 0]</title>"
        puts $_file "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
        puts $_file "  <meta http-equiv=\"refresh\" content=\"$tchtmlrefresh\">"
        puts $_file "  <meta name=\"generator\" content=\"trivia.tcl script for eggdrop. http://www.eggdrop.za.net/\">"
        puts $_file "  <style type=\"text/css\">"
        puts $_file "  <!--"
        puts $_file "  body,td{font-family:$tchtmlfont;font-size:13px;}"
        puts $_file "  a{text-decoration:none;color:#09f;}"
        puts $_file "  -->"
        puts $_file "  </style>"
        puts $_file " </head>"
        puts $_file " <body>"
        puts $_file "  <h1>$tcchan on [lindex [split $server :] 0]</h1>"
        puts $_file "  <hr size=\"1\" noshade>"
        if {![onchan $botnick $tcchan]} {
                puts $_file "  <p>Hmmm... for some reason I'm not on $tcchan at the moment. Please try again later.</p>"
        } else {
                puts $_file "  <p>Trivia game is currently <b>[expr $tcplaying==1?"on":"off"]</b>. There are <b>$tcquestionstotal</b> questions in the database."
                puts $_file "  <p>People on $tcchan right now:<br>"
                puts $_file "  <table width=\"50%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><table width=\"100%\" cellspacing=\"3\" border=\"0\">"
                puts $_file "   <tr>"
                puts $_file "    <td><b>Nick</b></td>"
                puts $_file "    <td><b>Score</b></td>"
                puts $_file "    <td><b>Rank</b></td>"
                puts $_file "    <td><b>Idle</b></td>"
                puts $_file "   </tr>"
                foreach nick [lsort [chanlist $tcchan]] {
                        puts $_file "   <tr>"
                        puts $_file "    <td>[expr [isop $nick $tcchan]?"@":""][expr [isvoice $nick $tcchan]?"+":""]$nick[expr [string match $nick $botnick]?" (that's me!)":""]</td>"
                        if {[info exists tcscoresbyname([strlwr $nick])]} {
                                puts $_file "    <td>$tcscoresbyname([strlwr $nick])</td>"
                        } else {
                                puts $_file "    <td>-</td>"
                        }
                        if {[info exists tcranksbyname([strlwr $nick])]} {
                                puts $_file "    <td>$tcranksbyname([strlwr $nick])</td>"
                        } else {
                                puts $_file "    <td>-</td>"
                        }
                        puts $_file "   <td>[expr [getchanidle $nick $tcchan]>10?"[getchanidle $nick $tcchan]m":"-"]</td>"
                        puts $_file "   </tr>"
                }
                puts $_file "  </table></td></tr></table>"
        }
        if {$tcscorestotal>0} {
                puts $_file "  <p><small>There [expr $tcscorestotal==1?"is":"are"] currently <b>$tcscorestotal</b> [expr $tcscorestotal==1?"nick":"nicks"] in the score table:<br>"
                set _rank 1
                while {$_rank<=$tcscorestotal} {
                        puts $_file "  <b>$_rank</b>. $tcrealnames($tcranksbynum($_rank)) $tcscoresbyrank($_rank)<br>"
                        incr _rank
                }
        } else {
                puts $_file "  <p><small>There are currently no nicks in the score table.<br>"
        }
        puts $_file "  </small></p>"
        puts $_file "  <hr size=\"1\" noshade>"
        puts $_file "  <small>Generated on [strftime %A,\ %d\ %B\ %Y\ @\ %H:%M:%S] by <a href=\"http://www.eggdrop.za.net/\">trivia.tcl</a> for <a href=\"http://www.eggheads.org \">eggdrop</a>.<br>"
        puts $_file "  This page is automatically updated (and refreshed if supported by your browser) every [expr $tchtmlrefresh==1?"second":"$tchtmlrefresh seconds"].</small>"
        puts $_file " </body>"
        puts $_file "</html>"
        close $_file
        file rename -force $tchtmlfile~new $tchtmlfile
        set tchtmlrefreshtimer [utimer $tchtmlrefresh tchtml]
}

putlog "Multiple Choise tpi SuccessFully Loaded"

