Attribute VB_Name = "Saving"
Option Explicit
Public Type MONIERPLAYERRATE
    PlayerName As String
    Score As String
    Time As String
    Second As String
End Type

Public F_SAVERATE As String

Public ElitePlayer(1 To 11) As MONIERPLAYERRATE

Public Sub GetRecord(ByVal RecordNumber As Long, PlayerRate As MONIERPLAYERRATE)
    F_SAVERATE = App.Path & "\Bin\Score.hsg"
    If Dir(F_SAVERATE) = "" Then GoTo enderr
    
    Dim MyFile As Long
    Dim TmpStr As String
    MyFile = FreeFile
    Open F_SAVERATE For Random As MyFile Len = 500
        Get MyFile, RecordNumber, PlayerRate
    Close MyFile
    
    TmpStr = PlayerRate.PlayerName
    
    PlayerRate.PlayerName = BinaryUnCrypt(PlayerRate.PlayerName)
    PlayerRate.Score = BinaryUnCrypt(PlayerRate.Score)
    PlayerRate.Second = BinaryUnCrypt(PlayerRate.Second)
    PlayerRate.Time = BinaryUnCrypt(PlayerRate.Time)
    
    Exit Sub
enderr:
    Exit Sub
End Sub

Public Sub PutRecord(ByVal RecordNumber As Long, PlayerRate As MONIERPLAYERRATE)
    F_SAVERATE = App.Path & "\Bin\Score.hsg"
    
    Dim MyFile As Long
    
    PlayerRate.PlayerName = BinaryEncrypt(PlayerRate.PlayerName)
    PlayerRate.Score = BinaryEncrypt(PlayerRate.Score)
    PlayerRate.Second = BinaryEncrypt(PlayerRate.Second)
    PlayerRate.Time = BinaryEncrypt(PlayerRate.Time)
    
    MyFile = FreeFile
    On Error GoTo enderr
    Open F_SAVERATE For Random As MyFile Len = 500
        Put MyFile, RecordNumber, PlayerRate
    Close MyFile
    
    Exit Sub
enderr:
    Exit Sub
End Sub

Public Sub SaveRate(ByVal PlayerName As String, ByVal Score As Long, ByVal sTime As String, ByVal eSecond As Long)
    Dim Cnt As Long
    Dim GottenIndex As Long
    Dim TmpPlayerRate As MONIERPLAYERRATE
    Dim RecordCount As Long
    
    LoadRates RecordCount
    
    TmpPlayerRate.PlayerName = PlayerName
    TmpPlayerRate.Score = Score
    TmpPlayerRate.Second = eSecond
    TmpPlayerRate.Time = sTime
    
    GottenIndex = 0
    
    For Cnt = 1 To 11
        If Score > Val(ElitePlayer(Cnt).Score) Then
            GottenIndex = Cnt
            Exit For
        ElseIf Score = Val(ElitePlayer(Cnt).Score) Then
            If eSecond <= Val(ElitePlayer(Cnt).Second) Then
                GottenIndex = Cnt
                Exit For
            End If
        End If
    Next Cnt
    
    If RecordCount > 0 Then
        If GottenIndex = 0 Then Exit Sub
    End If
    
    If GottenIndex = 0 Then GottenIndex = 1
    
    For Cnt = 1 To GottenIndex - 1
        PutRecord Cnt, ElitePlayer(Cnt)
    Next Cnt
    PutRecord GottenIndex, TmpPlayerRate
    If GottenIndex = 11 Then Exit Sub
    For Cnt = GottenIndex + 1 To 11
        PutRecord Cnt, ElitePlayer(Cnt - 1)
    Next Cnt
    
    LoadRates
End Sub

Public Sub LoadRates(Optional RecordCount As Long)
    Dim Cnt As Long
    If Dir(F_SAVERATE) = "" Then
        RecordCount = 0
        Exit Sub
    End If
    If FileLen(F_SAVERATE) / 100 = 0 Then
        RecordCount = 0
        Exit Sub
    End If
    For Cnt = 1 To 11
        GetRecord Cnt, ElitePlayer(Cnt)
        Debug.Print ElitePlayer(Cnt).PlayerName
    Next Cnt
End Sub
