1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
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: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | class Bfbc2 extends Protocol |
38: | { |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | protected $packets = [ |
46: | self::PACKET_VERSION => "\x00\x00\x00\x00\x18\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00version\x00", |
47: | self::PACKET_STATUS => "\x00\x00\x00\x00\x1b\x00\x00\x00\x01\x00\x00\x00\x0a\x00\x00\x00serverInfo\x00", |
48: | self::PACKET_PLAYERS => "\x00\x00\x00\x00\x24\x00\x00\x00\x02\x00\x00\x00\x0b\x00\x00\x00listPlayers\x00\x03\x00\x00\x00\x61ll\x00", |
49: | ]; |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | protected $responses = [ |
57: | "processVersion", |
58: | "processDetails", |
59: | "processPlayers", |
60: | ]; |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | protected $transport = self::TRANSPORT_TCP; |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | protected $protocol = 'bfbc2'; |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | protected $name = 'bfbc2'; |
82: | |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | protected $name_long = "Battlefield Bad Company 2"; |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | protected $join_link = null; |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | protected $port_diff = 29321; |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | protected $normalize = [ |
111: | |
112: | 'general' => [ |
113: | |
114: | 'dedicated' => 'dedicated', |
115: | 'hostname' => 'hostname', |
116: | 'mapname' => 'map', |
117: | 'maxplayers' => 'max_players', |
118: | 'numplayers' => 'num_players', |
119: | 'password' => 'password', |
120: | ], |
121: | 'player' => [ |
122: | 'name' => 'name', |
123: | 'score' => 'score', |
124: | 'ping' => 'ping', |
125: | ], |
126: | 'team' => [ |
127: | 'score' => 'tickets', |
128: | ], |
129: | ]; |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | public function processResponse() |
138: | { |
139: | |
140: | |
141: | |
142: | |
143: | $results = []; |
144: | |
145: | |
146: | |
147: | foreach ($this->packets_response as $i => $packet) { |
148: | |
149: | $buffer = new Buffer($packet); |
150: | |
151: | |
152: | $buffer->skip(4); |
153: | |
154: | |
155: | $packetLength = $buffer->getLength(); |
156: | |
157: | |
158: | |
159: | if ($packetLength != ($buffer->readInt32() - 4)) { |
160: | throw new Exception(__METHOD__ . " packet length does not match expected length!"); |
161: | } |
162: | |
163: | |
164: | $results = array_merge( |
165: | $results, |
166: | call_user_func_array([$this, $this->responses[$i]], [$buffer]) |
167: | ); |
168: | } |
169: | |
170: | unset($buffer, $packetLength); |
171: | |
172: | return $results; |
173: | } |
174: | |
175: | |
176: | |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | |
186: | protected function decode(Buffer $buffer) |
187: | { |
188: | |
189: | $items = []; |
190: | |
191: | |
192: | $itemCount = $buffer->readInt32(); |
193: | |
194: | |
195: | for ($i = 0; $i < $itemCount; $i++) { |
196: | |
197: | $buffer->readInt32(); |
198: | |
199: | |
200: | $items[$i] = $buffer->readString(); |
201: | } |
202: | |
203: | return $items; |
204: | } |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | protected function processDetails(Buffer $buffer) |
214: | { |
215: | |
216: | |
217: | $items = $this->decode($buffer); |
218: | |
219: | |
220: | $result = new Result(); |
221: | |
222: | |
223: | $result->add('dedicated', 1); |
224: | |
225: | |
226: | $result->add('hostname', $items[1]); |
227: | $result->add('num_players', (int)$items[2]); |
228: | $result->add('max_players', (int)$items[3]); |
229: | $result->add('gametype', $items[4]); |
230: | $result->add('map', $items[5]); |
231: | $result->add('roundsplayed', (int)$items[6]); |
232: | $result->add('roundstotal', (int)$items[7]); |
233: | $result->add('num_teams', (int)$items[8]); |
234: | |
235: | |
236: | $index_current = 9; |
237: | |
238: | |
239: | $teamCount = $result->get('num_teams'); |
240: | |
241: | |
242: | for ($id = 1; $id <= $teamCount; $id++, $index_current++) { |
243: | |
244: | $result->addTeam('tickets', $items[$index_current]); |
245: | |
246: | $result->addTeam('id', $id); |
247: | } |
248: | |
249: | |
250: | $result->add('targetscore', (int)$items[$index_current]); |
251: | $result->add('online', 1); |
252: | $result->add('ranked', (($items[$index_current + 2] == 'true') ? 1 : 0)); |
253: | $result->add('punkbuster', (($items[$index_current + 3] == 'true') ? 1 : 0)); |
254: | $result->add('password', (($items[$index_current + 4] == 'true') ? 1 : 0)); |
255: | $result->add('uptime', (int)$items[$index_current + 5]); |
256: | $result->add('roundtime', (int)$items[$index_current + 6]); |
257: | $result->add('mod', $items[$index_current + 7]); |
258: | |
259: | $result->add('ip_port', $items[$index_current + 9]); |
260: | $result->add('punkbuster_version', $items[$index_current + 10]); |
261: | $result->add('join_queue', (($items[$index_current + 11] == 'true') ? 1 : 0)); |
262: | $result->add('region', $items[$index_current + 12]); |
263: | |
264: | unset($items, $index_current, $teamCount, $buffer); |
265: | |
266: | return $result->fetch(); |
267: | } |
268: | |
269: | |
270: | |
271: | |
272: | |
273: | |
274: | |
275: | |
276: | protected function processVersion(Buffer $buffer) |
277: | { |
278: | |
279: | $items = $this->decode($buffer); |
280: | |
281: | |
282: | $result = new Result(); |
283: | |
284: | $result->add('version', $items[2]); |
285: | |
286: | unset($buffer, $items); |
287: | |
288: | return $result->fetch(); |
289: | } |
290: | |
291: | |
292: | |
293: | |
294: | |
295: | |
296: | |
297: | |
298: | protected function processPlayers(Buffer $buffer) |
299: | { |
300: | |
301: | |
302: | $items = $this->decode($buffer); |
303: | |
304: | |
305: | $result = new Result(); |
306: | |
307: | |
308: | $numTags = $items[1]; |
309: | |
310: | |
311: | $tags = array_slice($items, 2, $numTags); |
312: | |
313: | |
314: | $playerCount = $items[$numTags + 2]; |
315: | |
316: | |
317: | for ($i = 0, $x = $numTags + 3; $i < $playerCount; $i++, $x += $numTags) { |
318: | |
319: | foreach ($tags as $index => $tag) { |
320: | $result->addPlayer($tag, $items[($x + $index)]); |
321: | } |
322: | } |
323: | |
324: | return $result->fetch(); |
325: | } |
326: | } |
327: | |