#249: Upgrade GameQ to latest version

This commit is contained in:
Ulrich Block 2013-12-30 12:05:58 +01:00
parent a15a970026
commit 269e71ecc7
18 changed files with 1885 additions and 33 deletions

View File

@ -291,7 +291,7 @@ class GameQ
|| !is_array($server_info[self::SERVER_OPTIONS])
|| empty($server_info[self::SERVER_OPTIONS]))
{
// Make an id so each server has an id when returned
// Default the options to an empty array
$server_info[self::SERVER_OPTIONS] = array();
}
@ -794,6 +794,12 @@ class GameQ
$write = NULL;
$except = NULL;
// Check to see if $read is empty, if so stream_select() will throw a warning
if(empty($read))
{
return $responses;
}
// This is when it should stop
$time_stop = microtime(TRUE) + $this->timeout;

44
web/third_party/gameq/README.md vendored Normal file
View File

@ -0,0 +1,44 @@
Information
===========
GameQ is a PHP program that allows you to query multiple types of multiplayer game servers at the same time.
GameQ v2 is based off of the original GameQ PHP program from http://gameq.sourceforge.net/. That project was no longer being supported.
Requirements
============
* PHP 5.2 (Recommended 5.3, 5.4)
Extras you might need:
* Bzip2 - Used for A2S compressed responses (http://www.php.net/manual/en/book.bzip2.php)
* Zlib - Used for AA3 (before version 3.2) compressed responses (http://www.php.net/manual/en/book.zlib.php)
Example
=======
Usage & Examples: https://github.com/Austinb/GameQ/wiki/Usage-&-examples-v2
Quick and Dirty:
$gq = new GameQ();
$gq->addServer(array(
'id' => 'my_server',
'type' => 'css', // Counter-Strike: Source
'host' => '127.0.0.1:27015',
));
$results = $gq->requestData(); // Returns an array of results
print_r($results);
Want more? Check out the wiki page or /examples for more.
ChangeLog
=========
See https://github.com/Austinb/GameQ/commits/v2 for an incremental list of changes
License
=======
See LICENSE for more information
Donations
=========
If you like this project and use it a lot please feel free to donate here: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VAU2KADATP5PU.

View File

@ -26,5 +26,7 @@ class GameQ_Protocols_Aa extends GameQ_Protocols_Gamespy2
protected $name = "aa";
protected $name_long = "America's Army";
protected $link_join = "aao://%s:%d/";
protected $port = 1717;
}

View File

@ -26,5 +26,7 @@ class GameQ_Protocols_Aa3 extends GameQ_Protocols_Source
protected $name = "aa3";
protected $name_long = " America's Army 3 (> 3.2)";
protected $link_join = "aa3://%s:%d/";
protected $port = 27020;
}

View File

