
alias -l sayquiz {
  if (%lapquizopt.c1 == $null ) %lapquizopt.c1 = 08
  if (%lapquizopt.c2 == $null ) %lapquizopt.c2 = 02
  if ( c isincs $gettok($chan(%lapquiz.chan).mode,1,32)) msg %lapquiz.chan  $+ $strip($1-,c)
  msg  %lapquiz.chan  $+ %lapquizopt.c1 $+ , $+ %lapquizopt.c2 $+ $1- $+ 
}

menu channel {
  -
  Quiz starten
  .$getstring(menuoptions): /dialog -m lapquiz_options lapquiz_options
  .$iif( %lapquiz.run == 1, , $getstring(menustart)):/startlapquiz #
  .$iif( %lapquiz.run == 1, $iif( %lapquiz.pause == 1, $getstring(menucontinue), $getstring(menupause)), ):/lapquizpause
  .$iif( %lapquiz.run == 1, $getstring(menuloaddatabase), ):/selectdb
  .$iif( %lapquiz.run == 1, $iif( %lapquiz.dbname != $null, $getstring(menunextquestion), ), ):/questionfromdb
  .$iif( %lapquiz.run == 1, $getstring(menunewquestion), ):/newquestion
  .$iif( %lapquiz.answer != $null, $getstring(menutip), ):/lapquiztip
  .$iif( %lapquiz.answer != $null, $getstring(menuanswerprivate), ):/echo %lapquiz.chan  $+ %lapquizopt.c1 $+ , $+ %lapquizopt.c2 $+ $comptext($getstring(solvemsg)) $+ 
  .$iif( %lapquiz.run == 1, $getstring(menuranking), ):/sayranking
  .$iif( %lapquiz.run == 1, $getstring(menuend) , ):/endlapquiz
  -
}

alias autotip {
  if ( %lapquizopt.autotip != 1) halt
  if ( %lapquiz.pause == 1) halt
  if ( %lapquiz.tippos > 5 ) halt
  if ( $len(%lapquiz.answer) < 2 ) halt
  if ( %lapquiz.tippos > $len(%lapquiz.answer)) halt
  var %i = %lapquiz.tippos
  %lapquiz.tip = $str(., $calc(%i - 1))
  while ( %i <= $len(%lapquiz.answer)) {
    %lapquiz.tip = %lapquiz.tip $+ $mid(%lapquiz.answer, %i, 1) $+ $str(., $iif( $calc($len(%lapquiz.answer) - %i) < 5, $calc($len(%lapquiz.answer) - %i), 5))
    %i = %i + 5
  }
  if ( $len(%lapquiz.answer) < 5) var %lapquiz.tempvar = $len(%lapquiz.answer)
  else var %lapquiz.tempvar = 5

  if ( %lapquiz.tippos == %lapquiz.tempvar ) {
    if ( %lapquizopt.killatend == 1 ) {
      killquestion
    }
    else if ( %lapquizopt.solveatend == 1) {
      sayquiz $comptext($getstring(solvemsg))
    }
    else {
      if ( %lapquiz.tip !=  $null) $lapquiztip(%lapquiz.tip)
    }
  }
  else if ( %lapquiz.tip !=  $null) $lapquiztip(%lapquiz.tip)
  inc %lapquiz.tippos
  if ( %lapquizopt.tipinterval == $null) %lapquizopt.tipinterval = 10
  .timerlapquiztip 1 %lapquizopt.tipinterval /autotip
}
alias -l selectdb {
  %lapquiz.dbname = $sfile( $mircdir, Öffnen)
  unset %lapquiz.rowpos
  if ( %lapquiz.dbname != $null ) { %lapquiz.dbnamesh = $gettok( %lapquiz.dbname, -1, 92) | sayquiz $comptext($getstring(loaddatabasemsg)) | questionfromdb }
}

alias -l lapquizpause {
  %lapquiz.pause = $calc(( %lapquiz.pause - 1 ) * -1)
  if ( %lapquiz.pause == 1) sayquiz $comptext($getstring(pausemsg))
  else {
    sayquiz $comptext($getstring(continuemsg))
    if ( %lapquizopt.autotip) autotip
  }
}

