Completed
Push — v3 ( 2fd840...8b0263 )
by Austin
04:24
created

Arma3::processRules()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 98
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 98
ccs 41
cts 41
cp 1
rs 8.1707
c 0
b 0
f 0
cc 6
eloc 41
nc 24
nop 1
crap 6

How to fix   Long Method   

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\Buffer;
22
use GameQ\Result;
23
24
/**
25
 * Class Armed Assault 3
26
 *
27
 * Rules protocol reference: https://community.bistudio.com/wiki/Arma_3_ServerBrowserProtocol2
28
 *
29
 * @package GameQ\Protocols
30
 * @author  Austin Bischoff <[email protected]>
31
 * @author  Memphis017 <https://github.com/Memphis017>
32
 */
33
class Arma3 extends Source
34
{
35
    /**
36
     * DLC ^2 constants
37
     */
38
    const DLC_KARTS = 1,
39
        DLC_MARKSMEN = 2,
40
        DLC_HELICOPTERS = 4,
41
        DLC_APEX = 16;
42
43
    /**
44
     * Defines the names for the specific game DLCs
45
     *
46
     * @var array
47
     */
48
    protected $dlcNames = [
49
        self::DLC_KARTS       => 'Karts',
50
        self::DLC_MARKSMEN    => 'Marksmen',
51
        self::DLC_HELICOPTERS => 'Helicopters',
52
        self::DLC_APEX        => 'Apex',
53
    ];
54
55
56
    /**
57
     * String name of this protocol class
58
     *
59
     * @type string
60
     */
61
    protected $name = 'arma3';
62
63
    /**
64
     * Longer string name of this protocol class
65
     *
66
     * @type string
67
     */
68
    protected $name_long = "Arma3";
69
70
    /**
71
     * Query port = client_port + 1
72
     *
73
     * @type int
74
     */
75
    protected $port_diff = 1;
76
77
    /**
78
     * Process the rules since Arma3 changed their response for rules
79
     *
80
     * @param Buffer $buffer
81
     *
82
     * @return array
83
     * @throws \GameQ\Exception\Protocol
84
     */
85 2
    protected function processRules(Buffer $buffer)
86
    {
87
        // Total number of packets, burn it
88 2
        $buffer->readInt16();
89
90
        // Will hold the data string
91 2
        $data = '';
92
93
        // Loop until we run out of strings
94 2
        while ($buffer->getLength()) {
95
            // Burn the delimiters (i.e. \x01\x04\x00)
96 2
            $buffer->readString();
97
98
            // Add the data to the string, we are reassembling it
99 2
            $data .= $buffer->readString();
100
        }
101
102
        // Restore escaped sequences
103 2
        $data = str_replace(["\x01\x01", "\x01\x02", "\x01\x03"], ["\x01", "\x00", "\xFF"], $data);
104
105
        // Make a new buffer with the reassembled data
106 2
        $responseBuffer = new Buffer($data);
107
108
        // Kill the old buffer, should be empty
109 2
        unset($buffer, $data);
110
111
        // Set the result to a new result instance
112 2
        $result = new Result();
113
114
        // Get results
115 2
        $result->add('rules_protocol_version', $responseBuffer->readInt8());
116 2
        $result->add('overflow', $responseBuffer->readInt8());
117 2
        $dlcBit = $responseBuffer->readInt8(); // Grab DLC bit and use it later
118 2
        $responseBuffer->skip(); // Reserved, burn it
119
        // Grab difficulty so we can man handle it...
120 2
        $difficulty = $responseBuffer->readInt8();
121
122
        // Process difficulty
123 2
        $result->add('3rd_person', $difficulty >> 7);
124 2
        $result->add('advanced_flight_mode', ($difficulty >> 6) & 1);
125 2
        $result->add('difficulty_ai', ($difficulty >> 3) & 3);
126 2
        $result->add('difficulty_level', $difficulty & 3);
127
128 2
        unset($difficulty);
129
130
        // Crosshair
131 2
        $result->add('crosshair', $responseBuffer->readInt8());
132
133
        // Next are the dlc bits so loop over the dlcBit so we can determine which DLC's are running
134 2
        for ($x = 1; $x <= $dlcBit; $x *= 2) {
135
            // Enabled, add it to the list
136 2
            if ($x & $dlcBit) {
137 2
                $result->addSub('dlcs', 'name', $this->dlcNames[$x]);
138 2
                $result->addSub('dlcs', 'hash', dechex($responseBuffer->readInt32()));
139
            }
140
        }
141
142
        // No longer needed
143 2
        unset($dlcBit);
144
145
        // Grab the mod count
146 2
        $modCount = $responseBuffer->readInt8();
147
148
        // Add mod count
149 2
        $result->add('mod_count', $modCount);
150
151
        // Loop the mod count and add them
152 2
        for ($x = 0; $x < $modCount; $x++) {
153
            // Add the mod to the list
154 1
            $result->addSub('mods', 'hash', dechex($responseBuffer->readInt32()));
155 1
            $result->addSub('mods', 'steam_id', hexdec($responseBuffer->readPascalString(0, true)));
156 1
            $result->addSub('mods', 'name', $responseBuffer->readPascalString(0, true));
157
        }
158
159 2
        unset($modCount, $x);
160
161
        // Burn the signatures count, we will just loop until we run out of strings
162 2
        $responseBuffer->read();
163
164
        // Make signatures array
165 2
        $signatures = [];
166
167
        // Loop until we run out of strings
168 2
        while ($responseBuffer->getLength()) {
169
            //$result->addSub('signatures', 0, $responseBuffer->readPascalString(0, true));
170 2
            $signatures[] = $responseBuffer->readPascalString(0, true);
171
        }
172
173
        // Add as a simple array
174 2
        $result->add('signatures', $signatures);
0 ignored issues
show
Documentation introduced by
$signatures is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
175
176
        // Add signatures count
177 2
        $result->add('signature_count', count($signatures));
178
179 2
        unset($responseBuffer, $signatures);
180
181 2
        return $result->fetch();
182
    }
183
}
184