@ -18,7 +18,7 @@
/**
* Battlefield 3 Protocol Class
*
*
* Good place for doc status and info is http://www.fpsadmin.com/forum/showthread.php?t=24134
*
* @author Austin Bischoff <austin@codebeard.com>
@ -90,7 +90,7 @@ class GameQ_Protocols_Bf3 extends GameQ_Protocols
*
* @var int
*/
protected $port = 25200; // Default port, used if not set when instanced
protected $port = 47200; // Default port, used if not set when instanced
/**
* The protocol being used
@ -195,7 +195,7 @@ class GameQ_Protocols_Bf3 extends GameQ_Protocols
$result->add('region', $words[$index_current + 10]);
$result->add('pingsite', $words[$index_current + 11]);
$result->add('country', $words[$index_current + 12]);
// Added in R29, No docs as of yet
$result->add('quickmatch', $words[$index_current + 13] === 'true'); // Guessed from research

View File

@ -0,0 +1,189 @@
<?php
/**
* This file is part of GameQ.
*
* GameQ is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GameQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Battlefield 4 Protocol Class
*
* Good place for doc status and info is http://battlelog.battlefield.com/bf4/forum/view/2955064768683911198/
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class GameQ_Protocols_Bf4 extends GameQ_Protocols_Bf3
{
/**
* The protocol being used
*
* @var string
*/
protected $protocol = 'bf4';
/**
* String name of this protocol class
*
* @var string
*/
protected $name = 'bf4';
/**
* Longer string name of this protocol class
*
* @var string
*/
protected $name_long = "Battlefield 4";
/*
* Internal Methods
*/
protected function process_status()
{
// Make sure we have a valid response
if(!$this->hasValidResponse(self::PACKET_STATUS))
{
return array();
}
// Make buffer for data
$buf = new GameQ_Buffer($this->preProcess_status($this->packets_response[self::PACKET_STATUS]));
$buf->skip(8); /* skip header */
// Decode the words into an array so we can use this data
$words = $this->decodeWords($buf);
// Make sure we got OK
if (!isset ($words[0]) || $words[0] != 'OK')
{
throw new GameQ_ProtocolsException('Packet Response was not OK! Buffer:'.$buf->getBuffer());
}
// Set the result to a new result instance
$result = new GameQ_Result();
// Server is always dedicated
$result->add('dedicated', TRUE);
// No mods, as of yet
$result->add('mod', FALSE);
// These are the same no matter what mode the server is in
$result->add('hostname', $words[1]);
$result->add('numplayers', $words[2]);
$result->add('maxplayers', $words[3]);
$result->add('gametype', $words[4]);
$result->add('map', $words[5]);
$result->add('roundsplayed', $words[6]);
$result->add('roundstotal', $words[7]);
// Figure out the number of teams
$num_teams = intval($words[8]);
// Set the current index
$index_current = 9;
// Loop for the number of teams found, increment along the way
for($id=1; $id<=$num_teams; $id++)
{
$result->addSub('teams', 'tickets', $words[$index_current]);
$result->addSub('teams', 'id', $id);
// Increment
$index_current++;
}
// Get and set the rest of the data points.
$result->add('targetscore', $words[$index_current]);
$result->add('online', TRUE); // Forced TRUE, it seems $words[$index_current + 1] is always empty
$result->add('ranked', $words[$index_current + 2] === 'true');
$result->add('punkbuster', $words[$index_current + 3] === 'true');
$result->add('password', $words[$index_current + 4] === 'true');
$result->add('uptime', $words[$index_current + 5]);
$result->add('roundtime', $words[$index_current + 6]);
$result->add('ip_port', $words[$index_current + 7]);
$result->add('punkbuster_version', $words[$index_current + 8]);
$result->add('join_queue', $words[$index_current + 9] === 'true');
$result->add('region', $words[$index_current + 10]);
$result->add('pingsite', $words[$index_current + 11]);
$result->add('country', $words[$index_current + 12]);
// @todo: Supposed to be a field here <matchMakingEnabled: boolean>, its in R13 docs but doesnt return in response
$result->add('blaze_player_count', $words[$index_current + 13]);
$result->add('blaze_game_state', $words[$index_current + 14]);
unset($buf, $words);
return $result->fetch();
}
protected function process_players()
{
// Make sure we have a valid response
if(!$this->hasValidResponse(self::PACKET_PLAYERS))
{
return array();
}
// Set the result to a new result instance
$result = new GameQ_Result();
// Make buffer for data
$buf = new GameQ_Buffer($this->preProcess_players($this->packets_response[self::PACKET_PLAYERS]));
$buf->skip(8); /* skip header */
$words = $this->decodeWords($buf);
// Not too important if players are missing.
if (!isset ($words[0]) || $words[0] != 'OK')
{
return array();
}
// Count the number of words and figure out the highest index.
$words_total = count($words)-1;
// The number of player info points
$num_tags = $words[1];
// Pull out the tags, they start at index=3, length of num_tags
$tags = array_slice($words, 2, $num_tags);
// Just incase this changed between calls.
$result->add('numplayers', $words[($num_tags+2)]);
// Loop until we run out of positions
for($pos=(3+$num_tags);$pos<=$words_total;$pos+=$num_tags)
{
// Pull out this player
$player = array_slice($words, $pos, $num_tags);
// Loop the tags and add the proper value for the tag.
foreach($tags AS $tag_index => $tag)
{
$result->addPlayer($tag, $player[$tag_index]);
}
}
// @todo: Add some team definition stuff
unset($buf, $tags, $words, $player);
return $result->fetch();
}
}

View File

@ -196,6 +196,13 @@ abstract class GameQ_Protocols_Core
*/
protected $normalize = FALSE;
/**
* Quick join link for specific games
*
* @var string
*/
protected $join_link = NULL;
/**
* Create the instance.
*
@ -570,6 +577,9 @@ abstract class GameQ_Protocols_Core
$results['gq_type'] = (string) $this;
$results['gq_transport'] = $this->transport;
// Process the join link
$results['gq_joinlink'] = $this->getJoinLink();
// Return the raw results
return $results;
}
@ -634,4 +644,23 @@ abstract class GameQ_Protocols_Core
return FALSE;
}
/**
* Create a server join link based on the server information
*
* @return string
*/
protected function getJoinLink()
{
$link = '';
// We have a join_link defined
if(!empty($this->join_link))
{
// @todo: Make this smarter, not all games use $this->port as the client port
$link = sprintf($this->join_link, $this->ip, $this->port);
}
return $link;
}
}

View File

@ -17,14 +17,14 @@
*/
/**
* DayZ Mod Protocol Class
* DayZ Standalone Protocol Class
*
* @author Marcel Bößendörfer <m.boessendoerfer@marbis.net>
* Note that this is not DayZ Mod but a standalone game in Steam
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class GameQ_Protocols_Dayz extends GameQ_Protocols_Armedassault2
class GameQ_Protocols_Dayz extends GameQ_Protocols_Source
{
protected $name = "dayz";
protected $name_long = "DayZ Mod";
protected $port = 2302;
protected $name_long = "DayZ Standalone";
}

View File

@ -17,24 +17,13 @@
*/
/**
* Just Cause 2 Multiplayer Protocol Class
* DayZ Mod Protocol Class
*
* @author Ulrich Block <ulblock@gmx.de>
* @author Marcel Bößendörfer <m.boessendoerfer@marbis.net>
* @author Austin Bischoff <austin@codebeard.com>
*/
class GameQ_Protocols_Jcmp extends GameQ_Protocols_Source
class GameQ_Protocols_Dayzmod extends GameQ_Protocols_Armedassault2
{
protected $name = "jcmp";
protected $name_long = "Just Cause 2 Multiplayer";
// Source Query is not able to return larger player amounts. Map field is used for player return.
function process_details()
{
$return = parent::process_details();
@list($return['num_players'], $return['max_players']) = explode('/', str_replace('Players: ', '', $return['map']));
$return['map'] = $return['game_dir'];
return $return;
}
}
protected $name = "dayzmod";
protected $name_long = "DayZ Mod";
}

View File

@ -0,0 +1,49 @@
<?php
/**
* This file is part of GameQ.
*
* GameQ is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GameQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Just Cause 2 Multiplayer Protocol Class
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class GameQ_Protocols_Jc2 extends GameQ_Protocols_Source
{
protected $name = "jc2";
protected $name_long = "Just Cause 2 Multiplayer";
protected function process_details()
{
// Process the server details first
$results = parent::process_details();
// Now we need to fix the "map" for their hack
if(preg_match('/(?P<cur>\d{1,})\/(?P<max>\d{1,})/i', trim($results['map']), $m))
{
// Define the player counts
$results['num_players'] = $m['cur'];
$results['max_players'] = $m['max'];
// Reset map since we have no idea what it is
$results['map'] = '';
unset($m);
}
return $results;
}
}

View File

@ -0,0 +1,154 @@
<?php
/**
* This file is part of GameQ.
*
* GameQ is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GameQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Mafia 2 Multiplayer Protocol Class
*
* Loosely based on SAMP protocol
*
* Query port = server port + 1
*
* Thanks to rststeam for example protocol information
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class GameQ_Protocols_M2mp extends GameQ_Protocols
{
/**
* Array of packets we want to look up.
* Each key should correspond to a defined method in this or a parent class
*
* @var array
*/
protected $packets = array(
self::PACKET_ALL => "M2MP",
);
/**
* Methods to be run when processing the response(s)
*
* @var array
*/
protected $process_methods = array(
"process_all",
);
/**
* Default port for this server type
*
* @var int
*/
protected $port = 27016; // Default port, used if not set when instanced
/**
* The protocol being used
*
* @var string
*/
protected $protocol = 'm2mp';
/**
* String name of this protocol class
*
* @var string
*/
protected $name = 'm2mp';
/**
* Longer string name of this protocol class
*
* @var string
*/
protected $name_long = "Mafia 2 Multiplayer";
/*
* Internal methods
*/
/**
* Pre-process the server details data that was returned.
*
* @param array $packets
*/
protected function preProcess($packets)
{
// Make buffer so we can check this out
$buf = new GameQ_Buffer(implode('', $packets));
// Grab the header
$header = $buf->read(4);
// Now lets verify the header
if($header != "M2MP")
{
throw new GameQ_ProtocolsException('Unable to match M2MP response header. Header: '. $header);
return FALSE;
}
// Return the data with the header stripped, ready to go.
return $buf->getBuffer();
}
/**
* Process the server details
*
* @throws GameQ_ProtocolsException
*/
protected function process_all()
{
// Make sure we have a valid response
if(!$this->hasValidResponse(self::PACKET_ALL))
{
return array();
}
// Set the result to a new result instance
$result = new GameQ_Result();
// Always dedicated
$result->add('dedicated', TRUE);
// Preprocess and make buffer
$buf = new GameQ_Buffer($this->preProcess($this->packets_response[self::PACKET_ALL]));
// Pull out the server information
// Note the length information is incorrect, we correct using offset options in pascal method
$result->add('servername', $buf->readPascalString(1, TRUE));
$result->add('num_players', $buf->readPascalString(1, TRUE));
$result->add('max_players', $buf->readPascalString(1, TRUE));
$result->add('gamemode', $buf->readPascalString(1, TRUE));
$result->add('password', (bool) $buf->readInt8());
// Read the player info, it's in the same query response for some odd reason.
while($buf->getLength())
{
// Check to see if we ran out of info
if($buf->getLength() <= 1)
{
break;
}
// Only player information is available
$result->addPlayer('name', $buf->readPascalString(1, TRUE));
}
unset($buf);
return $result->fetch();
}
}