alias -l lapquiztip {
  if ( $1 == $null) %lapquiz.tip = $?="Tip"
  else %lapquiz.tip = $1-
  if ( %lapquiz.tip != $null ) sayquiz $comptext($getstring(tipmsg))
}



alias -l vok {
  if ( %lapquizopt.enablevok != 1 ) halt
  var %i = 1
  unset %lapquiz.vok
  while (%i <= $len(%lapquiz.answer) ) {

    if ( $mid( %lapquiz.answer, %i, 1) == a )  %lapquiz.vok = %lapquiz.vok $+ $mid(%lapquiz.answer, %i, 1)
    else if ( $mid( %lapquiz.answer, %i, 1) == i )  %lapquiz.vok = %lapquiz.vok $+ $mid(%lapquiz.answer, %i, 1)
    else if ( $mid( %lapquiz.answer, %i, 1) == o )  %lapquiz.vok = %lapquiz.vok $+ $mid(%lapquiz.answer, %i, 1)
    else if ( $mid( %lapquiz.answer, %i, 1) == u )  %lapquiz.vok = %lapquiz.vok $+ $mid(%lapquiz.answer, %i, 1)
    else if ( $mid( %lapquiz.answer, %i, 1) == e )  %lapquiz.vok = %lapquiz.vok $+ $mid(%lapquiz.answer, %i, 1)
    else %lapquiz.vok = %lapquiz.vok $+ .
    inc %i
  }
  $lapquiztip(%lapquiz.vok)

}

on *:EXIT:endlapquiz

on *:NICK:{
  if ( %lapquiz.run != 1) halt
  var %i = 0
  while ( %i < %lapquiz.numnicks ) {
    %lapquiz.tempnick = %lapquiz.nicks [ $+ [ %i ] ]
    if ( $nick == %lapquiz.tempnick ) {
      %lapquiz.points. [ $+ [ $newnick ] ] = %lapquiz.points. [ $+ [ %lapquiz.tempnick ] ]
      [ [ $+(%,lapquiz.rank.,$newnick) ] ] =  [ [ $+(%,lapquiz.rank.,%lapquiz.tempnick) ] ]
      %lapquiz.nicks [ $+ [ %i ] ] = $newnick
      unset %lapquiz.rank. [ $+ [ %lapquiz.tempnick ] ]
      unset %lapquiz.points. [ $+ [ %lapquiz.tempnick ] ]
    }
    inc %i
  }
  %i = 0
  while ( %i < %lapquiz.revolt.count ) {
    if ( %lapquiz.revolt.nick [ $+ [ %i ] ] == $nick) [ [  $+(%,lapquiz.revolt.nick, %i) ] ] = $newnick
    inc %i
  }

  unset %i
}

alias killquestion {
  %lapquiz.time = $calc($ctime - %lapquiz.startquestion)
  sayquiz $comptext($getstring(killquestionmsg1))
  sayquiz $comptext($getstring(killquestionmsg2))
  %lapquiz.answer = $null
  if ( %lapquiz.dbname != $null) if ( $1 != 1) .timerlapquiznew 1 3 questionfromdb
}

on *:START: {
  if ( %lapquizopt.version != 0.1) {
    lqversion
    %lapquizopt.version = 0.1
  }
  if ( %lapquizopt.languagefile == $null ) {
    if ($exists($scriptdir $+ language.ini)) {
      %lapquizopt.languagefile = $replace($scriptdir $+ language.ini, $mircdir, $chr(0))
    }
    else {
      %lapquizopt.languagefile = $replace($sfile($mircdir, Lapquiz - Wählen sie eine Sprachdatei aus), $mircdir, $chr(0))
    }
  }
  if (%lapquizopt.c1 == $null ) %lapquizopt.c1 = 08
  if (%lapquizopt.c2 == $null ) %lapquizopt.c2 = 02
  if (%lapquizopt.tipinterval == $null ) %lapquizopt.tipinterval = 5
}



on *:TEXT:!vok:%lapquiz.chan:if ( %lapquiz.pause != 1 ) vok
on *:TEXT:!ranks*:%lapquiz.chan: if ( %lapquizopt.enablerank == 1) $sayrankings($1)
on *:TEXT:!revolt*:%lapquiz.chan: {
  if ( %lapquizopt.enablerevolt != 1 ) halt
  if ( %lapquizopt.pause == 1 ) halt
  if ( %lapquiz.answer == $null ) halt
  var %i = 0
  while ( %i < %lapquiz.revolt.count ) {
    if ( %lapquiz.revolt.nick [ $+ [ %i ] ] == $nick) halt
    inc %i
  }
  %lapquiz.revolt.nick = $nick
  %lapquiz.revolt.nick [ $+ [ %lapquiz.revolt.count ] ] = $nick
  %lapquiz.revolt.needed = $calc(%lapquizopt.revoltneed - %lapquiz.revolt.count)
  sayquiz $comptext($getstring(revoltmsg))
  inc %lapquiz.revolt.count
  if ( %lapquiz.revolt.count == %lapquizopt.revoltneed ) killquestion
}
on *:TEXT:!help*:%lapquiz.chan: {
  if ( %lapquizopt.helppublic == 1 ) %lapquiz.helpto = $chan
  else %lapquiz.helpto = $nick 
  if ( %lapquizopt.enablevok == 1 ) %lapquiz.tempvar = Enabled
  else %lapquiz.tempvar = Disablad
  msg %lapquiz.helpto $getstring(helpvok) - %lapquiz.tempvar
  if ( lapquizopt.enableranks == 1 ) %lapquiz.tempvar = Enabled
  else %lapquiz.tempvar = Disabled
  msg %lapquiz.helpto $getstring(helpranks) - %lapquiz.tempvar
  if ( %lapquizopt.enablerevolt == 1 ) %lapquiz.tempvar = Enabled
  else %lapquiz.tempvar = Disabled
  msg %lapquiz.helpto $getstring(helprevolt) - %lapquiz.tempvar
}

on *:TEXT:%lapquiz.answer:%lapquiz.chan: {
  if ( %lapquiz.pause == 1) halt
  if ( %lapquiz.points. [ $+ [ $nick ] ] == $null ) {
    %lapquiz.points. [ $+ [ $nick ] ] = 1
    if  ( %lapquiz.numnicks == $null ) %lapquiz.numnicks = 0
    %lapquiz.nicks [ $+ [ %lapquiz.numnicks ] ] = $nick
    inc %lapquiz.numnicks
  }
  else inc %lapquiz.points. [ $+ [ $nick ] ]
  sort
  %lapquiz.time = $calc($ctime - %lapquiz.startquestion)
  %lapquiz.nick = $nick
  sayquiz $comptext($getstring(answermsg1))
  sayquiz $comptext($getstring(answermsg2))
  sayquiz $comptext($getstring(answermsg3))
  unset %lapquiz.answer
  .timerlapquiztip off
  if ( %lapquiz.dbname != $null) .timerlapquiznew 1 3 questionfromdb
}

alias -l newquestion {
  if ( %lapquiz.answer != $null) {
    $killquestion(1)
    %lapquiz.answer = $null
  }
  if ( $1 == $null ) {
    %lapquiz.question = $?="Enter question"
    if (%lapquiz.question != $null) %lapquiz.answer = $?="Enter answer"
  }
  else { %lapquiz.question = $1 | %lapquiz.answer = $2 }
  if ( %lapquizopt.tipinterval==$null) %lapquizopt.tipinterval = 10
  %lapquiz.revolt.count = 0
  if (%lapquiz.question == $null ) halt
  if (%lapquiz.answer == $null ) halt
  else {   %lapquiz.startquestion = $ctime | sayquiz  $comptext($getstring(newquestionmsg)) | %lapquiz.tippos = 1 | .timerlapquiztip 1 %lapquizopt.tipinterval /autotip }
}

alias -l questionfromdb {
  if ( %lapquiz.answer != $null) {
    killquestion
    halt
  }
  if ( %lapquiz.dbname == $null ) { selectdb | halt }
  if ( %lapquiz.rowpos == $null ) %lapquiz.rowpos = 1
  %lapquiz.row = $read( %lapquiz.dbname, %lapquiz.rowpos )
  if ( %lapquiz.row == $null ) {
    sayquiz End of Database
    sayranking
    unset %lapquiz.dbname
    halt
  }
  $newquestion( $gettok(%lapquiz.row, 1, 124) , $gettok(%lapquiz.row, 2, 124))
  inc %lapquiz.rowpos 
}

