Lines
100.00%
14 / 14
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Loots | 100.00% | 14 / 14 | 100.00% | 2 / 2 | 6 | 100.00% | 1 / 1 | |||
| me | n/a | 0 / 0 | n/a | 0 / 0 | 0 | |||||
| send | n/a | 0 / 0 | n/a | 0 / 0 | 0 | |||||
| collect | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| pickUp | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Bot\Abilities\Quests; | |
| 6 | ||
| 7 | use Generator; | |
| 8 | use Lorekeeper\Client\Character; | |
| 9 | use Lorekeeper\Client\Command\Command; | |
| 10 | use Lorekeeper\Client\Command\Inventory\PickUpDropCommand; | |
| 11 | use Lorekeeper\Client\Script\Await; | |
| 12 | use Lorekeeper\Client\Script\Instruction; | |
| 13 | use Lorekeeper\Client\Script\Send; | |
| 14 | use Lorekeeper\Client\Script\TimedOut; | |
| 15 | use Lorekeeper\Core\Item\ItemId; | |
| 16 | use Lorekeeper\Server\Event\Event; | |
| 17 | use Lorekeeper\Server\Event\Inventory\DropPickedUpEvent; | |
| 18 | use Lorekeeper\Server\Event\Inventory\ItemDroppedEvent; | |
| 19 | use Psl\DateTime\Duration; | |
| 20 | ||
| 21 | trait Loots | |
| 22 | { | |
| 23 | abstract protected function me(): Character; | |
| 24 | ||
| 25 | abstract protected function send(Command $command): Send; | |
| 26 | ||
| 27 | /** @return Generator<int, Instruction, null|Event|TimedOut, bool> */ | |
| 28 | protected function collect(ItemId $reward, int $heldBefore): Generator | |
| 29 | { | |
| 30 | if ($this->me()->hero()->inventory->quantity($reward) > $heldBefore) { | |
| 31 | return true; | |
| 32 | } | |
| 33 | ||
| 34 | $pickingUp = $this->pickUp($reward); | |
| 35 | yield from $pickingUp; | |
| 36 | ||
| 37 | return $pickingUp->getReturn(); | |
| 38 | } | |
| 39 | ||
| 40 | /** @return Generator<int, Instruction, null|Event|TimedOut, bool> */ | |
| 41 | protected function pickUp(ItemId $item): Generator | |
| 42 | { | |
| 43 | if ($this->me()->drops()->find($item) === null) { | |
| 44 | yield new Await([ItemDroppedEvent::class], Duration::seconds(10)); | |
| 45 | } | |
| 46 | ||
| 47 | if ($this->me()->drops()->find($item) === null) { | |
| 48 | return false; | |
| 49 | } | |
| 50 | ||
| 51 | /** @var Event|TimedOut $answer */ | |
| 52 | $answer = yield $this->send(new PickUpDropCommand($this->me()->hero()->spawnId, $item))->awaits( | |
| 53 | Duration::seconds(10), | |
| 54 | DropPickedUpEvent::class, | |
| 55 | ); | |
| 56 | ||
| 57 | return $answer instanceof DropPickedUpEvent && $answer->item->value === $item->value; | |
| 58 | } | |
| 59 | } |