Forum

> > CS2D > General > Get player count JSON
Forums overviewCS2D overviewGeneral overviewLog in to reply

English Get player count JSON

61 replies
Page
To the start Previous 1 2 3 4 Next To the start

old Re: Get player count JSON

Mami Tomoe
User Off Offline

Quote
Okay, but how do I start?
Do I compile PHP? Do I need a website host or just a VPS to run it?
How to run whatever he just sent and then how to use it?

old Re: Get player count JSON

BuhaloStrikalo
User Off Offline

Quote
Im using XMLHttpRequest for my discord server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var xhr = new XMLHttpRequest();
//	80.211.225.24:36963

    //(`http://www.unrealsoftware.de/inc_pub/serverinfo.php?i=80.211.225.24&p=36963&g=0&123`)

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
    }

let gotPlayers = (() => {
    let rand = getRandomInt(1,100)
    xhr.open("GET", "http://www.unrealsoftware.de/inc_pub/serverinfo.php?i=80.211.225.24&p=36963&g=0&" + rand, true);
    xhr.onload = function (){
        let text = xhr.responseText
        var fPlayer = text.lastIndexOf('Player');
        text = text.substr(fPlayer,23)
        text = text.substr(17)
        let newname = 'Online: ' + text
        return text 
    }
    xhr.send(null)
});

old Re: Get player count JSON

Mami Tomoe
User Off Offline

Quote
Quote >


I hope bumping this after 245 days isn't a big deal, I didn't know whether to do this or to create a new thread, or to just ask @user DC: personally through PM.

I have learnt how to use PHP and other website things (very lightly, but enough to make that script work as intended).
My question is, is there an API document or anything to guide me through all the different types of information that I can request from the server? Or are those all of them?

old Re: Get player count JSON

DC
Admin Off Offline

Quote
@user Mami Tomoe: Using the forum is always better than PM if you're talking about something which might be interesting to anyone else. Also using existing threads is always right when your topic matches. Good job. You chose the right path.

There is indeed more than the described request.
When you rightclick a server in the server list in CS2D and click "view details" you get a player list with ids/names/team/scores/deaths. You can request that stuff too.


If you only need the list of player names (ID 251, sub ID 4):
sendRequest(chr(1).chr(0).chr(251).chr(4));

Response:
• 4 bytes header (like other messages)
• byte: player count, and then player count times (loop):
     • string player name

If you need the detailed player list (ID 251, sub ID 5):
sendRequest(chr(1).chr(0).chr(251).chr(5));

Response:
• 4 bytes header (like other messages)
• byte: player count, and then player count times (loop):
     • byte player ID
     • string player name
     • byte team
     • int score
     • int deaths

p.s.: sub ID 2 is for info requests in LAN (ignore this, not relevant to you, basically equal to sub ID 1). Sub ID 3 just responds with a matching header but without any extra payload. I don't even remember why it even exists. Maybe for NAT hole punching.

old Re: Get player count JSON

Mami Tomoe
User Off Offline

Quote
Thank you @user DC, I do not know what any of that means, and I've lost all hope in liking PHP/HTML, but I know that with enough time spent searching through old Stack Overflow threads, I should be able to implement that into my project.

Otherwise, I will come back here and ask for further assistance while hoping that somebody knows the solution (and is willing to share it).


EDIT FROM THE FUTURE:
I've learnt how to use those things in C#, so I no longer rely on PHP for it, and I have a question.

What other forms of "sub" are there for CS2D?
1 = Server data (player count, name, map, game mode, etc)
2 = LAN (unused)
3 = Verify response (server is online)
4 = Player count and names
5 = Player count and names (detailed)

Am I correct? Are there more?
edited 3×, last 06.05.21 11:44:43 pm

old Re: Get player count JSON

Mami Tomoe
User Off Offline

Quote
user DC has written
• byte: player count, and then player count times (loop):
     • byte player ID
     • string player name
     • byte team
     • int score
     • int deaths


What does player count times mean?

old Re: Get player count JSON

ohaz
User Off Offline

Quote
There is a byte detaling how many of the "following" blocks will be sent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
byte: 3
player1:
	* playerID
	* player name
	* team
	* score
	* deaths
player2:
	* playerID
	* player name
	* team
	* score
	* deaths
player3:
	* playerID
	* player name
	* team
	* score
	* deaths

So the byte at the beginning shows the amount of "player" sets you are going to receive

old Re: Get player count JSON

Mami Tomoe
User Off Offline

