Skip to content

Instantly share code, notes, and snippets.

@HellBz
Created April 2, 2020 22:56
Show Gist options
  • Save HellBz/88dbe8dfe7d20f94fe1b7afdaf07eb7f to your computer and use it in GitHub Desktop.
Save HellBz/88dbe8dfe7d20f94fe1b7afdaf07eb7f to your computer and use it in GitHub Desktop.
This is a Protocoll fur Query SCUM-Server via API of api.hellbz.de
<?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;
/**
* SCUM API Class Protocol Class
*
* Result from this call should be a header + JSON response
*
* References:
* - https://api.hellbz.de/scum/api.php?address=176.57.153.23&port=28200
*
* @author HellBz <coding@hellbz.de>
*/
class Scumapi extends Http
{
/**
* Packets to send
*
* @var array
*/
protected $packets = [
self::PACKET_STATUS => "GET /scum/api.php%s HTTP/1.0\r\nHost: api.hellbz.de\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 = 'scumapi';
/**
* String name of this protocol class
*
* @var string
*/
protected $name = 'scumapi';
/**
* Longer string name of this protocol class
*
* @var string
*/
protected $name_long = "SCUM API (api.hellbz.de)";
/**
* Holds the real ip so we can overwrite it back
*
* @var string
*/
protected $realIp = null;
protected $realPortQuery = null;
/**
* query_port = client_port + 2
*
* @type int
*/
protected $port_diff = 2;
/**
* Normalize some items
*
* @var array
*/
protected $normalize = [
// General
'general' => [
// target => source
'dedicated' => 'dedicated',
'hostname' => 'hostname',
'mapname' => 'world',
'maxplayers' => 'maxplayers',
'numplayers' => 'numplayers',
'password' => 'password',
],
];
/**
* Before we send off the queries we need to update the packets
*
* @param \GameQ\Server $server
*
* @throws \GameQ\Exception\Protocol
*/
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, '?address='.$server->ip . '&port=' . $server->port_client);
}
$this->realIp = $server->ip;
$this->realPortQuery = $server->port_query;
// Override the existing settings
$server->ip = 'api.hellbz.de';
$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 API protocol is invalid.");
}
// Check the status response
if ($json->success != true ) {
throw new Exception("JSON status from API protocol response was '{$json->error}', expected '200'.");
}
$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', $json->data{0}->name);
$result->add('game_port', $json->data{0}->port);
$result->add('serverversion', $json->data{0}->version);
$result->add('world', 'scum');
$result->add('uptime', $json->data{0}->time);
$result->add('password', (int)$json->data{0}->password);
$result->add('numplayers', $json->data{0}->players);
$result->add('maxplayers', $json->data{0}->players_max);
return $result->fetch();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment