Lines
100.00%
42 / 42
Functions and Methods
100.00%
6 / 6
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Targets | 100.00% | 42 / 42 | 100.00% | 6 / 6 | 17 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| in | 100.00% | 23 / 23 | 100.00% | 1 / 1 | 3 | |||||
| kindOf | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| next | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 4 | |||||
| heard | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 5 | |||||
| revived | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Bot\Abilities\Fight; | |
| 6 | ||
| 7 | use Lorekeeper\Client\Command\Combat\Attack; | |
| 8 | use Lorekeeper\Client\Script\TimedOut; | |
| 9 | use Lorekeeper\Core\Game\Combat\Traits\Targetable; | |
| 10 | use Lorekeeper\Core\World\Map\Map; | |
| 11 | use Lorekeeper\Core\World\Map\MapCell; | |
| 12 | use Lorekeeper\Core\World\Map\MapMonster; | |
| 13 | use Lorekeeper\Core\World\Monster\Monster; | |
| 14 | use Lorekeeper\Core\World\Monster\MonsterSpawnId; | |
| 15 | use Lorekeeper\Server\Event\Combat\MonsterRespawnedEvent; | |
| 16 | use Lorekeeper\Server\Event\Combat\MonstersDiedEvent; | |
| 17 | use Lorekeeper\Server\Event\Event; | |
| 18 | use Lorekeeper\Server\Event\Reward\MonsterRewardedEvent; | |
| 19 | use RuntimeException; | |
| 20 | ||
| 21 | final readonly class Targets | |
| 22 | { | |
| 23 | /** | |
| 24 | * @param non-empty-list<Targetable> $targets | |
| 25 | * @param array<int, true> $dead | |
| 26 | */ | |
| 27 | private function __construct( | |
| 28 | private array $targets, | |
| 29 | private array $dead = [], | |
| 30 | ) {} | |
| 31 | ||
| 32 | /** @throws RuntimeException */ | |
| 33 | public static function in(Map $map, MapCell $cell, Attack $attack): self | |
| 34 | { | |
| 35 | $aimed = $attack->target(); | |
| 36 | if (!$aimed instanceof MonsterSpawnId) { | |
| 37 | return new self([$aimed]); | |
| 38 | } | |
| 39 | ||
| 40 | $here = array_values(array_filter( | |
| 41 | iterator_to_array($map->monsters, preserve_keys: false), | |
| 42 | static fn(MapMonster $monster): bool => $monster->cell->value === $cell->value, | |
| 43 | )); | |
| 44 | $kind = self::kindOf($aimed, $here) ?? throw new RuntimeException( | |
| 45 | "Monster {$aimed->value} does not stand in {$cell->value}.", | |
| 46 | ); | |
| 47 | ||
| 48 | return new self([ | |
| 49 | $aimed, | |
| 50 | ...array_values(array_map( | |
| 51 | static fn(MapMonster $monster): MonsterSpawnId => $monster->id, | |
| 52 | array_filter( | |
| 53 | $here, | |
| 54 | static fn(MapMonster $monster): bool => ( | |
| 55 | $monster->monster === $kind | |
| 56 | && $monster->id->value !== $aimed->value | |
| 57 | ), | |
| 58 | ), | |
| 59 | )), | |
| 60 | ]); | |
| 61 | } | |
| 62 | ||
| 63 | /** @param list<MapMonster> $here */ | |
| 64 | private static function kindOf(MonsterSpawnId $spawn, array $here): ?Monster | |
| 65 | { | |
| 66 | foreach ($here as $monster) { | |
| 67 | if ($monster->id->value === $spawn->value) { | |
| 68 | return $monster->monster; | |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | return null; | |
| 73 | } | |
| 74 | ||
| 75 | public function next(): ?Targetable | |
| 76 | { | |
| 77 | foreach ($this->targets as $target) { | |
| 78 | if (!$target instanceof MonsterSpawnId || !array_key_exists($target->value, $this->dead)) { | |
| 79 | return $target; | |
| 80 | } | |
| 81 | } | |
| 82 | ||
| 83 | return null; | |
| 84 | } | |
| 85 | ||
| 86 | public function heard(null|Event|TimedOut $answer): self | |
| 87 | { | |
| 88 | $dead = $this->dead; | |
| 89 | ||
| 90 | if ($answer instanceof MonstersDiedEvent) { | |
| 91 | foreach ($answer->monsters as $monster) { | |
| 92 | $dead[$monster->value] = true; | |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | if ($answer instanceof MonsterRewardedEvent) { | |
| 97 | $dead[$answer->monster->value] = true; | |
| 98 | } | |
| 99 | ||
| 100 | if ($answer instanceof MonsterRespawnedEvent) { | |
| 101 | unset($dead[$answer->id->value]); | |
| 102 | } | |
| 103 | ||
| 104 | return new self($this->targets, $dead); | |
| 105 | } | |
| 106 | ||
| 107 | public function revived(): self | |
| 108 | { | |
| 109 | return new self($this->targets); | |
| 110 | } | |
| 111 | } |