Skip to content

Commit

Permalink
Added Grand Theft Auto Rage Protocol Class (#544)
Browse files Browse the repository at this point in the history
* Added Grand Theft Auto Rage Protocol Class

* Tests for Grand Theft Auto Rage Protocol

Co-authored-by: K700 <ruslan-k700@yandex.ru>
  • Loading branch information
Ruslan-700 and K700 authored Jun 7, 2020
1 parent 0c0c02b commit 60a8140
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 0 deletions.
164 changes: 164 additions & 0 deletions src/GameQ/Protocols/Gtar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?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;
use GameQ\Server;

/**
* Grand Theft Auto Rage Protocol Class
* https://rage.mp/masterlist/
*
* Result from this call should be a header + JSON response
*
* @author K700 <admin@fianna.ru>
* @author Austin Bischoff <austin@codebeard.com>
*/
class Gtar extends Http
{
/**
* Packets to send
*
* @var array
*/
protected $packets = [
self::PACKET_STATUS => "GET /master/ HTTP/1.0\r\nHost: cdn.rage.mp\r\nAccept: */*\r\n\r\n",
];

/**
* Http protocol is SSL
*
* @var string
*/
protected $transport = self::TRANSPORT_SSL;

/**
* The protocol being used
*
* @var string
*/
protected $protocol = 'gtar';

/**
* String name of this protocol class
*
* @var string
*/
protected $name = 'gtar';

/**
* Longer string name of this protocol class
*
* @var string
*/
protected $name_long = "Grand Theft Auto Rage";

/**
* Holds the real ip so we can overwrite it back
*
* @var string
*/
protected $realIp = null;

protected $realPortQuery = null;

/**
* Normalize some items
*
* @var array
*/
protected $normalize = [
// General
'general' => [
// target => source
'dedicated' => 'dedicated',
'hostname' => 'hostname',
'mod' => 'mod',
'maxplayers' => 'maxplayers',
'numplayers' => 'numplayers',
],
];

public function beforeSend(Server $server)
{
// Loop over the packets and update them
foreach ($this->packets as $packetType => $packet) {
// Fill out the packet with the server info
$this->packets[$packetType] = sprintf($packet, $server->ip . ':' . $server->port_query);
}

$this->realIp = $server->ip;
$this->realPortQuery = $server->port_query;

// Override the existing settings
$server->ip = 'cdn.rage.mp';
$server->port_query = 443;
}

/**
* Process the response
*
* @return array
* @throws Exception
*/
public function processResponse()
{
// No response, assume offline
if (empty($this->packets_response)) {
return [
'gq_address' => $this->realIp,
'gq_port_query' => $this->realPortQuery,
];
}

// Implode and rip out the JSON
preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);

// Return should be JSON, let's validate
if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) {
throw new Exception("JSON response from Gtar protocol is invalid.");
}

$address = $this->realIp.':'.$this->realPortQuery;
$server = $json->$address;

if (empty($server)) {
return [
'gq_address' => $this->realIp,
'gq_port_query' => $this->realPortQuery,
];
}

$result = new Result();

// Server is always dedicated
$result->add('dedicated', 1);

$result->add('gq_address', $this->realIp);
$result->add('gq_port_query', $this->realPortQuery);

// Add server items
$result->add('hostname', $server->name);
$result->add('mod', $server->gamemode);
$result->add('numplayers', $server->players);
$result->add('maxplayers', $server->maxplayers);

return $result->fetch();
}
}
84 changes: 84 additions & 0 deletions tests/Protocols/Gtar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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\Tests\Protocols;

class Gtar extends Base
{

/**
* Holds stub on setup
*
* @type \GameQ\Protocols\Gtan
*/
protected $stub;

/**
* Holds the expected packets for this protocol class
*
* @type array
*/
protected $packets = [
\GameQ\Protocol::PACKET_STATUS => "GET /master/ HTTP/1.0\r\nHost: cdn.rage.mp\r\nAccept: */*\r\n\r\n",
];

/**
* Setup
*/
public function setUp()
{

// Create the stub class
$this->stub = $this->getMockBuilder('\GameQ\Protocols\Gtar')
->enableProxyingToOriginalMethods()
->getMock();
}

/**
* Test the packets to make sure they are correct for source
*/
public function testPackets()
{

// Test to make sure packets are defined properly
$this->assertEquals($this->packets, \PHPUnit\Framework\Assert::readAttribute($this->stub, 'packets'));
}

/**
* Test responses for Grand Theft Auto Network
*
* @dataProvider loadData
*
* @param $responses
* @param $result
*/
public function testResponses($responses, $result)
{

// Pull the first key off the array this is the server ip:port
$server = key($result);

$testResult = $this->queryTest(
$server,
'gtar',
$responses
);

$this->assertEquals($result[ $server ], $testResult, '', 0.000000001);
}
}
Loading

0 comments on commit 60a8140

Please sign in to comment.