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
3declare(strict_types=1);
4
5namespace Lorekeeper\Bot\Abilities\Moves;
6
7use Generator;
8use Lorekeeper\Bot\Abilities\Quests\Loots;
9use Lorekeeper\Client\Command\Combat\Attack;
10use Lorekeeper\Client\Script\Instruction;
11use Lorekeeper\Client\Script\TimedOut;
12use Lorekeeper\Core\Item\ItemId;
13use Lorekeeper\Core\Support\Quantity;
14use Lorekeeper\Core\World\Map\MapSpot;
15use Lorekeeper\Server\Event\Event;
16use Psl\DateTime\Duration;
17use RuntimeException;
18
19trait 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}