alias -l startlapquiz {
  if ( %lapquiz.run != $null ) { echo -a  $+ %lapquizopt.c1 $+ , $+ %lapquizopt.c2 $+ Lapquiz is runnig! You must stop it before you can restart it! | halt }
  %lapquiz.run = 1
  %lapquiz.chan = $1
  %lapquiz.version = %lapquizopt.version
  sayquiz $comptext($getstring(startmsg))
}

alias -l endlapquiz {
  if (%lapquiz.run == $null ) say Lapquiz is not running
  sayquiz $comptext($getstring(endmsg))
  unset %lapquiz.*
}

alias -l sort {
  var %i = 0
  while (%i < %lapquiz.numnicks ) {
    %lapquiz.tempnick = %lapquiz.nicks [ $+ [ %i ] ]
    %lapquiz.temppoints  = %lapquiz.points. [ $+ [ %lapquiz.tempnick ] ]
    %tok = $instok(%tok, %lapquiz.temppoints $+ . $+ %lapquiz.tempnick,0, 32)
    inc %i
  }
  %sort = $sorttok(%tok, 32, nr)
  %i = 1
  while (%i <= $numtok(%sort, 32)) {
    [ [ $+(%,lapquiz.rank.,$gettok($gettok(%sort, %i, 32), 2, 46)) ] ] = %i
    inc %i
  }
  unset %tok
}

alias -l sayranking {
  sayquiz $comptext($getstring(rankingmsg1))
  var %i = 0
  %rank = 1
  if ( $1 > 0 ) %rank = $1
  if (  %rank + 5 < %lapquiz.numnicks ) var %j =  %rank + 5 
  else var %j = %lapquiz.numnicks
  while (%rank <= %lapquiz.numnicks) {
    while (%i < $calc(%lapquiz.numnicks) ) {
      %lapquiz.tempnick = %lapquiz.nicks [ $+ [ %i ] ]
      %lapquiz.temppoints  = %lapquiz.points. [ $+ [ %lapquiz.tempnick ] ]
      if (  [ [ $+(%,lapquiz.rank.,%lapquiz.tempnick) ] ] == %rank ) sayquiz %rank $+ . %lapquiz.tempnick Points: %lapquiz.temppoints
      inc %i
    }
    inc %rank
    %i = 0
  }
  sayquiz $comptext($getstring(rankingmsg2))
  unset %i
  unset %j
  unset %rank
}

alias -l comptext_ {
  %lapquiz.line_ = $1
  %lapquiz.reg = /([^\?])\?([^\?\s"\)])([^\s"\)]+)/
  while ( $regex(%lapquiz.line_, %lapquiz.reg) == 1 ) {
    %lapquiz.regml_ = $regml(2) $+ $regml(3)
    %lapquiz.line_ = $replace(%lapquiz.line_, ? $+ %lapquiz.regml_ , % [ $+ [ $+(lapquiz., %lapquiz.regml_) ] ])
  }
  if ( $left(%lapquiz.line_, 1) == ?) {
    %lapquiz.reg = /\?([^\?\s"\)])([^\s"\)]+)/
    if ( $regex(%lapquiz.line_, %lapquiz.reg) == 1 ) {
      %lapquiz.regml_ = $regml(1) $+ $regml(2)
      %lapquiz.line = $replace(%lapquiz.line_, ? $+ %lapquiz.regml_ , % [ $+ [ $+(lapquiz., %lapquiz.regml_) ] ])
    }
  }
  return %lapquiz.line_

}


alias comptext {
  %lapquiz.line = $1
  %lapquiz.reg = /([^\?])\?([^\?\s"\)])([^\s"\)]+)/
  while ( $regex(%lapquiz.line, %lapquiz.reg) == 1 ) {
    %lapquiz.regml = $regml(2) $+ $regml(3)
    %lapquiz.line = $replace(%lapquiz.line, ? $+ %lapquiz.regml , % [ $+ [ $+(lapquiz., $comptext_(%lapquiz.regml)) ] ])
  }
  if ( $left(%lapquiz.line, 1) == ?) {
    %lapquiz.reg = /\?([^\?\s"\)])([^\s"\)]+)/
    if ( $regex(%lapquiz.line, %lapquiz.reg) == 1 ) {
      %lapquiz.regml = $regml(1) $+ $regml(2)
      %lapquiz.line = $replace(%lapquiz.line, ? $+ %lapquiz.regml , %  [ $+ [ $+(lapquiz., $comptext_(%lapquiz.regml)) ] ])
    }
  }
  unset %lapquiz.reg
  return $replace(%lapquiz.line, ??, ?)
}

