on 1:TEXT:!valori:%game_channel: {
  if (%finalquick_gioco == ON) /msg %game_channel 08,05 Valori:0,05 a=1 b=4 c=5 d=4 e=1 f=4 g=5 h=7 i=1 j=8 k=8 l=3 m=3 n=3 o=1 p=4 q=7 r=2 s=2 t=2 u=4 v=5 w=8 x=8 y=8 z=6  
}

on 1:TEXT:!punti:%game_channel: {
  if (%finalquick_gioco == ON) {
    if (%fq_punti_tot_ [ $+ [ $nick ] ] != $null) {
      /msg $nick 0,05 Hai totalizzato 12 %fq_punti_tot_ [ $+ [ $nick ] ] 
      close -m $nick
    }
  }
}

on 1:TEXT:!ferma finalquick:%game_channel: {
  if (%finalquick_gioco == ON) /fine_finalquick
}
on 1:CONNECT:{
  set %finalquick_gioco OFF
}

on 1:TEXT:!gioca finalquick*:%game_channel: {
  if (%finalquick_gioco != ON) {
    set %game_channel $chan 
    set %fq_nick_me $me
    set %fq_punteggio $3
    if (%fq_punteggio == $null) set %fq_punteggio 1000
    set %finalquick_gioco ON
    avvia_finalquick
  }
}


on 1:TEXT:*:?: {
  if (%finalquick_gioco == ON) {
    if (%fq_manche_stato != ON) {
      msg $nick 0,05 Nessuna manche in corso ! 
      goto esci_da_verifica_parola
    }
    set %fq_temp_parola $1
    %fq_temp_len = $len(%fq_temp_parola)
    if (%fq_temp_len < %fq_len_manche) {
      msg $nick 0,05 La lunghezza minima  12 %fq_len_manche 0 lettere ! 
      goto esci_da_verifica_parola
    }
    set %fq_t 1
    while (%fq_t <= %fq_temp_len) {
      if ( $mid(%fq_temp_parola,%fq_t,1) !isin %fq_riga_ [ $+ [ %fq_t ] ] ) {
        msg $nick 0,05 La parola 12 %fq_temp_parola 0 non  presente nella griglia. Riprova ! 
        goto esci_da_verifica_parola
      }
      inc %fq_t
    }
    set %fq_r $read($scriptdirDictionary\ $+ italiano. [ $+ [ $left(%fq_temp_parola,1) ] ],w,%fq_temp_parola,1)
    if ($readn != 0) {
      if (%fq_parola_manche_ [ $+ [ $nick ] ] != $null) {
        $remtok(%fq_giocatori_manche,$nick,1,32)
        unset %fq_parola_manche_ [ $+ [ $nick ] ]
      }
      %fq_giocatori_manche = $addtok(%fq_giocatori_manche,$nick,32)
      %fq_parola_manche_ [ $+ [ $nick ] ] = %fq_temp_parola 
      %fq_parola = $lower(%fq_temp_parola)
      %fq_punti = 0
      %fq_cont = 1
      while (%fq_cont <= $len(%fq_parola)) {
        %fq_t_let = $mid(%fq_parola,%fq_cont,1)
        if (%fq_t_let isin aeio) { %fq_val = 1 } 
        elseif (%fq_t_let isin rst) { %fq_val = 2 }
        elseif (%fq_t_let isin lmn) { %fq_val = 3 }
        elseif (%fq_t_let isin bdfpu) { %fq_val = 4 }
        elseif (%fq_t_let isin cgv) { %fq_val = 5 }
        elseif (%fq_t_let == z) { %fq_val = 6 }
        elseif (%fq_t_let isin hq) { %fq_val = 7 }
        elseif (%fq_t_let isin jkwxy) { %fq_val = 8 }
        else { %fq_val = 0 }
        if (%fq_cont == 1) { %fq_val = %fq_val * 2 } 
        if (%fq_cont == 2) { %fq_val = %fq_val * 4 } 
        if (%fq_cont == 3) { %fq_val = %fq_val * 6 } 
        if (%fq_cont == 4) { %fq_val = %fq_val * 8 } 
        if (%fq_cont == 5) { %fq_val = %fq_val * 10 }
        if (%fq_cont == 6) { %fq_val = %fq_val * 12 }
        if (%fq_cont == 7) { %fq_val = %fq_val * 14 } 
        if (%fq_cont == 8) { %fq_val = %fq_val * 16 } 
        %fq_punti = $calc(%fq_punti + %fq_val)
        inc %fq_cont
      }
      %fq_punti_parola_ [ $+ [ $nick ] ] = %fq_punti
      if (%fq_lunghezza == $null) { %fq_lunghezza = 0 }
      if (%fq_temp_len > %fq_lunghezza ) { %fq_lunghezza = %fq_temp_len }
      msg $nick 0,05 Con la parola 12 %fq_temp_parola 0 ottieni 12 %fq_punti 0 punti. 
    }
    else {
      msg $nick 0,05 Parola non presente nel mio dizionario 
    }
    :esci_da_verifica_parola
    close -m $nick
  }
}

