Lines 100.00% 16 / 16
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Hunt 100.00% 16 / 16 100.00% 4 / 4 7 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 describe 100.00% 2 / 2 100.00% 1 / 1 2
 play 100.00% 11 / 11 100.00% 1 / 1 2
 minutes 100.00% 2 / 2 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Bot\Steps\Fights;
6
7use Generator;
8use Lorekeeper\Bot\Steps\Action;
9use Lorekeeper\Bot\Steps\StepScript;
10use Lorekeeper\Client\Script\Failed;
11use Lorekeeper\Core\Item\ItemId;
12use Lorekeeper\Core\Support\Quantity;
13use Lorekeeper\Core\World\Map\MapSpot;
14use Lorekeeper\Core\World\Monster\MonsterName;
15use Override;
16use Psl\DateTime\Duration;
17
18final readonly class Hunt implements Action
19{
20    public function __construct(
21        public MapSpot $spot,
22        public ?MonsterName $monster,
23        public ItemId $item,
24        public Quantity $quantity,
25        public Duration $limit,
26    ) {}
27
28    #[Override]
29    public function describe(): string
30    {
31        $who = $this->monster === null ? '' : "{$this->monster->value} ";
32
33        return "Hunt {$who}in {$this->spot->cell->value} for item {$this->item->value} ×{$this->quantity->value}";
34    }
35
36    #[Override]
37    public function play(StepScript $bot): Generator
38    {
39        $hunting = $bot->hunt(
40            $this->spot,
41            $bot->attackIn($this->spot->cell, $this->monster),
42            $this->item,
43            $this->quantity,
44            $this->limit,
45        );
46        yield from $hunting;
47
48        return $hunting->getReturn()
49            ? null
50            : new Failed("item {$this->item->value} ×{$this->quantity->value} was not held after {$this->minutes()}");
51    }
52
53    private function minutes(): string
54    {
55        $minutes = (int) $this->limit->getTotalMinutes();
56
57        return $minutes === 1 ? '1 minute' : "{$minutes} minutes";
58    }
59}