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