dialog -l lapquiz_options {
  title "Lapquiz - Optionen"
  size -1 -1 257 102
  option dbu
  button "OK", 6, 91 87 37 12, ok
  edit "", 16, 38 58 75 11, autohs
  text "Sprachdatei:", 15, 7 59 32 8
  box "Misc", 14, 3 52 124 20
  check "Antwort sagen wenn alle Tips gesagt wurden", 11, 134 20 118 10
  check "Frage beenden wenn alle Tips gesagt wurden", 12, 134 29 119 10
  check "Autotip", 10, 134 11 119 10
  box "Tips", 9, 131 4 124 47
  box "Requests", 1, 3 4 125 46
  check "!vok erlauben", 17, 6 11 50 10
  check "!revolt erlauben", 18, 6 19 50 10
  text "Benötigte Stimmen:", 19, 59 20 48 8, disable
  edit "", 20, 107 18 18 11, disable
  check "!ranks erlauben", 21, 6 28 50 10
  text "Tip-Interval:", 22, 135 39 39 8
  edit "", 23, 167 38 34 10
  button "...", 24, 114 58 11 11
  button "Abbrechen", 25, 132 87 37 12, cancel
  button "Info", 26, 2 87 22 12
  box "Farben", 13, 131 51 124 31
  combo 8, 192 70 48 50, drop
  text "Textfarbe:", 2, 136 59 28 8
  text "Hintergrundfarbe:", 3, 136 70 46 8
  edit "", 5, 182 69 10 11, limit 2
  edit "", 4, 182 58 10 11, limit 2
  combo 7, 192 58 48 50, drop
  radio "!help public", 27, 6 36 50 10
  radio "!help private", 28, 58 36 50 10
}


on 1:dialog:lapquiz_options:init:0: {
  did -o lapquiz_options 4   1 %lapquizopt.c1
  did -o lapquiz_options 5   1 %lapquizopt.c2
  $fillcolordropdown(7)
  $fillcolordropdown(8)
  did -c lapquiz_options 7 $calc(%lapquizopt.c1 +1)
  did -c lapquiz_options 8 $calc(%lapquizopt.c2 +1)
  did -o lapquiz_options 16 1 %lapquizopt.languagefile
  did -o lapquiz_options 23 1 %lapquizopt.tipinterval
  did -o lapquiz_options 20 1 %lapquizopt.revoltneed
  if ( %lapquizopt.enablevok == 1) did -c lapquiz_options 17
  if ( %lapquizopt.enablerevolt == 1) {
    did -c lapquiz_options 18
    did -e lapquiz_options 19
    did -e lapquiz_options 20
  }
  if ( %lapquizopt.enableranks == 1) did -c lapquiz_options 21
  if ( %lapquizopt.autotip == 1) did -c lapquiz_options 10
  if ( %lapquizopt.solveatend == 1) did -c lapquiz_options 11
  if ( %lapquizopt.killatend == 1) did -c lapquiz_options 12
  if ( %lapquizopt.helppublic == 1) did -c lapquiz_options 27
  else did -c lapquiz_options 28
}

on 1:dialog:lapquiz_options:edit:*: {
  if ( $did > 5 )  return
  if ( $did < 4 )  return
  if ( $did($did) > 15 ) did -o lapquiz_options $did 1 15
  did -c lapquiz_options $calc( $did + 3 ) $calc($did( $did ) +1)
}

