Mirage Engine
Improve Server Saving - Printable Version

+- Mirage Engine (https://mirage-engine.uk/forums)
+-- Forum: Mirage Source (Nostalgia) (https://mirage-engine.uk/forums/forumdisplay.php?fid=61)
+--- Forum: Archive (2006-2011) (https://mirage-engine.uk/forums/forumdisplay.php?fid=18)
+---- Forum: Resources (https://mirage-engine.uk/forums/forumdisplay.php?fid=49)
+---- Thread: Improve Server Saving (/showthread.php?tid=376)



Improve Server Saving - William - 20-10-2006

Difficulty
1/5

Introduction
Are you tired of the lag, when the server saves all players online. Well that should be over now. This tutorial will make it so 'only the player that you play with' will be saved. And not the other two characters on your account (since they are unchanged). And it also splits the saving part into two stages. So after 5 minutes, it saves 1/2 of the player, and 5 minutes later, it saves the other part of the player (2/2). Then its complete, and it starts over again. Also, some other minor improvements has been done. Such as a variable has become byte instead of long. And DoEvents takes time to run over and over again. So that will only run when there is a player to save (Etc..).
Remember, that if you save your files as binary. There are somethings that needs to be changed in the both SavePlayer subs.
This line of code, can also be added to the default Sub SavePlayer(byval Index as long):
Code:
If Player(Index).CharNum = i Then
It will make it so that only 1 player is saved, and not all the 3 players on the account.

Tutorial Start: All Server Side
Find: Private Sub tmrPlayerSave_Timer()
Replace that whole sub with:
Code:
Private Sub tmrPlayerSave_Timer()
Static MinPassed As Long
Dim i As Byte 'make it long if MAX_PLAYERS > 255

    MinPassed = MinPassed + 1
    
    If MinPassed >= 5 And MinPassed < 10 Then
        If TotalOnlinePlayers > 0 Then
            Call GlobalMsg("Saving 1/2 of the player..", Yellow)
            For i = 1 To MAX_PLAYERS 'use High_Index if you added that tut
                If IsPlaying(i) Then
                    Call tmrSavePlayerMain(i)
                    DoEvents
                End If
            Next i
        End If
        MinPassed = 10
    End If

    If MinPassed >= 15 Then
        If TotalOnlinePlayers > 0 Then
            Call GlobalMsg("Saving 2/2 of the player..", Yellow)
            For i = 1 To MAX_PLAYERS 'use High_Index if you added that tut
                If IsPlaying(i) Then
                    Call tmrSavePlayerOther(i)
                    DoEvents
                End If
            Next i
        End If
        MinPassed = 0
    End If
End Sub

Now, below: Sub SavePlayer(ByVal Index As Long)
Add the following:
Code:
Sub tmrSavePlayerMain(ByVal Index As Long)
Dim FileName As String
Dim i As Byte

    FileName = App.Path & "\Accounts\" & Trim(Player(Index).Login) & ".ini"
    For i = 1 To MAX_CHARS
        If Player(Index).CharNum = i Then
            ' General
            Call PutVar(FileName, "CHAR" & i, "Name", Trim(Player(Index).Char(i).Name))
            Call PutVar(FileName, "CHAR" & i, "Class", STR(Player(Index).Char(i).Class))
            Call PutVar(FileName, "CHAR" & i, "Sex", STR(Player(Index).Char(i).Sex))
            Call PutVar(FileName, "CHAR" & i, "Sprite", STR(Player(Index).Char(i).Sprite))
            Call PutVar(FileName, "CHAR" & i, "Level", STR(Player(Index).Char(i).Level))
            Call PutVar(FileName, "CHAR" & i, "Exp", STR(Player(Index).Char(i).Exp))
            Call PutVar(FileName, "CHAR" & i, "Access", STR(Player(Index).Char(i).Access))
            Call PutVar(FileName, "CHAR" & i, "PK", STR(Player(Index).Char(i).PK))
            Call PutVar(FileName, "CHAR" & i, "Guild", STR(Player(Index).Char(i).Guild))
        
            ' Vitals
            Call PutVar(FileName, "CHAR" & i, "HP", STR(Player(Index).Char(i).HP))
            Call PutVar(FileName, "CHAR" & i, "MP", STR(Player(Index).Char(i).MP))
            Call PutVar(FileName, "CHAR" & i, "SP", STR(Player(Index).Char(i).SP))
        
            ' Stats
            Call PutVar(FileName, "CHAR" & i, "STR", STR(Player(Index).Char(i).STR))
            Call PutVar(FileName, "CHAR" & i, "DEF", STR(Player(Index).Char(i).DEF))
            Call PutVar(FileName, "CHAR" & i, "SPEED", STR(Player(Index).Char(i).SPEED))
            Call PutVar(FileName, "CHAR" & i, "MAGI", STR(Player(Index).Char(i).MAGI))
            Call PutVar(FileName, "CHAR" & i, "POINTS", STR(Player(Index).Char(i).POINTS))
        
            ' Worn equipment
            Call PutVar(FileName, "CHAR" & i, "ArmorSlot", STR(Player(Index).Char(i).ArmorSlot))
            Call PutVar(FileName, "CHAR" & i, "WeaponSlot", STR(Player(Index).Char(i).WeaponSlot))
            Call PutVar(FileName, "CHAR" & i, "HelmetSlot", STR(Player(Index).Char(i).HelmetSlot))
            Call PutVar(FileName, "CHAR" & i, "ShieldSlot", STR(Player(Index).Char(i).ShieldSlot))
        End If
    Next i
End Sub

Sub tmrSavePlayerOther(ByVal Index As Long)
Dim FileName As String
Dim i As Byte
Dim n As Long

    FileName = App.Path & "\Accounts\" & Trim(Player(Index).Login) & ".ini"
    For i = 1 To MAX_CHARS
      If Player(Index).CharNum = i Then
        ' Check to make sure that they aren't on map 0, if so reset'm
        If Player(Index).Char(i).Map = 0 Then
            Player(Index).Char(i).Map = START_MAP
            Player(Index).Char(i).x = START_X
            Player(Index).Char(i).y = START_Y
        End If
            
        ' Position
        Call PutVar(FileName, "CHAR" & i, "Map", STR(Player(Index).Char(i).Map))
        Call PutVar(FileName, "CHAR" & i, "X", STR(Player(Index).Char(i).x))
        Call PutVar(FileName, "CHAR" & i, "Y", STR(Player(Index).Char(i).y))
        Call PutVar(FileName, "CHAR" & i, "Dir", STR(Player(Index).Char(i).Dir))
        
        ' Inventory
        For n = 1 To MAX_INV
            Call PutVar(FileName, "CHAR" & i, "InvItemNum" & n, STR(Player(Index).Char(i).Inv(n).Num))
            Call PutVar(FileName, "CHAR" & i, "InvItemVal" & n, STR(Player(Index).Char(i).Inv(n).Value))
            Call PutVar(FileName, "CHAR" & i, "InvItemDur" & n, STR(Player(Index).Char(i).Inv(n).Dur))
        Next n
        
        ' Spells
        For n = 1 To MAX_PLAYER_SPELLS
            Call PutVar(FileName, "CHAR" & i, "Spell" & n, STR(Player(Index).Char(i).Spell(n)))
        Next n
     End if
    Next i
End Sub

Thats it! Your saving process has now been reduced and improved. To try it out, just change the interval on the timer (tmrPlayerSave) to 1000. And you will see how it works, when you open the client.

Also, if you want the server to save the players less. Just change the 5, 10 and 15 values to something like: 10, 20 and 30. That would double the time for it.

Tell me, if you know something else that can be improved with it. Or if you just don't like my idea behind it.


- Bradyok - 14-02-2007

I know this is a somewhat old topic, but instead of

Code:
For i = 1 To MAX_CHARS
        If Player(Index).CharNum = i Then

You could just do

Code:
i = Player(Index).CharNum



- William - 14-02-2007

I think I remember having a problem with that. But it should work.


- JokeofWeek - 06-04-2007

Sweet tutorial Big Grin

I modified it so that it uses the existing SavePlayer sub using an optional variable.

Basically, change your tmrPlayerSave code to

Code:
Private Sub tmrPlayerSave_Timer()
Static MinPassed As Long
Dim i As Byte 'make it long if MAX_PLAYERS > 255

    MinPassed = MinPassed + 1
  
    If MinPassed >= 5 And MinPassed < 10 Then
        If TotalOnlinePlayers > 0 Then
            Call GlobalMsg("Saving 1/2 of the player..", Yellow)
            For i = 1 To max_players 'use High_Index if you added that tut
                If IsPlaying(i) Then
                    Call SavePlayer(i, 1)
                    DoEvents
                End If
            Next i
        End If
        MinPassed = 10
    End If

    If MinPassed >= 15 Then
        If TotalOnlinePlayers > 0 Then
            Call GlobalMsg("Saving 2/2 of the player..", Yellow)
            For i = 1 To max_players 'use High_Index if you added that tut
                If IsPlaying(i) Then
                    Call SavePlayer(i, 2)
                    DoEvents
                End If
            Next i
        End If
        MinPassed = 0
    End If
End Sub

and then modify your save player code to look like this :

Code:
Sub SavePlayer(ByVal Index As Long, Optional IsHalf As Byte)
Dim FileName As String
Dim i As Long
Dim n As Long

    FileName = App.Path & "\Accounts\" & Trim$(Player(Index).Login) & ".ini"
    
    Call PutVar(FileName, "GENERAL", "Login", Trim$(Player(Index).Login))
    Call PutVar(FileName, "GENERAL", "Password", Trim$(Player(Index).Password))

    For i = 1 To MAX_CHARS
        
            If IsHalf = 0 Or IsHalf = 1 Then
                ' General
                Call PutVar(FileName, "CHAR" & i, "Name", Trim$(Player(Index).Char(i).Name))
                Call PutVar(FileName, "CHAR" & i, "Class", STR(Player(Index).Char(i).Class))
                Call PutVar(FileName, "CHAR" & i, "Sex", STR(Player(Index).Char(i).Sex))
                Call PutVar(FileName, "CHAR" & i, "Sprite", STR(Player(Index).Char(i).Sprite))
                Call PutVar(FileName, "CHAR" & i, "Level", STR(Player(Index).Char(i).Level))
                Call PutVar(FileName, "CHAR" & i, "Exp", STR(Player(Index).Char(i).Exp))
                Call PutVar(FileName, "CHAR" & i, "Access", STR(Player(Index).Char(i).Access))
                Call PutVar(FileName, "CHAR" & i, "PK", STR(Player(Index).Char(i).PK))
                Call PutVar(FileName, "CHAR" & i, "Guild", STR(Player(Index).Char(i).Guild))
                
                ' Vitals
                Call PutVar(FileName, "CHAR" & i, "HP", STR(Player(Index).Char(i).HP))
                Call PutVar(FileName, "CHAR" & i, "MP", STR(Player(Index).Char(i).MP))
                Call PutVar(FileName, "CHAR" & i, "SP", STR(Player(Index).Char(i).SP))
                
                ' Stats
                Call PutVar(FileName, "CHAR" & i, "STR", STR(Player(Index).Char(i).STR))
                Call PutVar(FileName, "CHAR" & i, "DEF", STR(Player(Index).Char(i).DEF))
                Call PutVar(FileName, "CHAR" & i, "SPEED", STR(Player(Index).Char(i).SPEED))
                Call PutVar(FileName, "CHAR" & i, "MAGI", STR(Player(Index).Char(i).MAGI))
                Call PutVar(FileName, "CHAR" & i, "POINTS", STR(Player(Index).Char(i).POINTS))
                
                ' Worn equipment
                Call PutVar(FileName, "CHAR" & i, "ArmorSlot", STR(Player(Index).Char(i).ArmorSlot))
                Call PutVar(FileName, "CHAR" & i, "WeaponSlot", STR(Player(Index).Char(i).WeaponSlot))
                Call PutVar(FileName, "CHAR" & i, "HelmetSlot", STR(Player(Index).Char(i).HelmetSlot))
                Call PutVar(FileName, "CHAR" & i, "ShieldSlot", STR(Player(Index).Char(i).ShieldSlot))
            End If
        
            ' Check to make sure that they aren't on map 0, if so reset'm
            If IsHalf = 0 Then
                If Player(Index).Char(i).Map = 0 Then
                    Player(Index).Char(i).Map = START_MAP
                    Player(Index).Char(i).X = START_X
                    Player(Index).Char(i).y = START_Y
                End If
            End If
            
        ' Position
            If IsHalf = 0 Or IsHalf = 2 Then
                Call PutVar(FileName, "CHAR" & i, "Map", STR(Player(Index).Char(i).Map))
                Call PutVar(FileName, "CHAR" & i, "X", STR(Player(Index).Char(i).X))
                Call PutVar(FileName, "CHAR" & i, "Y", STR(Player(Index).Char(i).y))
                Call PutVar(FileName, "CHAR" & i, "Dir", STR(Player(Index).Char(i).Dir))
                
                ' Inventory
                For n = 1 To MAX_INV
                    Call PutVar(FileName, "CHAR" & i, "InvItemNum" & n, STR(Player(Index).Char(i).Inv(n).Num))
                    Call PutVar(FileName, "CHAR" & i, "InvItemVal" & n, STR(Player(Index).Char(i).Inv(n).Value))
                    Call PutVar(FileName, "CHAR" & i, "InvItemDur" & n, STR(Player(Index).Char(i).Inv(n).Dur))
                Next n
                
                ' Spells
                For n = 1 To MAX_PLAYER_SPELLS
                    Call PutVar(FileName, "CHAR" & i, "Spell" & n, STR(Player(Index).Char(i).Spell(n)))
                Next n
            End If

    Next i
End Sub

Basically this does it so that there is an optional byte added. If you do not put any value in it, it will save everything for the player. If you put a 1, it will save the first half. If you put a 2, it will save the second half. Don't worry, this will work with your existing Call SavePlayer calls, so you don't have to change them.