Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @Author: xISRAPILx
- */
- class Query{
- /** @var string */
- private $address;
- /** @var int */
- private $port;
- /** @var resource */
- private $socket;
- /**
- * Query constructor.
- *
- * @param string $address
- * @param int $port
- */
- public function __construct(string $address, int $port){
- $this->address = $address;
- $this->port = $port;
- $this->connect();
- }
- public function __destruct(){
- fclose($this->socket);
- }
- /**
- * @return bool
- */
- public function connect() : bool{
- $this->socket = fsockopen("udp://".$this->address, $this->port, $error_id, $error, 4);
- return !$this->socket ? false : true;
- }
- /**
- * Получение базовой информации с сервера(Онлайн, название, игроковой режим и тд.).
- *
- * @return array|null
- */
- public function getInformation() : ?array{
- if(!fwrite($this->socket, $this->createBasePacket("i")))
- return null;
- if(!fread($this->socket, 11))
- return null;
- $information["password"] = (bool) Binary::readByte(fread($this->socket, 1)); //Статус пароля сервера
- $information["players"] = Binary::readLShort(fread($this->socket, 2)); //Кол-во игроков онлайн
- $information["max_players"] = Binary::readLShort(fread($this->socket, 2)); //Максимальное количество игроков.
- $information["hostname"] = Binary::readLInt(fread($this->socket, 4)); //Длина названия сервера
- $information["hostname"] = fread($this->socket, $information["hostname"]); //Само название сервера
- $information["gamemode"] = Binary::readLInt(fread($this->socket, 4)); //Длина игрового режима сервера
- $information["gamemode"] = fread($this->socket, $information["gamemode"]); //Сам игроков режим сервера
- $information["language"] = Binary::readLInt(fread($this->socket, 4)); //Длина языка сервера
- $information["language"] = fread($this->socket, $information["language"]); //Сам язык сервера
- return $information;
- }
- /**
- * @param string $opcode
- *
- * @return string
- */
- public function createBasePacket(string $opcode = "i") : string{
- $address = explode(".", $this->address);
- $packet = "SAMP";
- $packet .= Binary::writeByte($address[0]);
- $packet .= Binary::writeByte($address[1]);
- $packet .= Binary::writeByte($address[2]);
- $packet .= Binary::writeByte($address[3]);
- $packet .= Binary::writeLShort($this->port);
- $packet .= $opcode;
- return $packet;
- }
- /**
- * @return string
- */
- public function getAddress() : string{
- return $this->address;
- }
- /**
- * @param string $address
- */
- public function setAddress(string $address) : void{
- fclose($this->socket);
- $this->address = $address;
- $this->connect();
- }
- /**
- * @return int
- */
- public function getPort() : int{
- return $this->port;
- }
- /**
- * @param int $port
- */
- public function setPort(int $port) : void{
- $this->port = $port;
- }
- }
- class Binary{
- /**
- * @param string $value
- *
- * @return int
- */
- public static function readByte(string $value) : int{
- return ord($value{0});
- }
- /**
- * @param int $value
- *
- * @return string
- */
- public static function writeByte(int $value) : string{
- return chr($value);
- }
- /**
- * @param string $value
- *
- * @return int
- */
- public static function readLShort(string $value) : int{
- return unpack("v", $value)[1];
- }
- /**
- * @param int $value
- *
- * @return string
- */
- public static function writeLShort(int $value) : string{
- return pack("v", $value);
- }
- /**
- * @param string $value
- *
- * @return int
- */
- public static function readLInt(string $value) : int{
- return unpack("V", $value)[1] << 32 >> 32;
- }
- /**
- * @param int $value
- *
- * @return string
- */
- public static function writeLInt(int $value) : string{
- return pack("V", $value);
- }
- }
Add Comment
Please, Sign In to add comment