|
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
|
|
|
* @type 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
|
|
|
* @type array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $responses = [ |
|
51
|
|
|
"\xFF\xFFinfoExResponse" => "processStatus", |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The query protocol used to make the call |
|
56
|
|
|
* |
|
57
|
|
|
* @type string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $protocol = 'etqw'; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* String name of this protocol class |
|
63
|
|
|
* |
|
64
|
|
|
* @type string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $name = 'etqw'; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Longer string name of this protocol class |
|
70
|
|
|
* |
|
71
|
|
|
* @type string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $name_long = "Enemy Territory Quake Wars"; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Normalize settings for this protocol |
|
77
|
|
|
* |
|
78
|
|
|
* @type 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
|
3 |
|
public function processResponse() |
|
107
|
|
|
{ |
|
108
|
|
|
// In case it comes back as multiple packets (it shouldn't) |
|
109
|
3 |
|
$buffer = new Buffer(implode('', $this->packets_response)); |
|
110
|
|
|
|
|
111
|
|
|
// Figure out what packet response this is for |
|
112
|
3 |
|
$response_type = $buffer->readString(); |
|
113
|
|
|
|
|
114
|
|
|
// Figure out which packet response this is |
|
115
|
3 |
|
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
|
3 |
|
$results = call_user_func_array([$this, $this->responses[$response_type]], [$buffer]); |
|
121
|
|
|
|
|
122
|
3 |
|
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
|
|
|
*/ |
|
136
|
3 |
|
protected function processStatus(Buffer $buffer) |
|
137
|
|
|
{ |
|
138
|
|
|
// Set the result to a new result instance |
|
139
|
3 |
|
$result = new Result(); |
|
140
|
|
|
|
|
141
|
|
|
// Defaults |
|
142
|
3 |
|
$result->add('dedicated', 1); |
|
143
|
|
|
|
|
144
|
|
|
// Now burn the challenge, version and size |
|
145
|
3 |
|
$buffer->skip(16); |
|
146
|
|
|
|
|
147
|
|
|
// Key / value pairs |
|
148
|
3 |
|
while ($buffer->getLength()) { |
|
149
|
3 |
|
$var = str_replace('si_', '', $buffer->readString()); |
|
150
|
3 |
|
$val = $buffer->readString(); |
|
151
|
3 |
|
if (empty($var) && empty($val)) { |
|
152
|
3 |
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
// Add the server prop |
|
155
|
3 |
|
$result->add($var, $val); |
|
156
|
3 |
|
} |
|
157
|
|
|
// Now let's do the basic player info |
|
158
|
3 |
|
$this->parsePlayers($buffer, $result); |
|
159
|
|
|
|
|
160
|
|
|
// Now grab the rest of the server info |
|
161
|
3 |
|
$result->add('osmask', $buffer->readInt32()); |
|
162
|
3 |
|
$result->add('ranked', $buffer->readInt8()); |
|
163
|
3 |
|
$result->add('timeleft', $buffer->readInt32()); |
|
164
|
3 |
|
$result->add('gamestate', $buffer->readInt8()); |
|
165
|
3 |
|
$result->add('servertype', $buffer->readInt8()); |
|
166
|
|
|
|
|
167
|
|
|
// 0: regular server |
|
168
|
3 |
|
if ($result->get('servertype') == 0) { |
|
169
|
3 |
|
$result->add('interested_clients', $buffer->readInt8()); |
|
170
|
3 |
|
} else { |
|
171
|
|
|
// 1: tv server |
|
172
|
|
|
$result->add('connected_clients', $buffer->readInt32()); |
|
173
|
|
|
$result->add('max_clients', $buffer->readInt32()); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
// Now let's parse the extended player info |
|
177
|
3 |
|
$this->parsePlayersExtra($buffer, $result); |
|
178
|
|
|
|
|
179
|
3 |
|
unset($buffer); |
|
180
|
|
|
|
|
181
|
3 |
|
return $result->fetch(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Parse players out of the status ex response |
|
186
|
|
|
* |
|
187
|
|
|
* @param Buffer $buffer |
|
188
|
|
|
* @param Result $result |
|
189
|
|
|
*/ |
|
190
|
3 |
|
protected function parsePlayers(Buffer &$buffer, Result &$result) |
|
191
|
|
|
{ |
|
192
|
|
|
// By default there are 0 players |
|
193
|
3 |
|
$players = 0; |
|
194
|
|
|
|
|
195
|
|
|
// Iterate over the players until we run out |
|
196
|
3 |
|
while (($id = $buffer->readInt8()) != 32) { |
|
197
|
2 |
|
$result->addPlayer('id', $id); |
|
198
|
2 |
|
$result->addPlayer('ping', $buffer->readInt16()); |
|
199
|
2 |
|
$result->addPlayer('name', $buffer->readString()); |
|
200
|
2 |
|
$result->addPlayer('clantag_pos', $buffer->readInt8()); |
|
201
|
2 |
|
$result->addPlayer('clantag', $buffer->readString()); |
|
202
|
2 |
|
$result->addPlayer('bot', $buffer->readInt8()); |
|
203
|
2 |
|
$players++; |
|
204
|
2 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
// Let's add in the current players as a result |
|
207
|
3 |
|
$result->add('numplayers', $players); |
|
208
|
|
|
|
|
209
|
|
|
// Free some memory |
|
210
|
3 |
|
unset($id); |
|
211
|
3 |
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Handle parsing extra player data |
|
215
|
|
|
* |
|
216
|
|
|
* @param Buffer $buffer |
|
217
|
|
|
* @param Result $result |
|
218
|
|
|
*/ |
|
219
|
3 |
|
protected function parsePlayersExtra(Buffer &$buffer, Result &$result) |
|
220
|
|
|
{ |
|
221
|
|
|
// Iterate over the extra player info |
|
222
|
3 |
|
while (($id = $buffer->readInt8()) != 32) { |
|
223
|
2 |
|
$result->addPlayer('total_xp', $buffer->readFloat32()); |
|
224
|
2 |
|
$result->addPlayer('teamname', $buffer->readString()); |
|
225
|
2 |
|
$result->addPlayer('total_kills', $buffer->readInt32()); |
|
226
|
2 |
|
$result->addPlayer('total_deaths', $buffer->readInt32()); |
|
227
|
2 |
|
} |
|
228
|
|
|
|
|
229
|
|
|
// @todo: Add team stuff |
|
230
|
|
|
|
|
231
|
|
|
// Free some memory |
|
232
|
3 |
|
unset($id); |
|
233
|
3 |
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|