Completed
Push — v3 ( e28a74...190f72 )
by Austin
07:50
created

Teeworlds::processAll()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 31
ccs 20
cts 20
cp 1
rs 8.8571
cc 2
eloc 20
nc 2
nop 1
crap 2
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
 * Teeworlds Protocol class
28
 *
29
 * Only supports versions > 0.5
30
 *
31
 * @author Austin Bischoff <[email protected]>
32
 * @author Marcel Bößendörfer <[email protected]>
33
 */
34
class Teeworlds extends Protocol
35
{
36
37
    /**
38
     * Array of packets we want to look up.
39
     * Each key should correspond to a defined method in this or a parent class
40
     *
41
     * @type array
42
     */
43
    protected $packets = [
44
        self::PACKET_ALL => "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x69\x65\x33\x05",
45
        // 0.5 Packet (not compatible, maybe some wants to implement "Teeworldsold")
46
        //self::PACKET_STATUS => "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFFgief",
47
    ];
48
49
    /**
50
     * Use the response flag to figure out what method to run
51
     *
52
     * @type array
53
     */
54
    protected $responses = [
55
        "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffinf35" => "processAll",
56
    ];
57
58
    /**
59
     * The query protocol used to make the call
60
     *
61
     * @type string
62
     */
63
    protected $protocol = 'teeworlds';
64
65
    /**
66
     * String name of this protocol class
67
     *
68
     * @type string
69
     */
70
    protected $name = 'teeworlds';
71
72
    /**
73
     * Longer string name of this protocol class
74
     *
75
     * @type string
76
     */
77
    protected $name_long = "Teeworlds Server";
78
79
    /**
80
     * The client join link
81
     *
82
     * @type string
83
     */
84
    protected $join_link = "steam://connect/%s:%d/";
85
86
    /**
87
     * Normalize settings for this protocol
88
     *
89
     * @type array
90
     */
91
    protected $normalize = [
92
        // General
93
        'general' => [
94
            // target       => source
95
            'dedicated'  => 'dedicated',
96
            'hostname'   => 'hostname',
97
            'mapname'    => 'map',
98
            'maxplayers' => 'num_players_total',
99
        ],
100
        // Individual
101
        'player'  => [
102
            'name'  => 'name',
103
            'score' => 'score',
104
        ],
105
    ];
106
107
    /**
108
     * Process the response
109
     *
110
     * @return array
111
     * @throws Exception
112
     */
113 5
    public function processResponse()
114
    {
115
        // Holds the results
116 5
        $results = [];
117
118
        // Iterate over the packets
119 5
        foreach ($this->packets_response as $response) {
120
            // Make a buffer
121 5
            $buffer = new Buffer($response);
122
123
            // Grab the header
124 5
            $header = $buffer->readString();
125
126
            // Figure out which packet response this is
127 5
            if (!array_key_exists($header, $this->responses)) {
128 2
                throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid");
129
            }
130
131
            // Now we need to call the proper method
132 3
            $results = array_merge(
133
                $results,
134 3
                call_user_func_array([$this, $this->responses[$header]], [$buffer])
135
            );
136
        }
137
138 3
        unset($buffer);
139
140 3
        return $results;
141
    }
142
143
    /**
144
     * Handle processing all of the data returned
145
     *
146
     * @param Buffer $buffer
147
     *
148
     * @return array
149
     */
150 3
    protected function processAll(Buffer $buffer)
151
    {
152
        // Set the result to a new result instance
153 3
        $result = new Result();
154
155
        // Always dedicated
156 3
        $result->add('dedicated', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, 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...
157
158 3
        $result->add('version', $buffer->readString());
159 3
        $result->add('hostname', $buffer->readString());
160 3
        $result->add('map', $buffer->readString());
161 3
        $result->add('game_descr', $buffer->readString());
162 3
        $result->add('flags', $buffer->readString()); // not sure about that
163 3
        $result->add('num_players', $buffer->readString());
164 3
        $result->add('maxplayers', $buffer->readString());
165 3
        $result->add('num_players_total', $buffer->readString());
166 3
        $result->add('maxplayers_total', $buffer->readString());
167
168
        // Players
169 3
        while ($buffer->getLength()) {
170 1
            $result->addPlayer('name', $buffer->readString());
171 1
            $result->addPlayer('clan', $buffer->readString());
172 1
            $result->addPlayer('flag', $buffer->readString());
173 1
            $result->addPlayer('score', $buffer->readString());
174 1
            $result->addPlayer('team', $buffer->readString());
175
        }
176
177 3
        unset($buffer);
178
179 3
        return $result->fetch();
180
    }
181
}
182