Passed
Pull Request — v3 (#709)
by
unknown
34:20
created

Scum::processResponse()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 73
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 73
rs 8.5559
cc 6
nc 4
nop 0

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
/**
4
 * This file is part of GameQ.
5
 *
6
 * GameQ is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GameQ is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace GameQ\Protocols;
21
22
use GameQ\Exception\Protocol as Exception;
23
use GameQ\Buffer;
24
use GameQ\Result;
25
use GameQ\Server;
26
27
/**
28
 * SCUM Master-Server Protocol Class
29
 * https://gamepires.com/games/scum/
30
 *
31
 * Result from this call should be a Master-Server list Query
32
 *
33
 * @author HellBz <[email protected]>
34
 * @author Austin Bischoff <[email protected]>
35
 */
36
class Scum extends Http
37
{
38
    /**
39
     * Packets to send
40
     *
41
     * @var array
42
     */
43
    protected $packets = [
44
        self::PACKET_STATUS => "\x04\x03\x00\x00"
45
    ];
46
47
    /**
48
     * Http protocol is SSL
49
     *
50
     * @var string
51
     */
52
    protected $transport = self::TRANSPORT_TCP;
53
54
    /**
55
     * The protocol being used
56
     *
57
     * @var string
58
     */
59
    protected $protocol = 'scum';
60
61
    /**
62
     * String name of this protocol class
63
     *
64
     * @var string
65
     */
66
    protected $name = 'scum';
67
68
    /**
69
     * Longer string name of this protocol class
70
     *
71
     * @var string
72
     */
73
    protected $name_long = "SCUM";
74
75
    /**
76
     * The client join link
77
     *
78
     * @type string
79
     */
80
    protected $join_link = "steam://connect/%s:%d/";
81
82
    /**
83
     * Holds the real ip so we can overwrite it back
84
     *
85
     * @var string
86
     */
87
    protected $realIp = null;
88
89
    protected $realPortQuery = null;
90
91
    /**
92
     * Normalize some items
93
     *
94
     * @var array
95
     */
96
    protected $normalize = [
97
        // General
98
        'general' => [
99
            // target       => source
100
            'dedicated' => 'dedicated',
101
            'hostname' => 'hostname',
102
            'mod' => 'mod',
103
            'maxplayers' => 'maxplayers',
104
            'numplayers' => 'numplayers',
105
        ],
106
    ];
107
108
    public function beforeSend(Server $server)
109
    {
110
111
        $this->realIp = $server->ip;
112
        $this->realPortQuery = $server->port_query;
113
114
        // Override the existing settings
115
        /*
116
        $master = array(    0 => array ( "ip" => "176.57.138.2",    "port" => 1040 ), 
117
                            1 => array ( "ip" => "206.189.248.133", "port" => 1040 ), 
118
                            2 => array ( "ip" => "172.107.16.215",  "port" => 1040 ) );
119
120
        shuffle($master); 
121
122
        print_r( $master );
123
124
        $server->ip = $master[0]['ip'];
125
126
        $server->port_query = $master[0]['port'];
127
        */
128
129
        //Set the Master-Server from SCUM, because random not work
130
        $server->ip = "172.107.16.215";
131
        $server->port_query = 1040;
132
    }
133
134
    /**
135
     * Process the response
136
     *
137
     * @return array
138
     * @throws Exception
139
     */
140
    public function processResponse()
141
    {
142
143
        $buffer_search = implode(array_map(function ($val) {
144
            return pack("C", $val);
145
        }, array_reverse(explode(".", $this->realIp)))).pack("S", ($this->realPortQuery + 2));
146
147
        // No response, assume offline
148
        if (empty($this->packets_response)) {
149
            return [
150
                'gq_address' => $this->realIp,
151
                'gq_port_client' => $this->realPortQuery,
152
                'gq_port_query' => ($this->realPortQuery + 2),
153
                'gq_online' => false
154
            ];
155
        }
156
157
        $buffer = new Buffer( implode('', $this->packets_response) );
158
159
        if (str_contains( $buffer->getData() , $buffer_search )) {
160
161
            $buffer->readString($buffer_search);
162
            $buffer->skip(5);
163
164
            $result = new Result();
165
166
            // Server is always dedicated and Mod is scum
167
            $result->add('dedicated', 1);
168
            $result->add('mod', 'scum');
169
170
            $result->add('gq_address', $this->realIp);
171
            $result->add('gq_port_client', $this->realPortQuery);
172
            $result->add('gq_port_query', ($this->realPortQuery + 2));
173
174
            // Add server items
175
            $result->add('hostname', $buffer->read(101) );
176
            $result->add('numplayers', $buffer->readInt8(2) );
0 ignored issues
show
Unused Code introduced by
The call to GameQ\Buffer::readInt8() has too many arguments starting with 2. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
            $result->add('numplayers', $buffer->/** @scrutinizer ignore-call */ readInt8(2) );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
177
            $result->add('maxplayers', $buffer->readInt8(2) );
178
179
            //Get From 109 until 110
180
            $time = base_convert(bin2hex( $buffer->read(1) ), 16, 10);
181
            $result->add('time', (strlen($time) != 1 ? $time : '0' . $time) . ':00');
182
183
            //Get From 111 until 112
184
            $buffer->skip(1);
185
            $pwd_bin = ( base_convert(bin2hex($buffer->read(1)), 16, 2 ));
186
            if(strlen($pwd_bin) >= 2){
187
                $result->add('password', ($pwd_bin[-2] == '1' ? 1 : 0) );
188
            }
189
190
            //Get From 120 until 126
191
            $buffer->skip(7);
192
            $byte8 = bin2hex($buffer->read(1));
193
            $byte7 = bin2hex($buffer->read(1));
194
            $byte6 = bin2hex($buffer->read(1));
195
            $byte5 = bin2hex($buffer->read(1));
196
            $byte4 = bin2hex($buffer->read(1));
197
            $byte3 = bin2hex($buffer->read(1));
198
            $byte2 = hexdec(bin2hex($buffer->read(1)));
199
            $byte1 = hexdec(bin2hex($buffer->read(1)));
200
201
            $result->add('version', $byte1 . "." . $byte2 . "." . hexdec( $byte3 . $byte4 ) . "." . hexdec( $byte5.$byte6.$byte7.$byte8 ));
202
203
            unset($buffer);
204
205
            return $result->fetch();
206
        } else {
207
208
            return [
209
                'gq_address' => $this->realIp,
210
                'gq_port_client' => $this->realPortQuery,
211
                'gq_port_query' => ($this->realPortQuery + 2),
212
                'gq_online' => false
213
            ];
214
        }
215
    }
216
}
217