alias fine_gioco_finalquick {
  timerfq* OFF
  unset %finalquick*
  unset %fq*
  halt
}

alias fine_finalquick {
  if (%finalquick_gioco == ON) {
    msg %game_channel  08,05finalquick.
    msg %game_channel 0,05TeRmInAtO Su $chan .   
    msg %game_channel 0,05 alle ore : 08,05 $time 0 il giorno 08,05 $date  
    timerfq* OFF
    unset %finalquick*
    unset %fq*
    halt
  }
}

alias avvia_finalquick {
  if (%finalquick_gioco == ON) {
    if (%fq_punteggio < 10) set %fq_punteggio 10
    if (%fq_punteggio > 2000) set %fq_punteggio 2000
    msg %game_channel 08,05finalquick.
    msg %game_channel 08,05AtTiVaTo Su $chan   
    msg %game_channel 0,05 Da11 $nick 0alle ore:12 $time 0il giorno12 $date  
    msg %game_channel 08,05 Punteggio da raggiungere: 0 %fq_punteggio  
    msg %game_channel 0,05 Completa la parola utilizzando una lettera per ogni riga. 
    set %fq_manche_num 0
    if ($exists(finalquickrecord.txt) == $false) finalquick_generahs
    set %finalquick_record $read -l 11 finalquickrecord.txt
    set %finalquick_reckeeper $gettok(%finalquick_record,1,35)
    set %finalquick_record $gettok(%finalquick_record,2,35)
    /timerfq_1 1 10 /avvia_manche_finalquick
  }
}

alias avvia_manche_finalquick {
  if (%finalquick_gioco == ON) {
    genera_griglia_finalquick
    set %fq_len_manche $rand(3,4)
    %fq_riga_da_visualizzare_ [ $+ [ %fq_len_manche ] ] = %fq_riga_da_visualizzare_ [ $+ [ %fq_len_manche ] ] 11,05<--lunghezza minima 0 %fq_len_manche 
    msg %game_channel 11,05 Tra 0 5 11 secondi la prossima manche 
    msg %game_channel 08,05 Nel rispetto di chi gioca non rompete la griglia grazie!!  
    inc %fq_manche_num
    /timerfq_1 1 5 /visualizza_griglia_finalquick
  }
}

