chillerlan /
php-teamspeak
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @created 13.05.2021 |
||
| 4 | * @author smiley <[email protected]> |
||
| 5 | * @copyright 2021 smiley |
||
| 6 | * @license MIT |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace chillerlan\TeamspeakExample; |
||
| 10 | |||
| 11 | use chillerlan\Teamspeak\TS3Client; |
||
| 12 | use function str_repeat; |
||
| 13 | |||
| 14 | /** @var \chillerlan\Teamspeak\TS3Client $ts3 */ |
||
| 15 | |||
| 16 | require_once __DIR__.'/common.php'; |
||
| 17 | |||
| 18 | #while(true){ |
||
| 19 | echo ts3ChannelTree($ts3); |
||
| 20 | |||
| 21 | # sleep(10); |
||
| 22 | #} |
||
| 23 | |||
| 24 | exit; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Fetches the responses from the server and initializes the tree |
||
| 28 | */ |
||
| 29 | function ts3ChannelTree(TS3Client $ts3):string{ |
||
| 30 | $ts3->connect(); |
||
| 31 | |||
| 32 | $channels = $ts3->send('channellist')->parseList(); |
||
| 33 | $clients = $ts3->send('clientlist')->parseList(); |
||
| 34 | |||
| 35 | return ts3RenderChannels($channels, $clients); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Parses the responses |
||
| 40 | * |
||
| 41 | * (this is not very efficient as we loop multiple times over the same lists, but it serves the purpose) |
||
| 42 | */ |
||
| 43 | function ts3RenderChannels(array $channels, array $clients, int $cid = null, int $depth = null):string{ |
||
| 44 | $str = ''; |
||
| 45 | |||
| 46 | foreach($channels as $channel){ |
||
| 47 | |||
| 48 | if($channel->pid === null){ |
||
| 49 | $depth = 0; |
||
| 50 | } |
||
| 51 | |||
| 52 | if($channel->pid === $cid){ |
||
| 53 | $str .= str_repeat(' ', $depth).$channel->channel_name."\n"; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 54 | |||
| 55 | foreach($clients as $client){ |
||
| 56 | if($client->cid === $channel->cid){ |
||
| 57 | $str .= str_repeat(' ', $depth).' - '.$client->client_nickname."\n"; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | // parse nested channels recursively |
||
| 62 | $str .= ts3RenderChannels($channels, $clients, $channel->cid, $depth); |
||
| 63 | } |
||
| 64 | |||
| 65 | $depth++; |
||
| 66 | } |
||
| 67 | |||
| 68 | return $str; |
||
| 69 | } |
||
| 70 |