FIX #976 Update GameQ

This commit is contained in:
Ulrich Block 2017-04-02 12:31:30 +02:00
parent 8e2db42e7d
commit 242df62d7f
33 changed files with 1773 additions and 117 deletions

View File

@ -33,14 +33,14 @@ abstract class Base
*
* @type array
*/
protected $options = [ ];
protected $options = [];
/**
* Construct
*
* @param array $options
*/
public function __construct($options = [ ])
public function __construct($options = [])
{
$this->options = $options;

View File

@ -64,7 +64,7 @@ class Normalize extends Base
if (isset($result['players']) && count($result['players']) > 0) {
// Iterate
foreach ($result['players'] as $key => $player) {
$result['players'][ $key ] = array_merge($player, $this->check('player', $player));
$result['players'][$key] = array_merge($player, $this->check('player', $player));
}
} else {
$result['players'] = [];
@ -74,7 +74,7 @@ class Normalize extends Base
if (isset($result['teams']) && count($result['teams']) > 0) {
// Iterate
foreach ($result['teams'] as $key => $team) {
$result['teams'][ $key ] = array_merge($team, $this->check('team', $team));
$result['teams'][$key] = array_merge($team, $this->check('team', $team));
}
} else {
$result['teams'] = [];
@ -104,8 +104,8 @@ class Normalize extends Base
// Normalized return array
$normalized = [];
if (isset($this->normalize[ $section ]) && !empty($this->normalize[ $section ])) {
foreach ($this->normalize[ $section ] as $property => $raw) {
if (isset($this->normalize[$section]) && !empty($this->normalize[$section])) {
foreach ($this->normalize[$section] as $property => $raw) {
// Default the value for the new key as null
$value = null;
@ -113,18 +113,18 @@ class Normalize extends Base
// Iterate over the raw property we want to use
foreach ($raw as $check) {
if (array_key_exists($check, $data)) {
$value = $data[ $check ];
$value = $data[$check];
break;
}
}
} else {
// String
if (array_key_exists($raw, $data)) {
$value = $data[ $raw ];
$value = $data[$raw];
}
}
$normalized[ 'gq_' . $property ] = $value;
$normalized['gq_' . $property] = $value;
}
}

View File

@ -51,7 +51,6 @@ class Stripcolors extends Base
// Switch based on the base (not game) protocol
switch ($server->protocol()->getProtocol()) {
case 'quake2':
case 'quake3':
case 'doom3':

View File

@ -44,11 +44,6 @@ class GameQ
* Constants
*/
/**
* Current version
*/
const VERSION = '3.0.0-alpha2';
/* Static Section */
/**
@ -389,7 +384,6 @@ class GameQ
'server_id' => $server_id,
'socket' => $socket,
];
} catch (QueryException $e) {
// Check to see if we are in debug, if so bubble up the exception
if ($this->debug) {

View File

@ -0,0 +1,183 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
use GameQ\Buffer;
use GameQ\Result;
/**
* Class Armed Assault 3
*
* Rules protocol reference: https://community.bistudio.com/wiki/Arma_3_ServerBrowserProtocol2
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
* @author Memphis017 <https://github.com/Memphis017>
*/
class Arma3 extends Source
{
/**
* DLC ^2 constants
*/
const DLC_KARTS = 1,
DLC_MARKSMEN = 2,
DLC_HELICOPTERS = 4,
DLC_APEX = 16;
/**
* Defines the names for the specific game DLCs
*
* @var array
*/
protected $dlcNames = [
self::DLC_KARTS => 'Karts',
self::DLC_MARKSMEN => 'Marksmen',
self::DLC_HELICOPTERS => 'Helicopters',
self::DLC_APEX => 'Apex',
];
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'arma3';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Arma3";
/**
* Query port = client_port + 1
*
* @type int
*/
protected $port_diff = 1;
/**
* Process the rules since Arma3 changed their response for rules
*
* @param Buffer $buffer
*
* @return array
* @throws \GameQ\Exception\Protocol
*/
protected function processRules(Buffer $buffer)
{
// Total number of packets, burn it
$buffer->readInt16();
// Will hold the data string
$data = '';
// Loop until we run out of strings
while ($buffer->getLength()) {
// Burn the delimiters (i.e. \x01\x04\x00)
$buffer->readString();
// Add the data to the string, we are reassembling it
$data .= $buffer->readString();
}
// Restore escaped sequences
$data = str_replace(["\x01\x01", "\x01\x02", "\x01\x03"], ["\x01", "\x00", "\xFF"], $data);
// Make a new buffer with the reassembled data
$responseBuffer = new Buffer($data);
// Kill the old buffer, should be empty
unset($buffer, $data);
// Set the result to a new result instance
$result = new Result();
// Get results
$result->add('rules_protocol_version', $responseBuffer->readInt8());
$result->add('overflow', $responseBuffer->readInt8());
$dlcBit = $responseBuffer->readInt8(); // Grab DLC bit and use it later
$responseBuffer->skip(); // Reserved, burn it
// Grab difficulty so we can man handle it...
$difficulty = $responseBuffer->readInt8();
// Process difficulty
$result->add('3rd_person', $difficulty >> 7);
$result->add('advanced_flight_mode', ($difficulty >> 6) & 1);
$result->add('difficulty_ai', ($difficulty >> 3) & 3);
$result->add('difficulty_level', $difficulty & 3);
unset($difficulty);
// Crosshair
$result->add('crosshair', $responseBuffer->readInt8());
// Next are the dlc bits so loop over the dlcBit so we can determine which DLC's are running
for ($x = 1; $x <= $dlcBit; $x *= 2) {
// Enabled, add it to the list
if ($x & $dlcBit) {
$result->addSub('dlcs', 'name', $this->dlcNames[$x]);
$result->addSub('dlcs', 'hash', dechex($responseBuffer->readInt32()));
}
}
// No longer needed
unset($dlcBit);
// Grab the mod count
$modCount = $responseBuffer->readInt8();
// Add mod count
$result->add('mod_count', $modCount);
// Loop the mod count and add them
for ($x = 0; $x < $modCount; $x++) {
// Add the mod to the list
$result->addSub('mods', 'hash', dechex($responseBuffer->readInt32()));
$result->addSub('mods', 'steam_id', hexdec($responseBuffer->readPascalString(0, true)));
$result->addSub('mods', 'name', $responseBuffer->readPascalString(0, true));
}
unset($modCount, $x);
// Burn the signatures count, we will just loop until we run out of strings
$responseBuffer->read();
// Make signatures array
$signatures = [];
// Loop until we run out of strings
while ($responseBuffer->getLength()) {
//$result->addSub('signatures', 0, $responseBuffer->readPascalString(0, true));
$signatures[] = $responseBuffer->readPascalString(0, true);
}
// Add as a simple array
$result->add('signatures', $signatures);
// Add signatures count
$result->add('signature_count', count($signatures));
unset($responseBuffer, $signatures);
return $result->fetch();
}
}

View File

@ -19,31 +19,14 @@
namespace GameQ\Protocols;
/**
* Class Armed Assault 3
* Armed assault 3 dummy Protocol Class
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
* Added for backward compatibility, please update to class arma3
*
* @deprecated v3.0.10
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
class Armedassault3 extends Source
class Armedassault3 extends Arma3
{
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'armedassault3';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Armed Assault 3";
/**
* Query port = client_port + 1
*
* @type int
*/
protected $port_diff = 1;
}

View File

@ -0,0 +1,88 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
/**
* Class Battlefield 1942
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
class Bf1942 extends Gamespy
{
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'bf1942';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Battlefield 1942";
/**
* query_port = client_port + 8433
* 23000 = 14567 + 8433
*
* @type int
*/
protected $port_diff = 8433;
/**
* The client join link
*
* @type string
*/
protected $join_link = "bf1942://%s:%d";
/**
* Normalize settings for this protocol
*
* @type array
*/
protected $normalize = [
// General
'general' => [
// target => source
'dedicated' => 'dedicated',
'gametype' => 'gametype',
'hostname' => 'hostname',
'mapname' => 'mapname',
'maxplayers' => 'maxplayers',
'numplayers' => 'numplayers',
'password' => 'password',
],
// Individual
'player' => [
'name' => 'playername',
'kills' => 'kills',
'deaths' => 'deaths',
'ping' => 'ping',
'score' => 'score',
],
'team' => [
'name' => 'teamname',
],
];
}

View File

@ -18,11 +18,8 @@
namespace GameQ\Protocols;
use GameQ\Protocol;
use GameQ\Buffer;
/**
* Battlefield 2 Protocol Class
* Class Battlefield 2
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
@ -30,15 +27,6 @@ use GameQ\Buffer;
class Bf2 extends Gamespy3
{
/**
* Array of packets we want to query.
*
* @type array
*/
protected $packets = [
self::PACKET_ALL => "\xFE\xFD\x00\x10\x20\x30\x40\xFF\xFF\xFF\x01",
];
/**
* String name of this protocol class
*
@ -52,4 +40,59 @@ class Bf2 extends Gamespy3
* @type string
*/
protected $name_long = "Battlefield 2";
}
/**
* query_port = client_port + 8433
* 29900 = 16567 + 13333
*
* @type int
*/
protected $port_diff = 13333;
/**
* The client join link
*
* @type string
*/
protected $join_link = "bf2://%s:%d";
/**
* BF2 has a different query packet to send than "normal" Gamespy 3
*
* @var array
*/
protected $packets = [
self::PACKET_ALL => "\xFE\xFD\x00\x10\x20\x30\x40\xFF\xFF\xFF\x01",
];
/**
* Normalize settings for this protocol
*
* @type array
*/
protected $normalize = [
// General
'general' => [
// target => source
'dedicated' => 'dedicated',
'gametype' => 'gametype',
'hostname' => 'hostname',
'mapname' => 'mapname',
'maxplayers' => 'maxplayers',
'numplayers' => 'numplayers',
'password' => 'password',
],
// Individual
'player' => [
'name' => 'player',
'kills' => 'score',
'deaths' => 'deaths',
'ping' => 'ping',
'score' => 'score',
],
'team' => [
'name' => 'team',
'score' => 'score',
],
];
}

View File

@ -111,6 +111,7 @@ class Bf3 extends Protocol
// target => source
'dedicated' => 'dedicated',
'hostname' => 'hostname',
'mapname' => 'map',
'maxplayers' => 'max_players',
'numplayers' => 'num_players',
'password' => 'password',
@ -118,7 +119,7 @@ class Bf3 extends Protocol
'player' => [
'name' => 'name',
'score' => 'score',
'ping' => 'ping',
'ping' => 'ping',
],
'team' => [
'score' => 'tickets',
@ -135,10 +136,10 @@ class Bf3 extends Protocol
{
// Holds the results sent back
$results = [ ];
$results = [];
// Holds the processed packets after having been reassembled
$processed = [ ];
$processed = [];
// Start up the index for the processed
$sequence_id_last = 0;
@ -182,7 +183,7 @@ class Bf3 extends Protocol
// Now we need to call the proper method
$results = array_merge(
$results,
call_user_func_array([ $this, $this->responses[$sequence_id] ], [ $buffer ])
call_user_func_array([$this, $this->responses[$sequence_id]], [$buffer])
);
}
@ -203,7 +204,7 @@ class Bf3 extends Protocol
protected function decode(Buffer $buffer)
{
$items = [ ];
$items = [];
// Get the number of words in this buffer
$itemCount = $buffer->readInt32();
@ -241,13 +242,13 @@ class Bf3 extends Protocol
// These are the same no matter what mode the server is in
$result->add('hostname', $items[1]);
$result->add('num_players', (int) $items[2]);
$result->add('max_players', (int) $items[3]);
$result->add('num_players', (int)$items[2]);
$result->add('max_players', (int)$items[3]);
$result->add('gametype', $items[4]);
$result->add('map', $items[5]);
$result->add('roundsplayed', (int) $items[6]);
$result->add('roundstotal', (int) $items[7]);
$result->add('num_teams', (int) $items[8]);
$result->add('roundsplayed', (int)$items[6]);
$result->add('roundstotal', (int)$items[7]);
$result->add('num_teams', (int)$items[8]);
// Set the current index
$index_current = 9;
@ -264,22 +265,22 @@ class Bf3 extends Protocol
}
// Get and set the rest of the data points.
$result->add('targetscore', (int) $items[$index_current]);
$result->add('targetscore', (int)$items[$index_current]);
$result->add('online', 1); // Forced true, it seems $words[$index_current + 1] is always empty
$result->add('ranked', (int) $items[$index_current + 2]);
$result->add('punkbuster', (int) $items[$index_current + 3]);
$result->add('password', (int) $items[$index_current + 4]);
$result->add('uptime', (int) $items[$index_current + 5]);
$result->add('roundtime', (int) $items[$index_current + 6]);
$result->add('ranked', (int)$items[$index_current + 2]);
$result->add('punkbuster', (int)$items[$index_current + 3]);
$result->add('password', (int)$items[$index_current + 4]);
$result->add('uptime', (int)$items[$index_current + 5]);
$result->add('roundtime', (int)$items[$index_current + 6]);
// Added in R9
$result->add('ip_port', $items[$index_current + 7]);
$result->add('punkbuster_version', $items[$index_current + 8]);
$result->add('join_queue', (int) $items[$index_current + 9]);
$result->add('join_queue', (int)$items[$index_current + 9]);
$result->add('region', $items[$index_current + 10]);
$result->add('pingsite', $items[$index_current + 11]);
$result->add('country', $items[$index_current + 12]);
// Added in R29, No docs as of yet
$result->add('quickmatch', (int) $items[$index_current + 13]); // Guessed from research
$result->add('quickmatch', (int)$items[$index_current + 13]); // Guessed from research
unset($items, $index_current, $teamCount, $buffer);

View File

@ -0,0 +1,326 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
use GameQ\Protocol;
use GameQ\Buffer;
use GameQ\Result;
use GameQ\Exception\Protocol as Exception;
/**
* Battlefield Bad Company 2 Protocol Class
*
* NOTE: There are no qualifiers to the response packets sent back from the server as to which response packet
* belongs to which query request. For now this class assumes the responses are in the same order as the order in
* which the packets were sent to the server. If this assumption turns out to be wrong there is easy way to tell which
* response belongs to which query. Hopefully this assumption will hold true as it has in my testing.
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
class Bfbc2 extends Protocol
{
/**
* Array of packets we want to query.
*
* @type array
*/
protected $packets = [
self::PACKET_VERSION => "\x00\x00\x00\x00\x18\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00version\x00",
self::PACKET_STATUS => "\x00\x00\x00\x00\x1b\x00\x00\x00\x01\x00\x00\x00\x0a\x00\x00\x00serverInfo\x00",
self::PACKET_PLAYERS => "\x00\x00\x00\x00\x24\x00\x00\x00\x02\x00\x00\x00\x0b\x00\x00\x00listPlayers\x00\x03\x00\x00\x00\x61ll\x00",
];
/**
* Use the response flag to figure out what method to run
*
* @type array
*/
protected $responses = [
"processVersion",
"processDetails",
"processPlayers",
];
/**
* The transport mode for this protocol is TCP
*
* @type string
*/
protected $transport = self::TRANSPORT_TCP;
/**
* The query protocol used to make the call
*
* @type string
*/
protected $protocol = 'bfbc2';
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'bfbc2';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Battlefield Bad Company 2";
/**
* The client join link
*
* @type string
*/
protected $join_link = null;
/**
* query_port = client_port + 29321
* 48888 = 19567 + 29321
*
* @type int
*/
protected $port_diff = 29321;
/**
* Normalize settings for this protocol
*
* @type array
*/
protected $normalize = [
// General
'general' => [
// target => source
'dedicated' => 'dedicated',
'hostname' => 'hostname',
'mapname' => 'map',
'maxplayers' => 'max_players',
'numplayers' => 'num_players',
'password' => 'password',
],
'player' => [
'name' => 'name',
'score' => 'score',
'ping' => 'ping',
],
'team' => [
'score' => 'tickets',
],
];
/**
* Process the response for the StarMade server
*
* @return array
* @throws \GameQ\Exception\Protocol
*/
public function processResponse()
{
//print_r($this->packets_response);
// Holds the results sent back
$results = [];
// Iterate over the response packets
// @todo: This protocol has no packet ordering, ids or anyway to identify which packet coming back belongs to which initial call.
foreach ($this->packets_response as $i => $packet) {
// Create a new buffer
$buffer = new Buffer($packet);
// Burn first 4 bytes, same across all packets
$buffer->skip(4);
// Get the packet length
$packetLength = $buffer->getLength();
// Check to make sure the expected length matches the real length
// Subtract 4 for the header burn
if ($packetLength != ($buffer->readInt32() - 4)) {
throw new Exception(__METHOD__ . " packet length does not match expected length!");
}
// We assume the packets are coming back in the same order as sent, this maybe incorrect...
$results = array_merge(
$results,
call_user_func_array([$this, $this->responses[$i]], [$buffer])
);
}
unset($buffer, $packetLength);
return $results;
}
/*
* Internal Methods
*/
/**
* Decode the buffer into a usable format
*
* @param \GameQ\Buffer $buffer
*
* @return array
*/
protected function decode(Buffer $buffer)
{
$items = [];
// Get the number of words in this buffer
$itemCount = $buffer->readInt32();
// Loop over the number of items
for ($i = 0; $i < $itemCount; $i++) {
// Length of the string
$buffer->readInt32();
// Just read the string
$items[$i] = $buffer->readString();
}
return $items;
}
/**
* Process the server details
*
* @param \GameQ\Buffer $buffer
*
* @return array
*/
protected function processDetails(Buffer $buffer)
{
// Decode into items
$items = $this->decode($buffer);
// Set the result to a new result instance
$result = new Result();
// Server is always dedicated
$result->add('dedicated', 1);
// These are the same no matter what mode the server is in
$result->add('hostname', $items[1]);
$result->add('num_players', (int)$items[2]);
$result->add('max_players', (int)$items[3]);
$result->add('gametype', $items[4]);
$result->add('map', $items[5]);
$result->add('roundsplayed', (int)$items[6]);
$result->add('roundstotal', (int)$items[7]);
$result->add('num_teams', (int)$items[8]);
// Set the current index
$index_current = 9;
// Pull the team count
$teamCount = $result->get('num_teams');
// Loop for the number of teams found, increment along the way
for ($id = 1; $id <= $teamCount; $id++, $index_current++) {
// Shows the tickets
$result->addTeam('tickets', $items[$index_current]);
// We add an id so we know which team this is
$result->addTeam('id', $id);
}
// Get and set the rest of the data points.
$result->add('targetscore', (int)$items[$index_current]);
$result->add('online', 1); // Forced true, shows accepting players
$result->add('ranked', (($items[$index_current + 2] == 'true') ? 1 : 0));
$result->add('punkbuster', (($items[$index_current + 3] == 'true') ? 1 : 0));
$result->add('password', (($items[$index_current + 4] == 'true') ? 1 : 0));
$result->add('uptime', (int)$items[$index_current + 5]);
$result->add('roundtime', (int)$items[$index_current + 6]);
$result->add('mod', $items[$index_current + 7]);
$result->add('ip_port', $items[$index_current + 9]);
$result->add('punkbuster_version', $items[$index_current + 10]);
$result->add('join_queue', (($items[$index_current + 11] == 'true') ? 1 : 0));
$result->add('region', $items[$index_current + 12]);
unset($items, $index_current, $teamCount, $buffer);
return $result->fetch();
}
/**
* Process the server version
*
* @param \GameQ\Buffer $buffer
*
* @return array
*/
protected function processVersion(Buffer $buffer)
{
// Decode into items
$items = $this->decode($buffer);
// Set the result to a new result instance
$result = new Result();
$result->add('version', $items[2]);
unset($buffer, $items);
return $result->fetch();
}
/**
* Process the players
*
* @param \GameQ\Buffer $buffer
*
* @return array
*/
protected function processPlayers(Buffer $buffer)
{
// Decode into items
$items = $this->decode($buffer);
// Set the result to a new result instance
$result = new Result();
// Number of data points per player
$numTags = $items[1];
// Grab the tags for each player
$tags = array_slice($items, 2, $numTags);
// Get the player count
$playerCount = $items[$numTags + 2];
// Iterate over the index until we run out of players
for ($i = 0, $x = $numTags + 3; $i < $playerCount; $i++, $x += $numTags) {
// Loop over the player tags and extract the info for that tag
foreach ($tags as $index => $tag) {
$result->addPlayer($tag, $items[($x + $index)]);
}
}
return $result->fetch();
}
}

View File

@ -0,0 +1,50 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
/**
* Class Codmw3
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
class Codmw3 extends Source
{
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'codmw3';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Call of Duty: Modern Warfare 3";
/**
* query_port = client_port + 2
*
* @type int
*/
protected $port_diff = 2;
}

View File

@ -51,7 +51,7 @@ class Cs16 extends Source
* @return string
* @throws \GameQ\Exception\Protocol
*/
protected function processPackets($packet_id, array $packets = [ ])
protected function processPackets($packet_id, array $packets = [])
{
// The response is gold source if the packets are split

View File

@ -0,0 +1,43 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
/**
* Class Ffe - Fortress Forever
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
class Ffe extends Source
{
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'ffe';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Fortress Forever";
}

View File

@ -0,0 +1,243 @@
<?php
namespace GameQ\Protocols;
use GameQ\Protocol;
use GameQ\Buffer;
use GameQ\Result;
use GameQ\Exception\Protocol as Exception;
/**
* Frontlines Fuel of War Protocol Class
*
* Handles processing ffow servers
*
* Class is incomplete due to lack of players to test against.
* http://wiki.hlsw.net/index.php/FFOW_Protocol
*
* @package GameQ\Protocols
*/
class Ffow extends Protocol
{
/**
* Array of packets we want to look up.
* Each key should correspond to a defined method in this or a parent class
*
* @type array
*/
protected $packets = [
self::PACKET_CHALLENGE => "\xFF\xFF\xFF\xFF\x57",
self::PACKET_RULES => "\xFF\xFF\xFF\xFF\x56%s",
self::PACKET_PLAYERS => "\xFF\xFF\xFF\xFF\x55%s",
self::PACKET_INFO => "\xFF\xFF\xFF\xFF\x46\x4C\x53\x51",
];
/**
* Use the response flag to figure out what method to run
*
* @type array
*/
protected $responses = [
"\xFF\xFF\xFF\xFF\x49\x02" => 'processInfo', // I
"\xFF\xFF\xFF\xFF\x45\x00" => 'processRules', // E
"\xFF\xFF\xFF\xFF\x44\x00" => 'processPlayers', // D
];
/**
* The query protocol used to make the call
*
* @type string
*/
protected $protocol = 'ffow';
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'ffow';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Frontlines Fuel of War";
/**
* The client join link
*
* @type string
*/
protected $join_link = null;
/**
* query_port = client_port + 2
*
* @type int
*/
protected $port_diff = 2;
/**
* Normalize settings for this protocol
*
* @type array
*/
protected $normalize = [
// General
'general' => [
// target => source
'gametype' => 'gamemode',
'hostname' => 'servername',
'mapname' => 'mapname',
'maxplayers' => 'max_players',
'mod' => 'modname',
'numplayers' => 'num_players',
'password' => 'password',
],
// Individual
'player' => [
'name' => 'name',
'ping' => 'ping',
'score' => 'frags',
],
];
/**
* Parse the challenge response and apply it to all the packet types
*
* @param \GameQ\Buffer $challenge_buffer
*
* @return bool
* @throws \GameQ\Exception\Protocol
*/
public function challengeParseAndApply(Buffer $challenge_buffer)
{
// Burn padding
$challenge_buffer->skip(5);
// Apply the challenge and return
return $this->challengeApply($challenge_buffer->read(4));
}
/**
* Handle response from the server
*
* @return mixed
* @throws Exception
*/
public function processResponse()
{
// Init results
$results = [];
foreach ($this->packets_response as $response) {
$buffer = new Buffer($response);
// Figure out what packet response this is for
$response_type = $buffer->read(6);
// Figure out which packet response this is
if (!array_key_exists($response_type, $this->responses)) {
throw new Exception(__METHOD__ . " response type '" . bin2hex($response_type) . "' is not valid");
}
// Now we need to call the proper method
$results = array_merge(
$results,
call_user_func_array([$this, $this->responses[$response_type]], [$buffer])
);
unset($buffer);
}
return $results;
}
/**
* Handle processing the server information
*
* @param Buffer $buffer
*
* @return array
*/
protected function processInfo(Buffer $buffer)
{
// Set the result to a new result instance
$result = new Result();
$result->add('servername', $buffer->readString());
$result->add('mapname', $buffer->readString());
$result->add('modname', $buffer->readString());
$result->add('gamemode', $buffer->readString());
$result->add('description', $buffer->readString());
$result->add('version', $buffer->readString());
$result->add('port', $buffer->readInt16());
$result->add('num_players', $buffer->readInt8());
$result->add('max_players', $buffer->readInt8());
$result->add('dedicated', $buffer->readInt8());
$result->add('os', $buffer->readInt8());
$result->add('password', $buffer->readInt8());
$result->add('anticheat', $buffer->readInt8());
$result->add('average_fps', $buffer->readInt8());
$result->add('round', $buffer->readInt8());
$result->add('max_rounds', $buffer->readInt8());
$result->add('time_left', $buffer->readInt16());
unset($buffer);
return $result->fetch();
}
/**
* Handle processing the server rules
*
* @param Buffer $buffer
*
* @return array
*/
protected function processRules(Buffer $buffer)
{
// Set the result to a new result instance
$result = new Result();
// Burn extra header
$buffer->skip(1);
// Read rules until we run out of buffer
while ($buffer->getLength()) {
$key = $buffer->readString();
// Check for map
if (strstr($key, "Map:")) {
$result->addSub("maplist", "name", $buffer->readString());
} else // Regular rule
{
$result->add($key, $buffer->readString());
}
}
unset($buffer);
return $result->fetch();
}
/**
* Handle processing of player data
*
* @todo: Build this out when there is a server with players to test against
*
* @param Buffer $buffer
*
* @return array
*/
protected function processPlayers(Buffer $buffer)
{
// Set the result to a new result instance
$result = new Result();
unset($buffer);
return $result->fetch();
}
}

View File

@ -107,7 +107,7 @@ class Gamespy3 extends Protocol
{
// Holds the processed packets
$processed = [ ];
$processed = [];
// Iterate over the packets
foreach ($this->packets_response as $response) {
@ -141,8 +141,14 @@ class Gamespy3 extends Protocol
// Offload cleaning up the packets if they happen to be split
$packets = $this->cleanPackets(array_values($processed));
/*
* Fix: when server name contains string "\u0000" - query fails. "\u0000" also separates properties from
* server, so we are replacing double "\u0000" in server response.
*/
$packets = preg_replace("/(\\x00){2,}gametype/", "\x00gametype", implode('', $packets));
// Create a new buffer
$buffer = new Buffer(implode('', $packets), Buffer::NUMBER_TYPE_BIGENDIAN);
$buffer = new Buffer($packets, Buffer::NUMBER_TYPE_BIGENDIAN);
// Create a new result
$result = new Result();
@ -169,7 +175,7 @@ class Gamespy3 extends Protocol
*
* @return array
*/
protected function cleanPackets(array $packets = [ ])
protected function cleanPackets(array $packets = [])
{
// Get the number of packets
@ -289,7 +295,7 @@ class Gamespy3 extends Protocol
// Set the item group
$item_group = 'teams';
// Set the item type, rip off any trailing stuff and bad chars
$item_type = rtrim(str_replace([ "\x00", "\x02" ], '', $item), '_t');
$item_type = rtrim(str_replace(["\x00", "\x02"], '', $item), '_t');
} else {
// We can assume it is data belonging to a previously defined item

View File

@ -0,0 +1,67 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
use GameQ\Protocol;
/**
* Class Http
*
* Generic HTTP protocol class. Useful for making http based requests
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
abstract class Http extends Protocol
{
/**
* The query protocol used to make the call
*
* @type string
*/
protected $protocol = 'http';
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'http';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Generic HTTP protocol";
/**
* Http protocol is TCP
*
* @var string
*/
protected $transport = self::TRANSPORT_TCP;
/**
* The client join link
*
* @type string
*/
protected $join_link = null;
}

View File

@ -108,7 +108,7 @@ class Mumble extends Protocol
],
// Team
'team' => [
'name' => 'name',
'name' => 'name',
],
];
@ -135,7 +135,7 @@ class Mumble extends Protocol
// Let's iterate over the response items, there are a lot
foreach ($data as $key => $value) {
// Ignore root for now, that is where all of the channel/player info is housed
if (in_array($key, [ 'root' ])) {
if (in_array($key, ['root'])) {
continue;
}
@ -170,7 +170,7 @@ class Mumble extends Protocol
// Let's add all of the channel information
foreach ($data as $key => $value) {
// We will handle these later
if (in_array($key, [ 'channels', 'users' ])) {
if (in_array($key, ['channels', 'users'])) {
// skip
continue;
}

View File

@ -0,0 +1,45 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
/**
* Class Projectrealitybf2
*
* Based off of BF2
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
class Projectrealitybf2 extends Bf2
{
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'projectrealitybf2';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Project Reality: Battlefield 2";
}

View File

@ -0,0 +1,219 @@
<?php
namespace GameQ\Protocols;
use GameQ\Protocol;
use GameQ\Buffer;
use GameQ\Result;
use GameQ\Exception\Protocol as Exception;
/**
* Quake2 Protocol Class
*
* Handles processing Quake 3 servers
*
* @package GameQ\Protocols
*/
class Quake2 extends Protocol
{
/**
* Array of packets we want to look up.
* Each key should correspond to a defined method in this or a parent class
*
* @type array
*/
protected $packets = [
self::PACKET_STATUS => "\xFF\xFF\xFF\xFFstatus\x00",
];
/**
* Use the response flag to figure out what method to run
*
* @type array
*/
protected $responses = [
"\xFF\xFF\xFF\xFF\x70\x72\x69\x6e\x74" => 'processStatus',
];
/**
* The query protocol used to make the call
*
* @type string
*/
protected $protocol = 'quake2';
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'quake2';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Quake 2 Server";
/**
* The client join link
*
* @type string
*/
protected $join_link = null;
/**
* Normalize settings for this protocol
*
* @type array
*/
protected $normalize = [
// General
'general' => [
// target => source
'gametype' => 'gamename',
'hostname' => 'hostname',
'mapname' => 'mapname',
'maxplayers' => 'maxclients',
'mod' => 'g_gametype',
'numplayers' => 'clients',
'password' => 'password',
],
// Individual
'player' => [
'name' => 'name',
'ping' => 'ping',
'score' => 'frags',
],
];
/**
* Handle response from the server
*
* @return mixed
* @throws Exception
*/
public function processResponse()
{
// Make a buffer
$buffer = new Buffer(implode('', $this->packets_response));
// Grab the header
$header = $buffer->readString("\x0A");
// Figure out which packet response this is
if (empty($header) || !array_key_exists($header, $this->responses)) {
throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid");
}
return call_user_func_array([$this, $this->responses[$header]], [$buffer]);
}
/**
* Process the status response
*
* @param Buffer $buffer
*
* @return array
*/
protected function processStatus(Buffer $buffer)
{
// We need to split the data and offload
$results = $this->processServerInfo(new Buffer($buffer->readString("\x0A")));
$results = array_merge_recursive(
$results,
$this->processPlayers(new Buffer($buffer->getBuffer()))
);
unset($buffer);
// Return results
return $results;
}
/**
* Handle processing the server information
*
* @param Buffer $buffer
*
* @return array
*/
protected function processServerInfo(Buffer $buffer)
{
// Set the result to a new result instance
$result = new Result();
// Burn leading \ if one exists
$buffer->readString('\\');
// Key / value pairs
while ($buffer->getLength()) {
// Add result
$result->add(
trim($buffer->readString('\\')),
utf8_encode(trim($buffer->readStringMulti(['\\', "\x0a"])))
);
}
$result->add('password', 0);
$result->add('mod', 0);
unset($buffer);
return $result->fetch();
}
/**
* Handle processing of player data
*
* @param Buffer $buffer
*
* @return array
*/
protected function processPlayers(Buffer $buffer)
{
// Some games do not have a number of current players
$playerCount = 0;
// Set the result to a new result instance
$result = new Result();
// Loop until we are out of data
while ($buffer->getLength()) {
// Make a new buffer with this block
$playerInfo = new Buffer($buffer->readString("\x0A"));
// Add player info
$result->addPlayer('frags', $playerInfo->readString("\x20"));
$result->addPlayer('ping', $playerInfo->readString("\x20"));
// Skip first "
$playerInfo->skip(1);
// Add player name, encoded
$result->addPlayer('name', utf8_encode(trim(($playerInfo->readString('"')))));
// Skip first "
$playerInfo->skip(2);
// Add address
$result->addPlayer('address', trim($playerInfo->readString('"')));
// Increment
$playerCount++;
// Clear
unset($playerInfo);
}
$result->add('clients', $playerCount);
// Clear
unset($buffer, $playerCount);
return $result->fetch();
}
}

View File

@ -104,11 +104,11 @@ class Quake3 extends Protocol
$header = $buffer->readString("\x0A");
// Figure out which packet response this is
if (!array_key_exists($header, $this->responses)) {
if (empty($header) || !array_key_exists($header, $this->responses)) {
throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid");
}
return call_user_func_array([$this, $this->responses[ $header ]], [$buffer]);
return call_user_func_array([$this, $this->responses[$header]], [$buffer]);
}
protected function processStatus(Buffer $buffer)

View File

@ -0,0 +1,42 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
/**
* Class Quake Live
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
class Quakelive extends Source
{
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'quakelive';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Quake Live";
}

View File

@ -201,7 +201,7 @@ class Samp extends Protocol
$result->add('dedicated', 1);
// Pull out the server information
$result->add('password', (bool) $buffer->readInt8());
$result->add('password', $buffer->readInt8());
$result->add('num_players', $buffer->readInt16());
$result->add('max_players', $buffer->readInt16());

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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
/**
* Class 7 Days to Die
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
class Sevendaystodie extends Source
{
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'sevendaystodie';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "7 Days to Die";
/**
* query_port = client_port + 1
*
* @type int
*/
protected $port_diff = 1;
}

View File

@ -18,10 +18,10 @@
namespace GameQ\Protocols;
use GameQ\Protocol;
use GameQ\Buffer;
use GameQ\Result;
use GameQ\Exception\Protocol as Exception;
use GameQ\Protocol;
use GameQ\Result;
/**
* Valve Source Engine Protocol Class (A2S)
@ -155,9 +155,9 @@ class Source extends Protocol
public function processResponse()
{
$results = [ ];
$results = [];
$packets = [ ];
$packets = [];
// We need to pre-sort these for split packets so we can do extra work where needed
foreach ($this->packets_response as $response) {
@ -206,7 +206,7 @@ class Source extends Protocol
// Now we need to call the proper method
$results = array_merge(
$results,
call_user_func_array([ $this, $this->responses[$response_type] ], [ $buffer ])
call_user_func_array([$this, $this->responses[$response_type]], [$buffer])
);
unset($buffer);
@ -232,11 +232,11 @@ class Source extends Protocol
* @return string
* @throws \GameQ\Exception\Protocol
*/
protected function processPackets($packet_id, array $packets = [ ])
protected function processPackets($packet_id, array $packets = [])
{
// Init array so we can order
$packs = [ ];
$packs = [];
// We have multiple packets so we need to get them and order them
foreach ($packets as $i => $packet) {
@ -267,16 +267,18 @@ class Source extends Protocol
if ($packet_id & 0x80000000) {
// Check to see if we have Bzip2 installed
if (!function_exists('bzdecompress')) {
// @codeCoverageIgnoreStart
throw new Exception(
'Bzip2 is not installed. See http://www.php.net/manual/en/book.bzip2.php for more info.',
0
);
// @codeCoverageIgnoreEnd
}
// Get the length of the packet (long)
$packet_length = $buffer->readInt32Signed();
// Checksum for the decompressed packet (long)
// Checksum for the decompressed packet (long), burn it - doesnt work in split responses
$buffer->readInt32Signed();
// Try to decompress
@ -284,13 +286,20 @@ class Source extends Protocol
// Now verify the length
if (strlen($result) != $packet_length) {
// @codeCoverageIgnoreStart
throw new Exception(
"Checksum for compressed packet failed! Length expected: {$packet_length}, length
returned: " . strlen($result)
);
// @codeCoverageIgnoreEnd
}
// We need to burn the extra header (\xFF\xFF\xFF\xFF) on first loop
if ($i == 0) {
$result = substr($result, 4);
}
} else {
// Get the packet length (short)
// Get the packet length (short), burn it
$buffer->readInt16Signed();
// We need to burn the extra header (\xFF\xFF\xFF\xFF) on first loop

View File

@ -0,0 +1,53 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
/**
* Class Squad
*
* Port reference: http://forums.joinsquad.com/topic/9559-query-ports/
*
* @package GameQ\Protocols
* @author Austin Bischoff <austin@codebeard.com>
*/
class Squad extends Source
{
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'squad';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Squad";
/**
* query_port = client_port + 19378
* 27165 = 7787 + 19378
*
* @type int
*/
protected $port_diff = 19378;
}

View File

@ -162,7 +162,7 @@ class Starmade extends Protocol
{
// Init the parsed data array
$parsed = [ ];
$parsed = [];
// Read the number of parameters to parse
$parameterSize = $buffer->readInt32Signed();
@ -201,7 +201,7 @@ class Starmade extends Protocol
// Boolean
case 5:
$parsed[$i] = (bool) $buffer->readInt8Signed();
$parsed[$i] = (bool)$buffer->readInt8Signed();
break;
// 8-bit int

View File

@ -220,7 +220,7 @@ class Teamspeak3 extends Protocol
{
// Will hold the properties we are sending back
$properties = [ ];
$properties = [];
// All of these are split on space
$items = explode(' ', $data);

View File

@ -0,0 +1,59 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
/**
* Class Terraria
*
* @package GameQ\Protocols
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class Terraria extends Tshock
{
/**
* String name of this protocol class
*
* @type string
*/
protected $name = 'terraria';
/**
* Longer string name of this protocol class
*
* @type string
*/
protected $name_long = "Terraria";
/**
* query_port = client_port + 101
* 7878 = 7777 + 101
*
* @type int
*/
protected $port_diff = 101;
/**
* The client join link
*
* @type string
*/
protected $join_link = "steam://connect/%s:%d/";
}

View File

@ -0,0 +1,153 @@
<?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 Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
use GameQ\Exception\Protocol as Exception;
use GameQ\Result;
/**
* Tshock Protocol Class
*
* Result from this call should be a header + JSON response
*
* References:
* - https://tshock.atlassian.net/wiki/display/TSHOCKPLUGINS/REST+API+Endpoints#RESTAPIEndpoints-/status
* - http://tshock.co/xf/index.php?threads/rest-tshock-server-status-image.430/
*
* Special thanks to intradox and Ruok2bu for game & protocol references
*
* @author Austin Bischoff <austin@codebeard.com>
*/
class Tshock extends Http
{
/**
* Packets to send
*
* @var array
*/
protected $packets = [
self::PACKET_STATUS => "GET /v2/server/status?players=true&rules=true HTTP/1.0\r\nAccept: */*\r\n\r\n",
];
/**
* The protocol being used
*
* @var string
*/
protected $protocol = 'tshock';
/**
* String name of this protocol class
*
* @var string
*/
protected $name = 'tshock';
/**
* Longer string name of this protocol class
*
* @var string
*/
protected $name_long = "Tshock";
/**
* Normalize some items
*
* @var array
*/
protected $normalize = [
// General
'general' => [
// target => source
'dedicated' => 'dedicated',
'hostname' => 'hostname',
'mapname' => 'world',
'maxplayers' => 'maxplayers',
'numplayers' => 'numplayers',
'password' => 'password',
],
// Individual
'player' => [
'name' => 'nickname',
'team' => 'team',
],
];
/**
* Process the response
*
* @return array
* @throws Exception
*/
public function processResponse()
{
// Implode and rip out the JSON
preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
// Return should be JSON, let's validate
if (($json = json_decode($matches[0])) === null) {
throw new Exception("JSON response from Tshock protocol is invalid.");
}
// Check the status response
if ($json->status != 200) {
throw new Exception("JSON status from Tshock protocol response was '{$json->status}', expected '200'.");
}
$result = new Result();
// Server is always dedicated
$result->add('dedicated', 1);
// Add server items
$result->add('hostname', $json->name);
$result->add('game_port', $json->port);
$result->add('serverversion', $json->serverversion);
$result->add('world', $json->world);
$result->add('uptime', $json->uptime);
$result->add('password', (int)$json->serverpassword);
$result->add('numplayers', $json->playercount);
$result->add('maxplayers', $json->maxplayers);
// Parse players
foreach ($json->players as $player) {
$result->addPlayer('nickname', $player->nickname);
$result->addPlayer('username', $player->username);
$result->addPlayer('group', $player->group);
$result->addPlayer('active', (int)$player->active);
$result->addPlayer('state', $player->state);
$result->addPlayer('team', $player->team);
}
// Make rules into simple array
$rules = [];
// Parse rules
foreach ($json->rules as $rule => $value) {
// Add rule but convert boolean into int (0|1)
$rules[$rule] = (is_bool($value)) ? (int)$value : $value;
}
// Add rules
$result->add('rules', $rules);
unset($rules, $rule, $player, $value);
return $result->fetch();
}
}

View File

@ -18,9 +18,6 @@
namespace GameQ\Protocols;
use GameQ\Buffer;
use GameQ\Result;
/**
* Unreal Tournament 3 Protocol Class
*
@ -64,7 +61,7 @@ class Ut3 extends Gamespy3
/**
* Overload the response process so we can make some changes
*
* @return \GameQ\Result
* @return array
*/
public function processResponse()
{

View File

@ -85,7 +85,7 @@ class Ventrilo extends Protocol
// General
'general' => [
'dedicated' => 'dedicated',
'password' => 'auth',
'password' => 'auth',
'hostname' => 'name',
'numplayers' => 'clientcount',
'maxplayers' => 'maxclients',
@ -748,17 +748,17 @@ class Ventrilo extends Protocol
* @return string
* @throws \GameQ\Exception\Protocol
*/
protected function decryptPackets(array $packets = [ ])
protected function decryptPackets(array $packets = [])
{
// This will be returned
$decrypted = [ ];
$decrypted = [];
foreach ($packets as $packet) {
# Header :
$header = substr($packet, 0, 20);
$header_items = [ ];
$header_items = [];
$header_key = unpack("n1", $header);
@ -797,7 +797,7 @@ class Ventrilo extends Protocol
'totpck',
'pck',
'datakey',
'crc'
'crc',
], $header_items);
// Check to make sure the number of packets match

View File

@ -55,13 +55,17 @@ class Native extends Core
public function write($data)
{
// No socket for this server, make one
if (is_null($this->socket)) {
$this->create();
}
try {
// No socket for this server, make one
if (is_null($this->socket)) {
$this->create();
}
// Send the packet
return fwrite($this->socket, $data);
// Send the packet
return fwrite($this->socket, $data);
} catch (\Exception $e) {
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
}
/**
@ -138,10 +142,10 @@ class Native extends Core
$loop_active = true;
// Will hold the responses read from the sockets
$responses = [ ];
$responses = [];
// To store the sockets
$sockets_tmp = [ ];
$sockets_tmp = [];
// Loop and pull out all the actual sockets we need to listen on
foreach ($sockets as $socket_id => $socket_data) {
@ -150,7 +154,7 @@ class Native extends Core
$socket = $socket_data['socket'];
// Append the actual socket we are listening to
$sockets_tmp[ $socket_id ] = $socket->get();
$sockets_tmp[$socket_id] = $socket->get();
unset($socket);
}
@ -195,12 +199,12 @@ class Native extends Core
// Check to see if the response is empty, if so we are done with this server
if (strlen($response) == 0) {
// Remove this server from any future read loops
unset($sockets_tmp[ (int) $socket ]);
unset($sockets_tmp[(int)$socket]);
continue;
}
// Add the response we got back
$responses[ (int) $socket ][] = $response;
$responses[(int)$socket][] = $response;
}
// Because stream_select modifies read we need to reset it each time to the original array of sockets

View File

@ -32,7 +32,7 @@ class Result
*
* @var array
*/
protected $result = [ ];
protected $result = [];
/**
* Adds variable to results
@ -82,7 +82,7 @@ class Result
// Nothing of this type yet, set an empty array
if (!isset($this->result[$sub]) or !is_array($this->result[$sub])) {
$this->result[$sub] = [ ];
$this->result[$sub] = [];
}
// Find the first entry that doesn't have this variable