#10in1.tcl

set tgchan					"#MoveOn"
set tgchanname				"MoveOn"

set tground					"A1"
set tgqtype					"Multiple Choice"
set tgqdb					"move/multi.db"
set tgpointsperanswer		1

set tgscf					"move/quizz.scores"
set tgqdbsep				"#"
set tgmultiplesep			"^"
set tgkaossep				"*"

set tglagstart				7
set tgmaxmissed 			15000000
set tgmissed				0

set tgmaxhint				3
set tghintchar				"."
set tgtimehint				20

set tgcmdstart				"!start"
set tgflagsstart			"-|-"

set tgcmdstop				"!ayochatt"
set tgflagsstop				"-|-"

set tgcmdrank				"!rank*"
set tgflagsrank				"-|-"

set tgcmdscore				"!score*"
set tgflagsscore			"-|-"

set tgcmdreset				"!reset"
set tgflagsreset			"m|m"
set tgresetreqpw			1

set tgrunning				0
set tgplaying				0

set tgqanswer [list "Keren" "Cerdik" "Cakep" "Sip" "Oke" "Benar" "Betul" "Tepat" "Seep" "Pinter" "Mantap" "Oce" "Yup" "Yap" "Yess" "Yoi" "Oyee" "Waw" "Begh"]
set tgcongrats [list "Hebat bener" "Wow keren" "Mantap kali" "Keren abis" "Cakep nian" "Wew ebad" "Ck ck ck" "Canggih euy" "Beuh jenius" "Wew pinter" "Aih hebat" "Pinter ih"]
set tgnobodygotit [list "Euh payah!" "Payah nich!" "Sayang waktu telah habis!" "Cape deh!" "Gak tahu ya!" "Lewat dah!" "Telat deh!" "Telmi yee!" "Yah telat!" "Gak pinter ah!" "Pada gak skull ya!" "Masa gak bisa!" "Ndak cerdik ah!" "Skull gak sih!" "Kok gak dijawab!" "Yah lewat lagi!" "Hapalin dong!" "Gak belajar sih!" "Yee pada bengong!" "Kok gak ada yang tahu!"]
set tgwrongmsg [list "Sabar dong!" "Kan udah jawab tadi!" "Kok jawabannya diulang!" "Tunggu oiii!" "Kalo udah salah jawab, tunggu!" "Oiii sabar!" "Kasih kesempatan buat yang lain dong!" "Kan udah salah jawab tadi!" "Makanya jangan buru-buru!" "Kamu salah tebak sih!" "Tenang-tenang!" "Jawaban gak boleh diulang!" "Hanya boleh satu kali jawab!"]
set tglevel [list "Presiden" "Wakil Presiden" "Menteri" "Gubernur" "Walikota" "Bupati" "Camat" "Lurah" "RW" "RT"]

set tgf100nick [list]
set tgf100answ [list]

set tgadvs {
"Selamat Bermain Game 10in1."
"Game 10in1 edit by Crew #MoveOn!"
"Dilarang menggunakan nick, ident kotor!"
"No spamming, flooding, clones, cheating and inviting!"
"Remove any advertisements from your quit and part messages!"
"No offending, racism, harassments, abuses or discrimination!"
"Commands: !kediri !rank !rank <rank> !score !score <nick>"
"10Ketik 6!rank 10untuk melihat rangking"
"10Ketik 6!score 10untuk melihat score"
"10Ketik 6!score <Nick> "
"10Be MasTer Game in Here :P~"
"10RULES6 No clones, cheating, exploiting of known weaknesses"
"10*** Channel mantap buat cari gebetan ***"
"Your score is accumulated and autosaved!"
"You will be banned if breaking the rule!"
"Congratulations for the Game Winners!"
"Have a nice chat and enjoy your stay here!"
"Wish you all the best for staying here!"
}

#Misc checks & var initialisations

bind pubm $tgflagsstart "$tgchan %$tgcmdstart" tgstart
bind pubm $tgflagsstop "$tgchan %$tgcmdstop" tgstop
bind pubm $tgflagsrank "$tgchan %$tgcmdrank" tgshowrank
bind pubm $tgflagsscore "$tgchan %$tgcmdscore" tgshowscore
bind msg $tgflagsreset "$tgcmdreset" tgresetscores
bind kick - "$tgchan $botnick" tgbotgotkicked
bind evnt - disconnect-server tgbotgotdisconnected


# START 

proc tgstart {nick host hand chan text} {
	global tgplaying tgchan tghintnum tgwrngansw tglagfirststartid tglagstart tgmissed tgrunning

	if {$tgrunning==1} {return}
	if {[strlwr $tgchan]==[strlwr $chan]} {
		if {$tgplaying==0} {
			tggamemsg1 "6Game 10in1 dimulai oleh 10$nick6!"
			set tgmissed 0
			set tghintnum 0
			set tgplaying 1
			set tgwrngansw ""
			set tglagfirststartid [utimer $tglagstart tgnextq]
		}
		set tgrunning 1
		tggetscores
	}
}

proc tgmstart {} {
	global tgplaying  tghintnum tgqtype tgqdb tgpointsperanswer tgwrngansw tgmissed tgmaxmissed tground tgrunning
	global tgf100nick tgf100answ

	if {$tgmissed > $tgmaxmissed} {
		tggamemsg1 " 10 Tidak ada respon setelah 6 $tgmaxmissed10 pertanyaan6, Game 10in1 berhenti otomatis, ketik 10!start6 untuk memulai kembali."
		set tgrunning 0
		return
	}
	if {$tgplaying==0} {
		switch $tground {
			"A1" {
				set tground "A2"
				set tgqtype "Trivia"
				set tgqdb  "games/trivia.db"
				set tgpointsperanswer 2
			}
			"A2" {
				set tground "A3"
				set tgqtype "Family Feud"
				set tgqdb  "games/feud.db"
				set tgpointsperanswer 1
			}
			"A3" {
				set tground "A4"
				set tgqtype "Gosip Fakta"
				set tgqdb  "games/fact.db"
				set tgpointsperanswer 1
			}
			"A4" {
				set tground "A5"
				set tgqtype "Kamus"
				set tgqdb  "games/sunda.db"
				set tgpointsperanswer 2
			}
			"A5" {
				set tground "A6"
				set tgqtype "Word Game"
				set tgqdb  "games/word.db"
				set tgpointsperanswer 1
			}
			"A6" {
				set tground "A7"
				set tgqtype "Scramble"
				set tgqdb  "games/scramble.db"
				set tgpointsperanswer 3
			}
			"A7" {
				set tground "A8"
				set tgqtype "Conversation"
				set tgqdb  "games/conv.db"
				set tgpointsperanswer 3
			}
			"A8" {
				set tground "A9"
				set tgqtype "Family 100"
				set tgqdb  "games/feud.db"
				set tgpointsperanswer 3
				set tgf100nick [list]
				set tgf100answ [list]
			}
			"A9" {
				set tground "B1"
				set tgqtype "Multiple Choice"
				set tgqdb  "games/multi.db"
				set tgpointsperanswer 1
			}
			"B1" {
				set tground "B2"
				set tgqtype "English Trivia"
				set tgqdb  "games/triviaen.db"
				set tgpointsperanswer 3
			}
			"B2" {
				set tground "B3"
				set tgqtype "Family Feud"
				set tgqdb  "games/feud.db"
				set tgpointsperanswer 1
			}
			"B3" {
				set tground "B4"
				set tgqtype "True False"
				set tgqdb  "games/true.db"
				set tgpointsperanswer 1
			}
			"B4" {
				set tground "B5"
				set tgqtype "Kamus"
				set tgqdb  "games/sunda.db"
				set tgpointsperanswer 2
			}
			"B5" {
				set tground "B6"
				set tgqtype "Word Game"
				set tgqdb  "games/word.db"
				set tgpointsperanswer 1
			}
			"B6" {
				set tground "B7"
				set tgqtype "Scramble"
				set tgqdb  "games/scramble.db"
				set tgpointsperanswer 3
			}
			"B7" {
				set tground "B8"
				set tgqtype "Conversation"
				set tgqdb  "games/conv.db"
				set tgpointsperanswer 3
			}
			"B8" {
				set tground "B9"
				set tgqtype "Family 100"
				set tgqdb  "games/feud.db"
				set tgpointsperanswer 3
				set tgf100nick [list]
				set tgf100answ [list]
			}
			"B9" {
				set tground "A1"
				set tgqtype "Multiple Choice"
				set tgqdb  "games/multi.db"
				set tgpointsperanswer 1
			}
		}
		set tghintnum 0
		set tgplaying 1
		set tgwrngansw ""
		tgnextq
	}
}


# STOP

proc tgstop {nick host hand chan text} {
	global tghinttimer tgplaying tgchan tglagstartid tglagfirststartid tgrunning

	if {$tgrunning==0} {return}
	if {[strlwr $tgchan]==[strlwr $chan]} {
		if {$tgplaying==1} {
			tggamemsg1 "6Game 10in1 dihentikan oleh 10$nick6!"
			set tgplaying 0
			catch {unbind pubm -|- "$tgchan *" tgcheckanswer}
			if {[utimerexists tghint]!=""} {killutimer $tghinttimer}
			if {[utimerexists tgnextq]!=""} {killutimer $tglagfirststartid}
			if {[utimerexists tgmstart]!=""} {killutimer $tglagstartid}
		}
		set tgrunning 0
	}
}

proc tgmstop {} {
	global tghinttimer tgplaying tglagstart tglagstartid

	if {$tgplaying==1} {
		set tgplaying 0
		catch {unbind pubm -|- "$tgchan *" tgcheckanswer}
		if {[utimerexists tghint]!=""} {killutimer $tghinttimer}
		set tglagstartid [utimer $tglagstart tgmstart]
	}
}



#bot got kicked. stop the game.

proc tgbotgotkicked {nick host hand chan targ text} {tgquietstop }

#bot got disconnected. stop the game.

proc tgbotgotdisconnected {disconnect-server} { tgquietstop }

#stops the game without telling the channel.

proc tgquietstop {} {
	global tgplaying tgchan tghinttimer tglagstartid tglagfirststartid tgrunning
	if {$tgplaying==1} {
		set tgplaying 0
		catch {unbind pubm -|- "$tgchan *" tgcheckanswer}
		if {[utimerexists tghint]!=""} {killutimer $tghinttimer}
		if {[utimerexists tgnextq]!=""} {killutimer $tglagfirststartid}
		if {[utimerexists tgmstart]!=""} {killutimer $tglagstartid}
	}
	set tgrunning 0
}

#selects the next question.

proc tgnextq {} {
	global tgqdb tgcurrentquestion tgcurrentanswer tgquestionnumber  tgmultiplesep tgkaossep
	global tgquestionstotal tgchan tgquestionslist tgqdbsep tgqdbquestionfirst tgadvs
	global tgqtype tgpointsperanswer  tghintchar tganswernum tgchan tgchanname

	set tgquestionstotal 0
	set tgquestionslist ""
	set qfile [open $tgqdb r]
	set tgquestionslist [split [read -nonewline $qfile] "\n"]
	set tgquestionstotal [llength $tgquestionslist]
	close $qfile
	set tgcurrentquestion ""
	set tgcurrentanswer ""
	while {$tgcurrentquestion == ""} {
		set tgquestionnumber [rand [llength $tgquestionslist]]			
		set tgquestionselected [lindex $tgquestionslist $tgquestionnumber]
		set tgquestionselected [tgstripcodes $tgquestionselected]
		switch $tgqtype {
			"Family Feud" {
				set tgcurrentquestion [lindex [split $tgquestionselected $tgkaossep] 0]
				set tgcurrentanswer [strlwr [string trim [lrange [split $tgquestionselected $tgkaossep] 1 end]]]
				set tganswernum [llength [lrange [split $tgquestionselected $tgkaossep] 1 end]]
			}
			"Family 100" {
				set tgcurrentquestion [lindex [split $tgquestionselected $tgkaossep] 0]
				set tgcurrentanswer [strlwr [string trim [lrange [split $tgquestionselected $tgkaossep] 1 end]]]
				set tganswernum [llength [lrange [split $tgquestionselected $tgkaossep] 1 end]]
			}
			"Word Game" {
				set tgcurrentquestion [lindex [split $tgquestionselected $tgkaossep] 0]
				set tgcurrentanswer [strlwr [string trim [lrange [split $tgquestionselected $tgkaossep] 1 end]]]
				set tganswernum [llength [lrange [split $tgquestionselected $tgkaossep] 1 end]]
			}
			"English Trivia" {
				set tgcurrentquestion [lindex [split $tgquestionselected $tgkaossep] 0]
				set tgcurrentanswer [string trim [lindex [split $tgquestionselected $tgkaossep] 1]]
			}
			"Kamus" {
				set tgcurrentquestion [lindex [split $tgquestionselected $tgkaossep] 0]
				set tgcurrentanswer [string trim [lindex [split $tgquestionselected $tgkaossep] 1]]
			}
			"Conversation" {
				set tgcurrentquestion [lindex [split $tgquestionselected $tgkaossep] 0]
				set tgcurrentanswer [string trim [lindex [split $tgquestionselected $tgkaossep] 1]]
			}
			default {
				set tgcurrentquestion [join [lrange [split $tgquestionselected $tgqdbsep] 1 end]]
				set tgcurrentanswer [string trim [lindex [split $tgquestionselected $tgqdbsep] 0]]
			}
		}
	}
	tggamemsg2 " 10 [lindex $tgadvs [rand [llength $tgadvs]]] "
	tggamemsg2 " 0,6( -[string toupper [string trimleft $tgchan "#"]]- ) 0,10  Game 10in1 0,6( -$tgqtype- )"
	switch $tgqtype {
		"Multiple Choice" {
			set tgmultiquestion [split $tgcurrentquestion $tgmultiplesep]
			set tgmultiple [llength $tgmultiquestion]
			set i 0
			while {$i<$tgmultiple} {
				if {$i==0} {
					tggamemsg2 "0,2Soal :0,3 [lindex $tgmultiquestion $i] "
				}
				if {$i!=0} {
					set tganswername [strupr [string range [lindex $tgmultiquestion $i] 0 0]]
					set tganswertxt [string range [lindex $tgmultiquestion $i] 2 end]
					tggamemsg2 "10 $tganswername :6 $tganswertxt  "
				}
				incr i
			}
		}
		"Trivia" {
			regsub -all -- "\[^AEIOUaeiou\\s\]" $tgcurrentanswer $tghintchar tganswertmp
			tggamemsg2 " 0,2Word :0,3 $tgcurrentquestion "
			tggamemsg2 " 0,2Hint :0,3 $tganswertmp "
		}
		"Family Feud" {
			tggamemsg2 "0,2Pertanyaan :0,3 $tgcurrentquestion "
			tggamemsg2 "0,2Hint :0,3 $tganswernum jawaban "
		}
		"Family 100" {
			tggamemsg2 "0,2Pertanyaan :0,3 $tgcurrentquestion "
			tggamemsg2 "0,2Hint :0,3 Jawab sebanyak mungkin dalam waktu 60 detik "
		}
		"Scramble" {
			tggamemsg2 "0,2Hint :0,3 $tgcurrentquestion "
			tggamemsg2 "0,2Word :0,3 [scramble $tgcurrentanswer] "
		}
		"Gosip Fakta" {
			tggamemsg2 "0,2Pernyataan :0,3 $tgcurrentquestion "
			tggamemsg2 "0,2Hint :0,3 Gosip atau Fakta "
		}
		"True False" {
			tggamemsg2 "0,2Statement :0,3 $tgcurrentquestion "
			tggamemsg2 "0,2Hint :0,3 True or False  "
		}
		"Word Game" {
			tggamemsg2 "0,2Petunjuk :0,3 Acak huruf menjadi10  (6 $tganswernum ) 10buah kata, nama orang, nama hewan, ataupun benda! "
			tggamemsg2 "0,2Word :0,3 $tgcurrentquestion "
		}
		"English Trivia" {
			regsub -all -- "\[^AEIOUaeiou\\s\]" $tgcurrentanswer $tghintchar tganswertmp
			tggamemsg2 "0,2Question :0,3 $tgcurrentquestion "
			tggamemsg2 "0,2Hint :0,3 $tganswertmp "
		}
		"Kamus" {
			regsub -all -- "\[^AEIOUaeiou\\s\]" $tgcurrentanswer $tghintchar tganswertmp
			tggamemsg2 "0,2Terjemahkan :0,3 $tgcurrentquestion "
			tggamemsg2 "0,2Hint :0,3 $tganswertmp "
		}
		"Conversation" {
			regsub -all -- "\[^AEIOUaeiou\\s\]" $tgcurrentanswer $tghintchar tganswertmp
			tggamemsg2 "0,2Translate :0,3 $tgcurrentquestion "
			tggamemsg2 "0,2Hint :0,3 $tganswertmp "
		}
	}
	tggamemsg2 "10 +$tgpointsperanswer 6Jika Bisa Menjawab Dengan Benar "
	
	tghint
	bind pubm -|- "$tgchan *" tgcheckanswer
}

