Skip to content

Commit

Permalink
Go back to 1s wait for ready
Browse files Browse the repository at this point in the history
The old way to detect whether the bot is ready
was to wait for a reply to our "ping" package.
Sadly, with a bigger buddylist, this reply came
in the middle of the initial buddylist update,
so it was unsuited for detecting readiness.
We're now treating the client ot be ready if
no package was received for 1s.
  • Loading branch information
Nadyita committed Dec 12, 2024
1 parent 86f29f5 commit 9d1ce8a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Client/SingleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Amp\DeferredFuture;
use AO\Internal\SendQueue;
use AO\Package\Out\Pong;
use AO\{
AccountUnfreezer,
Connection,
Expand Down Expand Up @@ -52,6 +51,8 @@ class SingleClient {
private float $lastPackage = 0;
private float $lastPong = 0;

private ?string $readyHandler = null;

/**
* True when the bot has finished receiving the initial
* buddylist updates which don't correspond to state changes.
Expand Down Expand Up @@ -396,6 +397,12 @@ protected function handleIncomingPackage(Package\InPackage $package): void {
} elseif ($package instanceof In\GroupLeft) {
$this->handleGroupLeft($package);
}
if (!$this->isReady) {
if (isset($this->readyHandler)) {
EventLoop::cancel($this->readyHandler);
}
$this->readyHandler = EventLoop::delay(1, $this->triggerOnReady(...));
}
}

protected function handleCharacterLookupResult(In\CharacterLookupResult $package): void {
Expand Down Expand Up @@ -436,9 +443,6 @@ protected function handleBuddyRemoved(In\BuddyRemoved $package): void {
}

protected function handleGroupJoined(In\GroupJoined $package): void {
if (!$this->isReady && $this->lastPong < 1) {
$this->write(new Pong('ready'));
}
$group = new Group(
id: $package->groupId,
name: $package->groupName,
Expand Down

0 comments on commit 9d1ce8a

Please sign in to comment.