Skip to content
Snippets Groups Projects
Commit 7c601967 authored by Stuart John Watson's avatar Stuart John Watson
Browse files

Fixed game ping code

parent 86259b6f
No related branches found
No related tags found
1 merge request!1Major Rebuild: AJAX and working game info
......@@ -13,20 +13,36 @@ function getInfo(){
return ["online"=>false,"error"=>socket_strerror(socket_last_error($sock))];
}
$input = "\x0f\x00\x2f\x09127.0.0.1c\xdd\x01\x01\x00";
//Documentation on the ping protcol: http://wiki.vg/Server_List_Ping
// Size Protcol Version Next State
// _|_ Id_ _|_ _Address____ _Port__ _|_ _Request Packet
$input = "\x0f\x00\x2f\x09127.0.0.1\x63\xdd\x01\x01\x00";
socket_write($sock,$input,strlen($input));
$out = socket_read($sock,2048);
socket_close($sock);
//Read an inital response from the socket
//This will let us get an initial size.
$out = socket_read($sock,8);
//Get the length of the packet
$t = readVarint($out);
$out = $t["data"];
$len = $t["res"];
//Keep reading from the socket
$toRead = $len - strlen($out);
while($toRead){
$out .= socket_read($sock,$toRead);
$toRead = $len - strlen($out);
}
socket_close($sock);
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"];
......@@ -62,6 +78,4 @@ function readVarint($data){
}
return ["res"=>$result,"data"=>substr($data,$i+1)];
}
#echo json_encode(minecraft());
?>
......@@ -19,32 +19,41 @@ function getInfo(){
$out = socket_read($sock,2048);
socket_close($sock);
if ( substr($out,0,3) !== $reqId){
//Check the response starts with the request Id
if ( substr($out,0,strlen($reqId)) !== $reqId){
return ["online"=>false,"error"=>"server sent unexpected response"];
}
$out = substr($out,3);
//Chop off request Id
$out = substr($out,strlen($reqId));
//Read players & number of attributes
$numPlayers = readInt($out);
$numAttrs = readInt($out);
//Rad the basic version
$protcolVersion = readInt($out);
$gameMode = readInt($out);
$timeLeft = readInt($out);
$maxClients = readInt($out);
$masterMode = readInt($out);
//Check if we have 7 attributes
if ($numAttrs == 7){
//If we do we know if the game is paused and the speed
$gamePaused = readInt($out);
$gameSpeed = readInt($out);
} else {
$gamePaused = 0;
$gameSpeed = 100;
}
//Everything else is strings, take the reminder and explode on null
$tmp = explode("\x00",$out);
$mapName = $tmp[1];
$serverDesc = $tmp[2];
//The first part is the map, the second is the desc
$mapName = $tmp[0];
$serverDesc = $tmp[1];
//Conversion from game mode id to string
$nnn = [
"Free for all",
"Coop Edit",
......@@ -84,16 +93,16 @@ function getInfo(){
function readInt(&$out){
$c = ord(substr($out,0,1));
if ($c == -128){
if ($c == 128){ //Number that follows is a unsigned sort (2 bytes)
$c = unpack("v",substr($out,1,2))[1];
$out = substr($out,3);
} elseif ($c == -127){ //Number that follows is a unsigned int (4 bytes)
$c = unpack("V",substr($out,1,4))[1];
$out = substr($out,3);
} else { //Number is just a byte
$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;
}
return $c;
}
#echo json_encode(sauerbraten());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment