Passed
Push — v3 ( efc739...bb78f7 )
by Austin
02:17
created

Starmade::parseServerParameters()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 64
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 11.1512

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 64
ccs 24
cts 31
cp 0.7742
rs 7.6666
cc 10
nc 10
nop 1
crap 11.1512

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Protocol;
22
use GameQ\Buffer;
23
use GameQ\Result;
24
use GameQ\Exception\Protocol as Exception;
25
26
/**
27
 * StarMade Protocol Class
28
 *
29
 * StarMade server query protocol class
30
 *
31
 * Credit to Robin Promesberger <[email protected]> for providing Java based querying as a roadmap
32
 *
33
 * @author Austin Bischoff <[email protected]>
34
 */
35
class Starmade extends Protocol
36
{
37
38
    /**
39
     * Array of packets we want to query.
40
     *
41
     * @type array
42
     */
43
    protected $packets = [
44
        self::PACKET_STATUS => "\x00\x00\x00\x09\x2a\xff\xff\x01\x6f\x00\x00\x00\x00",
45
    ];
46
47
    /**
48
     * The transport mode for this protocol is TCP
49
     *
50
     * @type string
51
     */
52
    protected $transport = self::TRANSPORT_TCP;
53
54
    /**
55
     * The query protocol used to make the call
56
     *
57
     * @type string
58
     */
59
    protected $protocol = 'starmade';
60
61
    /**
62
     * String name of this protocol class
63
     *
64
     * @type string
65
     */
66
    protected $name = 'starmade';
67
68
    /**
69
     * Longer string name of this protocol class
70
     *
71
     * @type string
72
     */
73
    protected $name_long = "StarMade";
74
75
    /**
76
     * The client join link
77
     *
78
     * @type string
79
     */
80
    protected $join_link = null;
81
82
    /**
83
     * Normalize settings for this protocol
84
     *
85
     * @type array
86
     */
87
    protected $normalize = [
88
        // General
89
        'general' => [
90
            // target       => source
91
            'dedicated'  => 'dedicated',
92
            'hostname'   => 'hostname',
93
            'maxplayers' => 'max_players',
94
            'numplayers' => 'num_players',
95
            'password'   => 'password',
96
        ],
97
    ];
98
99
    /**
100
     * Process the response for the StarMade server
101
     *
102
     * @return array
103
     * @throws \GameQ\Exception\Protocol
104
     */
105 6
    public function processResponse()
106
    {
107
108
        // Implode the packets, not sure if there is any split logic for multiple packets
109 6
        $buffer = new Buffer(implode('', $this->packets_response), Buffer::NUMBER_TYPE_BIGENDIAN);
110
111
        // Get the passed length in the data side of the packet
112 6
        $buffer->readInt32Signed();
113
114
        // Read off the timestamp (in milliseconds)
115 6
        $buffer->readInt64();
116
117
        // Burn the check id == 42
118 6
        $buffer->readInt8();
119
120
        // Read packetId, unused
121 6
        $buffer->readInt16Signed();
122
123
        // Read commandId, unused
124 6
        $buffer->readInt8Signed();
125
126
        // Read type, unused
127 6
        $buffer->readInt8Signed();
128
129 6
        $parsed = $this->parseServerParameters($buffer);
130
131
        // Set the result to a new result instance
132 6
        $result = new Result();
133
134
        // Best guess info version is the type of response to expect.  As of this commit the version is "2".
135 6
        $result->add('info_version', $parsed[0]);
136 6
        $result->add('version', $parsed[1]);
137 6
        $result->add('hostname', $parsed[2]);
138 6
        $result->add('game_descr', $parsed[3]);
139 6
        $result->add('start_time', $parsed[4]);
140 6
        $result->add('num_players', $parsed[5]);
141 6
        $result->add('max_players', $parsed[6]);
142 6
        $result->add('dedicated', 1); // All servers are dedicated as far as I can tell
143 6
        $result->add('password', 0); // Unsure if you can password servers, cant read that value
144
        //$result->add('map', 'Unknown');
145
146 6
        unset($parsed);
147
148 6
        return $result->fetch();
149
    }
150
151
    /**
152
     * Parse the server response parameters
153
     *
154
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
155
     *
156
     * @param \GameQ\Buffer $buffer
157
     *
158
     * @return array
159
     * @throws \GameQ\Exception\Protocol
160
     */
161 6
    protected function parseServerParameters(Buffer &$buffer)
162
    {
163
164
        // Init the parsed data array
165 6
        $parsed = [];
166
167
        // Read the number of parameters to parse
168 6
        $parameterSize = $buffer->readInt32Signed();
169
170
        // Iterate over the parameter size
171 6
        for ($i = 0; $i < $parameterSize; $i++) {
172
            // Read the type of return this is
173 6
            $dataType = $buffer->readInt8Signed();
174
175
            switch ($dataType) {
176
                // 32-bit int
177 6
                case 1:
178 6
                    $parsed[$i] = $buffer->readInt32Signed();
179 6
                    break;
180
181
                // 64-bit int
182 6
                case 2:
183 6
                    $parsed[$i] = $buffer->readInt64();
184 6
                    break;
185
186
                // Float
187 6
                case 3:
188 6
                    $parsed[$i] = $buffer->readFloat32();
189 6
                    break;
190
191
                // String
192 6
                case 4:
193
                    // The first 2 bytes are the string length
194 6
                    $strLength = $buffer->readInt16Signed();
195
196
                    // Read the above length from the buffer
197 6
                    $parsed[$i] = $buffer->read($strLength);
198
199 6
                    unset($strLength);
200 6
                    break;
201
202
                // Boolean
203 6
                case 5:
204
                    $parsed[$i] = (bool)$buffer->readInt8Signed();
205
                    break;
206
207
                // 8-bit int
208 6
                case 6:
209 6
                    $parsed[$i] = $buffer->readInt8Signed();
210 6
                    break;
211
212
                // 16-bit int
213
                case 7:
214
                    $parsed[$i] = $buffer->readInt16Signed();
215
                    break;
216
217
                // Array
218
                case 8:
219
                    // Not implemented
220
                    throw new Exception("StarMade array parsing is not implemented!");
221
            }
222
        }
223
224 6
        return $parsed;
225
    }
226
}
227