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\Helpers\Str; |
23: | use GameQ\Result; |
24: | |
25: | /** |
26: | * Call of Duty: Modern Warfare 2 Protocol Class |
27: | * |
28: | * @package GameQ\Protocols |
29: | * @author Wilson Jesus <> |
30: | */ |
31: | class Codmw2 extends Quake3 |
32: | { |
33: | /** |
34: | * String name of this protocol class |
35: | * |
36: | * @var string |
37: | */ |
38: | protected $name = 'codmw2'; |
39: | |
40: | /** |
41: | * Longer string name of this protocol class |
42: | * |
43: | * @var string |
44: | */ |
45: | protected $name_long = "Call of Duty: Modern Warfare 2"; |
46: | |
47: | /** |
48: | * @param Buffer $buffer |
49: | * @return array |
50: | * @throws \GameQ\Exception\Protocol |
51: | */ |
52: | protected function processPlayers(Buffer $buffer) |
53: | { |
54: | // Temporarily cache players in order to remove last |
55: | $players = []; |
56: | |
57: | // Loop until we are out of data |
58: | while ($buffer->getLength()) { |
59: | // Make a new buffer with this block |
60: | $playerInfo = new Buffer($buffer->readString("\x0A")); |
61: | |
62: | // Read player info |
63: | $player = [ |
64: | 'frags' => $playerInfo->readString("\x20"), |
65: | 'ping' => $playerInfo->readString("\x20"), |
66: | ]; |
67: | |
68: | // Skip first " |
69: | $playerInfo->skip(1); |
70: | |
71: | // Add player name, encoded |
72: | $player['name'] = Str::isoToUtf8(trim(($playerInfo->readString('"')))); |
73: | |
74: | // Add player |
75: | $players[] = $player; |
76: | } |
77: | |
78: | // Remove last, empty player |
79: | array_pop($players); |
80: | |
81: | // Set the result to a new result instance |
82: | $result = new Result(); |
83: | |
84: | // Add players |
85: | $result->add('players', $players); |
86: | |
87: | // Add Playercount |
88: | $result->add('clients', count($players)); |
89: | |
90: | // Clear |
91: | unset($buffer, $players); |
92: | |
93: | return $result->fetch(); |
94: | } |
95: | } |
96: |