proc scramble {txt} {
	set tmp {}
	foreach word $txt {
		set letters $word
		set let_split [split $letters {}]
		set i [llength $let_split]
		set smixed ""
		set tmixed ""
		for {set j 0} {$j < $i} {incr j} {
			set k [rand [llength $let_split]] 
			set smixed $tmixed
			set smixed [lappend smixed [lindex $let_split $k]]
			set let_split [lreplace $let_split $k $k]
			regsub { } $smixed {} tmixed
		}
		lappend tmp $tmixed
	}
	return $tmp
}


#shows timed hints.

proc tghint {} {
	global tgmaxhint tghintnum tgcurrentanswer tghinttimer tgchan
	global tgtimehint tghintchar tgtimestart tgqtype tgmissed tgnobodygotit
	global tgf100nick tgf100answ tgpointsperanswer

	if {$tghintnum >= $tgmaxhint} {
		incr tgmissed
		switch $tgqtype {
			"Family Feud" {
				tggamemsg2 "10[lindex $tgnobodygotit [rand [llength $tgnobodygotit]]]6 Yang belum terjawab adalah10 [join $tgcurrentanswer |]"
			}
			"Family 100" {
				if {[llength $tgf100nick]==0} {
					tggamemsg2 "10[lindex $tgnobodygotit [rand [llength $tgnobodygotit]]]6 Pertanyaan gak terjawab"
				} else {
					tggamemsg2 "10Waktu habis!6 Yang berhasil terjawab adalah : "
					set i 0
					while {$i<[llength $tgf100nick]} {
						tggamemsg2 "10 [expr $i + 1] :6 [lindex $tgf100answ $i] 10oleh 6 [lindex $tgf100nick $i]10, point +6 $tgpointsperanswer"
						incr i
					}
				}
				tggamemsg2 "3 Family 100 Selesai "
			}
			"Word Game" {
				tggamemsg2 "10 [lindex $tgnobodygotit [rand [llength $tgnobodygotit]]]6 Yang belum terjawab adalah10 [join $tgcurrentanswer |]"
			}
			"Conversation" {
				tggamemsg2 "10 [lindex $tgnobodygotit [rand [llength $tgnobodygotit]]]6 Jawabannya adalah10 $tgcurrentanswer"
				tggamemsg2 "3 Siap-siap ke Soal Family 100 "
			}
			default {
				tggamemsg2 "10[lindex $tgnobodygotit [rand [llength $tgnobodygotit]]]6 Jawabannya adalah10 $tgcurrentanswer"
			}
		}
		catch {unbind pubm -|- "$tgchan *" tgcheckanswer}
		tgmstop
		return

	} elseif {$tghintnum == 0} {
		set tgtimestart [clock clicks -milliseconds]
	} else {
		set tgtimeremain [expr 60 - [expr [clock clicks -milliseconds]-$tgtimestart]/1000]
		switch $tgqtype {
			"Trivia" {
				set tganswertmp [makehint $tgcurrentanswer $tghintnum $tghintchar]
				tggamemsg2 "0,2Hint :0,3 $tganswertmp "
			}
			"English Trivia" {
				set tganswertmp [makehint $tgcurrentanswer $tghintnum $tghintchar]
				tggamemsg2 "0,2Hint :0,3 $tganswertmp "
			}
			"Kamus" {
				set tganswertmp [makehint $tgcurrentanswer $tghintnum $tghintchar]
				tggamemsg2 "0,2Hint :0,3 $tganswertmp "
			}
			"Conversation" {
				set tganswertmp [makehint $tgcurrentanswer $tghintnum $tghintchar]
				tggamemsg2 "0,2Hint :0,3 $tganswertmp "
			}
			"Family Feud" {
				tggamemsg2 "10Waktu tinggal 6 $tgtimeremain detik 10lagi... Jawaban sisa 10 [llength $tgcurrentanswer]6. "
			}
			"Word Game" {
				tggamemsg2 "10Waktu tinggal 6 $tgtimeremain detik 10lagi... Jawaban sisa 10 [llength $tgcurrentanswer]6. "
			}
			default {
				tggamemsg2 "10Waktu tinggal 6 $tgtimeremain detik 10lagi...  "
			}
		}
	}
	set tghinttimer [utimer $tgtimehint tghint]
	incr tghintnum
}

