English Get player count JSON

61 replies
Goto Page
To the start Previous 1 2 3 4 Next To the start
09.05.20 07:36:14 pm
Up
Mami Tomoe
User
Offline Off
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?
fish
02.06.20 12:12:29 pm
Up
BuhaloStrikalo
User
Offline Off
Im using XMLHttpRequest for my discord server.

Code:
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)
});
02.02.21 12:46:30 pm
Up
Mami Tomoe
User
Offline Off
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?
fish
02.02.21 06:29:58 pm
Up
DC
Admin
Offline Off
@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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
02.02.21 10:00:21 pm
Up
Mami Tomoe
User
Offline Off
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
fish
07.05.21 12:13:00 am
Up
DC
Admin
Offline Off
You are correct. There are no other sub IDs.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
07.05.21 02:03:06 am
Up
Mami Tomoe
User
Offline Off
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?
fish
07.05.21 08:07:57 am
Up
ohaz
User
Offline Off
There is a byte detaling how many of the "following" blocks will be sent:

Code:
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
https://ohaz.engineer - Software Engineering
07.05.21 10:42:41 am
Up
Mami Tomoe
User
Offline Off
@user ohaz: I tried that, and it doesn't seem to work, only the first player shows up "kinda properly" but not really.

Code:
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;
          }
fish
07.05.21 07:00:31 pm
Up
DC
Admin
Offline Off
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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
08.05.21 07:40:10 pm
Up
Mami Tomoe
User
Offline Off
@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
fish
09.05.21 04:46:35 pm
Up
MikuAuahDark
User
Offline Off
thread cs2d Reading server response from RCon in PHP

All the RCon packets are documented in the CS2D Help I think.
file cs2d LuaJIT for Dedicated Server (13) JIT POWER! | Know your Lua errors! | Part of LÖVE development team since 11.3
09.05.21 06:48:28 pm
Up
DC
Admin
Offline Off
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)!
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
09.05.21 11:12:29 pm
Up
Mami Tomoe
User
Offline Off
@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.
fish
09.05.21 11:57:23 pm
Up
DC
Admin
Offline Off
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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
10.05.21 01:12:06 am
Up
Mami Tomoe
User
Offline Off
@user DC: Doesn't it send it through UDP? Seems unreliable, nevermind then, thank you!
fish
10.05.21 01:25:32 am
Up
DC
Admin
Offline Off
Yes it does use UDP and it is unreliable just like all the other custom network messages.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
17.05.21 12:29:07 am
Up
Mami Tomoe
User
Offline Off
@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?
fish
17.05.21 12:34:59 am
Up
DC
Admin
Offline Off
I guess it's still ASCII in most cases. At least for all that rcon related stuff. Always 1 byte per char.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
17.05.21 12:52:55 am
Up
Mami Tomoe
User
Offline Off
@user DC: I've had better luck with UTF-7, it seems to show more characters properly, while ASCII shows only a few.
fish
To the start Previous 1 2 3 4 Next To the start