Lines 100.00% 17 / 17
Functions and Methods 100.00% 6 / 6
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
QuestsProgression 100.00% 17 / 17 100.00% 6 / 6 9 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 completed 100.00% 1 / 1 100.00% 1 / 1 1
 has 100.00% 4 / 4 100.00% 1 / 1 3
 complete 100.00% 3 / 3 100.00% 1 / 1 2
 forget 100.00% 4 / 4 100.00% 1 / 1 1
 reset 100.00% 4 / 4 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Core\Player\Hero\Progression;
6
7use Lorekeeper\Core\World\Quest\Quest;
8use Lorekeeper\Core\World\Quest\QuestGroup;
9use Lorekeeper\Core\World\Quest\QuestGroupId;
10
11final class QuestsProgression
12{
13    /** @param list<QuestGroupId> $completed */
14    public function __construct(
15        public private(set) array $completed = [],
16    ) {}
17
18    public function completed(Quest $quest): bool
19    {
20        return $quest->groupId->map($this->has(...))->unwrapOr(false);
21    }
22
23    public function has(QuestGroupId $id): bool
24    {
25        foreach ($this->completed as $done) {
26            if ($done->equals($id)) {
27                return true;
28            }
29        }
30
31        return false;
32    }
33
34    public function complete(QuestGroupId $id): void
35    {
36        if ($this->has($id)) {
37            return;
38        }
39
40        $this->completed[] = $id;
41    }
42
43    public function forget(QuestGroupId $id): void
44    {
45        $this->completed = array_values(array_filter(
46            $this->completed,
47            static fn(QuestGroupId $done): bool => !$done->equals($id),
48        ));
49    }
50
51    public function reset(QuestGroup $group): void
52    {
53        $this->completed = array_values(array_filter(
54            $this->completed,
55            static fn(QuestGroupId $done): bool => $done->group !== $group,
56        ));
57    }
58}