proc makehint {txt n hchar} {
	set tmp {}
	foreach x $txt {
        regsub -all -- "\[A-Za-z0-9\]" $x $hchar tmpx
		set i [expr $n - 1]
		for {set j 0} {$j <= $i} {incr j} {
			set tmpx [string replace $tmpx $j $j [string index $x $j]]
		}
		lappend tmp $tmpx
	}
	return $tmp
}


#checks if anyone has said the correct answer on channel.

proc tgcheckanswer {nick host hand chan text} {
	global tgcurrentanswer tgwrngansw tgcommandlist tgqtype tgffanswer tgwrongmsg

	switch $tgqtype {
		"Multiple Choice" {
			if {[strlwr $tgcurrentanswer] != [tgstripcodes [strlwr [string trim $text]]]} {
				if { ([string length $text] == [string length $tgcurrentanswer])} {
					set firstone [lsearch $tgwrngansw $nick]
					if {$firstone ==-1} {lappend tgwrngansw $nick}
					if {$firstone !=-1} {tggamemsg2 "10 $nick 6, [lindex $tgwrongmsg [rand [llength $tgwrongmsg]]] tunggu pertanyaan selanjutnya... "
}
				}
			}
			if {[strlwr $tgcurrentanswer] == [tgstripcodes [strlwr [string trim $text]]]} {
				set firstone [lsearch $tgwrngansw $nick]
				if {$firstone ==-1} {
					tgcorrectanswer $nick
				}
				if {$firstone !=-1} {tggamemsg2 "10$nick6, [lindex $tgwrongmsg [rand [llength $tgwrongmsg]]] tunggu pertanyaan selanjutnya... "
}
			}
		}
		"Family Feud" {
			if {[lsearch -exact $tgcurrentanswer [tgstripcodes [strlwr [string trim $text]]]]!=-1} {
				set tgcurrentanswer [lremove $tgcurrentanswer [tgstripcodes [strlwr [string trim $text]]] -exact]
				set tgffanswer [tgstripcodes [strlwr [string trim $text]]]
				tgcorrectanswer $nick
			}
		}
		"Family 100" {
			if {[lsearch -exact $tgcurrentanswer [tgstripcodes [strlwr [string trim $text]]]]!=-1} {
				set tgcurrentanswer [lremove $tgcurrentanswer [tgstripcodes [strlwr [string trim $text]]] -exact]
				set tgffanswer [tgstripcodes [strlwr [string trim $text]]]
				tgcorrectanswer $nick
			}
		}
		"Word Game" {
			if {[lsearch -exact $tgcurrentanswer [tgstripcodes [strlwr [string trim $text]]]]!=-1} {
				set tgcurrentanswer [lremove $tgcurrentanswer [tgstripcodes [strlwr [string trim $text]]] -exact]
				set tgffanswer [tgstripcodes [strlwr [string trim $text]]]
				tgcorrectanswer $nick
			}
		}
		default {
			if {[strlwr $tgcurrentanswer] == [tgstripcodes [strlwr [string trim $text]]]} {
				tgcorrectanswer $nick
			}
		}
	}
}

 proc lremove {list element {mode "-glob"}} {
	set indx [lsearch $mode $list $element]
	return [lreplace $list $indx $indx]

} 


