Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*------------------------------------------------------------
- | M2MP Query Script
- |------------------------------------------------------------
- | This class enables you to connect to a M2MP server
- | and get info about it.
- |------------------------------------------------------------
- | Author: Lordkire
- ------------------------------------------------------------*/
- class M2MPServer {
- private $ip, $port, $timeout, $retries, $maxretries, $socket, $connected;
- public function __construct($ip, $port = 27016, $timeout = 2, $retries = 1) {
- $this->ip = $ip;
- $this->port = $port;
- $this->timeout = $timeout;
- $this->retries = 0;
- $this->maxretries = $retries;
- $this->socket = @fsockopen('udp://' . $this->ip, $this->port, $errno, $errstr, $this->timeout);
- socket_set_timeout($this->socket, $this->timeout);
- if (!$this->socket) {
- $this->connected = false;
- return;
- }
- if ($this->sendPacket('p')) {
- $this->connected = true;
- return;
- }
- else {
- $this->connected = false;
- }
- }
- private function sendPacket($opcode) {
- fwrite($this->socket, 'M2MP' . $opcode);
- $packet = fread($this->socket, 1024);
- if (!$packet) {
- if ($this->retries < $this->maxretries) {
- $this->retries += 1;
- return $this->sendPacket($opcode);
- }
- else {
- $this->retries = 0;
- return false;
- }
- }
- else {
- $this->retries = 0;
- $packet = rtrim($packet, "\x00");
- return $packet;
- }
- }
- // This function is used to get the IP address and (query) port number of the server.
- // Returns an array with:
- // - index 0: IP addres (str)
- // - index 1: Query port (int)
- public function getAddress() {
- return array($this->ip, $this->port);
- }
- // This function is used to check if the script is connected to the server.
- // It is best to check this first before using the other functions (saves time-out time).
- // Returns true if connected, false if not connected.
- public function isConnected() {
- return $this->connected;
- }
- // This function is used to get the ping from the webserver that runs the script to the M2MP server.
- // Returns the ping (int) if the query succeeded, and false if it failed.
- public function getPing() {
- $startTime = microtime();
- fwrite($this->socket, 'M2MPp');
- $packet = fread($this->socket, 1024);
- $stopTime = microtime();
- if (!$packet) {
- if ($this->retries < $this->maxretries) {
- $this->retries += 1;
- return $this->getPing();
- }
- else {
- $this->retries = 0;
- return false;
- }
- }
- else {
- $this->retries = 0;
- return ceil(($stopTime - $startTime) * 1000);
- }
- }
- // This function is used to get basic info about the server.
- // The function returns an array with the following keys if the query succeeded:
- // - 'hostname': name of the server (str)
- // - 'players': number of players in the server (int)
- // - 'maxplayers': maximum number of players (int)
- // - 'gamemode': name of the gamemode (str)
- // - 'passworded': wether the server requires a password (bool)
- // - 'mapname': name of the map (str)
- // - 'version': server version (str)
- // - 'weburl': web URL as specified in the server's config (str)
- // - 'httpport' : httpport as specified in the server's config (str)
- // Or it returns false in case the query failed.
- public function getInfo() {
- $packet = $this->sendPacket('i');
- if ($packet) {
- $packet = explode('<info>', $packet);
- $info = array(
- 'hostname' => $packet[1],
- 'players' => (int)$packet[2],
- 'maxplayers' => (int)$packet[3],
- 'gamemode' => $packet[4],
- 'passworded' => (bool)$packet[5],
- 'mapname' => $packet[6],
- 'version' => $packet[7],
- 'weburl' => $packet[8],
- 'httpport' => $packet[9]);
- return $info;
- }
- else {
- return false;
- }
- }
- // This function is used to get the playerlist of the server.
- // It returns an array with players arrays if the query succeeded, or false if the query failed.
- // The player arrays have the following keys:
- // - 'id': the playerid (int)
- // - 'name': the player's nickname (str)
- // - 'score': the player's score (int)
- public function getPlayers() {
- $packet = $this->sendPacket('l');
- if ($packet) {
- $packet = explode('<info>', $packet);
- if ($packet[1] > 0) {
- $packet = explode('<player>', $packet[2]);
- foreach ($packet as $player) {
- $player = explode('<data>', $player);
- $players[] = array(
- 'id' => (int)$player[0],
- 'name' => $player[1],
- 'score' => (int)$player[2]);
- }
- return $players;
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- // This function checks if a player with name == $name is on the server.
- // It returns true if there is, and false if there isn't, or if the query failed.
- public function isPlayerOnline($name) {
- if ($players = $this->getPlayers()) {
- foreach ($players as $player) {
- if ($player['name'] == $name) {
- return true;
- }
- }
- return false;
- }
- else {
- return false;
- }
- }
- // This static function returns an array of M2MPServer objects based on the master list.
- // If the master list could not be found, it returns false.
- public static function getMasterList($url = 'http://www.m2-multiplayer.com/master/list.php') {
- $data = file_get_contents($url);
- if ($data) {
- $data = explode('<br />', $data);
- unset($data[count($data) - 1]);
- foreach ($data as $server) {
- $server = explode(':', $server);
- $servers[] = new M2MPServer($server[0], $server[1] + 1);
- }
- return $servers;
- }
- else {
- return false;
- }
- }
- // This static function returns an array with info about the master list, with the following keys:
- // - 'players': the total number of players online (int)
- // - 'maxplayers': the total number of player slots (int)
- // - 'servers': the nummber of servers online (int)
- // Or it returns false in case the master list could not be found.
- public static function getMasterInfo($url = 'http://www.m2-multiplayer.com/master/list.php') {
- if ($servers = self::getMasterList($url)) {
- $masterInfo = array(
- 'players' => 0,
- 'maxplayers' => 0,
- 'servers' => 0);
- foreach ($servers as $server) {
- if ($server->isConnected() && ($info = $server->getInfo())) {
- $masterInfo['players'] += $info['players'];
- $masterInfo['maxplayers'] += $info['maxplayers'];
- $masterInfo['servers'] += 1;
- }
- }
- return $masterInfo;
- }
- else {
- return false;
- }
- }
- public function __destruct() {
- @fclose($this->socket);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement