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\Helpers\Str; |
24: | use GameQ\Protocol; |
25: | use GameQ\Result; |
26: | use GameQ\Server; |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | class Teamspeak2 extends Protocol |
39: | { |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | protected $packets = [ |
47: | self::PACKET_DETAILS => "sel %d\x0asi\x0a", |
48: | self::PACKET_CHANNELS => "sel %d\x0acl\x0a", |
49: | self::PACKET_PLAYERS => "sel %d\x0apl\x0a", |
50: | ]; |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | protected $transport = self::TRANSPORT_TCP; |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | |
64: | protected $protocol = 'teamspeak2'; |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | protected $name = 'teamspeak2'; |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | protected $name_long = "Teamspeak 2"; |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | protected $join_link = "teamspeak://%s:%d/"; |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | protected $normalize = [ |
93: | |
94: | 'general' => [ |
95: | 'dedicated' => 'dedicated', |
96: | 'hostname' => 'server_name', |
97: | 'password' => 'server_password', |
98: | 'numplayers' => 'server_currentusers', |
99: | 'maxplayers' => 'server_maxusers', |
100: | ], |
101: | |
102: | 'player' => [ |
103: | 'id' => 'p_id', |
104: | 'team' => 'c_id', |
105: | 'name' => 'nick', |
106: | ], |
107: | |
108: | 'team' => [ |
109: | 'id' => 'id', |
110: | 'name' => 'name', |
111: | ], |
112: | ]; |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | public function beforeSend(Server $server) |
122: | { |
123: | |
124: | if (!isset($this->options[Server::SERVER_OPTIONS_QUERY_PORT]) |
125: | || empty($this->options[Server::SERVER_OPTIONS_QUERY_PORT]) |
126: | ) { |
127: | throw new Exception(__METHOD__ . " Missing required setting '" . Server::SERVER_OPTIONS_QUERY_PORT . "'."); |
128: | } |
129: | |
130: | |
131: | foreach ($this->packets as $packet_type => $packet) { |
132: | |
133: | $this->packets[$packet_type] = sprintf($packet, $server->portClient()); |
134: | } |
135: | } |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | public function processResponse() |
144: | { |
145: | |
146: | $buffer = new Buffer(implode('', $this->packets_response)); |
147: | |
148: | |
149: | if (($header = trim($buffer->readString("\n"))) !== '[TS]') { |
150: | throw new Exception(__METHOD__ . " Expected header '{$header}' does not match expected '[TS]'."); |
151: | } |
152: | |
153: | |
154: | $sections = array_filter(explode("OK", $buffer->getBuffer()), function ($value) { |
155: | $value = trim($value); |
156: | |
157: | return !empty($value); |
158: | }); |
159: | |
160: | |
161: | $sections = array_map('trim', $sections); |
162: | |
163: | |
164: | $result = new Result(); |
165: | |
166: | |
167: | foreach ($sections as $section) { |
168: | |
169: | $check = substr($section, 0, 7); |
170: | |
171: | |
172: | if ($check == 'server_') { |
173: | |
174: | $this->processDetails($section, $result); |
175: | } elseif ($check == "id\tcode") { |
176: | |
177: | $this->processChannels($section, $result); |
178: | } elseif ($check == "p_id\tc_") { |
179: | |
180: | $this->processPlayers($section, $result); |
181: | } |
182: | } |
183: | |
184: | unset($buffer, $sections, $section, $check); |
185: | |
186: | return $result->fetch(); |
187: | } |
188: | |
189: | |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | protected function processDetails($data, Result &$result) |
201: | { |
202: | |
203: | $buffer = new Buffer($data); |
204: | |
205: | |
206: | $result->add('dedicated', 1); |
207: | |
208: | |
209: | while ($buffer->getLength()) { |
210: | |
211: | $row = trim($buffer->readString("\n")); |
212: | |
213: | |
214: | list($key, $value) = explode('=', $row, 2); |
215: | |
216: | |
217: | $result->add($key, Str::isoToUtf8($value)); |
218: | } |
219: | |
220: | unset($data, $buffer, $row, $key, $value); |
221: | } |
222: | |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | protected function processChannels($data, Result &$result) |
232: | { |
233: | |
234: | $buffer = new Buffer($data); |
235: | |
236: | |
237: | $columns = explode("\t", trim($buffer->readString("\n")), 9); |
238: | |
239: | |
240: | while ($buffer->getLength()) { |
241: | |
242: | $row = trim($buffer->readString("\n")); |
243: | |
244: | |
245: | $data = array_combine($columns, explode("\t", $row, 9)); |
246: | |
247: | foreach ($data as $key => $value) { |
248: | |
249: | $result->addTeam($key, Str::isoToUtf8($value)); |
250: | } |
251: | } |
252: | |
253: | unset($data, $buffer, $row, $columns, $key, $value); |
254: | } |
255: | |
256: | |
257: | |
258: | |
259: | |
260: | |
261: | |
262: | |
263: | |
264: | protected function processPlayers($data, Result &$result) |
265: | { |
266: | |
267: | $buffer = new Buffer($data); |
268: | |
269: | |
270: | $columns = explode("\t", trim($buffer->readString("\n")), 16); |
271: | |
272: | |
273: | while ($buffer->getLength()) { |
274: | |
275: | $row = trim($buffer->readString("\n")); |
276: | |
277: | |
278: | $data = array_combine($columns, explode("\t", $row, 16)); |
279: | |
280: | foreach ($data as $key => $value) { |
281: | |
282: | $result->addPlayer($key, Str::isoToUtf8($value)); |
283: | } |
284: | } |
285: | |
286: | unset($data, $buffer, $row, $columns, $key, $value); |
287: | } |
288: | } |
289: | |