|
|
|
การดึงค่า Array ใน Scope ของอีกไฟล์นึงครับ [มีโค้ตให้ดูตัวอย่างครับ] |
|
|
|
|
|
|
|
นี่คือ ไฟล์ MCQuery.php ของผมนะครับ
Code (PHP)
public function Query()
{
//challenge
$Data = $this->WriteData( self :: HANDSHAKE );
if( $Data === false )
{
$this->error="Failed to receive challenge";
}
$challenge=pack( 'N', $Data );
$Data = $this->WriteData( self :: STATISTIC, $challenge . pack( 'c*', 0x00, 0x00, 0x00, 0x00 ) );
if( !$Data )
{
$this->error="Failed to receive status";
}
$Last = '';
$Info = Array( );
$Data = substr( $Data, 11 ); // splitnum + 2 int
$Data = explode( "\x00\x00\x01player_\x00\x00", $Data );
if( count( $Data ) !== 2 )
{
$this->error="Failed to parse server's response";
}
$Players = @substr( $Data[ 1 ], 0, -2 );
$Data = explode( "\x00", $Data[ 0 ] );
// Array with known keys in order to validate the result
// It can happen that server sends custom strings containing bad things (who can know!)
$Keys = Array(
'hostname' => 'HostName',
'gametype' => 'GameType',
'version' => 'Version',
'plugins' => 'Plugins',
'map' => 'Map',
'numplayers' => 'Players',
'maxplayers' => 'MaxPlayers',
'hostport' => 'HostPort',
'hostip' => 'HostIp',
'game_id' => 'GameName'
);
foreach( $Data as $Key => $Value )
{
if( ~$Key & 1 )
{
if( !array_key_exists( $Value, $Keys ) )
{
$Last = false;
continue;
}
$Last = $Keys[ $Value ];
$Info[ $Last ] = '';
}
else if( $Last != false )
{
$Info[ $Last ] = $Value;
}
}
// Ints
$Info[ 'Players' ] = $this->error==null?@intval( $Info[ 'Players' ] ):null;
$Info[ 'MaxPlayers' ] = $this->error==null?@intval( $Info[ 'MaxPlayers' ] ):null;
$Info[ 'HostPort' ] = $this->error==null?@intval( $Info[ 'HostPort' ] ):null;
// Parse "plugins", if any
if( @$Info[ 'Plugins' ] )
{
$Data = explode( ": ", $Info[ 'Plugins' ], 2 );
$Info[ 'RawPlugins' ] = $Info[ 'Plugins' ];
$Info[ 'Software' ] = $Data[ 0 ];
if( count( $Data ) == 2 )
{
$Info[ 'Plugins' ] = explode( "; ", $Data[ 1 ] );
}
}
else
{
$Info[ 'Software' ] = $this->error==null?'Vanilla':null;
}
$this->Info = $Info;
if( $Players )
{
$this->Players = explode( "\x00", $Players );
}
}
ขอบคุณครับ
Tag : PHP
|
|
|
|
|
|
Date :
2018-06-23 23:39:54 |
By :
AloneSpace |
View :
615 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
งงครับ อธิบายใหม่ครับ
|
|
|
|
|
Date :
2018-06-28 09:17:43 |
By :
deawx |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|