Quote
@user ohaz: I tried that, and it doesn't seem to work, only the first player shows up "kinda properly" but not really.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public List<Types.PlayerData> PlayerData(string ip, ushort port)
		{
			List<Types.PlayerData> playerData = new List<Types.PlayerData>();

			// This constructor arbitrarily assigns the local port number.
			UdpClient udpClient = new UdpClient(30001);

			try
			{
				udpClient.Connect(ip, port);

				// Sends a message to the host to which you have connected.
				byte[] sendBytes = new byte[] { 1, 0, 251, 5 };

				udpClient.Send(sendBytes, sendBytes.Length);

				// Receive bytes.
				IAsyncResult asyncResult = udpClient.BeginReceive(null, null);

				asyncResult.AsyncWaitHandle.WaitOne(3000);

				if (asyncResult.IsCompleted)
				{
					// IPEndPoint object will allow us to read datagrams sent from any source.
					IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

					Index = 0;
					ByteArray = udpClient.EndReceive(asyncResult, ref RemoteIpEndPoint);

					byte[] bytes = { ReadByte(), ReadByte(), ReadByte(), ReadByte() };

					if (bytes[0] == 1 && bytes[1] == 0 && bytes[2] == 251 && bytes[3] == 5)
					{
						byte PlayerCount = ReadByte();

						for (byte i = 0; i < PlayerCount; i++)
						{
							byte ID = ReadByte();
							string Name = ReadString();
							byte Team = ReadByte();
							int Score = ReadByte();
							int Deaths = ReadByte();

							playerData.Add(new Types.PlayerData
							{
								ID = ID,
								Name = Name,
								Team = Team,
								Score = Score,
								Deaths = Deaths
							});
						}
					}
					else
					{
						// Unexpected server reply.

						udpClient.Close();

						return null;
					}

					udpClient.Close();
				}
				else
				{
					// Timeout.

					udpClient.Close();

					return null;
				}
			}
			catch
			{
				udpClient.Close();

				return null;
			}

			return playerData;
		}

old Re: Get player count JSON

DC
Admin Off Offline

Quote
You have to be really careful about the data types you're using for reading the data.

One mistake I spotted is that you're using ReadByte for score and deaths. You have to use ReadInt instead. One of these mistakes will screw up all data which comes afterwards.

old Re: Get player count JSON

Mami Tomoe
User Off Offline

Quote
@user DC: How does "readInt" work? I used readByte and readString from the PHP (I converted them), but how does readInt work? Is it similar to the way string works (data length and then data)?

Edit: Nevermind, the PHP contained those too.
I have another question, is it possible to artificially use Remote Control (RCon) through sending packets back and forth?
If so, how does it work?
edited 3×, last 09.05.21 10:38:12 am

old Re: Get player count JSON

DC
Admin Off Offline

Quote
Yep, documented in CS2D in-game help. Works exactly the same way as requesting and receiving server infos. You just need to send/receive other data.

Note that external remote control (without joining the server) only works if you set a password (cs2d cmd sv_rcon) but do NOT limit access to certain users (cs2d cmd sv_rconusers)!

old Re: Get player count JSON

Mami Tomoe
User Off Offline

Quote
@user DC and @user MikuAuahDark, thank you!
It seems to work now!

I have one other question remaining, is there an alternative to cs2d cmd cmsg that works via IP?

I want a way to print a console message containing the error if any, into the miniature remote control menu, just in case.
It's not super important, but it's a nice touch.

old Re: Get player count JSON

DC
Admin Off Offline

Quote
You mean like cs2d cmd cmsg but with an IP as destination of the message instead of a player ID? Nope. Doesn't exist.

There is cs2d cmd logaddress_add though which sends ALL console messages to an external IP. So you could do that, send the message and afterwards use cs2d cmd logaddress_remove.

If you just want to call cs2d cmd cmsg externally you can call it via rcon of course.

old Re: Get player count JSON

DC
Admin Off Offline

Quote
Yes it does use UDP and it is unreliable just like all the other custom network messages.

old Re: Get player count JSON

Mami Tomoe
User Off Offline

Quote
@user DC
What format do strings get sent over through UDP?
Is it UTF-7? Or is it something else?

Actually, in general, what kind of encoding does CS2D use?

old Re: Get player count JSON

DC
Admin Off Offline

Quote
I guess it's still ASCII in most cases. At least for all that rcon related stuff. Always 1 byte per char.
To the start Previous 1 2 3 4 Next To the start
Log in to replyGeneral overviewCS2D overviewForums overview