Lines 100.00% 56 / 56
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Kills 100.00% 56 / 56 100.00% 5 / 5 18 100.00% 1 / 1
 now n/a 0 / 0 n/a 0 / 0 0
 kill 100.00% 32 / 32 100.00% 1 / 1 7
 cast 100.00% 3 / 3 100.00% 1 / 1 2
 attackIn 100.00% 11 / 11 100.00% 1 / 1 6
 resurrect 100.00% 6 / 6 100.00% 1 / 1 2
 unlocked 100.00% 4 / 4 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Bot\Abilities\Moves;
6
7use Closure;
8use Generator;
9use InvalidArgumentException;
10use Lorekeeper\Bot\Abilities\Fight\ClassRules;
11use Lorekeeper\Bot\Abilities\Fight\Cooldowns;
12use Lorekeeper\Bot\Abilities\Fight\Targets;
13use Lorekeeper\Client\Command\Combat\Attack;
14use Lorekeeper\Client\Command\Combat\AttackMonsterCommand;
15use Lorekeeper\Client\Command\Combat\ResurrectCommand;
16use Lorekeeper\Client\Command\Combat\SupportPlayerCommand;
17use Lorekeeper\Client\Command\Command;
18use Lorekeeper\Client\Script\Await;
19use Lorekeeper\Client\Script\Instruction;
20use Lorekeeper\Client\Script\Pause;
21use Lorekeeper\Client\Script\TimedOut;
22use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\Skill;
23use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\SkillSlot;
24use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\SkillTarget;
25use Lorekeeper\Core\Player\Hero\Class\HeroSkill;
26use Lorekeeper\Core\World\Map\MapCell;
27use Lorekeeper\Core\World\Map\MapSpot;
28use Lorekeeper\Core\World\Monster\MonsterName;
29use Lorekeeper\Server\Event\ActionTooSoonEvent;
30use Lorekeeper\Server\Event\Combat\MonsterRespawnedEvent;
31use Lorekeeper\Server\Event\Combat\MonstersDiedEvent;
32use Lorekeeper\Server\Event\Combat\PlayersStateChangedEvent;
33use Lorekeeper\Server\Event\Combat\ResurrectedEvent;
34use Lorekeeper\Server\Event\Event;
35use Lorekeeper\Server\Event\Player\PlayerStateUpdatedEvent;
36use Lorekeeper\Server\Event\Progression\LeveledUpEvent;
37use Lorekeeper\Server\Event\Reward\MonsterRewardedEvent;
38use Psl\DateTime\Duration;
39use Psl\DateTime\Timestamp;
40use RuntimeException;
41
42trait Kills
43{
44    use Jumps;
45
46    abstract protected function now(): Timestamp;
47
48    /**
49     * @param Closure(): bool $until
50     *
51     * @throws RuntimeException
52     *
53     * @return Generator<int, Instruction, null|Event|TimedOut, void>
54     */
55    protected function kill(MapSpot $spot, Attack $attack, Closure $until): Generator
56    {
57        yield from $this->jump($spot);
58        $targets = Targets::in($this->me()->location()->map, $spot->cell, $attack);
59        $cooldowns = new Cooldowns();
60
61        while (!$until()) {
62            if (!$this->me()->hero()->vitals->alive()) {
63                yield from $this->resurrect($spot);
64                $targets = $targets->revived();
65
66                continue;
67            }
68
69            $target = $targets->next();
70            $now = $this->now();
71            $skill = $target === null
72                ? null
73                : $cooldowns->ready(
74                    $this->unlocked(),
75                    $this->me()->hero()->vitals,
76                    ClassRules::of($this->me()->hero()->class->class->name),
77                    $now,
78                );
79            if ($target !== null && $skill !== null) {
80                yield $this->send($this->cast($skill, $attack->at($target)));
81                $cooldowns = $cooldowns->used($skill, $now, $this->me()->hero()->attributes->stats->haste);
82            }
83
84            $heard = yield new Await([
85                MonstersDiedEvent::class,
86                MonsterRewardedEvent::class,
87                MonsterRespawnedEvent::class,
88                PlayersStateChangedEvent::class,
89                LeveledUpEvent::class,
90                ActionTooSoonEvent::class,
91            ], $cooldowns->wait($now));
92            if ($heard instanceof ActionTooSoonEvent) {
93                $cooldowns = $cooldowns->refused($this->now());
94            }
95
96            $targets = $targets->heard($heard);
97        }
98    }
99
100    /** @throws InvalidArgumentException */
101    private function cast(Skill $skill, Attack $attack): Command
102    {
103        if ($skill->target === SkillTarget::Enemy) {
104            return $attack->with($skill->slot);
105        }
106
107        return new SupportPlayerCommand($this->me()->location()->area, $skill->slot, $this->me()->hero()->spawnId);
108    }
109
110    /** @throws RuntimeException */
111    protected function attackIn(MapCell $cell, ?MonsterName $name = null): AttackMonsterCommand
112    {
113        foreach ($this->me()->location()->map->monsters as $monster) {
114            if ($monster->cell->value !== $cell->value) {
115                continue;
116            }
117
118            if ($name !== null && $monster->monster->name->value !== $name->value) {
119                continue;
120            }
121
122            return new AttackMonsterCommand($this->me()->location()->area, SkillSlot::AutoAttack, $monster->id);
123        }
124
125        throw new RuntimeException(
126            $name === null
127                ? "There are no monsters in {$cell->value}."
128                : "There is no {$name->value} in {$cell->value}.",
129        );
130    }
131
132    /** @return Generator<int, Instruction, null|Event|TimedOut, void> */
133    private function resurrect(MapSpot $spot): Generator
134    {
135        yield new Pause(Duration::seconds(10));
136        if (!$this->me()->hero()->vitals->alive()) {
137            yield $this->send(
138                new ResurrectCommand($this->me()->location()->area, $this->me()->hero()->spawnId),
139            )->awaits(Duration::seconds(15), ResurrectedEvent::class, PlayerStateUpdatedEvent::class);
140        }
141
142        yield from $this->jump($spot);
143    }
144
145    /** @return list<Skill> */
146    private function unlocked(): array
147    {
148        return array_values(array_map(
149            static fn(HeroSkill $held): Skill => $held->skill,
150            array_filter($this->me()->hero()->class->skills, static fn(HeroSkill $held): bool => $held->unlocked),
151        ));
152    }
153}