Lines
100.00%
39 / 39
Functions and Methods
100.00%
5 / 5
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| HeroStatsUpdatedEvent | 100.00% | 39 / 39 | 100.00% | 5 / 5 | 35 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 7 | |||||
| stat | 100.00% | 25 / 25 | 100.00% | 1 / 1 | 25 | |||||
| ratio | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| summary | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Player\Hero; | |
| 6 | ||
| 7 | use InvalidArgumentException; | |
| 8 | use Lorekeeper\Core\Game\Attribute\Stat\Boost\DamageBoost; | |
| 9 | use Lorekeeper\Core\Game\Attribute\Stat\Boost\DotBoost; | |
| 10 | use Lorekeeper\Core\Game\Attribute\Stat\Boost\HealingBoost; | |
| 11 | use Lorekeeper\Core\Game\Attribute\Stat\Boost\MagicalBoost; | |
| 12 | use Lorekeeper\Core\Game\Attribute\Stat\Boost\PhysicalBoost; | |
| 13 | use Lorekeeper\Core\Game\Attribute\Stat\Chance\CriticalChance; | |
| 14 | use Lorekeeper\Core\Game\Attribute\Stat\Chance\Evasion; | |
| 15 | use Lorekeeper\Core\Game\Attribute\Stat\Chance\HitChance; | |
| 16 | use Lorekeeper\Core\Game\Attribute\Stat\CriticalMultiplier; | |
| 17 | use Lorekeeper\Core\Game\Attribute\Stat\Haste; | |
| 18 | use Lorekeeper\Core\Game\Attribute\Stat\HealingIntake; | |
| 19 | use Lorekeeper\Core\Game\Attribute\Stat\Power\AttackPower; | |
| 20 | use Lorekeeper\Core\Game\Attribute\Stat\Power\SpellPower; | |
| 21 | use Lorekeeper\Core\Game\Attribute\Stat\Primary\Dexterity; | |
| 22 | use Lorekeeper\Core\Game\Attribute\Stat\Primary\Endurance; | |
| 23 | use Lorekeeper\Core\Game\Attribute\Stat\Primary\Intellect; | |
| 24 | use Lorekeeper\Core\Game\Attribute\Stat\Primary\Luck; | |
| 25 | use Lorekeeper\Core\Game\Attribute\Stat\Primary\Strength; | |
| 26 | use Lorekeeper\Core\Game\Attribute\Stat\Primary\Wisdom; | |
| 27 | use Lorekeeper\Core\Game\Attribute\Stat\Resistance\DamageResistance; | |
| 28 | use Lorekeeper\Core\Game\Attribute\Stat\Resistance\DotResistance; | |
| 29 | use Lorekeeper\Core\Game\Attribute\Stat\Resistance\MagicalResistance; | |
| 30 | use Lorekeeper\Core\Game\Attribute\Stat\Resistance\PhysicalResistance; | |
| 31 | use Lorekeeper\Core\Game\Attribute\Stat\Stat; | |
| 32 | use Lorekeeper\Server\Event\Event; | |
| 33 | use Lorekeeper\Server\Event\NamedAfterClass; | |
| 34 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 35 | use Lorekeeper\Server\Message\JsonMessage; | |
| 36 | use Lorekeeper\Server\Message\Message; | |
| 37 | use Override; | |
| 38 | use Psl\Option; | |
| 39 | use Psl\Type; | |
| 40 | use Psl\Type\Exception\AssertException; | |
| 41 | use Psl\Type\Exception\CoercionException; | |
| 42 | ||
| 43 | /** | |
| 44 | * Our player's stats changed. | |
| 45 | */ | |
| 46 | final readonly class HeroStatsUpdatedEvent implements Event | |
| 47 | { | |
| 48 | use NamedAfterClass; | |
| 49 | ||
| 50 | private const float BASE_HIT = 0.9; | |
| 51 | ||
| 52 | /** @param non-empty-list<Stat> $stats */ | |
| 53 | public function __construct( | |
| 54 | public array $stats, | |
| 55 | ) {} | |
| 56 | ||
| 57 | /** @return Option\Option<static> */ | |
| 58 | #[Override] | |
| 59 | public static function from(Message $message): Option\Option | |
| 60 | { | |
| 61 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'stu') { | |
| 62 | return Option\none(); | |
| 63 | } | |
| 64 | ||
| 65 | try { | |
| 66 | $sent = Type\dict(Type\string(), Type\num())->coerce($message->data['sta'] ?? null); | |
| 67 | $stats = []; | |
| 68 | foreach ($sent as $key => $value) { | |
| 69 | $stat = self::stat($key, $value); | |
| 70 | if ($stat !== null) { | |
| 71 | $stats[] = $stat; | |
| 72 | } | |
| 73 | } | |
| 74 | } catch (CoercionException|AssertException|InvalidArgumentException $exception) { | |
| 75 | throw new MalformedEventException(static::class, 'a stat is missing or out of range', $exception); | |
| 76 | } | |
| 77 | ||
| 78 | return $stats === [] ? Option\none() : Option\some(new static($stats)); | |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * @throws CoercionException | |
| 83 | * @throws InvalidArgumentException | |
| 84 | */ | |
| 85 | private static function stat(string $key, int|float $value): ?Stat | |
| 86 | { | |
| 87 | return match ($key) { | |
| 88 | '$STR' => new Strength(Type\uint()->coerce($value)), | |
| 89 | '$INT' => new Intellect(Type\uint()->coerce($value)), | |
| 90 | '$DEX' => new Dexterity(Type\uint()->coerce($value)), | |
| 91 | '$END' => new Endurance(Type\uint()->coerce($value)), | |
| 92 | '$WIS' => new Wisdom(Type\uint()->coerce($value)), | |
| 93 | '$LCK' => new Luck(Type\uint()->coerce($value)), | |
| 94 | '$ap' => new AttackPower(Type\uint()->coerce($value)), | |
| 95 | '$sp' => new SpellPower(Type\uint()->coerce($value)), | |
| 96 | '$thi' => new HitChance(self::BASE_HIT + self::ratio($value)), | |
| 97 | '$tha' => new Haste(self::ratio($value)), | |
| 98 | '$tcr' => new CriticalChance(self::ratio($value)), | |
| 99 | '$scm' => new CriticalMultiplier(self::ratio($value)), | |
| 100 | '$tdo' => new Evasion(self::ratio($value)), | |
| 101 | '$cai' => new DamageResistance(1 - self::ratio($value)), | |
| 102 | '$cpi' => new PhysicalResistance(1 - self::ratio($value)), | |
| 103 | '$cmi' => new MagicalResistance(1 - self::ratio($value)), | |
| 104 | '$cdi' => new DotResistance(1 - self::ratio($value)), | |
| 105 | '$cao' => new DamageBoost(self::ratio($value) - 1), | |
| 106 | '$cpo' => new PhysicalBoost(self::ratio($value) - 1), | |
| 107 | '$cmo' => new MagicalBoost(self::ratio($value) - 1), | |
| 108 | '$cho' => new HealingBoost(self::ratio($value) - 1), | |
| 109 | '$cdo' => new DotBoost(self::ratio($value) - 1), | |
| 110 | '$chi' => new HealingIntake(self::ratio($value)), | |
| 111 | default => null, | |
| 112 | }; | |
| 113 | } | |
| 114 | ||
| 115 | private static function ratio(int|float $value): float | |
| 116 | { | |
| 117 | return (float) $value; | |
| 118 | } | |
| 119 | ||
| 120 | #[Override] | |
| 121 | public function summary(): string | |
| 122 | { | |
| 123 | return count($this->stats) . ' stats updated'; | |
| 124 | } | |
| 125 | } |