Lines
100.00%
55 / 55
Functions and Methods
100.00%
9 / 9
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| VitalsTracking | 100.00% | 55 / 55 | 100.00% | 9 / 9 | 35 | 100.00% | 1 / 1 | |||
| follow | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 10 | |||||
| hit | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| spent | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| changed | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| updated | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| stood | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| health | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 5 | |||||
| mana | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 5 | |||||
| reading | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Client\Tracking; | |
| 6 | ||
| 7 | use Lorekeeper\Core\Game\Attribute\Health; | |
| 8 | use Lorekeeper\Core\Game\Attribute\Mana; | |
| 9 | use Lorekeeper\Core\Game\Attribute\Vitals; | |
| 10 | use Lorekeeper\Core\Game\Combat\CombatState; | |
| 11 | use Lorekeeper\Core\Support\Gauge; | |
| 12 | use Lorekeeper\Server\Event\Combat\PlayersHealthChangedEvent; | |
| 13 | use Lorekeeper\Server\Event\Combat\PlayersManaChangedEvent; | |
| 14 | use Lorekeeper\Server\Event\Combat\PlayersStateChangedEvent; | |
| 15 | use Lorekeeper\Server\Event\Combat\ResurrectedEvent; | |
| 16 | use Lorekeeper\Server\Event\Event; | |
| 17 | use Lorekeeper\Server\Event\Player\PlayerStateUpdatedEvent; | |
| 18 | use Lorekeeper\Server\Event\Player\PlayerVitalsUpdatedEvent; | |
| 19 | ||
| 20 | /** | |
| 21 | * @mago-expect lint:cyclomatic-complexity | |
| 22 | * @mago-expect lint:kan-defect | |
| 23 | */ | |
| 24 | final readonly class VitalsTracking | |
| 25 | { | |
| 26 | public static function follow(Vitals $vitals, Event $event, Identity $me): bool | |
| 27 | { | |
| 28 | $before = self::reading($vitals); | |
| 29 | ||
| 30 | match (true) { | |
| 31 | $event instanceof PlayerVitalsUpdatedEvent && $me->is($event->name) => $vitals->recount( | |
| 32 | $event->vitals->health, | |
| 33 | $event->vitals->mana, | |
| 34 | ), | |
| 35 | $event instanceof PlayersHealthChangedEvent => self::hit($vitals, $event, $me), | |
| 36 | $event instanceof PlayersManaChangedEvent => self::spent($vitals, $event, $me), | |
| 37 | $event instanceof PlayersStateChangedEvent => self::changed($vitals, $event, $me), | |
| 38 | $event instanceof PlayerStateUpdatedEvent && $me->is($event->name) => self::updated($vitals, $event), | |
| 39 | $event instanceof ResurrectedEvent => $vitals->revive(), | |
| 40 | default => null, | |
| 41 | }; | |
| 42 | ||
| 43 | return self::reading($vitals) !== $before; | |
| 44 | } | |
| 45 | ||
| 46 | private static function hit(Vitals $vitals, PlayersHealthChangedEvent $event, Identity $me): void | |
| 47 | { | |
| 48 | foreach ($event->players as $player) { | |
| 49 | if (!$me->is($player->player)) { | |
| 50 | continue; | |
| 51 | } | |
| 52 | ||
| 53 | self::health($vitals, $player->health); | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | private static function spent(Vitals $vitals, PlayersManaChangedEvent $event, Identity $me): void | |
| 58 | { | |
| 59 | foreach ($event->players as $player) { | |
| 60 | if (!$me->is($player->player)) { | |
| 61 | continue; | |
| 62 | } | |
| 63 | ||
| 64 | self::mana($vitals, $player->mana); | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | private static function changed(Vitals $vitals, PlayersStateChangedEvent $event, Identity $me): void | |
| 69 | { | |
| 70 | foreach ($event->players as $player) { | |
| 71 | if (!$me->is($player->player)) { | |
| 72 | continue; | |
| 73 | } | |
| 74 | ||
| 75 | self::stood($vitals, $player->state); | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | private static function updated(Vitals $vitals, PlayerStateUpdatedEvent $event): void | |
| 80 | { | |
| 81 | self::stood($vitals, $event->state); | |
| 82 | if ($event->health !== null) { | |
| 83 | self::health($vitals, $event->health); | |
| 84 | } | |
| 85 | ||
| 86 | if ($event->mana !== null) { | |
| 87 | self::mana($vitals, $event->mana); | |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 91 | private static function stood(Vitals $vitals, CombatState $state): void | |
| 92 | { | |
| 93 | if ($state === CombatState::Dead) { | |
| 94 | $vitals->die(); | |
| 95 | ||
| 96 | return; | |
| 97 | } | |
| 98 | ||
| 99 | $vitals->revive(); | |
| 100 | } | |
| 101 | ||
| 102 | private static function health(Vitals $vitals, Health $now): void | |
| 103 | { | |
| 104 | if ($now->value > $vitals->health->maximum->value) { | |
| 105 | $vitals->recount(new Gauge($now, $now), $vitals->mana); | |
| 106 | ||
| 107 | return; | |
| 108 | } | |
| 109 | ||
| 110 | $change = $now->value - $vitals->health->current->value; | |
| 111 | match ($change <=> 0) { | |
| 112 | -1 => $vitals->hurt(new Health(abs($change))), | |
| 113 | 1 => $vitals->heal(new Health(abs($change))), | |
| 114 | default => null, | |
| 115 | }; | |
| 116 | } | |
| 117 | ||
| 118 | private static function mana(Vitals $vitals, Mana $now): void | |
| 119 | { | |
| 120 | if ($now->value > $vitals->mana->maximum->value) { | |
| 121 | $vitals->recount($vitals->health, new Gauge($now, $now)); | |
| 122 | ||
| 123 | return; | |
| 124 | } | |
| 125 | ||
| 126 | $change = $now->value - $vitals->mana->current->value; | |
| 127 | match ($change <=> 0) { | |
| 128 | -1 => $vitals->spend(new Mana(abs($change))), | |
| 129 | 1 => $vitals->regain(new Mana(abs($change))), | |
| 130 | default => null, | |
| 131 | }; | |
| 132 | } | |
| 133 | ||
| 134 | /** @return list<int> */ | |
| 135 | private static function reading(Vitals $vitals): array | |
| 136 | { | |
| 137 | return [ | |
| 138 | $vitals->health->current->value, | |
| 139 | $vitals->health->maximum->value, | |
| 140 | $vitals->mana->current->value, | |
| 141 | $vitals->mana->maximum->value, | |
| 142 | ]; | |
| 143 | } | |
| 144 | } |