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\Protocol; |
23: | use GameQ\Result; |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | class Ase extends Protocol |
32: | { |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | protected $packets = [ |
40: | self::PACKET_ALL => "s", |
41: | ]; |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | protected $protocol = 'ase'; |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | protected $name = 'ase'; |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | protected $name_long = "All-Seeing Eye"; |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | protected $normalize = [ |
70: | |
71: | 'general' => [ |
72: | |
73: | 'dedicated' => 'dedicated', |
74: | 'gametype' => 'gametype', |
75: | 'hostname' => 'servername', |
76: | 'mapname' => 'map', |
77: | 'maxplayers' => 'max_players', |
78: | 'mod' => 'game_dir', |
79: | 'numplayers' => 'num_players', |
80: | 'password' => 'password', |
81: | ], |
82: | |
83: | 'player' => [ |
84: | 'name' => 'name', |
85: | 'score' => 'score', |
86: | 'team' => 'team', |
87: | 'ping' => 'ping', |
88: | 'time' => 'time', |
89: | ], |
90: | ]; |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | public function processResponse() |
99: | { |
100: | |
101: | $buffer = new Buffer(implode('', $this->packets_response)); |
102: | |
103: | |
104: | if ($buffer->getLength() < 4) { |
105: | throw new \GameQ\Exception\Protocol(sprintf('%s The response from the server was empty.', __METHOD__)); |
106: | } |
107: | |
108: | |
109: | $header = $buffer->read(4); |
110: | |
111: | |
112: | if ($header !== 'EYE1') { |
113: | throw new \GameQ\Exception\Protocol(sprintf('%s The response header "%s" does not match expected "EYE1"', __METHOD__, $header)); |
114: | } |
115: | |
116: | |
117: | $result = new Result(); |
118: | |
119: | |
120: | $result->add('gamename', $buffer->readPascalString(1, true)); |
121: | $result->add('port', $buffer->readPascalString(1, true)); |
122: | $result->add('servername', $buffer->readPascalString(1, true)); |
123: | $result->add('gametype', $buffer->readPascalString(1, true)); |
124: | $result->add('map', $buffer->readPascalString(1, true)); |
125: | $result->add('version', $buffer->readPascalString(1, true)); |
126: | $result->add('password', $buffer->readPascalString(1, true)); |
127: | $result->add('num_players', $buffer->readPascalString(1, true)); |
128: | $result->add('max_players', $buffer->readPascalString(1, true)); |
129: | $result->add('dedicated', 1); |
130: | |
131: | |
132: | $this->processKeyValuePairs($buffer, $result); |
133: | |
134: | |
135: | $this->processPlayersAndTeams($buffer, $result); |
136: | |
137: | unset($buffer); |
138: | |
139: | return $result->fetch(); |
140: | } |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | protected function processKeyValuePairs(Buffer &$buffer, Result &$result) |
152: | { |
153: | |
154: | while ($buffer->getLength()) { |
155: | $key = $buffer->readPascalString(1, true); |
156: | |
157: | |
158: | if (empty($key)) { |
159: | break; |
160: | } |
161: | |
162: | |
163: | $result->add( |
164: | $key, |
165: | $buffer->readPascalString(1, true) |
166: | ); |
167: | } |
168: | |
169: | unset($key); |
170: | } |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | |
179: | protected function processPlayersAndTeams(Buffer &$buffer, Result &$result) |
180: | { |
181: | |
182: | while ($buffer->getLength()) { |
183: | |
184: | $flags = $buffer->readInt8(); |
185: | |
186: | |
187: | if ($flags & 1) { |
188: | $result->addPlayer('name', $buffer->readPascalString(1, true)); |
189: | } |
190: | if ($flags & 2) { |
191: | $result->addPlayer('team', $buffer->readPascalString(1, true)); |
192: | } |
193: | if ($flags & 4) { |
194: | $result->addPlayer('skin', $buffer->readPascalString(1, true)); |
195: | } |
196: | if ($flags & 8) { |
197: | $result->addPlayer('score', $buffer->readPascalString(1, true)); |
198: | } |
199: | if ($flags & 16) { |
200: | $result->addPlayer('ping', $buffer->readPascalString(1, true)); |
201: | } |
202: | if ($flags & 32) { |
203: | $result->addPlayer('time', $buffer->readPascalString(1, true)); |
204: | } |
205: | } |
206: | } |
207: | } |
208: | |