• Steam Condenser Examples (For Garry's Mod/Source Servers)
    2 replies, posted
I recently had some issues finding out how to use [URL="https://github.com/koraktor/steam-condenser-php/archive/1.3.8.tar.gz"]Steam Condenser[/URL] and display server information on my website. I only started learning PHP this week, and though this might be useful for some people. GameServers.php - [URL="http://stupids-servers.com/GameServers.php"]Live Example[/URL] [CODE]<style> #divContainer {width: 400px;} #divCentered {width: 100%; text-align: center;} .theDivL {float: left;} .theDivR {float: right;} </style> <?php require_once("./lib/steam-condenser.php"); // Path to where Steam Condenser is on your webserver. error_reporting(E_ERROR); // This part I don't like, but I keep getting warnings and such during the Exceptions. echo "<div id=\"divContainer\"><div class=\"theDivL\"><b>Stupid's TTT</b></div><div class=\"theDivR\">192.228.110.111:27015</div>"; // Setting up the div for Server #1 try { // Try and see if the connection works for Server #1 $server = new SourceServer('192.228.110.111', 27015); //Input Server #1 information here $server->initialize(); $tttInfo = $server->getServerInfo(); // Put Server Info into variable $tttInfo $tttPlayers = $tttInfo['numberOfPlayers']; // For later use echo "</br><div class=\"theDivL\">" . $tttInfo['mapName'] . "</div><div class=\"theDivR\">" . $tttInfo['numberOfPlayers'] . "/" . $tttInfo['maxPlayers'] . " Players</div></div>"; } catch (SocketException $e) { // Catch Exception when can't connect to server. echo "</br><div class=\"theDivL\">Server offline.</div></div>"; $tttPlayers = 0; // If there is an error, max TTT Players becomes 0. } catch (TimeoutException $e) { // Catch Exception when server times out. echo "</br><div class=\"theDivL\">Server has timed out.</div></div>"; $tttPlayers = 0; // If there is an error, max TTT Players becomes 0. } catch (Exception $e) { // Catch other Exception. echo "</br><div class=\"theDivL\">Unknown server error.</div></div>"; $tttPlayers = 0; // If there is an error, max TTT Players becomes 0. } echo "</br></br></br><div id=\"divContainer\"><div class=\"theDivL\"><b>Stupid's DarkRP</b></div><div class=\"theDivR\">162.251.238.11:27020</div>"; // Setting up the div for Server #2 try { // Try and see if the connection works for Server #2 $server = new SourceServer('162.251.238.11', 27020); $server->initialize(); $darkRPInfo = $server->getServerInfo(); // Put Server into variable $darkRPInfo $darkRPPlayers = $darkRPInfo['numberOfPlayers']; // For later use echo "</br><div class=\"theDivL\">" . $darkRPInfo['mapName'] . "</div><div class=\"theDivR\">" . $darkRPInfo['numberOfPlayers'] . "/" . $darkRPInfo['maxPlayers'] . " Players</div></div>"; } catch (SocketException $e) { // Catch Exception when can't connect to server. echo "</br><div class=\"theDivL\">Server offline.</div></div>"; $darkRPPlayers = 0; // If there is an error, max DarKRP Players becomes 0. } catch (TimeoutException $e) { // Catch Exception when server times out. echo "</br><div class=\"theDivL\">Server has timed out.</div></div>"; $darkRPPlayers = 0; // If there is an error, max DarKRP Players becomes 0. } catch (Exception $e) { // Catch other Exception. echo "</br><div class=\"theDivL\">Unknown server error.</div></div>"; $darkRPPlayers = 0; // If there is an error, max DarKRP Players becomes 0. } $totalOnlinePlayers = $tttPlayers + $darkRPPlayers; // Adding current players of Servers 1 & 2 together. $totalServers = 2; // Can change the number of servers you have. echo "</br></br><div id=\"divContainer\"><div id=\"divCentered\">" . $totalOnlinePlayers . " players are online on " . $totalServers . " servers.</div></div>"; ?>[/CODE] OnlinePlayers.php - [URL="http://stupids-servers.com/OnlinePlayers.php"]Live Example[/URL] [CODE] <style> #divContainer {width: 600px;} .theDivL {width: 25%; float: left;} #divCentered {width: 100%; text-align: center; line-height: 30px;} </style> <h3>Online Players</h3> <?php error_reporting(E_ERROR); // This part I don't like, but I keep getting warnings and such during the Exceptions. function convertToHoursMins($time, $format = '%d:%d') { //Not my code, used to convert to Hours & Minutes settype($time, 'integer'); if ($time < 1) { return; } $hours = floor($time / 60); $minutes = ($time % 60); if ($hours > 0) { return sprintf($format, $hours, $minutes); } else { return sprintf($format, $minutes); } } require_once("./lib/steam-condenser.php"); //Change this to the path you put Steam Condenser in try { //Try and connect to the server $server = new SourceServer('192.228.110.111', 27015); //Put Server Information Here $server->initialize(); $players = $server->getPlayers(" PUT YOUR RCON PASSWORD HERE "); //Player pings are only available if the RCON password is provided $tttInfo = $server->getServerInfo(); //Get Server Info to check numberOfPlayers online echo "<div id=\"divContainer\"><div class=\"theDivL\"><b><h5>Name</h5></div><div class=\"theDivL\"><h5>Score</h5></div><div class=\"theDivL\"><h5>Time Connected</h5></div><div class=\"theDivL\"><h5>Ping</h5></b></div>"; if ($tttInfo['numberOfPlayers'] == 0) { //If 0, display this message echo "<div id=\"divCentered\">There are currently no players online.</div>"; } else { //Else, display current online players in a table. foreach($players as $player) { //Display each players Name, Score, Time Played and Ping through a for loop $playerName = $player->getName(); $playerScore = $player->getScore(); $playerPing = $player->getPing(); $playerTimeSeconds = $player->getConnectTime() / 60; if ($playerTimeSeconds >= 60 && $playerTimeSeconds < 120) { //This is probably pretty messy, displaying different formatted time based on how many minutes played $playerTime = convertToHoursMins($playerTimeSeconds, '%01d hour %02d minutes'); } elseif ($playerTimeSeconds >= 120 && $playerTimeSeconds < 600) { $playerTime = convertToHoursMins($playerTimeSeconds, '%01d hours %02d minutes'); } elseif ($playerTimeSeconds >= 600) { $playerTime = convertToHoursMins($playerTimeSeconds, '%02d hours %02d minutes'); } elseif ($playerTimeSeconds >= 10 && $playerTimeSeconds < 60) { $playerTime = convertToHoursMins($playerTimeSeconds, '%02d minutes'); } elseif ($playerTimeSeconds > 1 && $playerTimeSeconds < 10) { $playerTime = convertToHoursMins($playerTimeSeconds, '%01d minutes'); } else { $playerTime = convertToHoursMins($playerTimeSeconds, '%01d minute'); } if ($playerName == "" or $playerTime =="") { //If Player is connecting, their name or time are blank which screws up the table. $playerName = "Player Connecting..."; $playerScore = "..."; $playerTime = "..."; $playerPing = "..."; } echo "<div class=\"theDivL\">{$playerName}</div><div class=\"theDivL\">{$playerScore}</div><div class=\"theDivL\">{$playerTime}</div><div class=\"theDivL\">{$playerPing}</div>"; } } echo "</div>"; // } catch (SocketException $e) { //Catch Exception when can't connect to server. echo "<div id=\"divContainer\"><div class=\"theDivL\"><b><h5>Name</h5></div><div class=\"theDivL\"><h5>Score</h5></div><div class=\"theDivL\"><h5>Time Connected</h5></div><div class=\"theDivL\"><h5>Ping</h5></b></div>"; echo "<div id=\"divCentered\">The server is currently offline.</div>"; } catch (TimeoutException $e) { //Catch Exception when server times out. echo "<div id=\"divContainer\"><div class=\"theDivL\"><b><h5>Name</h5></div><div class=\"theDivL\"><h5>Score</h5></div><div class=\"theDivL\"><h5>Time Connected</h5></div><div class=\"theDivL\"><h5>Ping</h5></b></div>"; echo "<div id=\"divCentered\">The server has timed out.</div>"; } catch (Exception $e) { //Catch other Exception. echo "<div id=\"divContainer\"><div class=\"theDivL\"><b><h5>Name</h5></div><div class=\"theDivL\"><h5>Score</h5></div><div class=\"theDivL\"><h5>Time Connected</h5></div><div class=\"theDivL\"><h5>Ping</h5></b></div>"; echo "<div id=\"divCentered\">Unknown server error.</div>"; } ?>[/CODE] And of course if anyone has any comments or suggestions, I'd be glad to listen. [URL="http://stupids-servers.com/ttt/"]Here's[/URL] an example of the code used on my website using Wordpress.
Very nice, all it needs is some styling and bam! Would you mind if I used this?
You should really rewrite your script so it doesn't work with separate variables such as "$darkRPPlayers" and "$tttPlayers". You should start off with an array, such as this: [code]$servers = array( "<ip>" => array( "name" => "DarkRP" ), "<ip2>" => array( "name" => "TTT" ) ); [/code] Then run a foreach($servers as $key => $value) loop, where you loop over all IPs that are in the $servers array. For each server, run the Steam condenser, and use $servers[$key]['players'], $servers[$key]['maxplayers'] etc to store the information. This makes you not have to use separate variables for every server, and makes adding servers way easier (you'd only have to supply an IP and name, that's it). If you'd like help with this, you can add me to Steam, I've written plenty of Steam server queries before, it'd also be useful to cache them, so not every pageload (read: every person visiting a page on your website) has to query the server.
Sorry, you need to Log In to post a reply to this thread.