Attribute VB_Name = "modTrivia"

Option Base 0
Option Explicit
Option Compare Text
Private Declare Function STimer Lib "user32" Alias "SetTimer" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KTimer Lib "user32" Alias "KillTimer" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Public Bot As Object
Public sAnswer As String
Public Trivia() As String
Public TimerI As Integer

Public Function SetTimer(ByVal hWnd As Long, ByVal Interval As Long) As Long
    SetTimer = STimer(hWnd, 0, Interval, AddressOf ExecTimer)
End Function

Public Sub KillTimer(ByVal hWnd As Long, ByRef TimerHandle As Long)
    KTimer hWnd, TimerHandle
    TimerHandle = 0
End Sub

Public Sub ExecTimer(ByVal hWnd As Long, ByVal uMsg As Long, ByVal EventID As Long, ByVal dwTime As Long)
Static splt() As String, lRnd As Long, Hint As String
    If TimerI = 4 Then
        Bot.Send "Answer: " & sAnswer
        sAnswer = vbNullString
        TimerI = 0
    ElseIf TimerI = 0 Then
        lRnd = Rand(0, UBound(Trivia))
        splt = Split(Trivia(lRnd), "|")
        Bot.Send "Q #" & lRnd & ": " & splt(0)
        sAnswer = splt(1)
        Hint = String$(Len(sAnswer), "-")
        TimerI = TimerI + 1
    Else
        Bot.Send "Hint: " & GetHint(sAnswer, Hint)
        TimerI = TimerI + 1
    End If
End Sub

Public Function Rand(ByVal lFrom As Long, ByVal lTo As Long) As Long
    Randomize
    Rand = lFrom + Int((Rnd * ((lTo + 1) - lFrom)))
End Function

Public Function GetHint(ByVal Answer As String, ByRef Hint As String) As String
Dim lRnd As Long
    If Len(Hint) > 22 Then
        Do
            lRnd = Rand(1, Len(Hint))
        Loop Until (Mid$(Hint, lRnd, 1) = "-")
        Mid$(Hint, lRnd, 1) = Mid$(Answer, lRnd, 1)
    End If
    If Len(Hint) > 12 Then
        Do
            lRnd = Rand(1, Len(Hint))
        Loop Until (Mid$(Hint, lRnd, 1) = "-")
        Mid$(Hint, lRnd, 1) = Mid$(Answer, lRnd, 1)
    End If
    If Len(Hint) > 3 Then
        Do
            lRnd = Rand(1, Len(Hint))
        Loop Until (Mid$(Hint, lRnd, 1) = "-")
        Mid$(Hint, lRnd, 1) = Mid$(Answer, lRnd, 1)
        GetHint = Hint
    End If
End Function

Public Sub GetScore(ByVal Username As String, ByRef Score As Long, ByRef Rank As Long)
Dim tStr() As String, i As Long, FF As Integer, b As String
    If Dir(App.Path & "\Trivia\Scores.dat") <> vbNullString Then
        If FileLen(App.Path & "\Trivia\Scores.dat") > 0 Then
            FF = FreeFile
            Open App.Path & "\Trivia\Scores.dat" For Input As #FF
                Do Until EOF(FF)
                    Line Input #FF, b
                    If b <> vbNullString Then
                        ReDim Preserve tStr(i)
                        tStr(i) = b
                        If LCase$(CStr(Split(b, Space(1))(0))) = LCase$(Username) Then
                            Score = CLng(Split(b, Space(1))(1))
                        End If
                        i = i + 1
                    End If
                Loop
            Close #FF
            Rank = 1
            For i = 0 To UBound(tStr)
                If CLng(Split(tStr(i), Space(1))(1)) > Score Then Rank = Rank + 1
            Next i
        Else
            Bot.AddChat vbRed, "Trivia scores file is empty!"
        End If
    Else
        Bot.AddChat vbRed, "Trivia scores file does not exist!"
    End If
End Sub

Public Sub AddScore(ByVal Username As String)
Dim tStr() As String, b As String, Score As Long, i As Long, FF As Integer
    FF = FreeFile
    ReDim Preserve tStr(0)
    If Dir(App.Path & "\Trivia\Scores.dat") <> vbNullString Then
        If FileLen(App.Path & "\Trivia\Scores.dat") > 0 Then
            Open App.Path & "\Trivia\Scores.dat" For Input As #FF
                Do Until EOF(FF)
                    Line Input #FF, b
                    If b <> vbNullString Then
                        If LCase$(CStr(Split(b, Space(1))(0))) = LCase$(Username) Then
                            Score = CLng(Split(b, Space(1))(1))
                        Else
                            ReDim Preserve tStr(i)
                            tStr(i) = b
                            i = i + 1
                        End If
                    End If
                Loop
            Close #FF
            Open App.Path & "\Trivia\Scores.dat" For Output As #FF
                Print #FF, Username & Space$(1) & (Score + 1)
                For i = 0 To UBound(tStr)
                    If tStr(i) <> vbNullString Then Print #FF, tStr(i)
                Next i
            Close #FF
        Else
            Open App.Path & "\Trivia\Scores.dat" For Output As #FF
                Print #FF, Username & " 1"
            Close #FF
        End If
    Else
        Open App.Path & "\Trivia\Scores.dat" For Output As #FF
            Print #FF, Username & " 1"
        Close #FF
    End If
End Sub

