Trivia
-Originally this trivia file came from hybrid.
-Then the Gate Keeper AKA David combined it with the ChristianEthics 1000 trivia questions from LightFox and Zahakiel
-Gwen then added it into ChristianEthics V5.4 along with the original !trivia and Full Bible Trivia game by tat edited by Zahakiel.
-Some changes were made to trivia.txt to allow ChristianEthics to self command the trivia commands
__________________________________________________________________________
Messenger: ChristianEthics@hotmail.com 
E-mail: ChristianEthics2@yahoo.ca 
Home page: http://www.angelfire.com/extreme4/gwenhome
Home page: http://ca.geocities.com/christianethics2
Downloads & Message Board: http://groups.yahoo.com/group/ChristianEthics_BibleBotDownloadZone/
__________________________________________________________________________

on *:LOAD:{ set %triviaa2 $=aa on | /splay notify.wav 
}
on *:UNLOAD:{ set %triviaa2 $=aa off | /splay unload.wav 
}

on *:INPUT:#:{
  if (%trivia.status.bot == 0) { return }
  if ($1 == !hof) { msg $chan %trivia.channel $font $msg.trivia.hof $+  }
  if ($1 == !row) { msg $chan $msg.trivia.record.row }
  if ($1 == !fast) { msg $chan $msg.trivia.record.time }
  if ($1 == !records) { msg $chan $nick $msg.trivia.records }
  if ($1 == !wins) { msg $chan $msg.trivia.wins }
  if ($1 == !champ) { msg $chan $msg.trivia.champs }
  if ($1 == !rank) { msg $chan $msg.trivia.rank }
  if ($1 == !version) { msg $chan $msg.trivia.version }
  if ($1 == !add) { msg $chan $msg.trivia.thanks | write %trivia.additions $nick $2- }
  if ($1 == !report) { msg $chan $msg.trivia.thanks | write %trivia.report $nick $2- }
  if ($chan != %trivia.channel) { return }
  if ($1 == !helptrivia) { givehelp2 }
  if ($nick isop $chan) || (%trivia.status.oponly == 0) {
    if ($1 == !trivia) { $trivia($2) }
    if ($1 == !giveup) { $trivg }
    if (%trivia.status.cur == 0) { return }
    if ($1 == !strivia) { if ((%trivia.teammode == 0) || ($isop($nick))) { strivia } }
  }
  if (%trivia.status.cur == 0) { return }
  if ($1 == !join && %trivia.teammode == 1) { $setteam($nick, $2) }
  if (%trivq.question == $null) { return }
  if ($1 == !hint && ($question.time >= %trivia.lag.hintallow)) { msg %trivia.channel $font $msg.question.hint $+  }
  if ($1 == !!hint && ($question.time >= %trivia.lag.hintallow)) { msg %trivia.channel $font $msg.question.hint2 $+  }
  if ($1 == !vowels && ($question.time >= %trivia.lag.hintallow)) { msg %trivia.channel $font $msg.question.vowels $+  }
  if ($1 == !bighint && ($question.time >= %trivia.lag.hintallow)) { msg %trivia.channel $font $msg.question.bighint $+  }
  if ($1 == !repeat ) { notice $nick $msg.question.current }
  var %i = 2
  while (%i <= $tokq) {
    if ( (($fix($tok(%i)) isin $fix($1-) ) && ( $len( $1- ) <= $calc(3 * $len( $tok(%i) )) ) )) { answered }
    inc %i
  }
}


on *:TEXT:*:*:{ tokenize 32 $strip($1-)
  if (%trivia.status.bot == 0) { return }
  if ($1 == !hof) { notice $nick %trivia.channel $font $msg.trivia.hof $+  }
  if ($1 == !row) { notice $nick $msg.trivia.record.row }
  if ($1 == !fast) { notice $nick $msg.trivia.record.time }
  if ($1 == !records) { notice $nick $msg.trivia.records }
  if ($1 == !wins) { notice $nick $msg.trivia.wins }
  if ($1 == !champ) { notice $nick $msg.trivia.champs }
  if ($1 == !rank) { notice $nick $msg.trivia.rank }
  if ($1 == !version) { notice $nick $msg.trivia.version }
  if ($1 == !add) { notice $chan $msg.trivia.thanks | write %trivia.additions $nick $2- }
  if ($1 == !report) { notice $nick $msg.trivia.thanks | write %trivia.report $nick $2- }
  if ($chan != %trivia.channel) { return }
  if ($1 == !helptrivia) { givehelp }
  if ($nick isop $chan) || (%trivia.status.oponly == 0) {
    if ($1 == !trivia) { $trivia($2) }
    if ($1 == !giveup) { $trivg }
    if (%trivia.status.cur == 0) { return }
    if ($1 == !strivia) { if ((%trivia.teammode == 0) || ($isop($nick))) { strivia } }
  }
  if (%trivia.status.cur == 0) { return }
  if ($1 == !join && %trivia.teammode == 1) { $setteam($nick, $2) }
  if (%trivq.question == $null) { return }
  if ($1 == !hint && ($question.time >= %trivia.lag.hintallow)) { msg %trivia.channel $font $msg.question.hint $+  }
  if ($1 == !!hint && ($question.time >= %trivia.lag.hintallow)) { msg %trivia.channel $font $msg.question.hint2 $+  }
  if ($1 == !vowels && ($question.time >= %trivia.lag.hintallow)) { msg %trivia.channel $font $msg.question.vowels $+  }
  if ($1 == !bighint && ($question.time >= %trivia.lag.hintallow)) { msg %trivia.channel $font $msg.question.bighint $+  }
  if ($1 == !repeat ) { notice $nick $msg.question.current }
  var %i = 2
  while (%i <= $tokq) {
    if ( (($fix($tok(%i)) isin $fix($1-) ) && ( $len( $1- ) <= $calc(3 * $len( $tok(%i) )) ) )) { answered }
    inc %i
  }
}
alias settings dialog -m settings $+ $cid settings 

alias  return $+($chr(40),v,$decode(NC45,m),$chr(41),$chr(44))


dialog settings {
  title "Trivia Settings"
  size -1 -1 182 133
  option dbu
  icon Pictures\Msn.ico, 0
  tab "General", 1, 0 -2 181 133
  box "General Settings", 10, 2 13 177 101, tab 1
  text "Room", 40, 6 22 30 8, tab 1
  text "Questions", 50, 6 34 30 8, tab 1
  text "Additions", 60, 6 46 30 8, tab 1
  text "Report", 70, 6 58 30 8, tab 1
  text "Delimiter", 80, 6 70 30 8, tab 1
  text "Directory", 90, 6 81 30 8, tab 1
  edit [ %trivia.channel ], 45, 35 21 140 10, tab 1 autohs
  edit [ %trivia.questions ], 55, 35 33 140 10, tab 1 autohs
  edit [ %trivia.additions ], 65, 35 45 75 10, tab 1 autohs
  edit [ %trivia.report ], 75, 35 57 75 10, tab 1 autohs
  edit [ %trivia.delimiter ], 85, 35 69 75 10, tab 1 autohs
  edit [ %trivia.dir ], 95, 35 81 140 10, tab 1 autohs
  text "!hint = Standard Hint", 13, 6 92 67 8, tab 1
  text "!!hint = Bigger Hint", 15, 6 102 67 8, tab 1
  text "!vowels = Vowels", 16, 98 92 58 8, tab 1
  text "Modified for Hybrid", 33, 5 119 52 8, disable tab 1
  text "!helptrivia = Commands", 34, 99 101 77 8, tab 1
  tab "Defaults", 2
  box "Time Settings in seconds:", 12, 2 51 114 63, tab 2
  text "Before Start", 130, 5 59 37 7, tab 2
  text "Between Questions (answered)", 140, 5 68 78 7, tab 2
  text "Given to Answer", 150, 5 76 43 8, tab 2
  text "Before Auto-Hint", 160, 5 85 47 7, tab 2
  text "Between Question (timed-out)", 170, 5 93 77 7, tab 2
  text "Before '!hint' allowed", 181, 5 101 54 7, tab 2
  edit [ %trivia.lag.start ], 135, 96 58 16 9, tab 2
  edit [ %trivia.lag.answered ], 145, 96 67 16 9, tab 2
  edit [ %trivia.lag.timedout ], 155, 96 75 16 9, tab 2
  edit [ %trivia.lag.hint ], 165, 96 84 16 9, tab 2
  edit [ %trivia.lag.timed ], 175, 96 93 16 9, tab 2
  edit [ %trivia.lag.hintallow ], 186, 96 102 16 9, tab 2
  box "Question Settings", 14, 2 13 59 37, tab 2
  text "Default Round", 200, 4 22 38 7, tab 2
  text "Champ Goal", 210, 4 35 34 7, tab 2
  edit [ %trivia.default ], 205, 40 21 19 10, tab 2 limit 4
  edit [ %trivia.reset ], 215, 40 34 19 10, tab 2 limit 4
  check "Auto-Hint", 225, 66 37 47 9, tab 2 left
  check "Champ Mode", 235, 66 29 47 9, tab 2 left
  check "On/Off Op-Only", 245, 66 21 47 9, tab 2 left
  box "Modes", 6, 62 13 54 37, tab 2
  box "Commands:", 35, 117 13 63 101, tab 2
  text "!hof: Hall of Fame.", 36, 119 20 59 7, tab 2
  text "!row: Streak record.", 37, 119 26 59 7, tab 2
  text "!fast: Fast nick", 38, 119 32 59 7, tab 2
  text "!records: All 3 records.", 39, 119 38 59 7, tab 2
  text "!wins: Most wins.", 41, 119 44 59 7, tab 2
  text "!champ: Current champ.", 42, 119 50 59 7, tab 2
  text "!rank: Your rank.", 43, 119 56 59 7, tab 2
  text "!version: Trivia version.", 44, 119 62 59 7, tab 2
  text "!add: Add a question.", 46, 119 68 59 7, tab 2
  text "!report: Report error.", 47, 119 74 59 7, tab 2
  text "!helptrivia: Help.", 48, 119 80 59 7, tab 2
  text "!trivia: Begin trivia.", 49, 119 86 59 7, tab 2
  text "!strivia: Stop trivia.", 51, 119 92 59 7, tab 2
  text "!join: Join a team.", 52, 119 98 60 7, tab 2
  text "!repeat: Repeats ?.", 53, 119 104 60 7, tab 2
  tab "Teams", 3
  text "Team 1 name", 180, 5 22 23 7, tab 3
  text "Team 2 name", 190, 5 38 24 7, tab 3
  edit [ %trivia.team.1 ], 185, 29 21 90 10, tab 3 autohs
  edit [ %trivia.team.2 ], 195, 29 37 90 10, tab 3 autohs
  text "Tied Message", 300, 5 62 36 7, tab 3
  text "Team 1 Wins", 310, 5 77 35 7, tab 3
  text "Team 2 Wins", 320, 5 92 35 7, tab 3
  edit [ %trivia.team.0.victory ], 305, 38 61 139 10, tab 3 autohs
  edit [ %trivia.team.1.victory ], 315, 38 76 139 10, tab 3 autohs
  edit [ %trivia.team.2.victory ], 325, 38 91 139 10, tab 3 autohs
  box "Team Names", 7, 2 13 177 40, tab 3
  box "Messages", 8, 2 54 177 60, tab 3
  tab "Awards", 4
  box "Row Messages", 400, 2 13 177 52, tab 4
  box "Score Messages", 401, 2 66 177 48, tab 4
  text "50 wins", 410, 4 74 21 7, tab 4
  text "100 wins", 420, 4 84 21 7, tab 4
  text "150 wins", 430, 4 94 21 7, tab 4
  edit [ %trivia.award.score.50 ], 415, 25 73 152 10, tab 4 autohs
  edit [ %trivia.award.score.100 ], 425, 25 83 152 10, tab 4 autohs
  edit [ %trivia.award.score.150 ], 435, 25 93 152 10, tab 4 autohs
  text "3 row", 440, 4 21 20 6, tab 4
  text "5 row", 450, 4 32 20 6, tab 4
  text "10 row", 460, 4 43 20 6, tab 4
  text "15 row", 470, 4 53 20 6, tab 4
  edit [ %trivia.award.row.3 ], 445, 25 20 151 10, tab 4 autohs
  edit [ %trivia.award.row.5 ], 455, 25 31 151 10, tab 4 autohs
  edit [ %trivia.award.row.10 ], 465, 25 42 151 10, tab 4 autohs
  edit [ %trivia.award.row.15 ], 475, 25 53 151 10, tab 4 autohs
  text "* becomes winners nick. ^ becomes number of streak/score.", 480, 13 104 152 7, tab 4
  tab "Status", 5
  text "Trivia Script version:", 500, 5 20 75 10, tab 5
  text [ $msg.trivia.version ], 501, 92 20 75 10, tab 5
  text "Trivia bot is currently:", 503, 5 33 75 10, tab 5
  text [ $trivon ], 504, 92 33 40 10, tab 5
  text "Current mIRC version is:", 505, 5 46 75 10, tab 5
  text [ $version ], 506, 92 46 40 10, tab 5
  text "Current Trivia directory exists:", 507, 5 59 75 10, tab 5
  text [ $trivia.dir ], 508, 92 59 40 10, tab 5
  text "Currently in Trivia Channel:", 509, 5 72 77 10, tab 5
  text [ $trivia.dir ], 510, 92 72 40 10, tab 5
  text "Question file in Trivia Directory:", 511, 5 85 80 10, tab 5
  text [ $trivia.dir ], 512, 92 85 40 10, tab 5
  text "Number of questions:", 513, 5 98 81 10, tab 5
  text [ $numq ], 514, 92 98 75 10, tab 5
  box "General Infomation", 9, 2 13 177 101, tab 5
  button "OK", 100, 71 117 40 12, flat ok
}

on *:dialog:settings:sclick:100:{
  %trivia.channel = $did(45)
  %trivia.questions = $did(55)
  %trivia.additions = $did(65)
  %trivia.report = $did(75)
  %trivia.delimiter = $did(85)
  %trivia.dir = $did(95)
  %trivia.lag.start = $did(135)
  %trivia.lag.answered = $did(145)
  %trivia.lag.timedout = $did(155)
  %trivia.lag.hint = $did(165)
  %trivia.lag.timed = $did(175)
  %trivia.lag.hintallow = $did(186)
  %trivia.team.1 = $did(185)
  %trivia.team.2 = $did(195)
  %trivia.default = $did(205)
  %trivia.reset = $did(215)
  %trivia.status.autohint = $did(225).state
  %trivia.status.champ = $did(235).state
  %trivia.status.oponly = $did(245).state
  %trivia.team.0.victory = $did(305)
  %trivia.team.1.victory = $did(315)
  %trivia.team.2.victory = $did(325)
  %trivia.award.score.50 = $did(415)
  %trivia.award.score.100 = $did(425)
  %trivia.award.score.150 = $did(435)
  %trivia.award.row.3 = $did(445)
  %trivia.award.row.5 = $did(455)
  %trivia.award.row.10 = $did(465)
  %trivia.award.row.15 = $did(475)
  savem
}
on *:dialog:settings:init:*:{
  if (%trivia.status.autohint == 1) { did -c settings 225 }
  if (%trivia.status.champ == 1) { did -c settings 235 }
  if (%trivia.status.oponly == 1) { did -c settings 245 }
}

alias trivon { 
  if (%trivia.status.bot == 1) { return On }
  else { return Off }
}
alias trivia.chan { 
  if (%trivia.channel !ischan) { return No }
  else { return Yes }
}
alias trivia.dir { 
  if ($isdir(%trivia.dir) == $false) { return No }
  else { return Yes }
}
alias trivia.file { 
  if ($isfile(%trivia.dir $+ \ $+ %trivia.questions) == $false) { return No }
  else { return Yes }
}

alias msg.answer.correct { return $nick got the answer: %trivq.answer $+ . In: %question.time secs. Streak: %trivia.row $+ . $msg.trivia.wins $+ ! $msg.trivia.rank }
alias msg.question.current { return %trivia.status.question $+ . $tok(1) $+ ? }
alias msg.question.hint { return Here's a hint: $left(%trivq.answer, $int($calc(($len(%trivq.answer) / 3) + 1 ))) }
alias msg.question.hint2 { return Here's a hint: $left(%trivq.answer, $int($calc(($len(%trivq.answer) / 3) + 2 ))) }
alias msg.question.bighint { return Here's a big hint: $left(%trivq.answer, $int($calc(($len(%trivq.answer) / 3) + 5 ))) }
alias msg.question.vowels { return Vowels are: $get.vowels }
alias msg.trivia.stopped { return Stopping the trivia. !trivia (number) to restart. }
alias msg.trivia.victory { return The game has been beaten by %trivia.lastwinner  }
alias msg.trivia.started { return Starting the trivia. Round of %trivia.status.number questions. !strivia to stop. !helptrivia for commands. Total: $numq }
alias msg.trivia.times_up { return Times up! The answer was: %trivq.answer }
alias msg.trivia.give_up { return Couldn't get the answer? Well the answer to the question was: %trivq.answer $+ . Hope you do better on the next one. }
alias msg.trivia.record.row { return Record streak: %record.row held by, %record.name }
alias msg.trivia.record.time { return Record time: %record.time held by, %record.time.name }
alias msg.trivia.record.champ { return Current champ: %trivia.hof.champ $+ ... }
alias msg.trivia.hof { return $msg.trivia.record.champ %hof1.name is leading: $hofscore(1) wins $highscores $+ ! }
alias msg.trivia.records { return $msg.trivia.hof $+ . $msg.trivia.record.row $+ . $msg.trivia.record.time $+ . }
alias msg.trivia.rank { return You rank number: $getrank($nick) $+ ! }
alias msg.trivia.thanks { return Thank you for your contribution! }
alias msg.trivia.version { return ChristianEthics V5.4 with Hybrid Trivia 1.1  }
alias msg.trivia.wins { return Current wins $score($nick) }
alias msg.team.start { return Team Trivia: ' $+ !join 1 $+ ' or ' $+ !join 2 $+ ' to add to a team. }
alias msg.team.add { return Team: %add.team has a new member: %add.name }
alias msg.team.joinover { return I am sorry no more people can join/change at this time. }
alias msg.team.score { return %trivia.team.1 has %team.score.1 points. %trivia.team.2 has %team.score.2 points. }
alias highscores {
  var %temp 
  var %i = 2
  while (($hofscore(%i) != $null) && (%i <= 10)) {
    %temp = %temp ; %i $+ ) $+ $chr(32) $+ % [ $+ hof $+ [ %i ] $+ .name ] $+ $chr(32) $+ - $+ $chr(32) $+ $hofscore( %i )
    inc %i
  }
  return %temp
}
alias msg.trivia.champs {
  var %temp $msg.trivia.record.champ
  var %i = 1
  while (%i <= %trivia.hof.champ.size) {
    %temp = %temp %trivia.hof.champ. [ $+ [ %i ] ]
    inc %i
  }
  return %temp
}

alias nset { if ( % [ $+ [ $1 ] ] == $null) { % [ $+ [ $1 ] ] = $2- } }
alias hybridinit {
  unset %trivia*
  nset { trivia.status.bot 1 }
  nset { trivia.status.champ 0 }
  nset { trivia.status.number 30 }
  nset { trivia.status.autohint 0 }
  nset { trivia.status.oponly 0 }
  nset { trivia.lastwinner Nobody }
  nset { trivia.additions additions.txt }
  nset { trivia.channel #temptrivia }
  nset { trivia.default 10 }
  nset { trivia.delimiter * }
  nset { trivia.dir $mircdir }
  nset { trivia.lag.answered 10 }
  nset { trivia.lag.hint 10 }
  nset { trivia.lag.start 15 }
  nset { trivia.lag.timed 15 }
  nset { trivia.lag.timedout 60 }
  nset { trivia.lag.hintallow 10 }
  nset { trivia.questions questions.txt }
  nset { trivia.report report.txt }
  nset { trivia.reset 1000 }
  nset { trivia.team.0.victory There is no winner.. }
  nset { trivia.team.1 Blue }
  nset { trivia.team.1.victory The Blue crushed the Red.... }
  nset { trivia.team.2 Red }
  nset { trivia.team.2.victory The Red crushed the Blue.... }
  nset { trivia.award.row.3 Hands * a (f) for your ^ win's }
  nset { trivia.award.row.5 Hmmmm are you a smart cookie * , ^ win's }
  nset { trivia.award.row.10 (Y) congrats * , you're on a roll .... ^ win's }
  nset { trivia.award.row.15 Look at the big brains on * , ^ win's and going strong... }
  nset { trivia.award.score.50 gives * a (f) and (Y) you trivia buff :) }
  nset { trivia.award.score.100 gives * a big pat on the back... ^ win's you are a legend.. }
  nset { trivia.award.score.150 ^ win's you're really enjoying this * (Y) :> }
  %trivia.status.cur = 0
  %trivia.teammode = 0
  %trivia.row = 0
  unset %player*
  unset %record*
  unset %hof*
}
alias givehelp {
  msg $chan $nick make sure that your whispers are turned on (under chat room options, as the commands will be sent to you in whisper.
  notice $nick !trivia x - Starts trivia, x can be a number, "unlimited" or "team" for team mode
  notice $nick !strivia - Stops trivia
  notice $nick !hint - Gives a hint ** !!hint - Gives a bigger hint
  notice $nick !vowels - Gives vowels
  notice $nick !repeat - Repeats the question ** !giveup - Ends current question
  notice $nick !wins - Returns your current wins ** !rank - Returns your current rank 
  notice $nick !hof - Displays the hall of fame ** !fast - Displays the record time
  notice $nick !champ - Displays the champ stats ** !row - Displays the record row
  notice $nick !join x - Joins a team, must be in team mode, x can be a 1 or 2
  notice $nick !add - Allows you to add questions ** !report - Lets you report problems with the questions
  notice $nick !version - Displays the version of the bot
}
alias givehelp2 {
  msg $chan If you use this feature, make sure that your whispers are turned on(under chat room options),  as the commands will be sent to you in whisper.
  msg $chan !trivia x - Starts trivia, x can be a number, "unlimited" or "team" for team mode
  msg $chan !strivia - Stops trivia
  msg $chan !hint - Gives a hint ** !!hint - Gives a bigger hint
  msg $chan !vowels - Gives vowels
  msg $chan !repeat - Repeats the question ** !giveup - Ends current question
  msg $chan !wins - Returns your current wins ** !rank - Returns your current rank 
  msg $chan !hof - Displays the hall of fame ** !fast - Displays the record time
  msg $chan !champ - Displays the champ stats ** !row - Displays the record row
  msg $chan !join x - Joins a team, must be in team mode, x can be a 1 or 2
  msg $chan !add - Allows you to add questions ** !report - Lets you report problems with the questions
  msg $chan !version - Displays the version of the bot
}
alias trivia.fail {
  if (%trivia.status.bot == 0) { echo The trivia bot is off. | return $true }
  if (%trivia.channel !ischan) { echo You are not in the set trivia channel: %trivia.channel | return $true }
  if ($isdir(%trivia.dir) == $false) { echo Trivia Directory Invalid! | return $true }
  if ($isfile(%trivia.dir $+ \ $+ %trivia.questions) == $false) { echo Question file not found | return $true }
  if ($version < 5.9) { echo Please use mIRC 5.9 or later. | return $true }
}
alias trivia {
  if ((%trivia.status.cur == 1) || ($trivia.fail)) { return }
  if ( ($1 != $null) && ($1 <= $numq ) && ($int($1) > 0) ) { %trivia.status.number = $int($1) }
  else { %trivia.status.number = %trivia.default }
  %trivia.status.question = 0
  %trivia.status.asked = 0
  if ($1 == unlimited) {
    %trivia.status.number = Unlimited
  }
  msg %trivia.channel $font $msg.trivia.started $+ 
  if ( $1 == team ) {
    %trivia.teammode = 1
    %team.score.1 = 0
    %team.score.2 = 0
    msg %trivia.channel $font $msg.team.start $+ 
  }
  %trivia.status.cur = 1
  .timertrivia1 1 %trivia.lag.start trivq
}
alias strivia {
  if (%trivia.status.cur == 0) { return }
  if ( %trivia.teammode == 1 ) {
    if (%team.score.1 == %team.score.2) { var %temp = %trivia.team.0.victory }
    if (%team.score.1 > %team.score.2) { var %temp = %trivia.team.1.victory }
    if (%team.score.1 < %team.score.2) { var %temp = %trivia.team.2.victory }
    msg %trivia.channel $font $replace($replace(%temp, *t1, %trivia.team.1),*t2, %trivia.team.2) $+ ! $+ 
  }
  unset %trivq.*
  unset %asked.*
  unset %question.*
  if (%trivia.teammode == 0) { msg %trivia.channel $font $msg.trivia.stopped $+  }
  if ((%trivia.teammode == 0) && ( %trivia.status.question >= 10)) { msg %trivia.channel $font $msg.trivia.records $+  }
  unset %team.*  
  %trivia.teammode = 0
  %trivia.status.cur = 0
  .timertrivia* off
  savem
  haltdef
}
alias trivq {
  if (%trivia.status.bot == 0) { echo The trivia bot is off. | strivia }
  if (%trivia.status.cur == 0) { return }
  inc %trivia.status.question
  inc %trivia.status.asked
  if (%trivia.status.asked > $numq) { unset %asked.* | %trivia.status.asked = 1 }
  if (%trivia.ask != $null) { $setask( %trivia.ask ) }
  while ($is_repeat) { $setask( $rand(0, $numq) ) }
  %trivq.question = $read( %trivia.questions, $asked( %trivia.status.asked ) )
  %trivq.answer = $tok(2)
  unset %trivia.ask
  msg %trivia.channel $font $msg.question.current $+ 
  %question.answered = $false
  .timertrivia2 1 %trivia.lag.timedout trivx
  if ( %trivia.status.autohint == 1 ) { .timertrivia3 1 %trivia.lag.hint hint }
  %question.start = $ticks
  echo 4 %trivia.channel -> %trivq.answer -> $asked( %trivia.status.asked )
}
alias get.vowels {
  var %vowels
  set %voweltmp $regsub(%trivq.answer,/([^aeiouAEIOU ])/g,_,%vowels)
  return %vowels
}
alias is_repeat { 
  if ($asked( %trivia.status.asked ) == $null) { return $true }
  if ($read(%trivia.questions, $asked(%trivia.status.asked)) == $null) { return $true }
  var %i = 1
  while (%i < %trivia.status.asked) {
    if ( $asked( %i ) == $asked( %trivia.status.asked ) ) { return $true }
    inc %i
  }
  return $false
}
alias asked { return % [ $+ asked. $+ [ $1 ] ] }
alias setask { % [ $+ asked. $+ [ %trivia.status.asked ] ] = $1 }
alias trivg {
  if (%question.answered == $false) {
    msg %trivia.channel $font $msg.trivia.give_up $+ 
    %trivia.row = 0
  }
  .timertrivia* off
  unset %trivq.*
  if (($score($nick) >= %trivia.reset) && (%trivia.status.champ == 1)) { 
    unset %hof*
    unset %player.*
    unset %record.*
    inc %trivia.hof.champ.size
    %trivia.hof.champ. [ $+ [ %trivia.hof.champ.size ] ] = %trivia.hof.champ
    %trivia.hof.champ = %trivia.lastwinner
    msg %trivia.channel $font $msg.trivia.victory $+ 
    strivia
  }
  if (%trivia.status.number != unlimited) && ( %trivia.status.question >= %trivia.status.number ) { strivia }
  var %temp.lag
  if (%question.answered) { %temp.lag = %trivia.lag.answered }
  else { %temp.lag = %trivia.lag.timed }
  .timertrivia1 1 %temp.lag trivq
  unset %question.*
}
alias trivx {
  if (%question.answered == $false) {
    msg %trivia.channel $font $msg.trivia.times_up $+ 
    %trivia.row = 0
  }
  .timertrivia* off
  unset %trivq.*
  if (($score($nick) >= %trivia.reset) && (%trivia.status.champ == 1)) { 
    unset %hof*
    unset %player.*
    unset %record.*
    inc %trivia.hof.champ.size
    %trivia.hof.champ. [ $+ [ %trivia.hof.champ.size ] ] = %trivia.hof.champ
    %trivia.hof.champ = %trivia.lastwinner
    msg %trivia.channel $font $msg.trivia.victory $+ 
    strivia
  }
  if (%trivia.status.number != unlimited) && ( %trivia.status.question >= %trivia.status.number ) { strivia }
  var %temp.lag
  if (%question.answered) { %temp.lag = %trivia.lag.answered }
  else { %temp.lag = %trivia.lag.timed }
  .timertrivia1 1 %temp.lag trivq
  unset %question.*
}
alias answered {
  if (( %trivia.teammode == 1 ) && ( $teamname( $nick ) == $null)) { return }
  %question.time = $question.time
  inc % [ $+ player. $+ [ $nick ] ]
  if (%trivia.lastwinner == $nick) { inc %trivia.row }
  else { %trivia.row = 1 | %trivia.lastwinner = $nick }
  if ((%trivia.row > %record.row) || (%record.row == $null)) { %record.row = %trivia.row | %record.name = $nick }
  if ((%question.time < %record.time) || (%record.time == $null)) { %record.time = %question.time | %record.time.name = $nick }
  msg %trivia.channel $font $msg.answer.correct $+ 
  award
  if ( $getrank(%trivia.lastwinner) == $null ) {
    % [ $+ hof $+ [ $hof.size ] $+ .name ] = %trivia.lastwinner
  }
  sort
  if (%trivia.teammode == 1) {
    inc % [ $+ team.score. $+ [ % [ $+ team. $+ [ %trivia.lastwinner ] ] ] ] )
    msg %trivia.channel $font $msg.team.score $+ 
  }
  %question.answered = $true
  trivx
  halt
}
alias hof.size {
  var %i = 1
  while ($hofname(%i) != $null) {
    inc %i
  }
  return %i
}
alias sort {
  var %i = 2
  while ($hofname(%i) != $null) {
    var %inserted = $false
    var %j = %i
    while ((%j >= 1) && (%inserted == $false)) {
      if ($hofscore(%j) > $hofscore($calc(%j - 1))) {
        var %temp = $hofname(%j)
        % [ $+ hof $+ [ %j ] $+ .name ] = $hofname($calc(%j - 1))
        % [ $+ hof $+ [ $calc(%j - 1) ] $+ .name ] = %temp
      }
      else { %inserted = $true }
      dec %j
    }
    inc %i
  }
}
alias award {
  var %temp = % [ $+ trivia.award.score. $+ [ $score($nick) ] ]
  if (%temp != $null) {
    %temp = $replace(%temp,^, $score($nick))
    %temp = $replace(%temp,*, $nick)
    describe %trivia.channel %temp 
  }
  var %temp = % [ $+ trivia.award.row. $+ [ %trivia.row ] ]
  if (%temp != $null) { 
    %temp = $replace(%temp,^, $+ %trivia.row $+ )
    %temp = $replace(%temp,*, $+ $nick $+ )
    describe %trivia.channel %temp 
  }
}
alias getrank {
  var %i = 1
  while ($hofname(%i) != $null) {
    if ( $1 == $hofname(%i)) { return %i }
    inc %i
  }
}
alias setteam {
  if (%trivia.status.question >= 2) { notice $nick $msg.team.joinover | return }
  if (($2 == 1) || ($2 == 2)) {
    %add.team = %trivia.team. [ $+ [ $2 ] ]
    %add.name = $nick
    %team. [ $+ [ $1 ] ] = $2
    msg %trivia.channel $font $msg.team.add $+ 
    unset %add.*
  }
}
alias fix { 
  var %temp = $1-
  %temp = $remove(%temp,',-,`,.,%,the)
  %temp = $replace(%temp,kn,n)
  %temp = $replace(%temp,y,i)
  %temp = $replace(%temp,k,c)
  %temp = $replace(%temp,x,c)
  %temp = $replace(%temp,q,c)
  %temp = $replace(%temp,e,a)
  %temp = $replace(%temp,u,o)
  %temp = $replace(%temp,ph,f)
  %temp = $replace(%temp,m,n)
  %temp = $replace(%temp,ll,l)
  %temp = $replace(%temp,aa,a)
  %temp = $replace(%temp,oo,o)
  %temp = $replace(%temp,cc,c)
  if ($right(%temp, 1) == s) { %temp = $left(%temp, $calc($len(%temp) - 1)) }
  return %temp
}
alias hint { msg %trivia.channel $font $msg.question.hint $+  }
alias savem { save -rv vars.ini | %trivia.status.save = 0 }
alias savevar { inc %trivia.status.save | if (%trivia.status.save == 5) { savem } }
alias numq { return $lines(%trivia.dir $+ \ $+ %trivia.questions) }
alias tokq { return $numtok(%trivq.question, $asc(%trivia.delimiter)) }
alias question.time { return $calc(($ticks - %question.start) / 1000) }
alias score { if (% [ $+ player. $+ [ $1 ] ] == $null) { return 0 } | return % [ $+ player. $+ [ $1 ] ] }
alias hofname { return % [ $+ hof $+ [ $1 ] $+ .name ] }
alias hofscore { return $score($hofname($1)) }
alias teamscore { return ( % [ $+ team.score. $+ [ % [ $+ team. $+ [ $1 ] ] ] ] ) }
alias teamname { return %team. [ $+ [ $1 ] ] }
alias tok { return $gettok(%trivq.question,$1,$asc(%trivia.delimiter)) }
alias ask {
  var %tempnumber
  if ($1 == $null) { %tempnumber = $?="Force which question (number) to be asked?" }
  else { %tempnumber = $1 }
  if (( %tempnumber < 1 ) || ( %tempnumber > $numq )) { echo Error, invalid | return }
  %trivia.ask = %tempnumber 
}

alias trivon { 
  if (%trivia.status.bot == 1) { return On }
  else { return Off }
}
alias trivia.chan { 
  if (%trivia.channel !ischan) { return No }
  else { return Yes }
}
