Skip to content

Commit

Permalink
Rename BuddyAdded
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadyita committed Mar 3, 2024
1 parent 509b75b commit 0709034
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/AccountUnfreezer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace AO;

interface AccountUnfreezer {
public function unfreeze(): bool;
}
23 changes: 18 additions & 5 deletions src/Client/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace AO\Client;

use function Amp\delay;
use AO\Package\{In, Out};
use AO\{AccountFrozenException, CharacterNotFoundException, Connection, Encryption, Group, LoginException, Package, Parser, Utils, WrongPacketOrderException};
use AO\{AccountFrozenException, AccountUnfreezer, CharacterNotFoundException, Connection, Encryption, Group, LoginException, Package, Parser, Utils, WrongPacketOrderException};
use Closure;
use Nadylib\LeakyBucket\LeakyBucket;
use Psr\Log\LoggerInterface;
use Revolt\EventLoop;

use Throwable;

class Basic {
Expand Down Expand Up @@ -54,6 +56,7 @@ public function __construct(
private Parser $parser,
private ?LoggerInterface $logger=null,
?LeakyBucket $bucket=null,
private ?AccountUnfreezer $accountUnfreezer=null,
) {
$this->bucket = $bucket ?? new LeakyBucket(size: 5, refillDelay: 1.0);
}
Expand Down Expand Up @@ -268,6 +271,16 @@ public function login(string $username, string $password, string $character): vo
if ($response instanceof In\LoginError) {
$errorMsgs = explode("|", $response->error);
if (count($errorMsgs) === 3 && $errorMsgs[2] === "/Account system denies login") {
if (isset($this->accountUnfreezer) && $this->accountUnfreezer->unfreeze()) {
$this->logger?->notice("Account {account} successfully unfrozen, waiting {delay}s", [
"account" => $username,
"delay" => 5,
]);
$this->accountUnfreezer = null;
delay(5);
$this->login(...func_get_args());
return;
}
$parts = explode(": ", $errorMsgs[0] ?? "");
throw new AccountFrozenException($parts[1] ?? "");
}
Expand Down Expand Up @@ -318,8 +331,8 @@ protected function handleIncomingPackage(Package\In $package): void {
$this->handleCharacterLookupResult($package);
} elseif ($package instanceof In\CharacterName) {
$this->handleCharacterName($package);
} elseif ($package instanceof In\BuddyAdded) {
$this->handleBuddyAdded($package);
} elseif ($package instanceof In\BuddyState) {
$this->handleBuddyState($package);
} elseif ($package instanceof In\BuddyRemoved) {
$this->handleBuddyRemoved($package);
} elseif ($package instanceof In\GroupJoined) {
Expand Down Expand Up @@ -354,8 +367,8 @@ protected function handleCharacterName(In\CharacterName $package): void {
$this->uidToName[$package->charId] = $package->name;
}

protected function handleBuddyAdded(In\BuddyAdded $package): void {
$this->logger?->debug("In\\BuddyAdded received, putting into buddylist with status \"{online}\"", [
protected function handleBuddyState(In\BuddyState $package): void {
$this->logger?->debug("In\\BuddyState received, putting into buddylist with status \"{online}\"", [
"online" => ($package->online ? "online" : "offline"),
]);
$this->buddylist[$package->charId] = $package->online;
Expand Down
10 changes: 6 additions & 4 deletions src/Client/WorkerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

class WorkerConfig {
public function __construct(
public int $dimension,
public string $username,
public string $password,
public string $character,
public readonly int $dimension,
public readonly string $username,
public readonly string $password,
public readonly string $character,
public readonly ?string $unfreezeLogin=null,
public readonly ?string $unfreezePassword=null,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use AO\Package;

class BuddyAdded extends Package\In {
class BuddyState extends Package\In {
public function __construct(
public int $charId,
public bool $online,
Expand Down
2 changes: 1 addition & 1 deletion src/Package/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function classIn(): string {
static::BroadcastMessage => In\BroadcastMessage::class,
static::SimpleSystemMessage => In\SimpleSystemMessage::class,
static::SystemMessage => In\SimpleSystemMessage::class,
static::BuddyAdd => In\BuddyAdded::class,
static::BuddyAdd => In\BuddyState::class,
static::BuddyRemove => In\BuddyRemoved::class,
static::PrivateChannelInvite => In\PrivateChannelInvited::class,
static::PrivateChannelKick => In\PrivateChannelKicked::class,
Expand Down
20 changes: 10 additions & 10 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function exampleBinaryPacketsIn(): array {
return [
[
new BinaryPackage\In(type: Package\Type::BuddyAdd, length: 15, body: hex2bin("0000303900000001000101")),
In\BuddyAdded::class,
In\BuddyState::class,
],
];
}
Expand All @@ -50,7 +50,7 @@ public static function examplePackages(): array {
return [
"In\\Ping" => [new In\Ping(extra: '')],
"Out\\Ping" => [new Out\Pong(extra: random_bytes(random_int(1, 64)))],
"In\\BuddyAdded" => [new In\BuddyAdded(charId: random_int(1, 2^32), online: false, extra: "abc")],
"In\\BuddyAdded" => [new In\BuddyState(charId: random_int(1, 2^32), online: false, extra: "abc")],
"Out\\LoginRequest" => [new Out\LoginRequest(username: "Zero", key: "OMGsupers3cr3t", zero: 0)],
"In\\GroupAnnounced" => [new In\GroupJoined(groupId: new Group\Id(type: Group\Type::Org, number: 12345), groupName: "Public", flags: 0, unknown: "")],
"Out\\PrivateGroupMessage" => [new Out\PrivateChannelMessage(channelId: 1, message: "lol?")],
Expand Down Expand Up @@ -108,21 +108,21 @@ public function testFromExampleLogin(): void {
In\CharacterName::class,
In\BroadcastMessage::class,
In\CharacterName::class,
In\BuddyAdded::class,
In\BuddyState::class,
In\CharacterName::class,
In\BuddyAdded::class,
In\BuddyState::class,
In\CharacterName::class,
In\BuddyAdded::class,
In\BuddyState::class,
In\CharacterName::class,
In\BuddyAdded::class,
In\BuddyState::class,
In\CharacterName::class,
In\BuddyAdded::class,
In\BuddyState::class,
In\CharacterName::class,
In\BuddyAdded::class,
In\BuddyState::class,
In\CharacterName::class,
In\BuddyAdded::class,
In\BuddyState::class,
In\CharacterName::class,
In\BuddyAdded::class,
In\BuddyState::class,
In\GroupJoined::class,
In\GroupJoined::class,
In\GroupJoined::class,
Expand Down

0 comments on commit 0709034

Please sign in to comment.