alias genera_griglia_finalquick {
  if (%finalquick_gioco == ON) {
    %fq_x = 1
    while (%fq_x < 9) { 
      %fq_t = 1
      if (%fq_x = 1) {
        %fq_t = 8
        %fq_d = %fq_x
      }
      if (%fq_x = 2) {
        %fq_t = 8
        %fq_e = %fq_x
      }
      while (%fq_t < 9) {
        :171
        %fq_temp = $rand(a,z)
        if (%fq_x = 1) && (%fq_temp = a) {
          unset %fq_temp
          goto 171
        }
        if (%fq_x = 1) && (%fq_temp = e) {
          unset %fq_temp
          goto 171
        }
        if (%fq_x = 1) && (%fq_temp = i) {
          unset %fq_temp
          goto 171
        }
        if (%fq_x = 1) && (%fq_temp = o) {
          unset %fq_temp
          goto 171
        }
        if (%fq_x = 1) && (%fq_temp = u) {
          unset %fq_temp
          goto 171
        }
        if (%fq_x = 1) && (%fq_temp = q) {
          unset %fq_temp
          goto 171
        }
        if (%fq_x = 1) && (%fq_temp = h) {
          unset %fq_temp
          goto 171
        }
        if %fq_temp = k {
          unset %fq_temp
          goto 171
        }
        if %fq_temp = y {
          unset %fq_temp
          goto 171
        }
        if %fq_temp = j {
          unset %fq_temp
          goto 171
        }
        if %fq_temp = w {
          unset %fq_temp
          goto 171
        }
        if %fq_temp = x {
          unset %fq_temp
          goto 171
        }
        %fq_riga = %fq_riga $+ [ $chr(32) ] $+ [ %fq_temp ]
        %fq_r = %fq_r $+ [ %fq_temp ]
        inc %fq_t
      }

      %fq_riga_da_visualizzare_ [ $+ [ %fq_x ] ] = $upper(%fq_riga)
      if (%fq_x != %fq_e) || (a isin %fq_r) || (e isin %fq_r) || (i isin %fq_r) || (o isin %fq_r) || (u isin $fq_r) {
        %fq_riga_ [ $+ [ %fq_x ] ] = %fq_r
        unset %fq_r
        unset %fq_riga
        inc %fq_x
      }
      else {
        unset %fq_r
        unset %fq_riga
      }
    }

    %fq_cont = 1
    while (%fq_cont < 9) {
      if (%fq_cont > 2) {
        %fq_x_x = %fq_cont * 2
        %fq_riga_da_visualizzare_ [ $+ [ %fq_cont ] ] = %fq_riga_da_visualizzare_ [ $+ [ %fq_cont ] ] 08,05 valore  x %fq_x_x 
        if (%fq_cont = 1) {
          %fq_riga_da_visualizzare_ [ $+ [ %fq_cont ] ] = 0,05    0,05 %fq_riga_da_visualizzare_ [ $+ [ %fq_d ] ]
        }
        if (%fq_cont = 2) {
          %fq_riga_da_visualizzare_ [ $+ [ %fq_cont ] ] = 0,05    0,05 %fq_riga_da_visualizzare_ [ $+ [ %fq_e ] ]
        }
      }
      inc %fq_cont
    }
  }
}

alias visualizza_griglia_finalquick {
  if (%finalquick_gioco == ON) {
    msg %game_channel 0,05 finalquick ... 12 manche n: 0 %fq_manche_num 
    .timer1 1 1 /mode %game_channel +m
    set %finalquick_temp 1
    while (%finalquick_temp < 9) {
      if (%finalquick_temp = 2) {
        %fq_t = 2
        msg %game_channel  11,05 completa la seguente parola 11,12 %fq_riga_da_visualizzare_  [ $+ [ %fq_d ] ] %fq_riga_da_visualizzare_ [ $+ [ %fq_e ] ] 14,05. 
      }
      if (%finalquick_temp = 3) {
        %fq_t = 3
        msg %game_channel 0,05 %fq_riga_da_visualizzare_ [ $+ [ %fq_t ] ] 
      }
      if (%finalquick_temp = 4) {
        %fq_t = 4
        msg %game_channel 0,05 %fq_riga_da_visualizzare_ [ $+ [ %fq_t ] ] 
      }
      if (%finalquick_temp = 5) {
        %fq_t = 5
        msg %game_channel 0,05 %fq_riga_da_visualizzare_ [ $+ [ %fq_t ] ] 
      }
      if (%finalquick_temp = 6) {
        %fq_t = 6
        msg %game_channel 0,05 %fq_riga_da_visualizzare_ [ $+ [ %fq_t ] ] 
      }
      if (%finalquick_temp = 7) {
        %fq_t = 7
        msg %game_channel 0,05 %fq_riga_da_visualizzare_ [ $+ [ %fq_t ] ] 
      }
      if (%finalquick_temp = 8) {
        %fq_t = 8
        msg %game_channel 0,05 %fq_riga_da_visualizzare_ [ $+ [ %fq_t ] ] 
      }
      inc %finalquick_temp
    }
    /timerfq_ [ $+ [ %fq_t ] ] 1 %fq_t msg %game_channel 11,05 Scrivete la vostra parole in query a 08,05 %fq_nick_me 
    inc %fq_t
    /timerfq_ [ $+ [ %fq_t ] ] 1 %fq_t msg %game_channel 0,05 Per cambiarla riscrivetela. 
    inc %fq_t
    /timerfq_ [ $+ [ %fq_t ] ] 1 %fq_t /manche_avviata_finalquick
  }
}

