VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsUsers"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'-----------------------------------------------------
' MirageBot Users Class
' Written by Christopher Nevin (lancergli@gmail.com)
'-----------------------------------------------------

Option Explicit

Private Users() As New objUser

Public Function Count() As Integer
    Count = UBound(Users)
End Function

Public Sub Clear()
    ReDim Users(0)
End Sub
'
'Public Function Create(Username As String, Statstring As String, Flags As Long, Ping As Long, Optional IsInvisible As Boolean = False, Optional IsShown As Boolean = False) As Object
'On Error GoTo hErr
'2   Dim U As New objUser
'3   U.Username = Username
'4   U.Statstring = Statstring
'5   U.Flags = Flags
'6   U.Ping = Ping
'7   U.TickJoin = DblTickCount()
'8   U.TickTalk = 0
'9   U.IsInvisible = IsInvisible
'10  U.IsShown = IsShown
'    Set Create = U
'    Exit Function
'hErr:
'    ErrorHandler Err.Number, Err.Description, Erl, "Users", "Create"
'End Function

Public Sub Add(Username As String, Statstring As String, Flags As Long, Ping As Long, Optional IsInvisible As Boolean = False, Optional IsShown As Boolean = False)
On Error GoTo hErr
1   If UBound(Users) > 0 Or Users(0).Account = Suffix(Username) Then Remove Username
2   Dim U As New objUser
3   U.Username = Username
4   U.Statstring = Statstring
5   U.Flags = Flags
6   U.Ping = Ping
7   U.TickJoin = DblTickCount()
8   U.TickTalk = 0
9   U.IsInvisible = IsInvisible
10  U.IsShown = IsShown
11  If LenB(Users(0).Username) Then ReDim Preserve Users(UBound(Users) + 1)
12  Set Users(UBound(Users)) = U
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Users", "Add"
End Sub

Public Function Match(strMatch As String) As String()
    Dim I As Integer, S() As String, C As Integer
    C = UBound(Users)
    ReDim S(0)
    For I = 0 To C
        If Matches(Users(I).Account, strMatch) Then
            If LenB(S(0)) = 0 Then
                S(0) = Users(I).Account
            Else
                ReDim Preserve S(UBound(S) + 1)
                S(UBound(S)) = Users(I).Account
            End If
        End If
    Next I
    Match = S
End Function

Public Sub Remove(Username As String)
On Error GoTo hErr
    Dim I As Integer
    I = Find(Username)
    If I > -1 Then
        If UBound(Users) = 0 Then
            ReDim Users(0)
        Else
            Dim r As Integer, C As Integer
            C = UBound(Users) - 1
            For r = I To C
                Set Users(r) = Users(r + 1)
            Next
            ReDim Preserve Users(C)
        End If
    End If
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Users", "Remove"
End Sub

Public Sub Update(Username As String, Flags As Long, Ping As Long)
On Error GoTo hErr
    Dim I As Integer
    I = Find(Username)
    If I > -1 Then
        Users(I).Flags = Flags
        Users(I).Ping = Ping
        Users(I).Username = Username
    End If
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Users", "Update"
End Sub

Public Function Find(ByVal Username As String) As Integer
On Error GoTo hErr
    Dim I As Integer, C As Integer
    Username = LCase$(Suffix(Username))
    C = UBound(Users)
    For I = 0 To C
        If (Username = LCase$(Users(I).Account)) Then
            Find = I
            Exit Function
        End If
    Next I
    Find = -1
    Exit Function
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Users", "Find"
End Function

Public Function GetByName(Username As String) As objUser
On Error GoTo hErr
    Dim I As Integer
    I = Find(Username)
    If I > -1 Then
        Set GetByName = Users(I)
    End If
    Exit Function
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Users", "GetByName"
End Function

Public Function GetByIndex(Index As Integer) As objUser
On Error GoTo hErr
    If Index < 0 Or Index > UBound(Users) Then Exit Function
    Set GetByIndex = Users(Index)
    Exit Function
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Users", "GetByIndex"
End Function

Public Function GetAll(Optional ByRef Success As Boolean) As Object()
    On Error Resume Next
    Dim U() As Object, I As Integer, L As Long, C As Long
    L = LBound(Users)
    C = UBound(Users)
    ReDim U(0)
    ReDim U(L To C)
    For I = L To C
        Set U(I) = Users(I)
    Next I
    GetAll = U
    If Err.Number = 0 Then Success = True
End Function

Public Sub Class_Initialize()
    ReDim Users(0) As New objUser
End Sub

Public Sub Class_Terminate()
    On Error Resume Next
    ReDim Users(0)
End Sub
