'              -=[ Trivia ]=-  Flexx(rus) Edition
'       raylu,    .
'              . 
'
'  0.4 (30  2005)
'     auto_repeat
'       
'               (  .trivia .triviarestart .top5)
'     .usercount (     )
'     .file (    )
'     .filename (     )
'
'  0.3 (29  2005)
'        Trivia  .        .triviarestart
'              
'         .trivia    
'     .triviarestart
'        timer_trigger
'       .rank .stats .score
'
'  0.2 (28  2005) 
'     .help .top5 .rank .stats .score  (.rank = .stats = .score)
'          
'            1       
'     
'
'  0.1 (27  2005)
'     
'           (    =0   )
'      (.file  .urank)
'
'            Diablo  StarCraft.     .
'     WC3 TFT                          (c) Flexx(rus) || EMAIL: flexx_rus@mail.ru
'         Frozen Throne rus-1

'  .trivia     Trivia.      .
'  .triviarestart   Trivia.     !
'  .writescores     .
'  .top5   TOP 5 .
'  .rank           .
'  .file     .
'  .filename       .

'question(1000), answer(1000), asked(1000) <<        max_lines!
dim lines_num, question(10000), answer(10000), asked(10000) 'questions and answers
public const max_lines=10000 

'hscores_users(500), hscores_scores(500) <<        hscores_maxcount!
dim hscores_users(500), hscores_scores(500)
public const hscores_maxcount=500
 
'  
public const ScoreFilePath = "C:\Program Files\SB\scores.txt" '      -  
public const QuestionFilesDir = "C:\Program Files\SB\" '   
QuestionFileName = "voprosi.txt" '    

',     Trivia
public const trivAccess = 20

'   
public const nHints = 7
public const timer_division = True ' "True"       scTimer.Interval*2
public const fast_question = False ' "True"     scTimer.Interval     ( )
scTimer.Interval = 7000
public const auto_repeat = False ' "True"        ,  "False"  

'======================================================================================
'                           !!! DO NOT EDIT BELOW HERE !!!
'======================================================================================

dim trivia_enabled, D2s, score, rank_maxbnd, send_to_whisper, old_file_name
dim hints, current_hint, current_answer, current_answer2, timer_trigger

'debug : show addition information to find FUCKING errors
public const EnableDebugMessages=False 

set score = CreateObject("Scripting.Dictionary")

' only for debuging script
Sub debug_log(msg)
 if EnableDebugMessages then
  AddChat vbCyan, "DEBUG: " & msg
 end if
end sub

' Fires when script loaded (reloaded)
Sub Event_Load()
 AddChat vbCyan, "-=[ TRIVIA ]=- Flexx(rus) Edition (version 0.4)"
 AddChat vbYellow, " : " & scTimer.Interval/1000 & " ."
 if timer_division then
 AddChat vbYellow, " : " & scTimer.Interval*2/1000 & " ."
 Else
 AddChat vbYellow, " : " & scTimer.Interval/1000 & " ." 
 end if
 trivia_enabled = False
 hints = -1
 timer_trigger = False
 scTimer.Enabled = True
 lines_num = 0
 call Load_Scores
 AddChat vbGreen, "   : " & ScoreFilePath & "   " & score.Count & " ."
 call Load_Questions
 call Update_Rank
End Sub

Sub Send_User_Rank(Username)
  dim i, user_found
  call Update_Rank

  user_found = False
  for i = 0 to rank_maxbnd 
   if hscores_users(i) = username then
    AddQ "/w " & D2s & Username & "  " & i+1 & "/" & score.Count & "  .   " & hscores_scores(i) & " .",1
    user_found = True
	exit for
   end if
  next
  if user_found = False then
   AddQ "/w " & D2s &  Username & "     .   0 .",1
  end if
End Sub

Sub Top5
  dim  t5, t5line
  call Update_Rank

  t5 = 4
  if t5>rank_maxbnd then
   t5 = rank_maxbnd
  end if
  
  t5line = "TOP 5 USERS: " 
  for i = 0 to t5 
   t5line = t5line & hscores_users(i) & " [" & hscores_scores(i)& "]   "
  next
  AddQ send_to_whisper & t5line
  send_to_whisper = vbNullString
end sub

Sub Update_Rank
 dim i, j, pts, plyrs, sctemp, unmtemp 
  pts = score.Items          'score
  plyrs = score.Keys         'users 

  rank_maxbnd = hscores_maxcount
  if rank_maxbnd>UBound(plyrs) then
   rank_maxbnd = UBound(plyrs)
  end if

  for i = 0 to rank_maxbnd
   hscores_users(i) = plyrs(i)
   hscores_scores(i) = CInt(pts(i))
  next