alias manche_avviata_finalquick {
  if (%finalquick_gioco == ON) {
    set %fq_manche_stato ON
    msg %game_channel 08,05 Avete 0 50 08 secondi da adesso . 
    /timerfq_1 1 45 msg %game_channel 0,05 Mancano solo 11 10 0 secondi..08,05Prr 

    /timerfq_2 1 50 /fine_tempo_manche_finalquick
  }
}

alias fine_tempo_manche_finalquick {
  if (%finalquick_gioco == ON) {
    msg %game_channel 11,05 Manche 12 %fq_manche_num 11 terminata! 
    %fq_num_nick = $numtok(%fq_giocatori_manche,32)
    if (%fq_num_nick != 0) {
      %fq_cont = 1
      while (%fq_cont <= %fq_num_nick) {
        %fq_temp_nick = $gettok(%fq_giocatori_manche, %fq_cont ,32)
        msg %fq_temp_nick 011,05 /0!11\ 0 TEMPO SCADUTO  11,05/0!11\ 

        inc %fq_cont
      }
    }
    set %fq_manche_stato OFF
    /timerfq_1 1 5 /fine_manche_finalquick
  }
}

alias finalquick_generahs {
  %x = 1
  while ( %x <= 11 ) {
    write finalquickrecord.txt none#0
    inc %x
  }
}

alias fine_manche_finalquick {
  if (%finalquick_gioco != ON) goto esci_da_fine_manche_finalquick
  msg %game_channel 0,05 Risultati manche: 
  %fq_num_nick = $numtok(%fq_giocatori_manche,32)
  if (%fq_num_nick == 0) {
    /timerfq_1 1 2 /msg %game_channel 08,05 Nessuna parola trovata ! 
    %fq_tempo = 5
  }
  else {
    /prepara_risultati_manche_finalquick
    %fq_cont = 1
    while (%fq_cont <= %fq_num_nick) {
      %fq_temp_nick = %fq_cl_m_ [ $+ [ %fq_cont ] ]
      %fq_temp_parola = %fq_parola_manche_ [ $+ [ %fq_temp_nick ] ]
      %fq_temp_punti = %fq_punti_parola_ [ $+ [ %fq_temp_nick ] ] 
      %fq_t_punti = punti
      %fq_temp_len = $len(%fq_temp_parola)
      if (%fq_temp_len == %fq_lunghezza) {
        %fq_temp_punti_tot = $calc(%fq_temp_punti + 4)
        %fq_t_punti = + 4 11,05 punti LUNGHEZZA per un totale di 0,05 %fq_temp_punti_tot 11,05 punti 
        if (%fq_punti_velocita == %fq_temp_nick) { 
          %fq_temp_punti_tot = $calc(%fq_temp_punti_tot + 4)
          %fq_t_punti = + 4 11,05 punti LUNGHEZZA 0,05 + 4 11,05 punti VELOCITA per un totale di 0,05 %fq_temp_punti_tot 11,05 punti 
        }
      }
      else {
        if (%fq_punti_velocita == %fq_temp_nick) { 
          %fq_temp_punti_tot = $calc(%fq_temp_punti + 4)
          %fq_t_punti = + 4 11,05 punti VELOCITA per un totale di 0,05 %fq_temp_punti_tot 11,05 punti 
        }
        else { 
          %fq_temp_punti_tot = %fq_temp_punti
        }
      }
      /timerfq_ [ $+ [ %fq_cont ] ] 1 %fq_cont /msg %game_channel 08,05 %fq_temp_nick 11,05 con la parola 0,05 %fq_temp_parola 11,05 ottiene 0,05 %fq_temp_punti %fq_t_punti 
      inc %fq_cont
      %fq_giocatori_partita = $addtok(%fq_giocatori_partita,%fq_temp_nick,32)
      %fq_p_t = %fq_punti_tot_ [ $+ [ %fq_temp_nick ] ]
      if (%fq_punti_tot_ [ $+ [ %fq_temp_nick ] ] == $null) { %fq_punti_tot_ [ $+ [ %fq_temp_nick ] ] = 0 }
      %fq_punti_tot_ [ $+ [ %fq_temp_nick ] ] = $calc(%fq_p_t + %fq_temp_punti_tot)
    }
    %fq_tempo = %fq_cont + 5
    unset %fq_parola*
    /prepara_classifica_finalquick
  }
  /timerfq_ [ $+ [ %fq_tempo ] ] 1 %fq_tempo /visualizza_classifica_finalquick
  :esci_da_fine_manche_finalquick
}

