Lines
100.00%
22 / 22
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Hunts | 100.00% | 22 / 22 | 100.00% | 2 / 2 | 8 | 100.00% | 1 / 1 | |||
| hunt | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 7 | |||||
| holds | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Bot\Abilities\Moves; | |
| 6 | ||
| 7 | use Generator; | |
| 8 | use Lorekeeper\Bot\Abilities\Quests\Loots; | |
| 9 | use Lorekeeper\Client\Command\Combat\Attack; | |
| 10 | use Lorekeeper\Client\Script\Instruction; | |
| 11 | use Lorekeeper\Client\Script\TimedOut; | |
| 12 | use Lorekeeper\Core\Item\ItemId; | |
| 13 | use Lorekeeper\Core\Support\Quantity; | |
| 14 | use Lorekeeper\Core\World\Map\MapSpot; | |
| 15 | use Lorekeeper\Server\Event\Event; | |
| 16 | use Psl\DateTime\Duration; | |
| 17 | use RuntimeException; | |
| 18 | ||
| 19 | trait Hunts | |
| 20 | { | |
| 21 | use Kills; | |
| 22 | use Loots; | |
| 23 | ||
| 24 | /** | |
| 25 | * @throws RuntimeException | |
| 26 | * | |
| 27 | * @return Generator<int, Instruction, null|Event|TimedOut, bool> | |
| 28 | */ | |
| 29 | protected function hunt(MapSpot $spot, Attack $attack, ItemId $item, Quantity $wanted, Duration $limit): Generator | |
| 30 | { | |
| 31 | $deadline = $this->now()->plus($limit); | |
| 32 | while (!$this->holds($item, $wanted) && !$this->now()->after($deadline)) { | |
| 33 | yield from $this->kill( | |
| 34 | $spot, | |
| 35 | $attack, | |
| 36 | fn(): bool => ( | |
| 37 | $this->holds($item, $wanted) | |
| 38 | || $this->me()->drops()->find($item) !== null | |
| 39 | || $this->now()->after($deadline) | |
| 40 | ), | |
| 41 | ); | |
| 42 | if ($this->me()->drops()->find($item) === null) { | |
| 43 | continue; | |
| 44 | } | |
| 45 | ||
| 46 | $pickingUp = $this->pickUp($item); | |
| 47 | yield from $pickingUp; | |
| 48 | if (!$pickingUp->getReturn()) { | |
| 49 | break; | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | return $this->holds($item, $wanted); | |
| 54 | } | |
| 55 | ||
| 56 | private function holds(ItemId $item, Quantity $wanted): bool | |
| 57 | { | |
| 58 | return $this | |
| 59 | ->me() | |
| 60 | ->hero() | |
| 61 | ->inventory->quantity($item) >= $wanted->value; | |
| 62 | } | |
| 63 | } |