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: * String name of this protocol class
36: *
37: * @var string
38: */
39: protected $name = 'ship';
40:
41: /**
42: * Longer string name of this protocol class
43: *
44: * @var string
45: */
46: protected $name_long = "The Ship";
47:
48: /**
49: * Specific player parse for The Ship
50: *
51: * Player response has unknown data after the last real player
52: *
53: * @param \GameQ\Buffer $buffer
54: * @return array
55: * @throws \GameQ\Exception\Protocol
56: */
57: protected function processPlayers(Buffer $buffer)
58: {
59: // Set the result to a new result instance
60: $result = new Result();
61:
62: // We need to read the number of players because this response has other data at the end usually
63: $num_players = $buffer->readInt8();
64:
65: // Player count
66: $result->add('num_players', $num_players);
67:
68: // No players, no work
69: if ($num_players == 0) {
70: return $result->fetch();
71: }
72:
73: // Players list
74: for ($player = 0; $player < $num_players; $player++) {
75: $result->addPlayer('id', $buffer->readInt8());
76: $result->addPlayer('name', $buffer->readString());
77: $result->addPlayer('score', $buffer->readInt32Signed());
78: $result->addPlayer('time', $buffer->readFloat32());
79: }
80:
81: // Extra data
82: if ($buffer->getLength() > 0) {
83: for ($player = 0; $player < $num_players; $player++) {
84: $result->addPlayer('deaths', $buffer->readInt32Signed());
85: $result->addPlayer('money', $buffer->readInt32Signed());
86: }
87: }
88:
89: unset($buffer);
90:
91: return $result->fetch();
92: }
93: }
94: