Lines 100.00% 6 / 6
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
BotScript 100.00% 6 / 6 100.00% 5 / 5 5 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 rules n/a 0 / 0 n/a 0 / 0 0
 run 100.00% 2 / 2 100.00% 1 / 1 1
 me 100.00% 1 / 1 100.00% 1 / 1 1
 send 100.00% 1 / 1 100.00% 1 / 1 1
 now 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Bot;
6
7use Closure;
8use Generator;
9use Lorekeeper\Client\Character;
10use Lorekeeper\Client\Command\Command;
11use Lorekeeper\Client\Script\Instruction;
12use Lorekeeper\Client\Script\Script;
13use Lorekeeper\Client\Script\Send;
14use Lorekeeper\Client\Script\TimedOut;
15use Lorekeeper\Server\Event\Event;
16use Override;
17use Psl\DateTime\Timestamp;
18
19/**
20 * @implements Script<object>
21 */
22abstract class BotScript implements Script
23{
24    private Character $me;
25
26    /** @var Closure(): Timestamp */
27    private readonly Closure $clock;
28
29    /** @param null|Closure(): Timestamp $clock */
30    public function __construct(?Closure $clock = null)
31    {
32        $this->clock = $clock ?? Timestamp::monotonic(...);
33    }
34
35    /** @return Generator<int, Instruction, null|Event|TimedOut, object> */
36    abstract protected function rules(): Generator;
37
38    /** @return Generator<int, Instruction, null|Event|TimedOut, object> */
39    #[Override]
40    final public function run(Character $me): Generator
41    {
42        $this->me = $me;
43
44        return $this->rules();
45    }
46
47    public function me(): Character
48    {
49        return $this->me;
50    }
51
52    public function send(Command $command): Send
53    {
54        return new Send($command);
55    }
56
57    public function now(): Timestamp
58    {
59        return ($this->clock)();
60    }
61}