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
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Player\Hero;
6
7use InvalidArgumentException;
8use Lorekeeper\Core\Game\Attribute\Stat\Boost\DamageBoost;
9use Lorekeeper\Core\Game\Attribute\Stat\Boost\DotBoost;
10use Lorekeeper\Core\Game\Attribute\Stat\Boost\HealingBoost;
11use Lorekeeper\Core\Game\Attribute\Stat\Boost\MagicalBoost;
12use Lorekeeper\Core\Game\Attribute\Stat\Boost\PhysicalBoost;
13use Lorekeeper\Core\Game\Attribute\Stat\Chance\CriticalChance;
14use Lorekeeper\Core\Game\Attribute\Stat\Chance\Evasion;
15use Lorekeeper\Core\Game\Attribute\Stat\Chance\HitChance;
16use Lorekeeper\Core\Game\Attribute\Stat\CriticalMultiplier;
17use Lorekeeper\Core\Game\Attribute\Stat\Haste;
18use Lorekeeper\Core\Game\Attribute\Stat\HealingIntake;
19use Lorekeeper\Core\Game\Attribute\Stat\Power\AttackPower;
20use Lorekeeper\Core\Game\Attribute\Stat\Power\SpellPower;
21use Lorekeeper\Core\Game\Attribute\Stat\Primary\Dexterity;
22use Lorekeeper\Core\Game\Attribute\Stat\Primary\Endurance;
23use Lorekeeper\Core\Game\Attribute\Stat\Primary\Intellect;
24use Lorekeeper\Core\Game\Attribute\Stat\Primary\Luck;
25use Lorekeeper\Core\Game\Attribute\Stat\Primary\Strength;
26use Lorekeeper\Core\Game\Attribute\Stat\Primary\Wisdom;
27use Lorekeeper\Core\Game\Attribute\Stat\Resistance\DamageResistance;
28use Lorekeeper\Core\Game\Attribute\Stat\Resistance\DotResistance;
29use Lorekeeper\Core\Game\Attribute\Stat\Resistance\MagicalResistance;
30use Lorekeeper\Core\Game\Attribute\Stat\Resistance\PhysicalResistance;
31use Lorekeeper\Core\Game\Attribute\Stat\Stat;
32use Lorekeeper\Server\Event\Event;
33use Lorekeeper\Server\Event\NamedAfterClass;
34use Lorekeeper\Server\Exception\MalformedEventException;
35use Lorekeeper\Server\Message\JsonMessage;
36use Lorekeeper\Server\Message\Message;
37use Override;
38use Psl\Option;
39use Psl\Type;
40use Psl\Type\Exception\AssertException;
41use Psl\Type\Exception\CoercionException;
42
43/**
44 * Our player's stats changed.
45 */
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}