From e01d07894d9ba954d424da1d46562c46cbd6a37f Mon Sep 17 00:00:00 2001 From: Nadyita Date: Mon, 4 Mar 2024 11:34:43 +0100 Subject: [PATCH] Better buddy-interfaces --- src/Client/Multi.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Client/Multi.php b/src/Client/Multi.php index 99ab2ad..d9d708d 100644 --- a/src/Client/Multi.php +++ b/src/Client/Multi.php @@ -210,6 +210,13 @@ public function lookupCharacter(int $uid, bool $cacheOnly=false, ?string $worker return $character ?? $this->getBestWorker($worker)?->lookupCharacter($uid, $cacheOnly); } + /** Get the character of the worker with the least buddies on its list */ + public function getMostEmptyWorker(): string { + $fill = $this->getBuddylistSize(); + asort($fill); + return array_keys($fill)[0]; + } + /** * Get the current amount of used up buddylist slots per worker * @@ -223,6 +230,24 @@ public function getBuddylistSize(): array { return $result; } + /** Add a buddy to the most empty worker */ + public function buddyAdd(int $uid): void { + $this->write( + package: new Package\Out\BuddyAdd(charId: $uid), + worker: $this->getMostEmptyWorker(), + ); + } + + /** Remove a buddy from all workers */ + public function buddyRemove(int $uid): void { + foreach ($this->connections as $id => $connection) { + $buddies = $connection->getBuddylist(); + if (isset($buddies[$uid])) { + $connection->write(new Package\Out\BuddyRemove($uid)); + } + } + } + /** * Get the buddylist as uid => online *