Lines 100.00% 33 / 33
Functions and Methods 100.00% 7 / 7
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Character 100.00% 33 / 33 100.00% 7 / 7 12 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 hero 100.00% 1 / 1 100.00% 1 / 1 1
 location 100.00% 1 / 1 100.00% 1 / 1 1
 state 100.00% 1 / 1 100.00% 1 / 1 1
 away 100.00% 1 / 1 100.00% 1 / 1 1
 drops 100.00% 1 / 1 100.00% 1 / 1 1
 hear 100.00% 27 / 27 100.00% 1 / 1 6
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Client;
6
7use Lorekeeper\Client\Tracking\AwayTracking;
8use Lorekeeper\Client\Tracking\ClassTracking;
9use Lorekeeper\Client\Tracking\DropTracking;
10use Lorekeeper\Client\Tracking\Identity;
11use Lorekeeper\Client\Tracking\InventoryTracking;
12use Lorekeeper\Client\Tracking\LocationTracking;
13use Lorekeeper\Client\Tracking\MemberTracking;
14use Lorekeeper\Client\Tracking\ProgressTracking;
15use Lorekeeper\Client\Tracking\QuestTracking;
16use Lorekeeper\Client\Tracking\StateTracking;
17use Lorekeeper\Client\Tracking\StatsTracking;
18use Lorekeeper\Client\Tracking\VitalsTracking;
19use Lorekeeper\Client\Tracking\WalletTracking;
20use Lorekeeper\Core\Game\Combat\CombatState;
21use Lorekeeper\Core\Player\Hero\Hero;
22use Lorekeeper\Server\Event\Event;
23use Psl\Observer\SubjectInterface;
24
25final class Character implements SubjectInterface
26{
27    use Observable;
28
29    public function __construct(
30        private readonly Hero $hero,
31        private Location $location,
32        private CombatState $state,
33        private bool $away,
34        private Drops $drops = new Drops(),
35    ) {}
36
37    public function hero(): Hero
38    {
39        return $this->hero;
40    }
41
42    public function location(): Location
43    {
44        return $this->location;
45    }
46
47    public function state(): CombatState
48    {
49        return $this->state;
50    }
51
52    public function away(): bool
53    {
54        return $this->away;
55    }
56
57    public function drops(): Drops
58    {
59        return $this->drops;
60    }
61
62    /** @mago-expect lint:halstead */
63    public function hear(Event $event): void
64    {
65        $hero = $this->hero;
66        $me = new Identity($hero->spawnId, $hero->name);
67
68        $changed = [
69            ProgressTracking::follow($hero, $event, $me),
70            VitalsTracking::follow($hero->vitals, $event, $me),
71            ClassTracking::follow($hero->class, $event, $me),
72            InventoryTracking::follow($hero->inventory, $event, $me, $this->drops),
73            WalletTracking::follow($hero->inventory->wallet, $event, $me),
74            QuestTracking::follow($hero->progression->quests, $event, $me),
75            StatsTracking::follow($hero->attributes->stats, $event),
76            MemberTracking::follow($hero, $event, $me),
77        ];
78
79        $drops = DropTracking::follow($this->drops, $event);
80        $location = LocationTracking::follow($this->location, $event, $me);
81        $state = StateTracking::follow($this->state, $event, $me);
82        $away = AwayTracking::follow($this->away, $event, $me);
83
84        if (
85            !in_array(true, $changed, strict: true)
86            && $drops === $this->drops
87            && $location === $this->location
88            && $state === $this->state
89            && $away === $this->away
90        ) {
91            return;
92        }
93
94        $this->drops = $drops;
95        $this->location = $location;
96        $this->state = $state;
97        $this->away = $away;
98        $this->notify();
99    }
100}