Passed
Push — v3 ( 37cdd6...e623f3 )
by
unknown
29:19 queued 26:35
created

Etqw::processStatus()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5.0128

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 46
ccs 23
cts 25
cp 0.92
rs 9.2408
cc 5
nc 6
nop 1
crap 5.0128
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\Exception\Protocol as Exception;
23
use GameQ\Protocol;
24
use GameQ\Result;
25
26
/**
27
 * Enemy Territory Quake Wars Protocol Class
28
 *
29
 * @author Austin Bischoff <[email protected]>
30
 */
31
class Etqw extends Protocol
32
{
33
34
    /**
35
     * Array of packets we want to look up.
36
     * Each key should correspond to a defined method in this or a parent class
37
     *
38
     * @var array
39
     */
40
    protected $packets = [
41
        self::PACKET_STATUS => "\xFF\xFFgetInfoEx\x00\x00\x00\x00",
42
        //self::PACKET_STATUS => "\xFF\xFFgetInfo\x00\x00\x00\x00\x00",
43
    ];
44
45
    /**
46
     * Use the response flag to figure out what method to run
47
     *
48
     * @var array
49
     */
50
    protected $responses = [
51
        "\xFF\xFFinfoExResponse" => "processStatus",
52
    ];
53
54
    /**
55
     * The query protocol used to make the call
56
     *
57
     * @var string
58
     */
59
    protected $protocol = 'etqw';
60
61
    /**
62
     * String name of this protocol class
63
     *
64
     * @var string
65
     */
66
    protected $name = 'etqw';
67
68
    /**
69
     * Longer string name of this protocol class
70
     *
71
     * @var string
72
     */
73
    protected $name_long = "Enemy Territory Quake Wars";
74
75
    /**
76
     * Normalize settings for this protocol
77
     *
78
     * @var array
79
     */
80
    protected $normalize = [
81
        // General
82
        'general' => [
83
            // target       => source
84
            'gametype'   => 'campaign',
85
            'hostname'   => 'name',
86
            'mapname'    => 'map',
87
            'maxplayers' => 'maxPlayers',
88
            'mod'        => 'gamename',
89
            'numplayers' => 'numplayers',
90
            'password'   => 'privateClients',
91
        ],
92
        // Individual
93
        'player'  => [
94
            'name'  => 'name',
95
            'score' => 'score',
96
            'time'  => 'time',
97
        ],
98
    ];
99
100
    /**
101
     * Process the response
102
     *
103
     * @return array
104
     * @throws \GameQ\Exception\Protocol
105
     */
106 18
    public function processResponse()
107
    {
108
        // In case it comes back as multiple packets (it shouldn't)
109 18
        $buffer = new Buffer(implode('', $this->packets_response));
110
111
        // Figure out what packet response this is for
112 18
        $response_type = $buffer->readString();
113
114
        // Figure out which packet response this is
115 18
        if (!array_key_exists($response_type, $this->responses)) {
116
            throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid");
117
        }
118
119
        // Offload the call
120 18
        $results = call_user_func_array([$this, $this->responses[$response_type]], [$buffer]);
121
122 18
        return $results;
123
    }
124
125
    /*
126
     * Internal methods
127
     */
128
129
    /**
130
     * Handle processing the status response
131
     *
132
     * @param Buffer $buffer
133
     *
134
     * @return array
135
     * @throws \GameQ\Exception\Protocol
136
     */
137 18
    protected function processStatus(Buffer $buffer)
138
    {
139
        // Set the result to a new result instance
140 18
        $result = new Result();
141
142
        // Defaults
143 18
        $result->add('dedicated', 1);
144
145
        // Now burn the challenge, version and size
146 18
        $buffer->skip(16);
147
148
        // Key / value pairs
149 18
        while ($buffer->getLength()) {
150 18
            $var = str_replace('si_', '', $buffer->readString());
151 18
            $val = $buffer->readString();
152 18
            if (empty($var) && empty($val)) {
153 18
                break;
154
            }
155
            // Add the server prop
156 18
            $result->add($var, $val);
157 3
        }
158
        // Now let's do the basic player info
159 18
        $this->parsePlayers($buffer, $result);
160
161
        // Now grab the rest of the server info
162 18
        $result->add('osmask', $buffer->readInt32());
163 18
        $result->add('ranked', $buffer->readInt8());
164 18
        $result->add('timeleft', $buffer->readInt32());
165 18
        $result->add('gamestate', $buffer->readInt8());
166 18
        $result->add('servertype', $buffer->readInt8());
167
168
        // 0: regular server
169 18
        if ($result->get('servertype') == 0) {
170 18
            $result->add('interested_clients', $buffer->readInt8());
171 3
        } else {
172
            // 1: tv server
173
            $result->add('connected_clients', $buffer->readInt32());
174
            $result->add('max_clients', $buffer->readInt32());
175
        }
176
177
        // Now let's parse the extended player info
178 18
        $this->parsePlayersExtra($buffer, $result);
179
180 18
        unset($buffer);
181
182 18
        return $result->fetch();
183
    }
184
185
    /**
186
     * Parse players out of the status ex response
187
     *
188
     * @param Buffer $buffer
189
     * @param Result $result
190
     * @return void
191
     * @throws \GameQ\Exception\Protocol
192
     */
193 18
    protected function parsePlayers(Buffer &$buffer, Result &$result)
194
    {
195
        // By default there are 0 players
196 18
        $players = 0;
197
198
        // Iterate over the players until we run out
199 18
        while (($id = $buffer->readInt8()) != 32) {
200 12
            $result->addPlayer('id', $id);
201 12
            $result->addPlayer('ping', $buffer->readInt16());
202 12
            $result->addPlayer('name', $buffer->readString());
203 12
            $result->addPlayer('clantag_pos', $buffer->readInt8());
204 12
            $result->addPlayer('clantag', $buffer->readString());
205 12
            $result->addPlayer('bot', $buffer->readInt8());
206 12
            $players++;
207 2
        }
208
209
        // Let's add in the current players as a result
210 18
        $result->add('numplayers', $players);
211
212
        // Free some memory
213 18
        unset($id);
214 9
    }
215
216
    /**
217
     * Handle parsing extra player data
218
     *
219
     * @param Buffer $buffer
220
     * @param Result $result
221
     * @return void
222
     * @throws \GameQ\Exception\Protocol
223
     */
224 18
    protected function parsePlayersExtra(Buffer &$buffer, Result &$result)
225
    {
226
        // Iterate over the extra player info
227 18
        while (($id = $buffer->readInt8()) != 32) {
228 12
            $result->addPlayer('total_xp', $buffer->readFloat32());
229 12
            $result->addPlayer('teamname', $buffer->readString());
230 12
            $result->addPlayer('total_kills', $buffer->readInt32());
231 12
            $result->addPlayer('total_deaths', $buffer->readInt32());
232 2
        }
233
234
        // @todo: Add team stuff
235
236
        // Free some memory
237 18
        unset($id);
238 9
    }
239
}
240