Skip to content

Commit

Permalink
[Change] Remove deprecated empty join_link property for Stationeers,
Browse files Browse the repository at this point in the history
[Add] Shift helper method to Arr,
[Change] Allow Stationeers metaserver to be set by options,
  • Loading branch information
bumbummen99 committed Dec 1, 2024
1 parent 8b595cf commit 491b6b2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
1 change: 0 additions & 1 deletion src/GameQ/Filters/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

namespace GameQ\Filters;

use GameQ\Concerns\WithArrayHelpers;
use GameQ\Server;

/**
Expand Down
23 changes: 23 additions & 0 deletions src/GameQ/Helpers/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,27 @@ public static function set(array &$array, array $path, $value)
/* Return the current, modified array (level) */
return $array;
}

/**
* This helper method is intended to shift the provided arguments to the left.
*
* **Example:** foo, bar, baz becomes bar, baz, baz
*
* @param mixed &...$args
* @return void
*/
public static function shift(&...$args)
{
/* Get the array keys to ensure numeric index */
$keys = array_keys($args);

/* Iterate the provided arguments keys in order */
foreach ($keys as $i => $key) {
/* Process until the last argument */
if ($i < count($keys) - 1) {
/* Shift next into current */
$args[$key] = $args[$keys[$i + 1]];
}
}
}
}
75 changes: 36 additions & 39 deletions src/GameQ/Protocols/Stationeers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace GameQ\Protocols;

use GameQ\Exception\Protocol as Exception;
use GameQ\Helpers\Arr;
use GameQ\Result;
use GameQ\Server;

Expand All @@ -32,29 +33,15 @@
class Stationeers extends Http
{
/**
* The host (address) of the server to query to get the list of servers
* The host (address) of the "Metaserver" to query to get the list of servers
*/
const SERVER_LIST_HOST = '40.82.200.175';

/**
* The port of the server to query to get the list of servers
* The port of the "Metaserver" to query to get the list of servers
*/
const SERVER_LIST_PORT = 8081;

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

/**
* Holds the real port so we can overwrite it back
*
* @var int
*/
protected $realPortQuery = null;

/**
* Packets to send
*
Expand Down Expand Up @@ -85,13 +72,6 @@ class Stationeers extends Http
*/
protected $name_long = "Stationeers";

/**
* The client join link
*
* @type string
*/
protected $join_link = "";

/**
* Normalize some items
*
Expand All @@ -110,6 +90,24 @@ class Stationeers extends Http
],
];

/**
* Holds the real ip so we can overwrite it back
*
* **NOTE:** These is used during the runtime.
*
* @var string
*/
protected $realIp = null;

/**
* Holds the real port so we can overwrite it back
*
* **NOTE:** These is used during the runtime.
*
* @var int
*/
protected $realPortQuery = null;

/**
* Handle changing the call to call a central server rather than the server directly
*
Expand All @@ -119,13 +117,13 @@ class Stationeers extends Http
*/
public function beforeSend(Server $server)
{
// Save the passed IP information so we can use it later for the response
$this->realIp = $server->ip;
$this->realPortQuery = $server->port_query;
/* Determine the connection information to be used for the "Metaserver" */
$metaServerHost = $server->getOption('meta_host') ? $server->getOption('meta_host') : self::SERVER_LIST_HOST;
$metaServerPort = $server->getOption('meta_port') ? $server->getOption('meta_port') : self::SERVER_LIST_PORT;

// Override the existing settings with the query host information
$server->ip = self::SERVER_LIST_HOST;
$server->port_query = self::SERVER_LIST_PORT;
/* Save the real connection information and overwrite the properties with the "Metaserver" connection information */
Arr::shift($this->realIp, $server->ip, $metaServerHost);
Arr::shift($this->realPortQuery, $server->port_query, $metaServerPort);
}

/**
Expand All @@ -136,6 +134,7 @@ public function beforeSend(Server $server)
*/
public function processResponse()
{
/* Ensure there is a reply from the "Metaserver" */
if (empty($this->packets_response)) {
return [];
}
Expand All @@ -145,7 +144,7 @@ public function processResponse()

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

// By default no server is found
Expand All @@ -160,25 +159,22 @@ public function processResponse()
}
}

// No longer needed, be free!
/* Send to the garbage collector */
unset($matches, $serverEntry, $json);

// The server information passed was not found in this host's list
if (!$server) {
/* Ensure the provided Server has been found in the list provided by the "Metaserver" */
if (! $server) {
throw new Exception(sprintf(
'%s Unable to find the server "%s:%d" in the Stationeers server list',
'%s Unable to find the server "%s:%d" in the Stationeer Metaservers server list',
__METHOD__,
$this->realIp,
$this->realPortQuery
));
}

/* Build the Result from the parsed JSON */
$result = new Result();

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

// Add server items
$result->add('dedicated', 1); // Server is always dedicated
$result->add('hostname', $server->Name);
$result->add('gq_address', $server->Address);
$result->add('gq_port_query', $server->Port);
Expand All @@ -190,6 +186,7 @@ public function processResponse()
$result->add('maxplayers', $server->MaxPlayers);
$result->add('type', $server->Type);

/* Send to the garbage collector */
unset($server);

return $result->fetch();
Expand Down

0 comments on commit 491b6b2

Please sign in to comment.