Passed
Push — v3 ( 836575...105154 )
by
unknown
02:40
created

Etqw::processStatus()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5.0164

Importance

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