Completed
Push — v3 ( 639384...32ed98 )
by Austin
08:20
created

Arma3::processRules()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 98
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 7

Importance

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

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
        DLC_JETS = 32,
43
        DLC_LAWS = 64,
44
        DLC_TACOPS = 128,
45
        DLC_TANKS = 256;
46
47
    /**
48
     * Defines the names for the specific game DLCs
49
     *
50
     * @var array
51
     */
52
    protected $dlcNames = [
53
        self::DLC_KARTS       => 'Karts',
54
        self::DLC_MARKSMEN    => 'Marksmen',
55
        self::DLC_HELICOPTERS => 'Helicopters',
56
        self::DLC_APEX        => 'Apex',
57
        self::DLC_JETS        => 'Jets',
58
        self::DLC_LAWS        => 'Laws of War',
59
        self::DLC_TACOPS      => 'Tac-Ops',
60
        self::DLC_TANKS       => 'Tanks'
61
    ];
62
63
    /**
64
     * String name of this protocol class
65
     *
66
     * @type string
67
     */
68
    protected $name = 'arma3';
69
70
    /**
71
     * Longer string name of this protocol class
72
     *
73
     * @type string
74
     */
75
    protected $name_long = "Arma3";
76
77
    /**
78
     * Query port = client_port + 1
79
     *
80
     * @type int
81
     */
82
    protected $port_diff = 1;
83
84
    /**
85
     * Process the rules since Arma3 changed their response for rules
86
     *
87
     * @param Buffer $buffer
88
     *
89
     * @return array
90
     * @throws \GameQ\Exception\Protocol
91
     */
92 4
    protected function processRules(Buffer $buffer)
93
    {
94
        // Total number of packets, burn it
95 4
        $buffer->readInt16();
96
97
        // Will hold the data string
98 4
        $data = '';
99
100
        // Loop until we run out of strings
101 4
        while ($buffer->getLength()) {
102
            // Burn the delimiters (i.e. \x01\x04\x00)
103 4
            $buffer->readString();
104
105
            // Add the data to the string, we are reassembling it
106 4
            $data .= $buffer->readString();
107
        }
108
109
        // Restore escaped sequences
110 4
        $data = str_replace(["\x01\x01", "\x01\x02", "\x01\x03"], ["\x01", "\x00", "\xFF"], $data);
111
112
        // Make a new buffer with the reassembled data
113 4
        $responseBuffer = new Buffer($data);
114
115
        // Kill the old buffer, should be empty
116 4
        unset($buffer, $data);
117
118
        // Set the result to a new result instance
119 4
        $result = new Result();
120
121
        // Get results
122 4
        $result->add('rules_protocol_version', $responseBuffer->readInt8());
123 4
        $result->add('overflow', $responseBuffer->readInt8());
124 4
        $dlcBit = $responseBuffer->readInt8(); // Grab DLC bit and use it later
125 4
        $responseBuffer->skip(); // Reserved, burn it
126
        // Grab difficulty so we can man handle it...
127 4
        $difficulty = $responseBuffer->readInt8();
128
129
        // Process difficulty
130 4
        $result->add('3rd_person', $difficulty >> 7);
131 4
        $result->add('advanced_flight_mode', ($difficulty >> 6) & 1);
132 4
        $result->add('difficulty_ai', ($difficulty >> 3) & 3);
133 4
        $result->add('difficulty_level', $difficulty & 3);
134
135 4
        unset($difficulty);
136
137
        // Crosshair
138 4
        $result->add('crosshair', $responseBuffer->readInt8());
139
140
        // Next are the dlc bits so loop over the dlcBit so we can determine which DLC's are running
141 4
        for ($x = 1; $x <= $dlcBit; $x *= 2) {
142
            // Enabled, add it to the list
143 4
            if ($x & $dlcBit && array_key_exists($x, $this->dlcNames)) {
144 4
                $result->addSub('dlcs', 'name', $this->dlcNames[$x]);
145 4
                $result->addSub('dlcs', 'hash', dechex($responseBuffer->readInt32()));
146
            }
147
        }
148
149
        // No longer needed
150 4
        unset($dlcBit);
151
152
        // Grab the mod count
153 4
        $modCount = $responseBuffer->readInt8();
154
155
        // Add mod count
156 4
        $result->add('mod_count', $modCount);
157
158
        // Loop the mod count and add them
159 4
        for ($x = 0; $x < $modCount; $x++) {
160
            // Add the mod to the list
161 3
            $result->addSub('mods', 'hash', dechex($responseBuffer->readInt32()));
162 3
            $result->addSub('mods', 'steam_id', hexdec($responseBuffer->readPascalString(0, true)));
163 3
            $result->addSub('mods', 'name', $responseBuffer->readPascalString(0, true));
164
        }
165
166 4
        unset($modCount, $x);
167
168
        // Burn the signatures count, we will just loop until we run out of strings
169 4
        $responseBuffer->read();
170
171
        // Make signatures array
172 4
        $signatures = [];
173
174
        // Loop until we run out of strings
175 4
        while ($responseBuffer->getLength()) {
176
            //$result->addSub('signatures', 0, $responseBuffer->readPascalString(0, true));
177 4
            $signatures[] = $responseBuffer->readPascalString(0, true);
178
        }
179
180
        // Add as a simple array
181 4
        $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...
182
183
        // Add signatures count
184 4
        $result->add('signature_count', count($signatures));
185
186 4
        unset($responseBuffer, $signatures);
187
188 4
        return $result->fetch();
189
    }
190
}
191