Passed
Pull Request — v3 (#660)
by
unknown
30:42
created

Stationeers::beforeSend()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 4
nop 1
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
 * This protocol uses a server list from a JSON response to find the server and parse the server's status information
30
 *
31
 * @author Austin Bischoff <[email protected]>
32
 */
33
class Stationeers extends Http
34
{
35
    /**
36
     * The host (address) of the "Metaserver" to query to get the list of servers
37
     */
38
    const SERVER_LIST_HOST = '40.82.200.175';
39
40
    /**
41
     * The port of the "Metaserver" to query to get the list of servers
42
     */
43
    const SERVER_LIST_PORT = 8081;
44
45
    /**
46
     * Packets to send
47
     *
48
     * @var array
49
     */
50
    protected $packets = [
51
        self::PACKET_STATUS => "GET /list HTTP/1.0\r\nAccept: */*\r\n\r\n",
52
    ];
53
54
    /**
55
     * The protocol being used
56
     *
57
     * @var string
58
     */
59
    protected $protocol = 'stationeers';
60
61
    /**
62
     * String name of this protocol class
63
     *
64
     * @var string
65
     */
66
    protected $name = 'stationeers';
67
68
    /**
69
     * Longer string name of this protocol class
70
     *
71
     * @var string
72
     */
73
    protected $name_long = "Stationeers";
74
75
    /**
76
     * Normalize some items
77
     *
78
     * @var array
79
     */
80
    protected $normalize = [
81
        // General
82
        'general' => [
83
            // target       => source
84
            'dedicated'  => 'dedicated',
85
            'hostname'   => 'hostname',
86
            'mapname'    => 'map',
87
            'maxplayers' => 'maxplayers',
88
            'numplayers' => 'numplayers',
89
            'password'   => 'password',
90
        ],
91
    ];
92
93
    /**
94
     * Holds the real ip so we can overwrite it back
95
     *
96
     * **NOTE:** These is used during the runtime.
97
     *
98
     * @var string
99
     */
100
    protected $realIp = null;
101
102
    /**
103
     * Holds the real port so we can overwrite it back
104
     *
105
     * **NOTE:** These is used during the runtime.
106
     *
107
     * @var int
108
     */
109
    protected $realPortQuery = null;
110
111
    /**
112
     * Handle changing the call to call a central server rather than the server directly
113
     *
114
     * @param Server $server
115
     *
116
     * @return void
117
     */
118
    public function beforeSend(Server $server)
119
    {
120
        /* Determine the connection information to be used for the "Metaserver" */
121
        $metaServerHost = $server->getOption('meta_host') ? $server->getOption('meta_host') : self::SERVER_LIST_HOST;
122
        $metaServerPort = $server->getOption('meta_port') ? $server->getOption('meta_port') : self::SERVER_LIST_PORT;
123
124
        /* Save the real connection information and overwrite the properties with the "Metaserver" connection information */
125
        Arr::shift($this->realIp, $server->ip, $metaServerHost);
126
        Arr::shift($this->realPortQuery, $server->port_query, $metaServerPort);
127
    }
128
129
    /**
130
     * Process the response
131
     *
132
     * @return array
133
     * @throws Exception
134
     */
135
    public function processResponse()
136
    {
137
        /* Ensure there is a reply from the "Metaserver" */
138
        if (empty($this->packets_response)) {
139
            return [];
140
        }
141
142
        // Implode and rip out the JSON
143
        preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
144
145
        // Return should be JSON, let's validate
146
        if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) {
147
            throw new Exception(__METHOD__ . " JSON response from Stationeers Metaserver is invalid.");
148
        }
149
150
        // By default no server is found
151
        $server = null;
152
153
        // Find the server on this list by iterating over the entire list.
154
        foreach ($json->GameSessions as $serverEntry) {
155
            // Server information passed matches an entry on this list
156
            if ($serverEntry->Address === $this->realIp && (int)$serverEntry->Port === $this->realPortQuery) {
157
                $server = $serverEntry;
158
                break;
159
            }
160
        }
161
162
        /* Send to the garbage collector */
163
        unset($matches, $serverEntry, $json);
164
165
        /* Ensure the provided Server has been found in the list provided by the "Metaserver" */
166
        if (! $server) {
167
            throw new Exception(sprintf(
168
                '%s Unable to find the server "%s:%d" in the Stationeer Metaservers server list',
169
                __METHOD__,
170
                $this->realIp,
171
                $this->realPortQuery
172
            ));
173
        }
174
175
        /* Build the Result from the parsed JSON */
176
        $result = new Result();
177
        $result->add('dedicated', 1); // Server is always dedicated
178
        $result->add('hostname', $server->Name);
179
        $result->add('gq_address', $server->Address);
180
        $result->add('gq_port_query', $server->Port);
181
        $result->add('version', $server->Version);
182
        $result->add('map', $server->MapName);
183
        $result->add('uptime', $server->UpTime);
184
        $result->add('password', (int)$server->Password);
185
        $result->add('numplayers', $server->Players);
186
        $result->add('maxplayers', $server->MaxPlayers);
187
        $result->add('type', $server->Type);
188
189
        /* Send to the garbage collector */
190
        unset($server);
191
192
        return $result->fetch();
193
    }
194
}
195