Skip to content

Commit

Permalink
Development (#5)
Browse files Browse the repository at this point in the history
* Bugfix

* Php Stan fix

* Updated dependencies

* * Added possibility to specify channels names by client
* Updated dependencies

---------

Co-authored-by: Dominik süßenbach <dominik.suessenbach@compleet.com>
  • Loading branch information
Domenikus and Dominik süßenbach authored Aug 29, 2023
1 parent 061dc3f commit ed9b6b5
Show file tree
Hide file tree
Showing 4 changed files with 672 additions and 551 deletions.
5 changes: 4 additions & 1 deletion app/Providers/ClientServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function register(): void
$defaultChannel = config('teamspeak.default_channel');
$channelNameLists = config('channel-names.lists');
$channelListName = config('channel-names.default');
$channelNameClientLists = config('channel-names.clients');

if (
(! is_numeric($parentChannel)) ||
Expand All @@ -34,6 +35,7 @@ public function register(): void
(! is_numeric($channelNeededSubscribePower) && ! is_null($channelNeededSubscribePower)) ||
(! is_numeric($defaultChannel) && ! is_null($defaultChannel)) ||
(! is_array($channelNameLists)) ||
(! is_array($channelNameClientLists)) ||
(! is_string($channelListName)) ||
(empty($channelNameLists[$channelListName]))
) {
Expand All @@ -49,10 +51,11 @@ public function register(): void
/** @var int|null $channelNeededSubscribePower */
/** @var int|null $defaultChannel */
$this->app->bind(ClientServiceInterface::class, function () use (
$channelNameClientLists,
$defaultChannel,
$channelNeededSubscribePower,
$channelNeededJoinPower, $channelClientLimit, $channelAdminGroupId, $parentChannel, $channelNames) {
return new ClientService((int) $parentChannel, $channelNames, $defaultChannel, $channelClientLimit, $channelAdminGroupId, $channelNeededJoinPower, $channelNeededSubscribePower);
return new ClientService((int) $parentChannel, $channelNames, $defaultChannel, $channelClientLimit, $channelAdminGroupId, $channelNeededJoinPower, $channelNeededSubscribePower, $channelNameClientLists);
});
}
}
21 changes: 16 additions & 5 deletions app/Services/ClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class ClientService implements ClientServiceInterface

protected int $parentChannel;

public function __construct(int $parentChannel, array $channelNames, ?int $defaultChannel, ?int $channelClientLimit = null, ?int $channelAdminGroupId = null, ?int $channelNeededJoinPower = null, ?int $channelNeededSubscribePower = null)
protected array $channelNameClientLists;

public function __construct(int $parentChannel, array $channelNames, ?int $defaultChannel, int $channelClientLimit = null, int $channelAdminGroupId = null, int $channelNeededJoinPower = null, int $channelNeededSubscribePower = null, array $channelNameClientLists = [])
{
$this->parentChannel = $parentChannel;
$this->channelNames = $channelNames;
Expand All @@ -29,6 +31,7 @@ public function __construct(int $parentChannel, array $channelNames, ?int $defau
$this->channelAdminGroupId = $channelAdminGroupId;
$this->channelNeededJoinPower = $channelNeededJoinPower;
$this->channelNeededSubscribePower = $channelNeededSubscribePower;
$this->channelNameClientLists = $channelNameClientLists;
}

public function handleClientMove(int $clientId, int $targetChannelId): void
Expand All @@ -40,7 +43,15 @@ public function handleClientMove(int $clientId, int $targetChannelId): void
return;
}

$newChannelId = TeamspeakGateway::createChannel($this->generateChannelName(), $this->parentChannel, $this->channelClientLimit);
/** @phpstan-ignore-next-line */
$clientIdentityId = $client['client_unique_identifier']->toString();
if (isset($this->channelNameClientLists[$clientIdentityId])) {
$channelName = $this->generateChannelName($this->channelNameClientLists[$clientIdentityId]);
} else {
$channelName = $this->generateChannelName($this->channelNames);
}
$newChannelId = TeamspeakGateway::createChannel($channelName, $this->parentChannel, $this->channelClientLimit);

if (! $newChannelId) {
return;
}
Expand All @@ -63,14 +74,14 @@ public function handleClientMove(int $clientId, int $targetChannelId): void
}
}

protected function generateChannelName(string $channelName = ''): string
protected function generateChannelName(array $listOfNames, string $channelName = ''): string
{
$attempts = 0;

do {
$channelName .= $this->channelNames[array_rand($this->channelNames)];
$channelName .= $listOfNames[array_rand($listOfNames)];
if ($attempts >= count($this->channelNames)) {
$this->generateChannelName($channelName);
$this->generateChannelName($listOfNames, $channelName);
}

$attempts++;
Expand Down
Loading

0 comments on commit ed9b6b5

Please sign in to comment.