alias visualizza_classifica_finalquick {
  if (%finalquick_gioco != ON) goto esci_da_visualizza_classifica_finalquick
  %fq_num_nick = $numtok(%fq_giocatori_partita,32)
  if (%fq_num_nick > 5 ) { %fq_num_nick = 5 }
  /msg %game_channel 0,05 CLASSIFICA dopo 08,05 %fq_manche_num 0,05 manche... 
  if (%fq_num_nick == 0) {
    /msg %game_channel 08,05 1. ---------- 
    goto prepara_nuova_manche
  }
  %fq_cont = 1
  while (%fq_cont <= %fq_num_nick) {
    inc %fq_tempo
    %fq_cl = %fq_cl_ [ $+ [ %fq_cont ] ]
    %fq_punti_cl = %fq_punti_tot_ [ $+ [ %fq_cl ] ]
    /timerfq_ [ $+ [ %fq_tempo ] ] 1 %fq_tempo /msg %game_channel 0,05 %fq_cont . 08,05 %fq_cl 11,05 con punti 0,05 %fq_punti_cl  0,05 media $calc(%fq_punti_cl / %fq_manche_num) 
    inc %fq_cont
  }
  if (%fq_punti_tot_ [ $+ [ %fq_cl_1 ] ] > %fq_punteggio) {
    set %finalquick_media $calc(%fq_punti_tot_ [ $+ [ %fq_cl_1 ] ] / %fq_manche_num)
    set %y 1
    while (%y <= 10) {
      set %finalquick_procline $read -l %y finalquickrecord.txt
      set %finalquick_ptsentry $gettok(%finalquick_procline,2,35)
      if (%finalquick_media > %finalquick_ptsentry) {
        set %z %y
        set %finalquick_inline %fq_cl_1 $+ $chr(35) $+ %finalquick_media
        msg %game_channel 08,05 %fq_cl_1 11 conquista la posizione numero 0 %y 11 con la media di 0 %finalquick_media  
        while (%z <= 10) {
          set %finalquick_temp $read -l %z finalquickrecord.txt
          write -l %z finalquickrecord.txt %finalquick_inline
          set %finalquick_inline %finalquick_temp    
          inc %z
        } 
        goto scstophere 
      }
      inc %y
    }
    :scstophere 
    %fq_tempo = %fq_tempo + 10
    /timerfq_ [ $+ [ %fq_tempo ] ] 1 %fq_tempo /msg %game_channel 0,05 finalquick TeRmInAtO!
    inc %fq_tempo
    /timerfq_ [ $+ [ %fq_tempo ] ] 1 %fq_tempo /msg %game_channel 0,05 alle ore : 08,05 $time 0 il giorno 08,05 $date 
    inc %fq_tempo
    /timerfq_ [ $+ [ %fq_tempo ] ] 1 %fq_tempo /msg %game_channel 11,05 Il vincitore  2,05 %fq_cl_1 ! 0,05 con la media di $calc(%fq_punti_tot_ [ $+ [ %fq_cl_1 ] ] / %fq_manche_num) 11,05 Ma Dai Veramente Dici ??? 
    inc %fq_tempo
    msg %game_channel 011,05 /0!11\ 0 Classifica Generale   11,05/0!11\ 
    set %finalquick_temp 1
    while (%finalquick_temp <= 5) {
      set %finalquick_classriga $read(" $+ $scriptdir\finalquickrecord.txt $+ ", %finalquick_temp)
      .timerfinalquick_mostraclass $+ %finalquick_temp 1 %finalquick_temp msg %game_channel 0,05 %finalquick_temp ) 2 $gettok(%finalquick_classriga,1,35) 11,05 con 0,05 $gettok(%finalquick_classriga,2,35) 11,05 punti 
      inc %finalquick_temp
    }

    /timerfq_ [ $+ [ %fq_tempo ] ] 1 %fq_tempo /fine_gioco_finalquick
  }
  :prepara_nuova_manche
  %fq_tempo = %fq_tempo + 10
  unset %fq_lunghezza
  unset %fq_giocatori_manche
  unset %fq_punti_parola_*
  /timerfq_ [ $+ [ %fq_tempo ] ] 1 %fq_tempo /avvia_manche_finalquick
  :esci_da_visualizza_classifica_finalquick
}

alias prepara_risultati_manche_finalquick {
  if (%finalquick_gioco != ON) goto esci_da_prepara_risultati_manche_finalquick
  %fq_cont = 1
  while (%fq_cont <= %fq_num_nick) {
    %fq_cl_m_ [ $+ [ %fq_cont ] ] = $gettok(%fq_giocatori_manche, %fq_cont ,32)
    if (%fq_cont == 1) {
      %fq_cl_m_1 = $gettok(%fq_giocatori_manche,1,32)
      %fq_punti_velocita = $gettok(%fq_giocatori_manche,1,32)
    }
    else {
      %fq_cont_x = %fq_cont
      while (%fq_cont_x > 1) {
        %fq_cont_y = %fq_cont_x - 1
        %fq_cl_m_x = %fq_cl_m_ [ $+ [ %fq_cont_x ] ]
        %fq_cl_m_y = %fq_cl_m_ [ $+ [ %fq_cont_y ] ]
        if ( %fq_punti_parola_ [ $+ [ %fq_cl_m_x ] ] < %fq_punti_parola_ [ $+ [ %fq_cl_m_y ] ] ) {
          %fq_cl_m_ [ $+ [ %fq_cont_y ] ] = %fq_cl_m_x
          %fq_cl_m_ [ $+ [ %fq_cont_x ] ] = %fq_cl_m_y
        }
        else {
          %fq_cl_m_ [ $+ [ %fq_cont_x ] ] = %fq_cl_m_x
          %fq_cont_x = 1
        }
        dec %fq_cont_x
      }
    }
    inc %fq_cont
  }
  :esci_da_prepara_risultati_manche_finalquick
}

alias prepara_classifica_finalquick {
  if (%finalquick_gioco != ON) goto esci_da_prepara_classifica_finalquick
  %fq_num_nick = $numtok(%fq_giocatori_partita,32)
  %fq_cont = 1
  while (%fq_cont <= %fq_num_nick) {
    %fq_cl_ [ $+ [ %fq_cont ] ] = $gettok(%fq_giocatori_partita, %fq_cont ,32)
    if (%fq_cont == 1) {
      %fq_cl_1 = $gettok(%fq_giocatori_partita,1,32)
    }
    else {
      %fq_cont_x = %fq_cont
      while (%fq_cont_x > 1) {
        %fq_cont_y = %fq_cont_x - 1
        %fq_cl_x = %fq_cl_ [ $+ [ %fq_cont_x ] ]
        %fq_cl_y = %fq_cl_ [ $+ [ %fq_cont_y ] ]
        if ( %fq_punti_tot_ [ $+ [ %fq_cl_x ] ] > %fq_punti_tot_ [ $+ [ %fq_cl_y ] ] ) {
          %fq_cl_ [ $+ [ %fq_cont_y ] ] = %fq_cl_x
          %fq_cl_ [ $+ [ %fq_cont_x ] ] = %fq_cl_y
        }
        else {
          %fq_cl_ [ $+ [ %fq_cont_x ] ] = %fq_cl_x
          %fq_cont_x = 1
        }
        dec %fq_cont_x
      }
    }
    inc %fq_cont
  }
  :esci_da_prepara_classifica_finalquick
}
