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 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 General Public License for more details. |
14: | * |
15: | * You should have received a copy of the GNU 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\Result; |
23: | |
24: | /** |
25: | * Class Ship |
26: | * |
27: | * @package GameQ\Protocols |
28: | * |
29: | * @author Nikolay Ipanyuk <rostov114@gmail.com> |
30: | * @author Austin Bischoff <austin@codebeard.com> |
31: | */ |
32: | class Ship extends Source |
33: | { |
34: | |
35: | /** |
36: | * String name of this protocol class |
37: | * |
38: | * @type string |
39: | */ |
40: | protected $name = 'ship'; |
41: | |
42: | /** |
43: | * Longer string name of this protocol class |
44: | * |
45: | * @type string |
46: | */ |
47: | protected $name_long = "The Ship"; |
48: | |
49: | /** |
50: | * Specific player parse for The Ship |
51: | * |
52: | * Player response has unknown data after the last real player |
53: | * |
54: | * @param \GameQ\Buffer $buffer |
55: | * |
56: | * @return array |
57: | */ |
58: | protected function processPlayers(Buffer $buffer) |
59: | { |
60: | |
61: | // Set the result to a new result instance |
62: | $result = new Result(); |
63: | |
64: | // We need to read the number of players because this response has other data at the end usually |
65: | $num_players = $buffer->readInt8(); |
66: | |
67: | // Player count |
68: | $result->add('num_players', $num_players); |
69: | |
70: | // No players, no work |
71: | if ($num_players == 0) { |
72: | return $result->fetch(); |
73: | } |
74: | |
75: | // Players list |
76: | for ($player = 0; $player < $num_players; $player++) { |
77: | $result->addPlayer('id', $buffer->readInt8()); |
78: | $result->addPlayer('name', $buffer->readString()); |
79: | $result->addPlayer('score', $buffer->readInt32Signed()); |
80: | $result->addPlayer('time', $buffer->readFloat32()); |
81: | } |
82: | |
83: | // Extra data |
84: | if ($buffer->getLength() > 0) { |
85: | for ($player = 0; $player < $num_players; $player++) { |
86: | $result->addPlayer('deaths', $buffer->readInt32Signed()); |
87: | $result->addPlayer('money', $buffer->readInt32Signed()); |
88: | } |
89: | } |
90: | |
91: | unset($buffer); |
92: | |
93: | return $result->fetch(); |
94: | } |
95: | } |
96: |