All pastes #222268 Raw Edit

Mine

public text v1 · immutable
#222268 ·published 2006-10-26 05:02 UTC
rendered paste body
; ***********************************************************
; * Trivia Script Written by Urza                           *
; * Email: Urza23@hotmail.com                               *
; * Planned updates: Add input events for local usage       *
; * Usage: right click a channel and click Trivia Options   *
; *        to view the options dialog                       *
; *        !trivia on/off to start/end trivia               *
; *        !hint for hints                                  *
; *        !next to go to the next question                 *
; *        !repeat to repeat the last question              *
; *        !trivia help to view this commands list          *
; * Notes: If questions file does not exist 6 defaults are  * 
; *                                          loaded         *
; ***********************************************************
menu Channel,menubar {
  Trivia Options: dialog $iif($dialog(TriviaOptions),-v,-m) TriviaOptions TriviaOptions 
}
dialog -l TriviaOptions {
  title "Trivia Options"
  size -1 -1 164 207
  option dbu

  tab "Options", 1, 0 -1 161 188
  box "Times", 2, 4 14 153 73, tab 1
  text "", 3, 93 33 54 49, tab 1
  text "Before first question:", 4, 8 24 60 8, tab 1
  edit "", 5, 70 23 15 10, tab 1
  text "To answer questions:", 6, 8 34 60 8, tab 1
  edit "", 7, 70 33 15 10, tab 1
  text "Between questions:", 8, 8 44 60 8, tab 1
  edit "", 9, 70 43 15 10, tab 1
  text "Before !next allowed:", 10, 8 54 60 8, tab 1
  edit "", 11, 70 53 15 10, tab 1
  text "Before hints allowed:", 12, 8 64 60 8, tab 1
  edit "", 13, 70 63 15 10, tab 1
  box "Allow/Restrict", 14, 4 90 152 92, tab 1
  check "Hints", 15, 8 111 29 10, tab 1
  text "Restrict to:", 16, 45 112 29 8, tab 1
  combo 17, 75 110 60 10, tab 1 drop
  check "Hint 1: Shows answer spacing", 18, 45 121 95 10, tab 1
  check "Hint 2: Shows Vowels", 19, 45 130 85 10, tab 1
  check "Hint 3: Shows first half of the answer", 20, 45 139 107 10, tab 1
  check "Hint 4: Shows first half of each word", 21, 45 148 101 10, tab 1
  check "!Next", 22, 8 158 29 10, tab 1
  text "Restrict to:", 23, 45 160 29 8, tab 1
  combo 24, 75 158 60 10, tab 1 drop
  text "Enable/Disable Trivia - Restrict to:", 69, 7 99 84 8, tab 1
  edit "", 68, 70 73 15 10, tab 1
  text "Between hints:", 67, 8 74 60 8, tab 1
  combo 70, 92 98 60 10, tab 1 drop
  combo 92, 75 170 60 10, tab 1 drop
  text "Restrict to:", 91, 45 172 29 8, tab 1
  check "!Repeat", 90, 8 170 29 10, tab 1


  tab "Options 2", 66
  check "Allow answer anywhere in line", 25, 8 129 86 10, tab 66
  check "Enable spell check. Answer must be", 26, 8 144 95 10, tab 66
  edit "", 27, 103 145 15 10, tab 66
  text "% right.", 28, 119 146 23 8, tab 66
  check "Disable after ", 71, 8 114 40 10, tab 66
  edit "", 72, 50 115 15 10, tab 66
  text "Unanswered questions", 73, 67 116 65 8, tab 66
  check "Maximum of ", 74, 8 99 40 10, tab 66
  edit "", 75, 48 99 15 10, tab 66
  text "Guesses per user", 76, 65 100 51 8, tab 66
  box "Colours", 77, 4 16 153 62, tab 66
  text "Colour 1:", 78, 8 26 25 8, tab 66
  combo 79, 36 25 56 10, tab 66 drop
  check "Bold", 80, 95 24 22 10, tab 66
  check "Underline", 81, 118 24 37 10, tab 66
  check "Background", 82, 95 34 40 10, tab 66
  combo 87, 36 55 55 10, tab 66 drop
  text "Colour 2:", 89, 7 56 25 8, tab 66
  combo 88, 36 65 55 10, tab 66 drop
  check "Background", 84, 94 64 40 10, tab 66
  check "Bold", 86, 94 54 22 10, tab 66
  check "Underline", 85, 117 54 37 10, tab 66
  combo 83, 36 35 56 10, tab 66 drop
  box "Assorted Options", 93, 5 90 152 69, tab 66
  link "Restore default options", 94, 5 175 100 10, tab 66

  tab "Access", 29
  box "Current Trivia Games", 30, 4 14 153 80, tab 29
  text "Trivia is currently running in the following rooms:", 31, 7 23 132 9, tab 29
  list 32, 7 33 147 45, tab 29 size vsbar
  button "Disable in this channel", 33, 47 79 66 12, tab 29
  box "Trivia Access", 34, 3 95 153 90, tab 29
  text "Allowed:", 35, 6 104 60 8, tab 29 center
  combo 36, 6 113 61 50, tab 29 size
  button "Add", 37, 70 113 20 12, tab 29
  button "Delete", 38, 70 136 20 12, tab 29
  text "Blocked:", 39, 92 104 60 8, tab 29 center
  combo 40, 92 113 60 50, tab 29 size
  radio "Disable by default, Allow only in specified channels", 41, 8 163 134 10, tab 29
  radio "Enable by default, Block in specified channels", 42, 8 172 132 10, tab 29


  tab "Questions", 43
  text "Questions:", 44, 4 14 31 8, tab 43
  list 45, 4 22 154 57, tab 43 size hsbar vsbar
  button "Add", 46, 4 82 40 15, tab 43
  button "Edit", 47, 63 82 40 15, tab 43
  button "Delete", 48, 118 82 40 15, tab 43
  text "Answers:", 49, 6 99 25 8, tab 43
  list 50, 4 108 154 50, tab 43 size hsbar vsbar
  button "Add", 51, 4 161 40 15, tab 43
  button "Edit", 52, 63 161 40 15, tab 43
  button "Delete", 53, 118 161 40 15, tab 43
  link "Click here to view the questions file", 100, 40 177 87 10, tab 43


  tab "Points", 54
  box "Select score set", 55, 4 14 154 44, tab 54
  radio "Overall Scores", 56, 8 24 50 10, tab 54
  radio "Network Scores", 57, 8 34 50 10, tab 54
  combo 58, 64 34 89 10, tab 54 drop
  radio "Channel Scores", 59, 8 44 50 10, tab 54
  combo 60, 64 44 89 10, tab 54 drop
  list 61, 3 62 154 105, tab 54 size
  button "Give Points", 62, 4 171 42 12, tab 54
  button "Remove points ", 63, 115 171 42 12, tab 54
  button "Set points to", 64, 60 171 42 12, tab 54


  button "Close", 65, 122 192 37 12, ok
}

