Author Topic: How to get the servers and players count  (Read 3172 times)

0 Members and 1 Guest are viewing this topic.

Offline chencong

  • Street Thug
  • *
  • Posts: 7
    • View Profile
    • HuiCheng Game Community
How to get the servers and players count
« on: August 08, 2013, 07:13:17 am »
I want to get the VC:MP servers and players count like SA:MP HomePage.
How to do that?

Offline Fuzzy168

  • VC:MP Veteran
  • *****
  • Posts: 729
  • Programming since 2011
    • View Profile
Re: How to get the servers and players count
« Reply #1 on: August 08, 2013, 07:15:49 am »
You open up the server browser..
I'm beginning to feel like a Lag God, Lag God

Offline chencong

  • Street Thug
  • *
  • Posts: 7
    • View Profile
    • HuiCheng Game Community
Re: How to get the servers and players count
« Reply #2 on: August 08, 2013, 07:22:13 am »
You open up the server browser..

I want to show it on my website...

Offline thijn

  • LU testers
  • VC:MP Veteran
  • *
  • Posts: 667
  • Im proud to be pro.
    • View Profile
    • Vice Underdogs
Re: How to get the servers and players count
« Reply #3 on: August 08, 2013, 05:57:15 pm »
You need to have a proper webhoster, since you need fsockopen and UDP support.

Place this in a file called VCMP.Class.php
Code: (VCMP.Class.php) [Select]
<?php
class VCMPQuery {

private $Socket false;

private $aData = array();

private $aServer = array();

public function __construct($Server$Port 5192)
{
$this->aServer[0] = $Server;
$this->aServer[1] = $Port;
$this->Socket fsockopen('udp://'.$Server$Port$Errno$Errstr2);
$this->aData[0] = true;
if(!$this->Socket)
{
$this->aData[0] = false;
return;
}

socket_set_timeout($this->Socket2);

$sPacket 'VCMP';
$sPacket .= chr(strtok($Server'.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr($Port 0xFF);
$sPacket .= chr($Port >> 0xFF);

fwrite($this->Socket$sPacket.'p4150');

if(fread($this->Socket10))
{
if(fread($this->Socket5) == 'p4150')
{
$this->aData[0] = true;
return;
}
}

$this->aData[0] = false;
}

public function getInfo()
{
// Request server info and player info
$aData = array();

@fwrite($this->Socket$this->createPacket('i'));

fread($this->Socket11);

$aData['password'] = ord(fread($this->Socket1));

$aData['players'] = ord(fread($this->Socket2));

$aData['maxplayers'] = ord(fread($this->Socket2));

$iStrlen ord(fread($this->Socket4));
if(!$iStrlen) return -1;

$aData['hostname'] = (string) fread($this->Socket$iStrlen);

$iStrlen ord(fread($this->Socket4));
$aData['gamemode'] = (string) fread($this->Socket$iStrlen);

$iStrlen ord(fread($this->Socket4));
$aData['mapname'] = (string) fread($this->Socket$iStrlen);

return $aData;
}
public function getPlayers()
{
fwrite($this->Socket$this->createPacket('c'));
fread($this->Socket11);
$plr_count ord(fread($this->Socket2));
if ($plr_count 0)
{
$players = array();
for ($i=0$i<$plr_count$i++)
{
$strlen ord(fread($this->Socket1));
$plrname fread($this->Socket$strlen);
$score $this->getLong(fread($this->Socket4));
$players[] = array( $plrname$score);
}
return $players;
}
return false;

}

public function __destruct()
{
@fclose($this->Socket);
}

private function createPacket($req)
{
$sPacket 'VCMP';
$sPacket .= chr(strtok($this->aServer[0], '.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr($this->aServer[1] & 0xFF);
$sPacket .= chr($this->aServer[1] >> 0xFF);
$sPacket .= $req;

return $sPacket;
}

public function isAlive()
{
return $this->aData[0];
}

private function getLong($dat)
{
$num=0;
if ((ord(substr($dat,3,1)) & 128) > 0) {
for ($i=0$i<strlen($dat); $i++) {
$num-=((255-ord(substr($dat,$i,1))) << 8*$i);
}
$num--;
} else {
for ($i=0$i<strlen($dat); $i++) {
$num+=(ord(substr($dat,$i,1)) << 8*$i);
}
}
return $num;
}

}
?>


And on the place where you want to show your player count paste this:
Code: [Select]
<?php
require_once("VCMP.Class.php");
$q = new VCMPQuery"YOUR.IP.HERE"port );
if ( 
$q->IsAlive() )
{
$data $q->getInfo();
echo 
$data['players'] . '/' $data['maxplayers'];
}
?>

This will show something like 10/50.