'sorting rank
  for i = 0 to rank_maxbnd
   for j = i to rank_maxbnd
    if hscores_scores(i) < hscores_scores(j) then
     sctemp = hscores_scores(i)
     unmtemp = hscores_users(i)
     hscores_scores(i) = hscores_scores(j)
     hscores_users(i) = hscores_users(j)
     hscores_scores(j) = sctemp
     hscores_users(j) = unmtemp
    end if
   next
  next
end sub

Sub Load_Scores
  debug_log("Load_Scores.begin")
  Dim fso, score_file, read_string, cLine 
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set score_file = fso.OpenTextFile(ScoreFilePath, 1, True)
   Do While score_file.AtEndOfStream <> True
      read_string = score_file.ReadLine
      cLine = Split(read_string, "|")
      score.Add cLine(0), cLine(1) 
   Loop
  score_file.Close
  debug_log("Load_Scores.end")  	
End Sub

Sub Load_Questions
  debug_log("Load_Questions.begin")
  Dim fso, triv_File, read_string, cLine
  AddChat vbGreen, "   : " & QuestionFilesDir & QuestionFileName
  Set fso = CreateObject("Scripting.FileSystemObject")
  If Not fso.FileExists(QuestionFilesDir & QuestionFileName) Then
   AddChat vbRed, "!     !   QuestionFilesDir  QuestionFileName  !"
   QuestionFileName = old_file_name
    AddQ send_to_whisper & "!     !" ,1
    send_to_whisper = vbNullString
   Exit Sub
  End If

  Set triv_File = fso.OpenTextFile(QuestionFilesDir & QuestionFileName, 1, True)
  lines_num = 0
   Do While triv_File.AtEndOfStream <> True
      read_string = triv_File.ReadLine
      cLine = Split(read_string, "*")
       If UBound(cLine) >= 1 Then
        question(lines_num) = cLine(0)
        answer(lines_num) = cLine(1)
        lines_num = lines_num + 1
        if lines_num>max_lines then 
         AddChat vbRed, "      !   [question, answer, max_lines, asked]  !"
         AddChat vbGreen, " " & lines_num-1 & " !"
         triv_File.Close
         for i = 0 to lines_num 
          asked(i) = False
         next
         Exit Sub
        end if
       Else
       AddChat vbRed, " *      " & lines_num & " .     !"
       End if
   Loop  
  triv_File.Close
  if lines_num<>0 Then
   AddQ send_to_whisper & " " & lines_num-1 & " !" ,1
   send_to_whisper = vbNullString
  end if
  for i = 0 to lines_num 
   asked(i) = False
  next
  AddChat vbGreen, " " & lines_num-1 & " !"
  Debug_log("Load_Questions.end")
End Sub

Sub Save_Scores
  Dim fso, score_file, sList, cUser
  debug_log("Save_Scores.begin") 

  if score.Count = 0 Then
   exit sub
  end if

  Set fso = CreateObject("Scripting.FileSystemObject")
  sList = score.Keys
  Set score_file = fso.OpenTextFile(ScoreFilePath, 2, True)
   For i=0 to UBound(sList)
    cUser = sList(i)
    score_file.WriteLine cUser & "|" & score.Item(cUser)
   Next
  score_file.Close
  AddChat vbGreen, "  !"
  debug_log("Save_Scores.end")
End Sub

Sub Event_ServerInfo(Message)
 AddChat vbGreen, "Event_ServerInfo: " & Message
End Sub

Sub Event_ServerError(Message)
 AddChat vbGreen, "Event_ServerError: " & Message
End Sub

Sub Event_UserTalk(Username, Flags, Message, Ping)
 call UserTalk(Username, Flags, Message, Ping)
End Sub

Sub UserTalk(Username, Flags, Message, Ping)
 dim i
  debug_log("Event_UserTalk.begin")
  If Message = BotVars.Trigger & "trivia" Then
   GetDBEntry Username, myAccess, myFlags
   If myAccess >= trivAccess Then
    If trivia_enabled Then 
     trivia_enabled = False
     hints = -1
     timer_trigger = False
     AddQ send_to_whisper & "Trivia !  " & BotVars.Trigger & "trivia  . (  " & trivAccess & " )",1
     send_to_whisper = vbNullString
    Else
     if lines_num = 0 then
      AddQ send_to_whisper & "   Trivia      !     " & BotVars.Trigger & "trivia  . (  " & trivAccess & " )",1
      exit sub
     end if
     trivia_enabled = True
     hints = -1
     call resetTimer
     AddQ send_to_whisper & "Trivia !  " & BotVars.Trigger & "trivia  . (  " & trivAccess & " )",1
     send_to_whisper = vbNullString
     if fast_question Then
      call scTimer_Timer
     end if
    End If
   End If
   Exit Sub
  End If

  if Message = BotVars.Trigger & "triviarestart" then
   GetDBEntry Username, myAccess, myFlags
   If myAccess >= trivAccess Then
     if lines_num = 0 then
      AddQ send_to_whisper & "   Trivia      !     " & BotVars.Trigger & "trivia  . (  " & trivAccess & " )",1
      exit sub
     end if
     for i = 0 to max_lines 
      asked(i) = False
     next
    call resetTimer
    hints = -1
    if trivia_enabled = False then 
     trivia_enabled = True
    end if
    AddQ send_to_whisper & "!    !",1
    send_to_whisper = vbNullString
    if fast_question Then
     call scTimer_Timer
     exit sub
    end if   
   end if
   exit sub
  end if

  If Message = BotVars.Trigger & "help" Then
   AddQ send_to_whisper & " : .top5 .rank .usercount .filename |  : .trivia .triviarestart .writescores .file",1
   send_to_whisper = vbNullString
   exit sub
  End If

  If Message = BotVars.Trigger & "score" Then
   call Send_User_Rank(Username)
   exit sub
  End If 

  If Message = BotVars.Trigger & "rank" Then
   call Send_User_Rank(Username)
   exit sub
  End If 

  If Message = BotVars.Trigger & "stats" Then
   call Send_User_Rank(Username)
   exit sub
  End If 

  If Message = BotVars.Trigger & "top5" Then
   call Top5
   exit sub
  End If
  
  If Message = BotVars.Trigger & "usercount" Then
   AddQ send_to_whisper & "   " & score.Count & " !" ,1
   send_to_whisper = vbNullString 
   exit sub
  End if 

  If Message = BotVars.Trigger & "writescores" Then
   GetDBEntry Username, myAccess, myFlags
    If myAccess >= trivAccess Then
     Call Save_Scores
     AddQ send_to_whisper & "  !",1
     send_to_whisper = vbNullString
    End if
   exit sub
  End If

  If Left(Message, 5) = BotVars.Trigger & "file" Then
   GetDBEntry Username, myAccess, myFlags
   If myAccess >= trivAccess Then
    old_file_name = QuestionFileName
    QuestionFileName = Mid(Message, 7)
    call Load_Questions
   end if  
   exit sub
  end if

  if Message = BotVars.Trigger & "filename" Then
   AddQ send_to_whisper & "   : " & QuestionFileName,1
   exit sub
  end if

  if trivia_enabled = False then 
   debug_log("Event_UserTalk.end")
   Exit Sub
  End if

  debug_log(LCase(current_answer))
  If LCase(Message) = LCase(current_answer) Then
   If score.Exists(Username) Then
    score.Item(Username) = score.Item(Username) + 1
   Else
    score.Add Username, 1 
   End If 
    AddQ "/me " & Username & " !  : " & current_answer & ".  : " & score.Item(Username) & " .",1
    current_answer = "hkjhjkhkjhkjhkjhkjhkjhkjhu8ybcdsvsdvkhskvjshyl"   'bug with scores fix
   call Save_Scores
   call resetTimer
   hints = -1
   if fast_question Then
    call scTimer_Timer
    exit sub
   end if
  End If
  debug_log("Event_UserTalk.end")
End Sub

Sub Event_UserEmote(Username, Flags, Message)
End Sub

Sub Event_WhisperFromUser(Username, Flags, Message)
 send_to_whisper = "/w " & D2s &  Username & " "
 Call UserTalk(Username, Flags, Message, 0)
 send_to_whisper = vbNullString
End Sub

Sub Event_UserJoins(Username, Flags, Message, Ping, Product, Level, OriginalStatstring)
End Sub

Sub Event_UserLeaves(Username, Flags)
End Sub

Sub Event_FlagUpdate(Username, NewFlags, Ping)
End Sub

Sub Event_LoggedOn(Username, Product)
 If Product = "VD2D" Or Product = "PX2D" Then D2s = "*"
End Sub

Sub Event_UserInChannel(Username, Flags, Message, Ping, Product)
End Sub

Sub Event_ChannelJoin(ChannelName, Flags)
End Sub