on *:dialog:TriviaOptions:init:*: {
  did -a $dname 17,24,70,92 Op and above
  did -a $dname 17,24,70,92 Halfop and above
  did -a $dname 17,24,70,92 Voice and above
  did -a $dname 17,24,70,92 Anybody
  did -a $dname 3 Enter the desired time in seconds for each option. $&
    If left blank or an invalid entry is made the defaults will be used
  did -b $dname 58,60

  var %x = 1, %colours = white,black,blue,green,red,dark red,purple,orange,yellow, $&
    light green,aqua,cyan,light blue,pink,grey,light grey
  while (%x <= $numtok(%colours,44)) {
    did -a $dname 79,83,87,88 $gettok(%colours,%x,44)
    inc %x
  }

  var %x = 1, %total = $ini(TriviaOptions.ini,Allowed,0)
  while (%x <= %total) {
    did -a $dname 36 $ini(TriviaOptions.ini,Allowed,%x)
    inc %x
  }

  var %x = 1, %total = $ini(TriviaOptions.ini,Blocked,0)
  while (%x <= %total) {
    did -a $dname 40 $ini(TriviaOptions.ini,Blocked,%x)
    inc %x
  }


  var %x = 1, %total = $lines(Questions.txt)
  while (%x <= %total) {
    var %question = $gettok($read(Questions.txt,%x),1,124)
    did -a $dname 45 %question
    inc %x
  }

  var %x = 1, %total = $hfind(TriviaScores,*.Global*.*,0,w)
  while (%x <= %total) {
    var %item = $hfind(TriviaScores,*.Global.*,%x,w), %network = $gettok(%item,1,46)
    var %pos = $pos(%item,.Global.,1), %network = $left(%item,$calc(%pos - 1))
    if (!$istok(%added,%network,124)) did -a $dname 58 %network
    var %added = $addtok(%added,%network,124)
    inc %x
  }
  unset %added
  did -c $dname 58 1

  var %x = 1, %total = $hfind(TriviaScores,*.#*.*,0,w)
  while (%x <= %total) {
    var %item = $hfind(TriviaScores,*.#*.*,%x,w)
    var %pos = $pos(%item,.#,1), %network = $left(%item,$calc(%pos - 1))
    var %lasttok = $calc($numtok(%item,46) - 1), %firsttok = $calc($numtok(%network,46) + 1)
    var %chan = $gettok(%item,$+(%firsttok,-,%lasttok),46)

    if (!$istok(%added,$+(%network,%chan),124)) did -a $dname 60 %chan $+($chr(40),%network,$chr(41))
    var  %added = $addtok(%added,$+(%network,%chan),124)
    inc %x
  }
  did -c $dname 60 1

  LoadOptions
  LoadGames
}

on *:dialog:TriviaOptions:sclick:*: {
  ; End a Game 
  if ($did == 94) { 
    DefaultOptions
    LoadOptions
  }
  elseif ($did == 33) {
    if ($did(32).sel) {
      var %item = $did(32).seltext, %chan = $gettok(%item,1,32)
      var %net = $remove($gettok(%item,2-,32),$chr(40),$chr(41))
      var %cid = $getcid(%net)
      StopTriv %chan %cid
      scid %cid msg %chan $c2($me) $c1(has stopped trivia)
    }
    else var %x = $input(Error: You need to select a game to end first,oh,Error)
  }
  ; Add Channel to access
  elseif ($did == 37) {
    var %access = $readini(TriviaOptions.ini,Options,AccessType)
    var %did = $iif(%access == Enable,40,36), %item = $did(%did)
    var %x = 1, %total = $did(%did).lines
    while (%x <= %total) {
      if (%item == $did(%did,%x)) halt
      inc %x
    }
    did -a $dname %did %item
    writeini TriviaOptions.ini $iif(%access == Enable,Blocked,Allowed) %item 1
  }
  ; Delete Channel from access
  elseif ($did == 38) { 
    var %access = $readini(TriviaOptions.ini,Options,AccessType)
    var %did = $iif($readini(TriviaOptions.ini,Options,AccessType) == Enable,40,36), %item = $did(%did)
    did -d $dname %did $did(%did).sel
    remini TriviaOptions.ini $iif(%access == Enable,Blocked,Allowed) %item
  }
  ; Load Answers
  elseif ($did == 45) {
    did -r $dname 50
    var %line = $read(Questions.txt,$did(45).sel), %answers = $numtok(%line,124), %x = 2
    while (%x <= %answers) {
      did -a $dname 50 $gettok(%line,%x,124)
      inc %x
    }
  }
  ; Add Question
  elseif ($did == 46) {
    var %question = $$input(Enter the question you would like to add.,ekq,Enter Question)
    var %answer = $$input(Your question has been added. $crlf $+ $&
      Enter the first answer to this question $crlf $+ $&
      Further answers can be added using the add button below the Answers section,ekq,Add Answer)
    did -a $dname 45 %Question
    did -c $dname 45 $did(45).lines
    did -r $dname 50
    did -a $dname 50 %answer
    write Questions.txt $+(%question,$chr(124),%answer)
  }
  ; Edit Question
  elseif ($did == 47) {
    if ($did(45).sel) {
      var %line = $v1, %Question = $did(45).seltext, %answers = $gettok($read(Questions.txt,%line),2-,124)
      var %NewQuestion = $$input(Enter the new question,ekq,Enter Question,%Question)
      write -ol $+ %line Questions.txt %NewQuestion $+ $iif(%answers,$chr(124) $+ %answers)
      did -o $dname 45 %line %NewQuestion
      did -c $dname 45 %line
    }
    else var %x = $input(Error: you need to select a question to edit first,ho,Error)
  }
  ; Delete Question
  elseif ($did == 48) {
    if ($did(45).sel) {
      var %line = $v1
      write -dl $+ %line Questions.txt
      did -d $dname 45 %line
      did -r $dname 50
    }
    else var %x = $input(Error: you need to select a question to delete first,ho,Error)
  }
  ; Add Answer
  elseif ($did == 51) {
    if ($did(45).sel) {
      var %lineNum = $v1, %line = $read(Questions.txt,%linenum)
      var %answer = $$input(Enter the answer you would like to add.,ekq,Enter Answer)
      write -ol $+ %lineNum Questions.txt $+(%line,$chr(124),%answer)
      did -a $dname 50 %answer
    }
    else var %x = $input(Error: You need to select a question before adding answers,ho,Error)
  }
  ; Edit Answer
  elseif ($did == 52) {
    if ($did(50).sel) && ($did(45).sel) {
      var %lineNum = $did(45).sel, %line = $read(Questions.txt,%lineNum)
      var %AnswerNum = $did(50).sel, %answer = $did(50).seltext, %token = $calc(%AnswerNum + 1)
      var %NewAnswer = $$input(Enter the new answer,ekq,Enter Answer,%answer)
      write -ol $+ %lineNum Questions.txt $puttok(%line,%NewAnswer,%token,124)
      did -o $dname 50 %AnswerNum %NewAnswer
      did -c $dname 50 %AnswerNum
    }
    else var %x = $input(Error: You need to select an answer to edit first,ho,Error)
  }
  ; Delete Answer
  elseif ($did == 53) {
    if ($did(50).lines == 1) var %x = $input(Error: A question must always have atleast one answer. $crlf $+ $&
      Use the edit button if you want to change this answer,oh,Error)
    elseif ($did(50).sel) && ($did(45).sel) {
      var %lineNum = $did(45).sel, %line = $read(Questions.txt,%lineNum)
      var %AnswerNum = $did(50).sel, %answer = $did(50).seltext, %token = $calc(%AnswerNum + 1)
      var %x = $deltok(%line,%token,124)
      write -ol $+ %lineNum Questions.txt $deltok(%line,%token,124)
      did -d $dname 50 %AnswerNum 
    }
    else var %x = $input(Error: You need to select an answer to delete first,ho,Error)
  }
  ; Run Questions file
  elseif ($did == 100) run Questions.txt

  ; Score radio buttons
  elseif ($did isnum 56-60) {
    did -r $dname 61
    if ($did isnum 57-58) {
      var %search = $+($did(58),.Global.*)
      did -b $dname 60
      did -e $dname 58
    }
    elseif ($did isnum 59-60) {
      var %chan = $gettok($did(60),1,32)
      var %network = $remove($gettok($did(60),2-,32),$chr(40),$chr(41))
      var %search = $+(%network,.,%chan,.*)
      did -b $dname 58
      did -e $dname 60
    }
    else {
      var %search = Global.*
      did -b $dname 58,60
    }
    ShowPoints %search
  }
  ; Edit Points
  elseif ($did isnum 62-64) {
    if ($did(61).sel) {
      ;62 = Add, 63 = Remove, 64 = Set to
      if ($did == 62)  var %points = $$input(How many points would you like to give?,kqe,Give Points)
      elseif ($did == 63) var %points =  $$input(How many points would you like to take away?,keq,Remove Points)
      else var %points = $$input(What would you like to set the points to?,keq,Set Points)

      var %nick = $gettok($did(61).seltext,1,32)
      if ($did(56).state == 1) var %search = Global.*
      elseif ($did(57).state == 1) var %search = $+($did(58),.Global.*)
      else {
        var  %chan = $gettok($did(60),1,32)
        var %network = $remove($gettok($did(60),2-,32),$chr(40),$chr(41))
        var %search = $+(%network,.,%chan,.*)
      }
      var %item = $+($left(%search,-1),%nick)

      if ($did == 62) && (%points isnum 1-) hinc TriviaScores %item %points
      elseif ($did == 63) && (%points isnum 1-) hdec TriviaScores %item %points
      elseif ($did == 64) && (%points isnum) hadd TriviaScores %item %points
      elseif ($did == 63) var %x = $input(Error: You must enter a number,oh,Error)
      else var %x = $input(Error: You must enter a positive number,oh,Error)
      ShowPoints %search
    }
    else var %x = $input(Error: You must select a user first,oh,Error)
  }

  SaveOptions 
  LoadOptions
}

on *:dialog:TriviaOptions:close:*: {
  SaveOptions
}

on *:start: {
  if (!$hget(TriviaScores)) {
    hmake TriviaScores 100
    if ($exists(TriviaScores.hsh)) hload TriviaScores TriviaScores.hsh
  }
  .timerTRIVIABACKUP 0 600 if ($hget(TriviaScores)) hsave TriviaScores TriviaScores.hsh
}

on *:text:*:#: {
  tokenize 32 $strip($1-)

  if ($readini(TriviaOptions.ini,Options,accesstype) == disable) {
    if ($checklist(allowed,$chan)) || ($checklist(allowed,$+($network,.,$chan))) var %status = allow
    else var %status = block
  }
  else { 
    if ($checklist(blocked,$chan)) || ($checklist(blocked,$+($network,.,$chan))) var %status = block
    else var %status = allow
  }
  if (%status == block) halt


  if ($1 == !trivia) {
    if ($istok(on|start|begin,$2,124)) {
      if ($CheckPermissions(OnOff)) halt
      elseif ($timer($+(TRIVIA.,$chan,.,$cid))) msg $chan $c1(Trivia is already running)
      else {
        if (!$exists(Questions.txt)) DefaultQuestions
        if (!$exists(TriviaOptions.ini)) DefaultOptions
        msg $chan $c2($nick) $c1(has started trivia. First question in) $c2($GetTime(FirstQ)) $c1(seconds)
        $+(.timerTRIVIA.,$chan,.,$cid) 1 $GetTime(FirstQ) AskQuestion $chan
        if ($dialog(TriviaOptions)) LoadGames

        if (!$hget(TriviaScores)) {
          hmake TriviaScores 100
          if ($exists(TriviaScores.hsh)) hload TriviaScores TriviaScores.hsh
          .timerTRIVIABACKUP 0 600 if ($hget(TriviaScores)) hsave TriviaScores TriviaScores.hsh
        }
      }
    }
    elseif ($istok(off|stop|end,$2,124)) {
      if ($CheckPermissions(OnOff)) halt
      elseif (!$timer($+(TRIVIA.,$chan,.,$cid))) msg $chan $c1(Trivia is not running)
      else {
        msg $chan $nick has stopped trivia. 
        StopTriv $chan $cid
      }
    }
    elseif ($istok(help|commands,$2,124)) {
      .notice $nick Trivia commands list has been sent via query (PM)
      .msg $nick Trivia Commands List
      .msg $nick !trivia on/off to start/end trivia               
      .msg $nick !hint for hints                                  
      .msg $nick !next to go to the next question                 
      .msg $nick !repeat to repeat the last question                    
    }
  } 

  elseif ($1 == !hint) {
    if ($CheckPermissions(Hints)) halt

    if ($($+(%,TRIVIAquestion.,$chan,$cid),2)) {
      var %QTime = $GetTime(AnswerQ), %BeforeHint = $GetTime(BeforeHint)
      var %MaxSecondsLeft = $calc(%QTime - %BeforeHint)
      var %secssofar = $calc($timer($+(TRIVIA.,$chan,.,$cid)).secs - %maxsecondsleft)
      if ($timer($+(TRIVIA.,$chan,.,$cid)).secs > %MaxSecondsLeft) {
        msg $chan $c1(You must wait for atleast) $c2(%secssofar) $&
          $c1(more seconds before hints are allowed)
      }
      elseif ($($+(%,HINT.,$chan,$cid),2)) {
        msg $chan $c1(You must wait for atleast) $c2($($+(%,HINT.,$chan,$cid),2)) $c1(seconds before another hint is allowed)
      }
      else {
        msg $chan $c1($MakeHint($chan)) $&
          $+($c1($chr(40)),$c2($timer($+(TRIVIA.,$chan,.,$cid)).secs) $c1(seconds remaining),$c1($chr(41)))
        set -z $+(%,HINT.,$chan,$cid) $GetTime(BetweenHints)
      }
    }
    elseif ($timer($+(TRIVIA.,$chan,.,$cid))) {
      msg $chan $c1(I cant give a hint if no question has been asked.) $&
        $+($c1($chr(40)),$c1(next question in) $c2($timer($+(TRIVIA.,$chan,.,$cid)).secs) $c1(seconds),$c1($chr(41)))
    }
  }

  elseif ($1 == !repeat) {
    if ($CheckPermissions(Repeat)) halt
    if ($($+(%,TRIVIAquestion.,$chan,$cid),2)) {
      msg $chan $c1(The last question asked was:) $c1($($+(%,TRIVIAquestion.,$chan,$cid),2)) $&
        $+($c1($chr(40)),$c2($timer($+(TRIVIA.,$chan,.,$cid)).secs) $c1(seconds remaining),$c1($chr(41)))
    }
    elseif ($timer($+(TRIVIA.,$chan,.,$cid))) {
      msg $chan $c1(No question has been asked.) $&
        $+($c1($chr(40)),$c1(next question in) $c2($timer($+(TRIVIA.,$chan,.,$cid)).secs) $c1(seconds),$c1($chr(41)))
    }
  }

  elseif ($1 == !next) {
    if ($CheckPermissions(Next)) halt
    if ($($+(%,TRIVIAquestion.,$chan,$cid),2)) {
      var %QTime = $GetTime(AnswerQ), %BeforeNext = $GetTime(BeforeNext)
      var %MaxSecondsLeft = $calc(%QTime - %BeforeNext)
      var %secssofar = $calc($timer($+(TRIVIA.,$chan,.,$cid)).secs - %maxsecondsleft)     
      if ($timer($+(TRIVIA.,$chan,.,$cid)).secs > %MaxSecondsLeft) {
        msg $chan $c1(You must wait for atleast) $c2(%secssofar) $&
          $c1(more seconds before using this command)
      }
      else AskQuestion $chan
    }
    elseif ($timer($+(TRIVIA.,$chan,.,$cid))) AskQuestion $chan
  }

  elseif ($1 == !score) {
    if (chan* iswm $2) || ($2 == server) var %nick = $nick
    else var %nick = $iif($2,$2,$nick)

    if ($2 == server) || ($3 == server) {
      var %rank = $getrank(%nick,$network).server
      var %search = $+($network,.Global.,%nick)
    }
    elseif (chan* iswm $2) || (chan* iswm $3) {
      var %rank = $getrank(%nick,$network,$chan).chan
      var %search = $+($network,.,$chan,.,%nick)
    }
    else var %rank = $getrank(%nick), %search = $+(Global.,%nick)

    var %points = $hget(TriviaScores,%search) 

    if (%rank) msg $chan %nick is currently ranked %rank %type with %points points
    else msg $chan $c2(%nick) $c1(has not scored any points %type)
  }

  elseif ($1 == !Top5) { 
    if ($2 == server) var %search = $+($network,.Global.*), %type = on $network
    elseif (chan* iswm $2) var %search = $+($network,.,$chan,.*), %type = on $chan
    else var %search = Global.*, %type = overall

    var %x = 1, %total = $hfind(TriviaScores,%search,0,w)
    write -c Temp.txt
    while (%x <= %total) { 
      var %item = $hfind(TriviaScores,%search,%x,w)
      write Temp.txt %item $hget(TriviaScores,%item)
      inc %x
    }
    filter -ffcteu 2 32 temp.txt SortedTemp.txt
    var %x = 1
    while (%x <= 5) {
      var %line = $read(SortedTemp.txt,%x), %nick = $gettok(%line,1,32), %pts = $gettok(%line,2,32)
      var %tokens = $numtok(%nick,46), %nick = $gettok(%nick,%tokens,46)
      if (%line) var %results = %results $+($c2(%x),$c1($chr(41))) $c2(%nick) $+($c1([),$c2(%pts) $c1(pts),$c1(]))
      else var %x = 5
      inc %x
    }
    if (%results) msg $c2($chan) $c1(Current top 5 %type $+ :) %results
    else msg $chan $c1(Nobody has scored any points $iif(%search != overall,%type))
    .remove Temp.txt
    .remove SortedTemp.txt
  }


  elseif ($timer($+(TRIVIA.,$chan,.,$cid))) {
    if ($($+(%,GUESSES.,$nick,$chan,$cid),2) >= $readini(TriviaOptions.ini,Options,MaxGuesses)) {
      if ($v1 == $v2) {
        notice $nick $c1(You have had too many guesses. Any further guesses will not count)
      }
      halt
    } 
    elseif ($CheckAnswers($nick,$chan,$1-)) {
      var %answer = $v1
      $+(.timerTRIVIA.,$chan,.,$cid) off
      unset $+(%,TRIVIA*.,$chan,$cid)
      msg $chan $c1(Congratulations) $c2($+($nick,.)) $c1(The answer was) $c2(%answer)
      msg $chan $c1(Next question in) $c2($GetTime(BetweenQ)) $c1(seconds)
      hinc TriviaScores $+(Global.,$nick)
      hinc TriviaScores $+($network,.Global.,$nick)
      hinc TriviaScores $+($network,.,$chan,.,$nick)
      $+(.timerTRIVIA.,$chan,.,$cid) 1 $GetTime(BetweenQ) AskQuestion $chan
    }
    elseif ($readini(TriviaOptions.ini,Options,MaxGuessesState) == 1) {
      if ($($+(%,TRIVIAquestion.,$chan,$cid),2)) {
        inc $+(%,GUESSES.,$nick,$chan,$cid)
      }
    }
  }
}

on *:part:#: {
  if ($nick == $me) {
    unset $+(%,TRIVIA*.,$chan,$cid)
    $+(.timerTRIVIA.,$chan,.,$cid) off
  }
} 
on *:exit: {
  hsave TriviaScores TriviaScores.hsh
  unset %TRIVIA*
}

alias -l AskQuestion {
  var %chan = $1, %line = $read(Questions.txt)
  unset $+(%,GUESSES.,*,$(%chan,2),$cid)
  set $+(%,TRIVIAquestion.,%chan,$cid) $gettok(%line,1,124)
  set $+(%,TRIVIAanswers.,%chan,$cid) $gettok(%line,2-,124)
  msg %chan $c1($($+(%,TRIVIAquestion.,%chan,$cid),2))
  $+(.timerTRIVIA.,%chan,.,$cid) 1 $GetTime(AnswerQ) NextQuestion %chan
}
alias -l c1 { return $makecol(1,$1-) }
alias -l c2 { return $makecol(2,$1-) }
alias -l CheckAnswers {
  var %nick = $1, %chan = $2
  tokenize 32 $3-
  var %answers = $($+(%,TRIVIAanswers.,%chan,$cid),2)

  if ($readini(TriviaOptions.ini,Options,AnswerAnywhere) == 1) {
    var %x = 1, %AnswerToks = $numtok(%answers,124), %SentToks = $0
    while (%x <= %AnswerToks) {
      var %y = 1, %Answer = $gettok(%Answers,%x,124), %TokensInAnswer = $numtok(%answer,32)
      while (%y <= %SentToks) {
        var %z = 0, %check
        while (%z < %TokensInAnswer) {
          var %Check = %check $($+($,$calc(%y + %z)),2)
          if ($readini(Triviaoptions.ini,Options,SpellCheck) == 1) {
            var %accuracy = $readini(TriviaOptions.ini,Options,SpellCheckAccuracy)
          }
          else var %accuracy = 100
          if ($compare(%Check,%answer) >= %accuracy) return %answer
          inc %z
        }        
        inc %y
      }
      inc %x
    }
  }
  else {
    var %x = 1, %AnswerToks = $numtok(%answers,124)
    while (%x <= %answertoks) {
      var %answer = $gettok(%answers,%x,124)
      if ($readini(Triviaoptions.ini,Options,SpellCheck) == 1) {
        var %accuracy = $readini(TriviaOptions.ini,Options,SpellCheckAccuracy)
      }
      else var %accuracy = 100
      if ($compare($1-,%answer) >= %accuracy) return %answer
      inc %x
    }
  }
}
alias -l checklist {
  var %list = $1, %item = $2, %x = 1, %total = $ini(TriviaOptions.ini,%list,0)
  while (%x <= %total) {
    var %citem = $ini(TrivaOptions.ini,%list,%x)
    if (%item == %citem) return $true
    inc %x
  }
  return $false
}
alias -l CheckPermissions {
  var %item = $1
  if ($readini(TriviaOptions.ini,Allow,%item) == 0) { 
    msg $chan $c1(This command is currently disabled)
    return halt 
  }
  if ($Readini(TriviaOptions.ini,Restrict,%item) < 4) {
    var %Restrict = $v1
    if (%Restrict == 1) && ($nick !isop $chan) var %block = 1, %Restriction = Op and above
    elseif (%restrict == 2) && ($nick !isop $chan) && ($nick !ishelp $chan) {
      var %block = 1, %Restriction = HalfOps and above
    }
    elseif (%restrict == 3) && ($nick isreg $chan) var %block = 1, %Restriction = Voice and above
    if (%block) {
      msg $chan $c1(This command is currently restricted to users with) $c2(%Restriction)
      return halt
    }
  }
}
alias Compare {
  if ($len($1) > $len($2)) var %longest = $1, %length = $len($1), %shortest = $2
  else var %longest = $2, %length = $len($2), %shortest = $1

  var %x = 1, %match = 0
  while (%x <= %length) {
    if ($mid($1,%x,1) isin $mid($2,$calc(%x - 1),3)) inc %match 
    inc %x
  }

  var %score = $calc(%match / %length  * 100)
  return %score
}
alias -l DefaultQuestions {
  write questions.txt "7x" was used to refer to the secret ingredient of what drink|coca cola
  write questions.txt "And the big wheel keep on turning neon burning up above and I'm just high on the world come on and take the low ride with me girl on the....." What's the Dire Straits song title?|tunnel of love
  write questions.txt "Beverly Hills Cop", when Axel Foley enters the hotel, he uses an alias. Who does he say he works for, and who does he say he's going to interview?|Rolling Stone,Michael Jackson
  write questions.txt "Little Boy" & "Fat Man" were the first|atomic bombs
  write questions.txt "Louis, 1 think this is the beginning of a beautiful friendship" are the last words of which film|casablanca
  write questions.txt "Never Say Never,"|Romeo Void
}
alias -l GetCid {
  var %x = 1, %total = $scon(0) 
  while (%x <= %total) {
    scon %x
    if ($network == $1) return $cid
    inc %x
  }
}
alias getrank {
  ;$getrank(N,NETWORK,CHANNEL).[CHAN|SERVER]

  if ($2) var %network = $2
  if ($3) var %chan = $3
  if ($prop == server) var %search = $+($network,.Global.*)
  elseif ($prop == chan) var %search = $+($network,.,$chan,.*)
  else var %search = Global.*

  var %x = 1, %total = $hfind(TriviaScores,%search,0,w)
  if ($1 == 0) return %total
  write -c Temp.txt
  while (%x <= %total) { 
    var %item = $hfind(TriviaScores,%search,%x,w)
    write Temp.txt %item $hget(TriviaScores,%item)
    inc %x
  }
  filter -ffcteu 2 32 temp.txt SortedTemp.txt
  if ($1 isnum) {
    var %data = $read(SortedTemp.txt,$1), %nick = $gettok(%data,1,32), %pts = $gettok(%data,2,32)
    var %tokens = $numtok(%nick,46), %nick = $gettok(%nick,%tokens,46)
    if (%data) var %return = %nick
  }
  else {
    var %search = $replace(%search,*,$1)
    var %data = $read(SortedTemp.txt,s,%search), %rank = $readn
    if (%data) var %return = %rank
  }

  .remove Temp.txt
  .remove SortedTemp.txt

  return %return
}
alias -l GetTime {
  return $readini(TriviaOptions.ini,Times,$1)
}
alias -l LoadGames {
  var %x = 1, %total = $timer(0)
  while (%x <= %total) {
    var %timer = $timer(%x)
    if ($regex(%timer,/TRIVIA\.(#.+).([0-9]+)/Si)) {
      var %chan = $regml(1), %network = $scid($regml(2)).network
      did -a TriviaOptions 32 %chan $+($chr(40),%network,$chr(41))
    }
    inc %x 
  }
}
alias -l makecol { 
  var %colour = $+(colour,$1)
  var %fg = $calc($readini(TriviaOptions.ini,%colour,Text) -1), %fg = $iif($len(%fg) == 1,0 $+ %fg,%fg)
  if ($readini(TriviaOptions.ini,%colour,BgState) == 1) {
    var %bg = $calc($readini(TriviaOptions.ini,%colour,BackGround) -1), %bg = $iif($len(%bg) == 1,0 $+ %bg,%bg)
  }
  var %bold = $readini(TriviaOptions.ini,%colour,Bold)
  var %underline = $readini(TriviaOptions.ini,%colour,Underline)

  var %col = $+($iif(%bold,),$iif(%underline,),,%fg,$iif(%bg,$chr(44) $+ %bg))
  return $+(%col,$2,$iif(%bold,),$iif(%underline,))
}
alias -l MakeHint {
  var %chan = $1, %answer = $gettok($($+(%,TRIVIAanswers.,%chan,$cid),2),1,124)
  var %x = 1
  while (%x <= 4) {
    var %hints = %hints $readini(TriviaOptions.ini,Hints,%x)
    inc %x
  }
  inc $+(%,TRIVIAhint.,%chan,$cid)
  while ($gettok(%hints,$($+(%,TRIVIAhint.,%chan,$cid),2),32) == 0) inc $+(%,TRIVIAhint.,%chan,$cid) 

  if ($($+(%,TRIVIAhint.,%chan,$cid),2) == 1) return Answer Spacing: $StarString(%answer)
  elseif ($($+(%,TRIVIAhint.,%chan,$cid),2) == 2) {
    var %x = 1, %length = $len(%answer)
    while (%x <= %length) {
      var %letter = $mid(%answer,%x,1)
      if (%letter == $chr(32)) var %VowelString = %VowelString $+ -
      elseif (%letter isletter aeiou) var %VowelString = %VowelString $+ %letter
      else var %VowelString = %VowelString $+ *
      inc %x
    }
    return Vowels: $replace(%VowelString,-,$chr(32))
  }
  elseif ($($+(%,TRIVIAhint.,%chan,$cid),2) == 3) {
    var %length = $len(%answer), %Fhalf = $int($calc(%length / 2)), %LHalf = $calc(%length - %Fhalf)
    var %Hint = $left(%answer,%Fhalf) $+ $StarString($right(%answer,%LHalf))
    return Hint: %Hint
  }
  else {
    var %x = 1, %tokens = $numtok(%answer,32)
    while (%x <= %tokens) {
      var %token = $gettok(%answer,%x,32)
      var %length = $len(%token), %Fhalf = $int($calc(%length / 2)), %LHalf = $calc(%length - %Fhalf)
      var %Hint = %Hint $left(%token,%Fhalf) $+ $StarString($right(%token,%LHalf))
      inc %x
    }
    return Hint: %Hint
  }
}
alias -l NextQuestion {
  var %chan = $1
  unset $+(%,TRIVIA*.,$(%chan,2),$cid)
  unset $+(%,GUESSES.,*,$(%chan,2),$cid)
  if ($readini(TriviaOptions.ini,Options,idleDisableState) == 1) {
    inc $+(%,MISSED.,%chan,$cid)
    if ($($+(%,MISSED.,$(%chan,2),$cid),2) >= $readini(TriviaOptions.ini,Options,IdleDisable)) {
      msg %chan $c1(Too many unanswered questions. Automatically disabling trivia.)
      StopTriv %chan $cid
      halt
    }
  }
  msg %chan $c1(Nobody got that one? Next question in) $c2($GetTime(BetweenQ)) $c1(seconds)
  $+(.timerTRIVIA.,%chan,.,$cid) 1 $GetTime(BetweenQ) AskQuestion %chan
}
alias -l ShowPoints {
  var %search = $1
  var %x = 1, %total = $hfind(TriviaScores,%search,0,w)
  write -c Temp.txt
  did -r TriviaOptions 61
  while (%x <= %total) { 
    var %item = $hfind(TriviaScores,%search,%x,w)
    write Temp.txt %item $hget(TriviaScores,%item)
    inc %x
  }
  filter -ffcteu 2 32 temp.txt SortedTemp.txt
  var %x = 1, %lines = $lines(SortedTemp.txt)
  while (%x <= %lines) {
    var %line = $read(SortedTemp.txt,%x), %nick = $gettok(%line,1,32), %pts = $gettok(%line,2,32)
    var %tokens = $numtok(%nick,46), %nick = $gettok(%nick,%tokens,46)
    did -a $dname 61 %nick $+($chr(40),%pts point $+ $iif(%pts != 1,s),$chr(41))
    inc %x
  }  
  .remove temp.txt
  .remove SortedTemp.txt
}
alias -l StopTriv {
  var %chan = $1, %cid = $2
  unset $+(%,TRIVIA*.,$(%chan,2),$(%cid,2))
  unset $+(%,MISSED.,$(%chan,2),$(%cid,2))
  unset $+(%,GUESSES.*,$(%chan,2),$(%cid,2))
  $+(.timerTRIVIA.,$(%chan,2),.,$(%cid,2)) off
  if ($dialog(TriviaOptions)) LoadGames
}


alias -l StarString {
  var %x = 1, %tokens = $numtok($1-,32)
  while (%x <= %tokens) {
    var %y = 1, %length = $len($gettok($1-,%x,32))
    var %StarString = %StarString $str(*,%length)
    inc %x
  }
  return %StarString
}



alias -l DefaultOptions {
  writeini TriviaOptions.ini Times FirstQ 10
  writeini TriviaOptions.ini Times AnswerQ 60
  writeini TriviaOptions.ini Times BetweenQ 5
  writeini TriviaOptions.ini Times BeforeNext 30
  writeini TriviaOptions.ini Times BeforeHint 10
  writeini TriviaOptions.ini Times BetweenHints 10
  writeini TriviaOptions.ini Allow Hints 1
  writeini TriviaOptions.ini Allow Next 1
  writeini TriviaOptions.ini Allow Repeat 1
  writeini TriviaOptions.ini Restrict OnOff 3
  writeini TriviaOptions.ini Restrict Hints 4
  writeini TriviaOptions.ini Restrict Next 4
  writeini TriviaOptions.ini Restrict Repeat 4
  writeini TriviaOptions.ini Hints 1 1 
  writeini TriviaOptions.ini Hints 2 1
  writeini TriviaOptions.ini Hints 3 1 
  writeini TriviaOptions.ini Hints 4 1 
  writeini TriviaOptions.ini Options AnswerAnywhere 0
  writeini TriviaOptions.ini Options SpellCheck 1
  writeini TriviaOptions.ini Options SpellCheckAccuracy 80
  writeini TriviaOptions.ini Options MaxGuessesState 0
  writeini TriviaOptions.ini Options MaxGuesses 5
  writeini TriviaOptions.ini Options IdleDisableState 1
  writeini TriviaOptions.ini Options IdleDisable 5
  writeini TriviaOptions.ini Colour1 Text 15
  writeini TriviaOptions.ini Colour1 Background 2
  writeini TriviaOptions.ini Colour1 BgState 0
  writeini TriviaOptions.ini Colour1 Bold 0
  writeini TriviaOptions.ini Colour1 Underline 0
  writeini TriviaOptions.ini Colour2 Text 5
  writeini TriviaOptions.ini Colour2 Background 2
  writeini TriviaOptions.ini Colour2 BgState 0
  writeini TriviaOptions.ini Colour2 Bold 0
  writeini TriviaOptions.ini Colour2 Underline 0
  writeini TriviaOptions.ini Options AccessType Disable 
}
alias -l SaveOptions {
  var %dname = TriviaOptions
  writeini TriviaOptions.ini Times FirstQ $iif($did(%dname,5) isnum,$ifmatch,10)
  writeini TriviaOptions.ini Times AnswerQ $iif($did(%dname,7) isnum,$ifmatch,60)
  writeini TriviaOptions.ini Times BetweenQ $iif($did(%dname,9) isnum,$ifmatch,5)
  writeini TriviaOptions.ini Times BeforeNext $iif($did(%dname,11) isnum,$ifmatch,30)
  writeini TriviaOptions.ini Times BeforeHint $iif($did(%dname,13) isnum,$ifmatch,10)
  writeini TriviaOptions.ini Times BetweenHints $iif($did(%dname,68) isnum,$ifmatch,10)
  writeini TriviaOptions.ini Allow Hints $did(%dname,15).state
  writeini TriviaOptions.ini Allow Next $did(%dname,22).state
  writeini TriviaOptions.ini Allow Repeat $did(%dname,90).state
  writeini TriviaOptions.ini Restrict OnOff $did(%dname,70).sel
  writeini TriviaOptions.ini Restrict Hints $did(%dname,17).sel
  writeini TriviaOptions.ini Restrict Next $did(%dname,24).sel
  writeini TriviaOptions.ini Restrict Repeat $did(%dname,90).sel
  writeini TriviaOptions.ini Hints 1 $did(%dname,18).state 
  writeini TriviaOptions.ini Hints 2 $did(%dname,19).state
  writeini TriviaOptions.ini Hints 3 $did(%dname,20).state 
  writeini TriviaOptions.ini Hints 4 $did(%dname,21).state 
  writeini TriviaOptions.ini Options AnswerAnywhere $did(%dname,25).state
  writeini TriviaOptions.ini Options SpellCheck $did(%dname,26).state
  writeini TriviaOptions.ini Options SpellCheckAccuracy $iif($did(%dname,27) isnum 1-100,$ifmatch,80)
  writeini TriviaOptions.ini Options MaxGuessesState $did(%dname,74).state
  writeini TriviaOptions.ini Options MaxGuesses $iif($did(%dname,75) isnum 1-,$ifmatch,5)
  writeini TriviaOptions.ini Options IdleDisableState $did(%dname,71).state
  writeini TriviaOptions.ini Options IdleDisable $iif($did(%dname,72) isnum 1-,$ifmatch,5)
  writeini TriviaOptions.ini Colour1 Text $did(%dname,79).sel
  writeini TriviaOptions.ini Colour1 Background $did(%dname,83).sel
  writeini TriviaOptions.ini Colour1 BgState $did(%dname,82).state
  writeini TriviaOptions.ini Colour1 Bold $did(%dname,80).state
  writeini TriviaOptions.ini Colour1 Underline $did(%dname,81).state
  writeini TriviaOptions.ini Colour2 Text $did(%dname,87).sel
  writeini TriviaOptions.ini Colour2 Background $did(%dname,88).sel
  writeini TriviaOptions.ini Colour2 BgState $did(%dname,84).state
  writeini TriviaOptions.ini Colour2 Bold $did(%dname,86).state
  writeini TriviaOptions.ini Colour2 Underline $did(%dname,85).state
  writeini TriviaOptions.ini Options AccessType $iif($did(%dname,41).state == 1,Disable,Enable) 
}  

alias -l LoadOptions {
  var %dname = TriviaOptions
  did -ra %dname 5 $readini(TriviaOptions.ini,Times,FirstQ)
  did -ra %dname 7 $readini(TriviaOptions.ini,Times,AnswerQ)
  did -ra %dname 9 $readini(TriviaOptions.ini,Times,BetweenQ)
  did -ra %dname 11 $readini(TriviaOptions.ini,Times,BeforeNext)
  did -ra %dname 13 $readini(TriviaOptions.ini,Times,BeforeHint)
  did -ra %dname 68 $readini(TriviaOptions.ini,Times,BetweenHints)
  did $iif($readini(TriviaOptions.ini,Allow,Hints) == 1,-c,-u) %dname 15
  did $iif($readini(TriviaOptions.ini,Allow,Hints) == 1,-e,-b) %dname 17,18,19,20,21
  did $iif($readini(TriviaOptions.ini,Allow,Next) == 1,-c,-u) %dname 22
  did $iif($readini(TriviaOptions.ini,Allow,Next) == 1,-e,-b) %dname 24
  did $iif($readini(TriviaOptions.ini,Allow,Repeat) == 1,-c,-u) %dname 90
  did $iif($readini(TriviaOptions.ini,Allow,Repeat) == 1,-e,-b) %dname 92
  did -c %dname 70 $iif($readini(TriviaOptions.ini,Restrict,OnOff),$ifmatch,4)  
  did -c %dname 17 $iif($readini(TriviaOptions.ini,Restrict,Hints),$ifmatch,4)
  did -c %dname 24 $iif($readini(TriviaOptions.ini,Restrict,Next),$ifmatch,4)
  did -c %dname 92 $iif($readini(TriviaOptions.ini,Restrict,Repeat),$ifmatch,4)
  did $iif($readini(TriviaOptions.ini,Hints,1) == 1,-c,-u) %dname 18
  did $iif($readini(TriviaOptions.ini,Hints,2) == 1,-c,-u) %dname 19
  did $iif($readini(TriviaOptions.ini,Hints,3) == 1,-c,-u) %dname 20
  did $iif($readini(TriviaOptions.ini,Hints,4) == 1,-c,-u) %dname 21
  did $iif($readini(TriviaOptions.ini,Options,AnswerAnywhere) == 1,-c,-u) %dname 25
  did $iif($readini(TriviaOptions.ini,Options,SpellCheck) == 1,-c,-u) %dname 26
  did $iif($readini(TriviaOptions.ini,Options,SpellCheck) == 1,-e,-b) %dname 27
  did -ra %dname 27 $readini(TriviaOptions.ini,Options,SpellCheckAccuracy)
  did -c %dname 79 $readini(TriviaOptions.ini,Colour1,Text)
  did -c %dname 83 $readini(TriviaOptions.ini,Colour1,Background)
  did $iif($readini(TriviaOptions.ini,Colour1,BgState) == 1,-c,-u) %dname 82
  did $iif($readini(TriviaOptions.ini,Colour1,BgState) == 1,-e,-b) %dname 83
  did $iif($readini(TriviaOptions.ini,Colour1,Bold) == 1,-c,-u) %dname 80
  did $iif($readini(TriviaOptions.ini,Colour1,Underline) == 1,-c,-u) %dname 81
  did -c %dname 87 $readini(TriviaOptions.ini,Colour2,Text)
  did -c %dname 88 $readini(TriviaOptions.ini,Colour2,Background)
  did $iif($readini(TriviaOptions.ini,Colour2,BgState) == 1,-c,-u) %dname 84
  did $iif($readini(TriviaOptions.ini,Colour2,BgState) == 1,-e,-b) %dname 88
  did $iif($readini(TriviaOptions.ini,Colour2,Bold) == 1,-c,-u) %dname 86
  did $iif($readini(TriviaOptions.ini,Colour2,Underline) == 1,-c,-u) %dname 85
  did $iif($readini(TriviaOptions.ini,Options,MaxGuessesState) == 1,-c,-u) %dname 74
  did $iif($readini(TriviaOptions.ini,Options,MaxGuessesState) == 1,-e,-b) %dname 75
  did -ra %dname 75 $readini(TriviaOptions.ini,Options,MaxGuesses)
  did $iif($readini(TriviaOptions.ini,Options,IdleDisableState) == 1,-c,-u) %dname 71
  did $iif($readini(TriviaOptions.ini,Options,IdleDisableState) == 1,-e,-b) %dname 72
  did -ra %dname 72 $readini(TriviaOptions.ini,Options,IdleDisable)

  did -c %dname $iif($readini(TriviaOptions.ini,Options,AccessType) == Disable,41,42)
  did -e %dname $iif($readini(TriviaOptions.ini,Options,AccessType) == Disable,36,40)
  did -b %dname $iif($readini(TriviaOptions.ini,Options,AccessType) == Disable,40,36)
}