Lines 100.00% 11 / 11
Functions and Methods 100.00% 7 / 7
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
GlobalCooldown 100.00% 11 / 11 100.00% 7 / 7 11 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 holds 100.00% 1 / 1 100.00% 1 / 1 3
 used 100.00% 5 / 5 100.00% 1 / 1 2
 refused 100.00% 1 / 1 100.00% 1 / 1 1
 ends 100.00% 1 / 1 100.00% 1 / 1 1
 shares 100.00% 1 / 1 100.00% 1 / 1 1
 pending 100.00% 1 / 1 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Bot\Abilities\Fight;
6
7use Lorekeeper\Core\Game\Attribute\Stat\Haste;
8use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\Skill;
9use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\SkillSlot;
10use Psl\DateTime\Duration;
11use Psl\DateTime\Timestamp;
12
13final readonly class GlobalCooldown
14{
15    private const int SKILLS_MS = 1500;
16
17    private const int SPACING_MS = 200;
18
19    private const int REFUSED_DELAY_MS = 250;
20
21    public function __construct(
22        private ?Timestamp $skills = null,
23        private ?Timestamp $actions = null,
24        public ?SkillSlot $last = null,
25    ) {}
26
27    public function holds(Skill $skill, Timestamp $now): bool
28    {
29        return self::pending($this->actions, $now) || self::shares($skill) && self::pending($this->skills, $now);
30    }
31
32    public function used(Skill $skill, Timestamp $now, Haste $haste): self
33    {
34        $actions = $now->plus(Duration::milliseconds(self::SPACING_MS));
35        $skills = (int) round(self::SKILLS_MS * (1 - $haste->effective()));
36
37        return self::shares($skill)
38            ? new self($now->plus(Duration::milliseconds($skills)), $actions, $skill->slot)
39            : new self($this->skills, $actions, $this->last);
40    }
41
42    public function refused(Timestamp $now): self
43    {
44        return new self($now->plus(Duration::milliseconds(self::REFUSED_DELAY_MS)), $this->actions);
45    }
46
47    /** @return list<Timestamp> */
48    public function ends(): array
49    {
50        return array_values(array_filter([$this->skills, $this->actions]));
51    }
52
53    private static function shares(Skill $skill): bool
54    {
55        return $skill->slot !== SkillSlot::AutoAttack;
56    }
57
58    private static function pending(?Timestamp $until, Timestamp $now): bool
59    {
60        return $until !== null && $until->after($now);
61    }
62}