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