Completed
Pull Request — v3 (#559)
by
unknown
30:00 queued 03:01
created

Arma3::processRules()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 96
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 6.027

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 43
c 3
b 0
f 0
dl 0
loc 96
ccs 40
cts 44
cp 0.9091
rs 8.6097
cc 6
nc 24
nop 1
crap 6.027

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