Sub scTimer_Timer()
 debug_log("scTimer_Timer.begin")
 dim i, old_hint, simbol_num, find_ask, c_hint, hint_opened, all_asked
  if trivia_enabled = False Then
   exit sub
  end if

  if lines_num = 0 then 
   exit sub
  end if

  if timer_division then
   if timer_trigger then
    timer_trigger = False
    Exit Sub
   Else
    timer_trigger = True
   end if
  end if

  'All asked?
  all_asked = True
  for i = 0 to lines_num-1
   if asked(i) = False then
    all_asked = False
   exit for
   end if
  next
  if all_asked = True Then
   if auto_repeat = True then
   AddQ "  .     ...    =)",1
    for i = 0 to lines_num 
     asked(i) = False
    next
    hints = -1
    timer_trigger = False
    trivia_enabled = True
    exit sub
   Else
    AddQ "  ! Trivia !  .triviarestart  .trivia    ...",1
    for i = 0 to lines_num 
     asked(i) = False
    next
    trivia_enabled = False
    hints = -1
    timer_trigger = False
    exit sub
   end if
  end if 
 
 ' Find random question
 ' Create current_answer and current_answer2 variables 
 ' Test if all questions was ended...
 if hints = -1 then
  current_answer2 = vbNullString
  current_answer = vbNullString 
  current_hint = vbNullString
  For i = 0 to 1000000
   randomize
   find_ask = Int((lines_num * Rnd))
   if asked(find_ask)=False then
    if question(i)<>vbNullString  Then
     Exit For
    end if
   end if
  next
   if question(find_ask) <> vbNullString Then
    if asked(find_ask) = True Then
     'question not found
     hints = -1
     exit sub
    end if
   Else
    'question not found
    hints = -1
    exit sub
   end if
   asked(find_ask)=True
   current_answer=answer(find_ask)
   AddQ question(find_ask),1
   hints = 0
   
   For i = 1 to len(current_answer) 
    current_answer2 = current_answer2 & mid(current_answer,i,1) & " "
   next
  Exit Sub
 end if

 ' no hints =)
 if nHints = 0 then
  AddQ " .  : " & current_answer,1
  current_answer = "hkjhjkhkjhkjhkjhkjhkjhkjhu8ybcdsvsdvkhskvjshyl"   'bug with scores fix
  hints = -1
  call resetTimer
  if fast_question Then
   call scTimer_Timer
   exit sub
  end if
 end if

 ' The first hint
 ' Create hint and show it
 If hints = 0 then
   For i = 1 to len(current_answer) 
    If mid(current_answer,i,1)<>" " then
     current_hint=current_hint & "_ "
    Else
     current_hint=current_hint & "  "
    End If
   Next
  hints = hints + 1
  AddQ ": " & current_hint,1

 ' if answer showed by hint
   hint_opened = True
   for i = 1 to len(current_hint)
    if mid(current_hint,i,1) = "_" then
     hint_opened = False
     exit for
    end if
   next

   If hint_opened = True then
    AddQ "    . : " & current_answer,1
    current_answer = "hkjhjkhkjhkjhkjhkjhkjhkjhu8ybcdsvsdvkhskvjshyl"   'bug with scores fix
    hints = -1
    call resetTimer
    if fast_question Then
     call scTimer_Timer
     exit sub
    end if
   end if
  Exit Sub
 end if
 
 If hints < nHints Then
  old_hint = current_hint
  ' open random simbol in hint 
  For i = 0 to 50000
   randomize
   simbol_num = Int((len(current_hint) * Rnd) + 1)
   c_hint = vbNullString
   c_hint = mid(current_hint,1,simbol_num-1)
   c_hint = c_hint & mid(current_answer2,simbol_num,1)
   c_hint = c_hint & mid(current_hint,simbol_num+1,len(current_hint)-simbol_num+1)
   current_hint = c_hint

   if current_hint <> old_hint then
    hints = hints + 1
    AddQ ": " & current_hint,1
    Exit For
   end if   
  next
 ' if answer showed by hint   
   hint_opened = True
   for i = 1 to len(current_hint)
    if mid(current_hint,i,1) = "_" then
     hint_opened = False
     exit for
    end if
   next
   If hint_opened = True then
    AddQ "    . : " & current_answer,1
    current_answer = "hkjhjkhkjhkjhkjhkjhkjhkjhu8ybcdsvsdvkhskvjshyl"   'bug with scores fix
    hints = -1
    call resetTimer
    if fast_question Then
     call scTimer_Timer
    end if
    exit sub
   end if
 Else
  AddQ " .  : " & current_answer,1
  current_answer = "hkjhjkhkjhkjhkjhkjhkjhkjhu8ybcdsvsdvkhskvjshyl"   'bug with scores fix
  hints = -1
  call resetTimer
  if fast_question Then
   call scTimer_Timer
   exit sub
  end if
 End If
 debug_log("scTimer_Timer.end")
End Sub

Sub Event_PressedEnter(Text)
' if connected = False then
'  Addchat vbGreen, Text
' end if
 Call UserTalk(myUsername, 0, Text, 0)
End Sub

Sub resetTimer()
 Timer_trigger = False
 scTimer.Enabled = False
 scTimer.Enabled = True
End Sub

Sub Event_KeyReturn(KeyName, KeyValue)
End Sub

Sub Event_Close()
 Save_Scores
End Sub

' (c) Flexx(rus) All rights reserved!