View File

@ -0,0 +1,30 @@
<?php
/**
* This file is part of GameQ.
*
* GameQ is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GameQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Rust Protocol Class
*
* Seems to respond to A2S but no rules, unsure if players is complete
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class GameQ_Protocols_Rust extends GameQ_Protocols_Source
{
protected $name = "rust";
protected $name_long = "Rust";
}

View File

@ -91,6 +91,8 @@ class GameQ_Protocols_Source extends GameQ_Protocols
*/
protected $source_engine = self::SOURCE_ENGINE;
protected $join_link = "steam://connect/%s:%d/";
/**
* Parse the challenge response and apply it to all the packet types
* that require it.
@ -200,15 +202,15 @@ class GameQ_Protocols_Source extends GameQ_Protocols
$result->add('dedicated', $buf->read());
$result->add('os', $buf->read());
$result->add('password', $buf->readInt8());
// Check engine type
if ($this->source_engine == self::GOLDSOURCE_ENGINE)
{
$result->add('ismod', $buf->readInt8());
}
$result->add('secure', $buf->readInt8());
// Check engine type
if ($this->source_engine == self::GOLDSOURCE_ENGINE)
{
@ -415,7 +417,7 @@ class GameQ_Protocols_Source extends GameQ_Protocols
// Now verify the length
if(strlen($result) != $packet_length)
{
throw new GameQ_ProtocolsException("Checksum for compressed packet failed! Length expected {$packet_length}, length returned".strlen($result));
throw new GameQ_ProtocolsException("Checksum for compressed packet failed! Length expected: {$packet_length}, length returned: ".strlen($result));
}
// Set the new packs

View File

@ -19,7 +19,7 @@
/**
* Starbound: Source Protocol Class
*
* @author Ulrich Block <ulblock@gmx.de>
* @author Ulrich Block <ulrich.block@easy-wi.com>
*/
class GameQ_Protocols_Starbound extends GameQ_Protocols_Source
{

View File

@ -123,6 +123,8 @@ class GameQ_Protocols_Teamspeak2 extends GameQ_Protocols
*/
protected $name_long = "Teamspeak 2";
protected $join_link = "teamspeak://%s:%d/";
/**
* We need to affect the packets we are sending before they are sent
*

View File

@ -123,6 +123,8 @@ class GameQ_Protocols_Teamspeak3 extends GameQ_Protocols
*/
protected $name_long = "Teamspeak 3";
protected $join_link = "ts3server://%s?port=%d";
/**
* Define the items being replaced to fix the return
*
@ -142,6 +144,26 @@ class GameQ_Protocols_Teamspeak3 extends GameQ_Protocols
"\\t" => "\t"
);
/**
* Overload so we can check for some special options
*
* @param string $ip
* @param int $port
* @param array $options
*/
public function __construct($ip = FALSE, $port = FALSE, $options = array())
{
// Got to do this first
parent::__construct($ip, $port, $options);
// Check for override in master server port (query)
if(isset($this->options['master_server_port']) && !empty($this->options['master_server_port']))
{
// Override the master server port
$this->master_server_port = (int) $this->options['master_server_port'];
}
}
/**
* We need to affect the packets we are sending before they are sent
*

View File

@ -111,6 +111,8 @@ class GameQ_Protocols_Ventrilo extends GameQ_Protocols
*/
protected $name_long = "Ventrilo";
protected $join_link = "ventrilo://%s:%d/";
/**
* Encryption table for the header
*

File diff suppressed because it is too large Load Diff