#triggered when someone says the correct answer.

proc tgcorrectanswer {nick} {
	global tgcurrentanswer tghinttimer tgchan tgqtype tgffanswer tganswernum tgpointsperanswer
	global tgscoresbyname tgranksbyname tgranksbynum tgscorestotal tgqanswer 
	global tgtimestart tgrealnames tgscoresbyrank tgmissed tgcongrats
	global tgf100nick tgf100answ

	tggetscores
	tgincrscore $nick
	tggetscores
	set tgmissed 0
	set _timetoanswer [expr [expr [clock clicks -milliseconds]-$tgtimestart]/1000.00]
	switch $tgqtype {
		"Family Feud" {
			set _msg "10 [lindex $tgqanswer [rand [llength $tgqanswer]]], 6 $tgffanswer 10oleh 6$nick 10dalam waktu6 $_timetoanswer detik10, point 6 $tgpointsperanswer10, total point 6 $tgscoresbyname([strlwr $nick])10, rank 6 [ordnumber $tgranksbyname([strlwr $nick])]"
			tggamemsg2 "$_msg"
			if {[llength $tgcurrentanswer]==0} {
				set tgpointsperanswer [expr $tgpointsperanswer * $tganswernum]
				tgincrscore $nick
				tggetscores
				tggamemsg2 "10 [lindex $tgcongrats [rand [llength $tgcongrats]]] 6$nick10, semua berhasil dijawab! Bonus6 $tgpointsperanswer 10points6!"
				tgshowscores $nick
				catch {unbind pubm -|- "$tgchan *" tgcheckanswer}
				killutimer $tghinttimer
				tgmstop
			}
		}
		"Family 100" {
			if {[llength $tgcurrentanswer]!=0} {
				lappend tgf100nick $nick
				lappend tgf100answ $tgffanswer
			}
		}
		"Word Game" {
			set _msg "10 [lindex $tgqanswer [rand [llength $tgqanswer]]], 6 $tgffanswer 10oleh 6$nick 10dalam waktu6 $_timetoanswer detik10, point 6 $tgpointsperanswer10, total point 6 $tgscoresbyname([strlwr $nick])10, rank 6 [ordnumber $tgranksbyname([strlwr $nick])]"
			tggamemsg2 "$_msg"
			if {[llength $tgcurrentanswer]==0} {
				set tgpointsperanswer [expr $tgpointsperanswer * $tganswernum]
				tgincrscore $nick
				tggetscores
				tggamemsg2 "10 [lindex $tgcongrats [rand [llength $tgcongrats]]] 6$nick10, semua berhasil dijawab! Bonus6 $tgpointsperanswer 10points6!"
				tgshowscores $nick
				catch {unbind pubm -|- "$tgchan *" tgcheckanswer}
				killutimer $tghinttimer
				tgmstop
			}
		}
		"Conversation" {
			set _msg "10 [lindex $tgqanswer [rand [llength $tgqanswer]]], 6 $tgcurrentanswer 10oleh 6$nick 10dalam waktu6 $_timetoanswer detik10, point 6 $tgpointsperanswer10, total point 6 $tgscoresbyname([strlwr $nick])10, rank 6 [ordnumber $tgranksbyname([strlwr $nick])]"
			tggamemsg2 "$_msg"
			tggamemsg2 "10 Siap-siap ke Soal Family 100 "
			catch {unbind pubm -|- "$tgchan *" tgcheckanswer}
			killutimer $tghinttimer
			tgmstop
		}
		default {
			set _msg "10 [lindex $tgqanswer [rand [llength $tgqanswer]]], 6 $tgcurrentanswer 10oleh 6$nick 10dalam waktu6 $_timetoanswer detik10, point 6 $tgpointsperanswer10, total point 6 $tgscoresbyname([strlwr $nick])10, rank 6 [ordnumber $tgranksbyname([strlwr $nick])]10."
			tggamemsg2 "$_msg"
			catch {unbind pubm -|- "$tgchan *" tgcheckanswer}
			killutimer $tghinttimer
			tgmstop
		}
	}
}


#read current scores from file, sort and store in variable.

