Lines 100.00% 11 / 11
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
FightFor 100.00% 11 / 11 100.00% 3 / 3 4 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 describe 100.00% 3 / 3 100.00% 1 / 1 2
 play 100.00% 7 / 7 100.00% 1 / 1 1
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\Core\World\Map\MapSpot;
11use Lorekeeper\Core\World\Monster\MonsterName;
12use Override;
13use Psl\DateTime\Duration;
14
15final readonly class FightFor implements Action
16{
17    public function __construct(
18        public MapSpot $spot,
19        public ?MonsterName $monster,
20        public Duration $time,
21    ) {}
22
23    #[Override]
24    public function describe(): string
25    {
26        $who = $this->monster === null ? '' : " {$this->monster->value}";
27        $minutes = (int) $this->time->getTotalMinutes();
28
29        return "Fight{$who} in {$this->spot->cell->value} for {$minutes} min";
30    }
31
32    #[Override]
33    public function play(StepScript $bot): Generator
34    {
35        $deadline = $bot->now()->plus($this->time);
36        yield from $bot->kill(
37            $this->spot,
38            $bot->attackIn($this->spot->cell, $this->monster),
39            static fn(): bool => !$bot->now()->before($deadline),
40        );
41
42        return null;
43    }
44}