on 1:dialog:lapquiz_options:sclick:*: {
  if ( $did == 18 ) {
    if ( $did(18).state == 1) {
      did -e lapquiz_options 19
      did -e lapquiz_options 20
    }
    else {
      did -b lapquiz_options 19
      did -b lapquiz_options 20
    }
  }
  if ( $did == 26) lqversion
  if ($did == 24) {
    %lapquiz.file = $replace($sfile($mircdir), $mircdir, $chr(0))
    if (%lapquiz.file != $null) did -o lapquiz_options 16  1 %lapquiz.file
  }
  if ($did == 11) did -u lapquiz_options 12
  if ($did == 12) did -u lapquiz_options 11

  if ($did == 6 ) {
    %lapquizopt.enablevok = $did(17).state
    %lapquizopt.enablerevolt = $did(18).state
    %lapquizopt.enableranks = $did(21).state
    %lapquizopt.autotip = $did(10).state
    %lapquizopt.solveatend = $did(11).state
    %lapquizopt.killatend = $did(12).state
    %lapquizopt.tipinterval = $int($did(23))
    %lapquizopt.revoltneed = $int($did(20))
    %lapquizopt.c1 = $did(4)
    %lapquizopt.c2 = $did(5)
    %lapquizopt.languagefile = $did(16)
    %lapquizopt.helppublic = $did(27).state
    if ( %lapquizopt.tipinterval == $null ) %lapquizopt.tipinterval = 5
    if ( %lapquizopt.tipinterval < 1) %lapquizopt.tipinterval = 5
    if ( %lapquizopt.revoltneed == $null) %lapquizopt.revoltneed = 5
    if ( %lapquizopt.revoltneed < 1) %lapquizopt.revoltneed = 5
  }
  if ( $did < 7 ) return
  if ( $did > 8 ) return 
  { 
    var %lapquiz.nn = $calc($did($did).sel - 1)
    did -o lapquiz_options $calc($did - 3) 1 $str( 0, $calc( ($len(%lapquiz.nn) - 2 )* -1)) $+ %lapquiz.nn
  }
}

alias -l fillcolordropdown {
  did -o lapquiz_options $1 1 Weiß
  did -o lapquiz_options $1 2 Schwarz
  did -o lapquiz_options $1 3 Dunkelblau
  did -o lapquiz_options $1 4 Dunkelgrün
  did -o lapquiz_options $1 5 Rot
  did -o lapquiz_options $1 6 Dunkelrot
  did -o lapquiz_options $1 7 Lila
  did -o lapquiz_options $1 8 Orange
  did -o lapquiz_options $1 9 Gelb
  did -o lapquiz_options $1 10 Grün
  did -o lapquiz_options $1 11 Dunkeltükis
  did -o lapquiz_options $1 12 Tükis
  did -o lapquiz_options $1 13 Blau
  did -o lapquiz_options $1 14 Pink
  did -o lapquiz_options $1 15 Dunkelgrau
  did -o lapquiz_options $1 16 Grau
}

alias -l getstring {
  var %lapquiz.tempvar = $readini(%lapquizopt.languagefile , lapquiz, $1-)
  if ( %lapquiz.tempvar == $null) {
    if ( $1- == menuoptions) %lapquiz.tempvar = Optionen
    else if ( $1- == menustart ) %lapquiz.tempvar = Starten
    else if ( $1- == menupause ) %lapquiz.tempvar = Pausieren
    else if ( $1- == menucontinue) %lapquiz.tempvar = Fortsetzen
    else if ( $1- == menuloaddatabase ) %lapquiz.tempvar = Datebbank laden
    else if ( $1- == menunextquestion ) %lapquiz.tempvar = Nächste Frage
    else if ( $1- == menunewquestion ) %lapquiz.tempvar = Neue Frage
    else if ( $1- == menutip ) %lapquiz.tempvar = Tip geben
    else if ( $1- == menuranking ) %lapquiz.tempvar = Plazierung
    else if ( $1- == menuend) %lapquiz.tempvar = Beenden
    else %lapquiz.tempvar = Es trat ein Fehler mit der Sprachdatei auf.
    echo -a   $+ %lapquizopt.c1 $+ , $+ %lapquizopt.c2 $+ Die Sprachdatei ist Ungültig es fehlt der Eintrag " $+ $1- $+ ". Bitte laden sie ein gültige Sprachdatei. $+ 
  }
  return %lapquiz.tempvar
}

alias uninstall_lapquiz {
  unset %lapquizopt.*
  .timeruimsg 1 1 echo -a Lapquiz wurde deinstalliert
  unload -rs $gettok( $script, -1, 92)
}
