Passed
Push — v3 ( 37cdd6...e623f3 )
by
unknown
29:19 queued 26:35
created

Stationeers   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 53
c 1
b 0
f 0
dl 0
loc 160
ccs 32
cts 40
cp 0.8
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B processResponse() 0 58 8
A beforeSend() 0 9 3
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace GameQ\Protocols;
20
21
use GameQ\Exception\Protocol as Exception;
22
use GameQ\Helpers\Arr;
23
use GameQ\Result;
24
use GameQ\Server;
25
26
/**
27
 * Stationeers Protocol Class
28
 *
29
 * **Note:** This protocol does use the offical, centralized "Metaserver" to query the list of all available servers. This
30
 * is effectively a host controlled by a third party which could interfere with the functionality of this protocol.
31
 *
32
 * @author Austin Bischoff <[email protected]>
33
 */
34
class Stationeers extends Http
35
{
36
    /**
37
     * The host (address) of the "Metaserver" to query to get the list of servers
38
     */
39
    const SERVER_LIST_HOST = '40.82.200.175';
40
41
    /**
42
     * The port of the "Metaserver" to query to get the list of servers
43
     */
44
    const SERVER_LIST_PORT = 8081;
45
46
    /**
47
     * Packets to send
48
     *
49
     * @var array
50
     */
51
    protected $packets = [
52
        self::PACKET_STATUS => "GET /list HTTP/1.0\r\nAccept: */*\r\n\r\n",
53
    ];
54
55
    /**
56
     * The protocol being used
57
     *
58
     * @var string
59
     */
60
    protected $protocol = 'stationeers';
61
62
    /**
63
     * String name of this protocol class
64
     *
65
     * @var string
66
     */
67
    protected $name = 'stationeers';
68
69
    /**
70
     * Longer string name of this protocol class
71
     *
72
     * @var string
73
     */
74
    protected $name_long = "Stationeers";
75
76
    /**
77
     * Normalize some items
78
     *
79
     * @var array
80
     */
81
    protected $normalize = [
82
        // General
83
        'general' => [
84
            // target       => source
85
            'dedicated'  => 'dedicated',
86
            'hostname'   => 'hostname',
87
            'mapname'    => 'map',
88
            'maxplayers' => 'maxplayers',
89
            'numplayers' => 'numplayers',
90
            'password'   => 'password',
91
        ],
92
    ];
93
94
    /**
95
     * Holds the real ip so we can overwrite it back
96
     *
97
     * **NOTE:** These is used during the runtime.
98
     *
99
     * @var string
100
     */
101
    protected $realIp = null;
102
103
    /**
104
     * Holds the real port so we can overwrite it back
105
     *
106
     * **NOTE:** These is used during the runtime.
107
     *
108
     * @var int
109
     */
110
    protected $realPortQuery = null;
111
112
    /**
113
     * Handle changing the call to call a central server rather than the server directly
114
     *
115
     * @param Server $server
116
     *
117
     * @return void
118
     */
119 12
    public function beforeSend(Server $server)
120
    {
121
        /* Determine the connection information to be used for the "Metaserver" */
122 12
        $metaServerHost = $server->getOption('meta_host') ? $server->getOption('meta_host') : self::SERVER_LIST_HOST;
123 12
        $metaServerPort = $server->getOption('meta_port') ? $server->getOption('meta_port') : self::SERVER_LIST_PORT;
124
125
        /* Save the real connection information and overwrite the properties with the "Metaserver" connection information */
126 12
        Arr::shift($this->realIp, $server->ip, $metaServerHost);
127 12
        Arr::shift($this->realPortQuery, $server->port_query, $metaServerPort);
128 6
    }
129
130
    /**
131
     * Process the response
132
     *
133
     * @return array
134
     * @throws Exception
135
     */
136 12
    public function processResponse()
137
    {
138
        /* Ensure there is a reply from the "Metaserver" */
139 12
        if (empty($this->packets_response)) {
140
            return [];
141
        }
142
143
        // Implode and rip out the JSON
144 12
        preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
145
146
        // Return should be JSON, let's validate
147 12
        if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) {
148
            throw new Exception(__METHOD__ . " JSON response from Stationeers Metaserver is invalid.");
149
        }
150
151
        // By default no server is found
152 12
        $server = null;
153
154
        // Find the server on this list by iterating over the entire list.
155 12
        foreach ($json->GameSessions as $serverEntry) {
156
            // Server information passed matches an entry on this list
157 12
            if ($serverEntry->Address === $this->realIp && (int)$serverEntry->Port === $this->realPortQuery) {
158 12
                $server = $serverEntry;
159 12
                break;
160
            }
161 2
        }
162
163
        /* Send to the garbage collector */
164 12
        unset($matches, $serverEntry, $json);
165
166
        /* Ensure the provided Server has been found in the list provided by the "Metaserver" */
167 12
        if (! $server) {
168
            throw new Exception(sprintf(
169
                '%s Unable to find the server "%s:%d" in the Stationeer Metaservers server list',
170
                __METHOD__,
171
                $this->realIp,
172
                $this->realPortQuery
173
            ));
174
        }
175
176
        /* Build the Result from the parsed JSON */
177 12
        $result = new Result();
178 12
        $result->add('dedicated', 1); // Server is always dedicated
179 12
        $result->add('hostname', $server->Name);
180 12
        $result->add('gq_address', $server->Address);
181 12
        $result->add('gq_port_query', $server->Port);
182 12
        $result->add('version', $server->Version);
183 12
        $result->add('map', $server->MapName);
184 12
        $result->add('uptime', $server->UpTime);
185 12
        $result->add('password', (int)$server->Password);
186 12
        $result->add('numplayers', $server->Players);
187 12
        $result->add('maxplayers', $server->MaxPlayers);
188 12
        $result->add('type', $server->Type);
189
190
        /* Send to the garbage collector */
191 12
        unset($server);
192
193 12
        return $result->fetch();
194
    }
195
}
196