Lines
100.00%
42 / 42
Methods
100.00%
10 / 10
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __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 | ||
| [Lorekeeper\Client\Observable] subscribe | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| [Lorekeeper\Client\Observable] unsubscribe | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| [Lorekeeper\Client\Observable] notify | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| 25 | final 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 | } |
From Lorekeeper\Client\Observable
| 11 | trait Observable | |
| 12 | { | |
| 13 | /** @var list<ObserverInterface<SubjectInterface>> */ | |
| 14 | private array $observers = []; | |
| 15 | ||
| 16 | /** @param ObserverInterface<SubjectInterface> $observer */ | |
| 17 | public function subscribe(ObserverInterface $observer): void | |
| 18 | { | |
| 19 | if (in_array($observer, $this->observers, strict: true)) { | |
| 20 | return; | |
| 21 | } | |
| 22 | ||
| 23 | $this->observers[] = $observer; | |
| 24 | } | |
| 25 | ||
| 26 | /** @param ObserverInterface<SubjectInterface> $observer */ | |
| 27 | public function unsubscribe(ObserverInterface $observer): void | |
| 28 | { | |
| 29 | $this->observers = array_values(array_filter( | |
| 30 | $this->observers, | |
| 31 | static fn(ObserverInterface $subscribed): bool => $subscribed !== $observer, | |
| 32 | )); | |
| 33 | } | |
| 34 | ||
| 35 | public function notify(): void | |
| 36 | { | |
| 37 | foreach ($this->observers as $observer) { | |
| 38 | $observer->update($this); | |
| 39 | } | |
| 40 | } | |
| 41 | } |