Lines 100.00% 31 / 31
Functions and Methods 100.00% 7 / 7
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Cooldowns 100.00% 31 / 31 100.00% 7 / 7 19 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 ready 100.00% 12 / 12 100.00% 1 / 1 5
 used 100.00% 5 / 5 100.00% 1 / 1 1
 refused 100.00% 3 / 3 100.00% 1 / 1 1
 wait 100.00% 6 / 6 100.00% 1 / 1 5
 turn 100.00% 3 / 3 100.00% 1 / 1 4
 waiting 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\Game\Attribute\Vitals;
9use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\Skill;
10use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\SkillSlot;
11use Psl\Comparison\Order;
12use Psl\DateTime\Duration;
13use Psl\DateTime\Timestamp;
14
15final readonly class Cooldowns
16{
17    /** @param array<string, Timestamp> $ready */
18    public function __construct(
19        private array $ready = [],
20        private GlobalCooldown $global = new GlobalCooldown(),
21    ) {}
22
23    /** @param list<Skill> $skills */
24    public function ready(array $skills, Vitals $vitals, ClassRules $rules, Timestamp $now): ?Skill
25    {
26        $usable = array_values(array_filter(
27            $skills,
28            fn(Skill $skill): bool => (
29                $skill->slot !== SkillSlot::Potion
30                && !$this->waiting($skill->slot->name, $now)
31                && !$this->global->holds($skill, $now)
32                && $skill->cost->affordable($vitals)
33                && $rules->allows($skill, $vitals)
34            ),
35        ));
36
37        usort($usable, static fn(Skill $a, Skill $b): int => self::turn($a, $rules) <=> self::turn($b, $rules));
38
39        return $usable[0] ?? null;
40    }
41
42    public function used(Skill $skill, Timestamp $now, Haste $haste): self
43    {
44        $cooldown = (int) round($skill->cooldown->getTotalMilliseconds() * (1 - $haste->effective()));
45
46        return new self(
47            [...$this->ready, $skill->slot->name => $now->plus(Duration::milliseconds($cooldown))],
48            $this->global->used($skill, $now, $haste),
49        );
50    }
51
52    public function refused(Timestamp $now): self
53    {
54        $ready = $this->ready;
55        unset($ready[$this->global->last->name ?? '']);
56
57        return new self($ready, $this->global->refused($now));
58    }
59
60    public function wait(Timestamp $now): Duration
61    {
62        $soonest = Duration::seconds(2);
63        foreach ([...array_values($this->ready), ...$this->global->ends()] as $at) {
64            $left = $at->since($now);
65            if ($left->isPositive() && $left->compare($soonest) === Order::Less) {
66                $soonest = $left;
67            }
68        }
69
70        return $soonest->compare(Duration::milliseconds(200)) === Order::Less ? Duration::milliseconds(200) : $soonest;
71    }
72
73    private static function turn(Skill $skill, ClassRules $rules): int
74    {
75        return match (true) {
76            $rules->guards($skill) => 0,
77            $skill->slot === SkillSlot::AutoAttack => 2,
78            default => 1,
79        };
80    }
81
82    private function waiting(string $slot, Timestamp $now): bool
83    {
84        return array_key_exists($slot, $this->ready) && $this->ready[$slot]->after($now);
85    }
86}