Commit f589d60d authored by Stuart John Watson's avatar Stuart John Watson

Started adding the game status info

parent a2c78afa
<?php
$GAMES_TO_INCLUDE = [
"minecraft",
"sauerbraten"
];
function getGameStatus($includeTemplate){
global $GAMES_TO_INCLUDE;
$gameInfo = [];
foreach ($GAMES_TO_INCLUDE as $game){
include "$game.php";
$a = "$game\\getInfo";
$gameInfo[$game] = $a();
if ($includeTemplate){
echo file_get_contents("$game.html");
}
}
return $gameInfo;
}
var_dump(getGameStatus(1));
?>
<h2>Minecraft - <span data-target="_online"></span></h2>
<dl>
<dt>Players:</dt>
<dd>
<span data-target="players_on"></span>/<span data-target="players_max"></span>
</dd>
<dt>Version:</dt>
<dd>
<span data-target="version"></span>
</dd>
</dl>
<p>
<a target="_blank" href="http://games.sucs.org/dynmap">Live Map</a>
</p>
<?php namespace minecraft;
function getInfo(){
$ADDRESS = "games";
$PORT = 25565;
if (($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) === false){
return ["online"=>false,"error"=>socket_strerror(socket_last_error($sock))];
}
if(socket_connect($sock,$ADDRESS,$PORT) === false){
return ["online"=>false,"error"=>socket_strerror(socket_last_error($sock))];
}
$input = "\x0f\x00\x2f\x09127.0.0.1c\xdd\x01\x01\x00";
socket_write($sock,$input,strlen($input));
$out = socket_read($sock,2048);
socket_close($sock);
$t = readVarint($out);
$out = $t["data"];
$len = $t["res"];
if ( substr($out,0,1) !== "\x00"){
return ["online"=>false,"error"=>"server sent unexpected result"];
}
$t = readVarint(substr($out,1));
$out = $t["data"];
$len = $t["res"];
$result = json_decode($out,$assoc=true);
$players = [];
if ($result["players"]["sample"]){
foreach ($result["players"]["sample"] as $p){
$players[] = $p["name"];
}
}
return [
"_online"=>true,
"description"=>$result["description"],
"players_on"=>$result["players"]["online"],
"players_max"=>$result["players"]["max"],
"version"=>$result["version"]["name"]
];
}
function readVarint($data){
$result = 0;
$first = true;
for($i=0;$i<strlen($data);$i++){
$part = ord(substr($data,$i,1));
$result |= ($part & 0x7F) << 7 * $i;
if (($part & 0x80) == 0){
break;
}
}
return ["res"=>$result,"data"=>substr($data,$i+1)];
}
#echo json_encode(minecraft());
?>
<h2>Sauerbraten - <span data-target="_online"></span></h2>
<dl>
<dt>Players:</dt>
<dd>
<span data-target="players_on"></span>/<span data-target="players_max"></span>
</dd>
<dt>Version:</dt>
<dd>
<span data-target="version"></span>
</dd>
</dl>
<p>
<a target="_blank" href="http://games.sucs.org/dynmap">Live Map</a>
</p>
<?php namespace sauerbraten;
function getInfo(){
$ADDRESS = "games.sucs.org";
$PORT = 28786;
if (($sock = socket_create(AF_INET,SOCK_DGRAM,0)) === false){
return ["online"=>false,"error"=>socket_strerror(socket_last_error($sock))];
}
if(socket_connect($sock,$ADDRESS,$PORT) === false){
return ["online"=>false,"error"=>socket_strerror(socket_last_error($sock))];
}
$reqId = "GI\n";
socket_write($sock,$reqId,3);
$out = socket_read($sock,2048);
socket_close($sock);
if ( substr($out,0,3) !== $reqId){
return ["online"=>false,"error"=>"server sent unexpected response"];
}
$out = substr($out,3);
$numPlayers = readInt($out);
$numAttrs = readInt($out);
$protcolVersion = readInt($out);
$gameMode = readInt($out);
$timeLeft = readInt($out);
$maxClients = readInt($out);
$masterMode = readInt($out);
if ($numAttrs == 7){
$gamePaused = readInt($out);
$gameSpeed = readInt($out);
} else {
$gamePaused = 0;
$gameSpeed = 100;
}
$tmp = explode("\x00",$out);
$mapName = $tmp[1];
$serverDesc = $tmp[2];
$nnn = [
"Free for all",
"Coop Edit",
"Teamplay",
"Instagib",
"Instagib Team",
"Efficiency",
"Efficiency team",
"Tactics",
"Tactics team",
"Capture",
"Regen capture",
"Capture the flag",
"Insta Capture the flag",
"Protect",
"Insta Protect",
"Hold",
"Insta Hold",
"Efficiency Capture the flag",
"Efficiency Protect",
"Efficiency Hold",
"Collect",
"Insta Collect",
"Efficiency Collect"
];
$gameMode = $nnn[$gameMode];
return [
"_online"=>true,
"description"=>$serverDesc,
"map"=>$mapName,
"players_on"=>$numPlayers,
"player_max"=>$maxClients,
"gameMode"=>$gameMode
];
}
function readInt(&$out){
$c = ord(substr($out,0,1));
if ($c == -128){
$out = substr($out,1);
return unpack("v",substr($out,1,2));
} elseif ($c == -127){
$out = substr($out,1);
return unpack("V",substr($out,1,4));
} else {
$out = substr($out,1);
return $c;
}
}
#echo json_encode(sauerbraten());
?>
......@@ -92,13 +92,11 @@
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-body">
<h2>Minecraft - <font color="green">Online</font></h2>
<p>
Vanilla running 1.8.1
</p>
<p>
<a target="_blank" href="http://games.sucs.org/dynmap">Live Map</a>
</p>
<?php
chdir('games');
include '_manager.php';
getGameStatus(1);
?>
</div>
</div>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment