Lines 100.00% 45 / 45
Methods 100.00% 6 / 6
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __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
 [Lorekeeper\Server\Event\NamedAfterClass] name 100.00% 6 / 6 100.00% 1 / 1 1
46final 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}

From Lorekeeper\Server\Event\NamedAfterClass

7trait NamedAfterClass
8{
9    public function name(): string
10    {
11        $class = static::class;
12
13        return (string) preg_replace(
14            '/Event$/',
15            replacement: '',
16            subject: substr($class, (int) strrpos($class, needle: '\\') + 1),
17        );
18    }
19}