Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[EO2.0] Loop through all players.


Boynaar44
 Share

Recommended Posts

So I made this because of an unofficial request from Erwin.

It will show you an example of how to loop through all players.

In this example we will loop through all players and get the average level.

ALL PLAYERS means, online and offline players.

It is server side only.

This tutorial requires you to add the reference "Microsoft Scripting Runtime" to your server project.

Project -> References… -> check the "Microsoft Scripting Runtime"

First we need to create a temporarily player type to load the player data we need of each player we loop through.

We do that by adding the next code inside the modTypes in your server project.

So search for: ' Public data structures

And add in the list under it

```
Public TempPlayerUse As PlayerRec
```

After we have done that, we need to create the subs which will actually load the players we loop through into the "TempPlayerUse".

We do that by adding the next 2 subs into your project, a good place would be at the bottom of your "modDatabase"

```

Sub LoadTempPlayerUse(ByVal Name As String)

Dim filename As String

Dim F As Long

Call ClearTempPlayerUse

filename = App.path & "\data\accounts\" & Trim(Name)

F = FreeFile

Open filename For Binary As #F

Get #F, , TempPlayerUse

Close #F

End Sub

Sub ClearTempPlayerUse()

Dim i As Long

Call ZeroMemory(ByVal VarPtr(TempPlayerUse), LenB(TempPlayerUse))

TempPlayerUse.Login = vbNullString

TempPlayerUse.Password = vbNullString

TempPlayerUse.Name = vbNullString

TempPlayerUse.Class = 1

End Sub

```

So you see in the code we just added, before it loads a player, it clears the already loaded player. Thats why we need 2 subs, one to load and another to clear.

Now we have that, we should make use of it and start looping through the players by reading all the player names inside the charlist.txt.

Here we will make use of the "Microsoft Scripting Runtime".

Create a new function called "GetAverageLevel", at the bottom of your "modPlayer" would be a good place for it.

```

Public Function GetAverageLevel()

' Here we will create the Microsoft Scripting Runtime object.

' We also need to define the charlist.txt path.

Dim averageLevels as Long

' Open the charlist.txt and count how many lines it has.

' Save the player names into an array between 0 and the count of lines in charlist.txt

' Close the charlist.txt

' Loop through the player names array we just created, we load the players and get their levels.

GetAverageLevel = averageLevels

End Function

```

To count how many lines the charlist.txt has we need this function.

Just add it under the unfinished GetAverageLevel function we just created.

```

Private Function getLineCount(ByVal strFilePath As String) As Long

Open strFilePath For Binary As #1

Dim strBuff As String: strBuff = Space(LOF(1))

Get #1, , strBuff

getLineCount = UBound(Split(strBuff, vbCrLf))

Close #1

End Function

```

Now we have that, we will continue working on the GetAverageLevel function.

Inside the GetAverageLevel function do the following.

Add under "Here we will create the Microsoft Scripting Runtime object."

```

Dim FSO As New FileSystemObject ' Implement the Microsoft Scripting Runtime to use it

Dim charlistFile As TextStream ' Here we will open the charlist.txt and since it is a TextStream we can read the lines.

```

Then add under "We also need to define the charlist.txt path."

```

Dim charlistPath As String ' To store the path of the charlist.txt

```

And then we still need some values to use.

Add under "Dim averageLevels as Long"

```

Dim accountNames() As String ' Account names array

Dim lineCount As Long ' The count of lines in charlist.txt

Dim i As Long ' We need this to make a loop.

```

After that we have created the FileSystemObject and dimmed the values we need.

We can now open the charlist.txt so we can load it into the "accountNames()" array later.

Add under "Open the charlist.txt and count how many lines it has."

```

charlistPath = App.path & "\data\Accounts\charlist.txt" ' Path

lineCount = getLineCount(charlistPath) ' Count the lines

Set charlistFile = FSO.OpenTextFile(charlistPath) ' Open the file

ReDim accountNames(0 To lineCount) ' Set the array width we are gonna use.

```

Now we have the total lines, we can save the names into the "accountNames()" array.

Add under "Save the player names into an array between 0 and the count of lines in charlist.txt"

```

Do While Not charlistFile.AtEndOfStream ' As long as we haven't reached the end, do the following.

accountNames(i) = charlistFile.ReadLine ' Add the current line into the current "accountNames" slot.

i = i + 1 ' Add 1 to the current slot we are working with.

Loop

```

Now we are done with the charlist.txt, we can close it.

Add under "Close the charlist.txt" the following code to close the file.

```

charlistFile.Close ' Close the opened file.

```

The accountNames are loaded, now we can read the levels by loading any of them into the TempPlayerUse which we created earlier.

We will load them one by one and count the levels and divide at the same time to get the average level.

Add under "Loop through the player names array we just created, we load the players and get their levels." the following code.

```

For i = 0 To UBound(accountNames) ' Loop from the first to the last array slot.

Call LoadTempPlayerUse(accountNames(i) & ".bin") ' Load the player into the current array slot.

If i = 0 Then ' We don't divide the first one

averageLevels = TempPlayerUse.Level

Else

averageLevels = (TempPlayerUse.Level + averageLevels) / 2 ' Calculate the current average.

End If

Next

```

That's all.

Why make the tutorial this way?

Because in the past there was a time for me I wanted this kind of tutorials.
Link to comment
Share on other sites

I agree with you, I use this new reference because (to be honest) I couldn't let it read the names from charlist.txt(line by line) and store it at the same time into a new array slot.

I thought Lightning had an external player editor, so I thought I will put my nose inside it and see how he did it. But suprising enough it's unfinished there…

So then I looked for a new way and that was this "Microsoft Scripting Runtime"
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...