Skip to content

Commit 557844e

Browse files
author
Cem Yılmaz
committed
Fix player count issue
1 parent dcae393 commit 557844e

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

SteamQueryNet/SteamQueryNet.Tests/ServerQueryTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ namespace SteamQueryNet.Tests
66
{
77
public class ServerQueryTests
88
{
9-
private const string IP_ADDRESS = "127.0.0.1";
10-
private const string HOST_NAME = "localhost";
9+
private const string IP_ADDRESS = "209.222.101.220";
1110
private const int PORT = 27015;
1211

1312
[Theory]
1413
[InlineData(IP_ADDRESS)]
15-
[InlineData(HOST_NAME)]
1614
public void ShouldInitializeWithProperHost(string host)
1715
{
1816
var squery = new ServerQuery(host, PORT);
17+
var t = squery.GetServerInfo();
1918
}
2019

2120
[Theory]

SteamQueryNet/SteamQueryNet/Models/ServerInfo.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,20 @@ public sealed class ServerInfo
3939
/// <summary>
4040
/// Number of players on the server.
4141
/// </summary>
42-
public byte Players { get; set; }
42+
43+
private byte _players;
44+
public byte Players
45+
{
46+
get
47+
{
48+
// Some servers send bots as players. We don't want that here.
49+
return (byte)(this._players - this.Bots);
50+
}
51+
set
52+
{
53+
this._players = value;
54+
}
55+
}
4356

4457
/// <summary>
4558
/// Maximum number of players the server reports it can hold.

SteamQueryNet/SteamQueryNet/ServerQuery.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,13 @@ private List<TObject> ExtractListData<TObject>(byte[] rawSource)
284284
var objectList = new List<TObject>();
285285

286286
// Skip the response headers.
287-
IEnumerable<byte> dataSource = rawSource.Skip(RESPONSE_HEADER_COUNT);
287+
byte player_count = rawSource[RESPONSE_CODE_INDEX];
288+
289+
// Skip +1 for player_count
290+
IEnumerable<byte> dataSource = rawSource.Skip(RESPONSE_HEADER_COUNT + 1);
288291

289292
// Iterate amount of times that the server said.
290-
for (byte i = 0; i < rawSource[RESPONSE_CODE_INDEX]; i++)
293+
for (byte i = 0; i < player_count; i++)
291294
{
292295
// Activate a new instance of the object.
293296
var objectInstance = Activator.CreateInstance<TObject>();
@@ -387,7 +390,7 @@ private IEnumerable<byte> ExtractData<TObject>(TObject objectRef, byte[] dataSou
387390
: property.PropertyType;
388391

389392
// Extract the value and the size from the source.
390-
(object result, int size) = ExtractMarshalType(enumerableSource.SkipWhile(x => x == 0), typeOfProperty);
393+
(object result, int size) = ExtractMarshalType(enumerableSource, typeOfProperty);
391394

392395
/* If the property is an enum we should parse it first then assign its value,
393396
* if not we can just give it to SetValue since it was converted by ExtractMarshalType already.*/

0 commit comments

Comments
 (0)