How to retrive the number of players of another server.


First, sorry for my bad english, I’m french canadian. Feel free to fix my typos and all that stuff I will appreciate it :slight_smile:

So I own a community in which I host multiple servers (Cinema, Trouble In Terrorist Town, Deathrun, etc.) and what i’m trying to do is some 3d panels which show the number of players on one of my server. My question is, how can I retrieve the informations of another server using lua? I was thinking about MySQL but idk if it could work like I want. Is there some kind of functions or other magic things to make it happen?

Sorry if i’m not clear enough, feel free to ask me if you’re not sure and i’ll do my best to explain.

Thank you :slight_smile:

You could use the web to communicate such as MySQL updating the player-counts every 5 minutes or so. You could use http to communicate with a php script which uses MySQL or flat-file, or whatever…

You could also use sockets ( there is a “gSocket” / Sockets library floating around somewhere ) which would allow instant communication, only when it matters ( such as game-event player_connect / player_disconnect or PlayerConnected / PlayerDisconnected hooks ) instead of constantly pinging.

So if I use that socket library. Do I need to communicate with a PHP script?

Why not every time the player count changes? That’d be such a simple query.

How slow do you think MySQL is :v:

I found this :

That’s what you were talking about Acecool, right?

Yes, you can do that; what I was referring to with “pinging” was the other server will not know when the other server updates the MySQL without checking. Should they check every time the local server changes ( which may not happen as frequently as another server ), or every few minutes?

[editline]21st September 2014[/editline]

With Sockets, you’ll be updated instantly when another server changes the player-count.

Oh, okay, that makes more sense.

Honestly I’d use neither of these methods. I’d just have a page that does a server info query and reports the results via JSON. No need to involve another module or a database.

If Steam API supports it, then it could be viable, but it’d still need to be http Fetched every few minutes.

No Steam API Key required list:


Keyed list:


These match:


You may be right; with the last one. It’d still need to be http Fetched every few minutes to update ( either the server doing it, and networking to each player, or each client doing it… )

Edit: Tested GetServerAtAddress

It returns table.response.servers[ 1 ] with appid, specport, region, addr, lan, gamedir, gmsindex, secure and table.response.success = true; No player information.

What if I make my game panel a PHP script with a nice style on it…And everytime someone join or disconnect from the server. The panel update. Maybe that could work. I’ve looked at the lua socket module and it kinda look too hard for me right now

Using a web interface to let other clients know ( ie just using a DHTML or HTML panel for it and refreshing it when they re-open the panel ) is also a decent way to do it. You’d need a form of flatfile or MySQL storage to log the data each time a new request comes in. fileio is considered “expensive”, but since you’re saving minimal data, you should be fine.

I’d also recommend that you only let the server make the updates ( ensure only the SERVER IPs can call the _GET or _POST vars which will update the MySQL / flatfile data-system with new information ).

For PHP for example, I was just thinking something like this PHP Source Query Class

SourceQuery sounds like the thing I need ! I will give it a shot! Thank you Banana Lord!

[editline]21st September 2014[/editline]

So what if…When a player join a server, I send something to my php script to tell him “Hey someone joined! Update the number of players!” I assume I would need to use sockets right? Or is there something else?

With the Source Query Class, you’d be best looking at:

So this would be from your web-host… Your game-server wouldn’t need to send any information out via Lua… The SourceQuery class would take care of it by sending a request to your server ( kind of like how the server-browser works, but it uses an rcon connection via sockets lib provided by your webhost, from what I gathered, instead ), you’d need to create a “new” instance of SourceQuery, connect to your server, then use SourceQuery->GetInfo( ); which should return a table including “Players”, then Disconnect and display the information.

With this approach, you could be having a LOT of clients opening the page and querying the server…

A better way would be to CACHE the results of the SourceQuery connect… So you’d look at the timestamp of your cache-file. If it is older than 60 seconds, or whatever you decide, create the new connection, grab the info, log it to the file, and display it.

IF the cache is less than 60 seconds, or whatever you decide, then display the contents of the file.

This will prevent users from being able to spam unnecessary connections.

The provided example does a pretty good job I think. You’re right though, if the clients will be hitting this page then you should definitely cache the results.

[editline]21st September 2014[/editline]

Or, better yet, have them hit a page that retrieves the cache, and just have a cronjob set up to update the cache once a minute.

Sounds interesting. Yeah I might make my game panels web-based. I’m thinking about it since yesterday trying to make a plan in my head so maybe I can start making them this week-end.

There is one thing i’m not quite sure about is how I will show the informations. I understood the part to save it into a file which is quite good, but how exactly can I make it refresh, let’s say every 60 seconds, server-side. I can do some HTML and PHP but i’m not an expert (yet, trying to get better though).