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: | class Etqw extends Protocol |
32: | { |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | protected $packets = [ |
41: | self::PACKET_STATUS => "\xFF\xFFgetInfoEx\x00\x00\x00\x00", |
42: | |
43: | ]; |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | protected $responses = [ |
51: | "\xFF\xFFinfoExResponse" => "processStatus", |
52: | ]; |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | protected $protocol = 'etqw'; |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | protected $name = 'etqw'; |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | protected $name_long = "Enemy Territory Quake Wars"; |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | protected $normalize = [ |
81: | |
82: | 'general' => [ |
83: | |
84: | 'gametype' => 'campaign', |
85: | 'hostname' => 'name', |
86: | 'mapname' => 'map', |
87: | 'maxplayers' => 'maxPlayers', |
88: | 'mod' => 'gamename', |
89: | 'numplayers' => 'numplayers', |
90: | 'password' => 'privateClients', |
91: | ], |
92: | |
93: | 'player' => [ |
94: | 'name' => 'name', |
95: | 'score' => 'score', |
96: | 'time' => 'time', |
97: | ], |
98: | ]; |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | public function processResponse() |
107: | { |
108: | |
109: | $buffer = new Buffer(implode('', $this->packets_response)); |
110: | |
111: | |
112: | $response_type = $buffer->readString(); |
113: | |
114: | |
115: | if (!array_key_exists($response_type, $this->responses)) { |
116: | throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid"); |
117: | } |
118: | |
119: | |
120: | $results = call_user_func_array([$this, $this->responses[$response_type]], [$buffer]); |
121: | |
122: | return $results; |
123: | } |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | protected function processStatus(Buffer $buffer) |
137: | { |
138: | |
139: | $result = new Result(); |
140: | |
141: | |
142: | $result->add('dedicated', 1); |
143: | |
144: | |
145: | $buffer->skip(16); |
146: | |
147: | |
148: | while ($buffer->getLength()) { |
149: | $var = str_replace('si_', '', $buffer->readString()); |
150: | $val = $buffer->readString(); |
151: | if (empty($var) && empty($val)) { |
152: | break; |
153: | } |
154: | |
155: | $result->add($var, $val); |
156: | } |
157: | |
158: | $this->parsePlayers($buffer, $result); |
159: | |
160: | |
161: | $result->add('osmask', $buffer->readInt32()); |
162: | $result->add('ranked', $buffer->readInt8()); |
163: | $result->add('timeleft', $buffer->readInt32()); |
164: | $result->add('gamestate', $buffer->readInt8()); |
165: | $result->add('servertype', $buffer->readInt8()); |
166: | |
167: | |
168: | if ($result->get('servertype') == 0) { |
169: | $result->add('interested_clients', $buffer->readInt8()); |
170: | } else { |
171: | |
172: | $result->add('connected_clients', $buffer->readInt32()); |
173: | $result->add('max_clients', $buffer->readInt32()); |
174: | } |
175: | |
176: | |
177: | $this->parsePlayersExtra($buffer, $result); |
178: | |
179: | unset($buffer); |
180: | |
181: | return $result->fetch(); |
182: | } |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | protected function parsePlayers(Buffer &$buffer, Result &$result) |
191: | { |
192: | |
193: | $players = 0; |
194: | |
195: | |
196: | while (($id = $buffer->readInt8()) != 32) { |
197: | $result->addPlayer('id', $id); |
198: | $result->addPlayer('ping', $buffer->readInt16()); |
199: | $result->addPlayer('name', $buffer->readString()); |
200: | $result->addPlayer('clantag_pos', $buffer->readInt8()); |
201: | $result->addPlayer('clantag', $buffer->readString()); |
202: | $result->addPlayer('bot', $buffer->readInt8()); |
203: | $players++; |
204: | } |
205: | |
206: | |
207: | $result->add('numplayers', $players); |
208: | |
209: | |
210: | unset($id); |
211: | } |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | protected function parsePlayersExtra(Buffer &$buffer, Result &$result) |
220: | { |
221: | |
222: | while (($id = $buffer->readInt8()) != 32) { |
223: | $result->addPlayer('total_xp', $buffer->readFloat32()); |
224: | $result->addPlayer('teamname', $buffer->readString()); |
225: | $result->addPlayer('total_kills', $buffer->readInt32()); |
226: | $result->addPlayer('total_deaths', $buffer->readInt32()); |
227: | } |
228: | |
229: | |
230: | |
231: | |
232: | unset($id); |
233: | } |
234: | } |
235: | |