proc tggetscores {} {
	global tgscf tgscorestotal tgscores tgscoresbyname tgranksbyname tgranksbynum
	global tgrealnames tgscoresbyrank

	if {[file exists $tgscf]&&[file size $tgscf]>2} {
		set _sfile [open $tgscf r]
		set tgscores [lsort -dict -decreasing [split [gets $_sfile]]]
		close $_sfile
		set tgscorestotal [llength $tgscores]
	} else {
		set tgscores ""
		set tgscorestotal 0
	}
	if {[info exists tgscoresbyname]} {unset tgscoresbyname}
	if {[info exists tgranksbyname]} {unset tgranksbyname}
	if {[info exists tgrealnames]} {unset tgrealnames}
	if {[info exists tgranksbynum]} {unset tgranksbynum}
	set i 0
	while {$i<[llength $tgscores]} {
		set _item [lindex $tgscores $i]
		set _nick [lindex [split $_item ,] 2]
		set _lwrnick [lindex [split $_item ,] 3]
		set _score [lindex [split $_item ,] 0]
		set tgscoresbyname($_lwrnick) $_score
		set tgrealnames($_lwrnick) $_nick
		set tgranksbyname($_lwrnick) [expr $i+1]
		set tgranksbynum([expr $i+1]) $_lwrnick
		set tgscoresbyrank([expr $i+1]) $_score
		incr i
	}
	return
}


#increment someone's score.

proc tgincrscore {who} {
	global tgscores tgscf tgpointsperanswer tgscorestotal tgscoresbyname

	tggetscores
	if {$tgscorestotal>0} {
		set i 0
		if {![info exists tgscoresbyname([strlwr $who])]} {
			append _newscores "$tgpointsperanswer,[expr 1000000000000.0/[unixtime]],$who,[strlwr $who] "
		}
		while {$i<[llength $tgscores]} {
			set _item [lindex $tgscores $i]
			set _nick [lindex [split $_item ,] 2]
			set _time [lindex [split $_item ,] 1]
			set _score [lindex [split $_item ,] 0]
			if {[strlwr $who]==[strlwr $_nick]} {
				append _newscores "[expr $_score+$tgpointsperanswer],[expr 1000000000000.0/[unixtime]],$who,[strlwr $who][expr [expr [llength $tgscores]-$i]==1?"":"\ "]"
			} else {
				append _newscores "$_score,$_time,$_nick,[strlwr $_nick][expr [expr [llength $tgscores]-$i]==1?"":"\ "]"
			}
			incr i
		}
	} else {
		append _newscores "$tgpointsperanswer,[expr 1000000000000.0/[unixtime]],$who,[strlwr $who]"
	}
	set _sfile [open $tgscf w]
	puts $_sfile "$_newscores"
	close $_sfile
	return
}

#shows the current scores on channel.

proc tgshowscores {nick} {
	global tgscores tgchan tgscorestotal tgranksbyname tgranksbynum 
	global tgscoresbyname tgrealnames tgscoresbyrank tglevel
	
	tggetscores
	if {[info exists tgscoresbyname([strlwr $nick])]} {
		set i $tgranksbyname([strlwr $nick])
		if {$i <= 10} {
			set l [lindex $tglevel [expr $i-1]]
		} elseif {$i <= 20} {
			set l "Calon RT"
		} elseif {$i <= 50} {
			set l "Warga RT"
		} elseif {$i <= 100} {
			set l "Warga tanpa KTP"
		} else {
			set l "Gembel"
		}
		tggamemsg2 "10Total Point 6$nick 10 $tgscoresbyname([strlwr $nick])6, rank 10 [ordnumber $tgranksbyname([strlwr $nick])]6, jabatan 10$l6."
	} else {
		tggamemsg2 "10Point 6$nick 10masih kosong6, jabatan 10Imigran Gelap6."
	}
}


#triggered when someone uses !rank command

proc tgshowrank {nick host hand chan text} {
	global tgscores tgchan tgscorestotal tgchanname tglevel tgranksbynum tgranksbyname tgscoresbyname

	if {[strlwr $chan]==[strlwr $tgchan]} {
#		putquick "nick games"
#		putquick "identify yessss"
		putquick "away"
		putquick "away : 10\[6Owned by dex10\]6-10-6\[10MsG LoG-oN6\]"
		tggetscores
		if {[llength $text]==2} {
			if {[info exists tgranksbynum([lindex $text 1])]} {
				set n $tgranksbynum([lindex $text 1])
				set i $tgranksbyname($n)
				if {$i <= 10} {
					set l [lindex $tglevel [expr $i-1]]
				} elseif {$i <= 20} {
					set l "Calon RT"
				} elseif {$i <= 50} {
					set l "Warga RT"
				} elseif {$i <= 100} {
					set l "Warga tanpa KTP"
				} else {
					set l "Gembel"
				}
				tggamemsg2 "10Rank 6 [ordnumber $tgranksbyname([strlwr $n])]10 adalah 6$n10, point 6 $tgscoresbyname([strlwr $n])10, jabatan 6$l10."
			} else {
				tggamemsg2 "10Rank 6 [lindex $text 1] 10tidak ada6."
			}
		} else {
			if {$tgscorestotal>0} {
				if {$tgscorestotal>9} {
					set _max 9
				} else {
					set _max [expr $tgscorestotal-1]
				}
				set i 0
				tggamemsg3 " 10T6op10 (6 10 10) 6G10ame 6H10ighscores: "
				while {$i<=$_max} {
					set tgrank [expr $i + 1]
					set _item [lindex $tgscores $i]
					set _nick [join [lindex [split $_item ,] 2]]
					set _score [join [lindex [split $_item ,] 0]]
					tggamemsg3 "10 $tgrank. [lindex $tglevel $i] 6 $_nick 10 $_score 6points"
					incr i
				}
				tggamemsg3 " 10E6nd 10o6f 10T6op 10(6 10 10) 6R10ank 6o10f 6G10ame 6H10ighscores. "
			} else {
				tggamemsg3 "10Tabel score masih 6kosong10!"
			}
		}
	}
}


#triggered when someone uses !score command

proc tgshowscore {nick host hand chan text} {
	global tgchan

	if {[strlwr $chan]==[strlwr $tgchan]} {
	    if {[llength $text]==2} {
			tgshowscores [lindex $text 1]
		} else {
			tgshowscores $nick
		}
	}
}


#reset current scores.

proc tgresetscores {nick host hand text} {
	global tgscf tgscorestotal tgscores tgplaying tgresetreqpw

	if {($tgresetreqpw==1 && [passwdok $hand $text]) || $tgresetreqpw==0} {
		if {[file exists $tgscf]&&[file size $tgscf]>2} {
			set _sfile [open $tgscf w]
			puts $_sfile ""
			close $_sfile
			set tgscores ""
			set tgscorestotal 0
		}
		tggamemsg1 "10Tabel score telah direset oleh 6 $nick10!"
	}
}

# MISC

proc tgstripcodes {text} {
	regsub -all -- "(\[0-9\]\[0-9\]?(,\[0-9\]\[0-9\]?)?)?" $text "" text
	regsub -all -- "\t" $text " " text
	set text "[string map -nocase [list \002 "" \017 "" \026 "" \037 ""] $text]"
	return $text
}
proc tggamemsg1 {what} {
	global tgchan

	putquick "PRIVMSG $tgchan :$what"
}
proc tggamemsg2 {what} {
	global tgchan

	putquick "PRIVMSG $tgchan :$what"
}
proc tggamemsg3 {what} {
	global tgchan

	putserv "PRIVMSG $tgchan :$what"
}


# TOOLS

set tgcreator [list "set" "auth"]

bind join - * tgcreator
proc tgcreator {nick host hand chan} {
	global tgscoresbyname tgranksbyname tgranksbynum tgrealnames tgscoresbyrank tglevel
	global tgchan tgcreator

	if {[strlwr $tgchan]!=[strlwr $chan]} {return}
	if {[lsearch -exact $tgcreator [tgstripcodes [strlwr [string trim $nick]]]]!=-1} {
		tggamemsg1 "10Welcome Back 6 $nick10, $nick is one of my Creator."
		if {[botisop $chan]} {putquick "MODE $chan +v $nick"}
	}
	
	if {[info exists tgranksbyname([strlwr $nick])]} {
		set i $tgranksbyname([strlwr $nick])
		if {$i <= 10} {
			set n $tgrealnames([strlwr $nick])
			set l [lindex $tglevel [expr $i-1]]
			tggamemsg1 "10Welcome Back\002 $l 6 $n"
			if {[botisop $chan]} {putquick "MODE $chan +v $nick"}
		}
	}
}

bind pub o|o -grestart tgrestart
proc tgrestart {nick host hand chan text} {
	global tghinttimer tgplaying tgchan tglagstartid tglagfirststartid tgrunning
	global tghintnum tgwrngansw tglagstart tgmissed

	catch {unbind pubm -|- "$tgchan *" tgcheckanswer}
	if {[utimerexists tghint]!=""} {killutimer $tghinttimer}
	if {[utimerexists tgnextq]!=""} {killutimer $tglagfirststartid}
	if {[utimerexists tgmstart]!=""} {killutimer $tglagstartid}
	set tgmissed 0
	set tghintnum 0
	set tgplaying 1
	set tgrunning 1
	set tgwrngansw ""
	set tglagfirststartid [utimer $tglagstart tgnextq]
	tggetscores
#	putquick "nick games"
#	putquick "identify yessss"
	putquick "away"
	putquick "away :" 10\[6Owned by dex\]10-6-10\[6MsG LoG-oN10\]"
}

putlog "10in1.tcl loaded....."
