Passed
Push — master ( a665f8...0b64a5 )
by Paweł
03:11
created

PlayerIO::askForResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Infrastructure\Cli;
6
7
use AardsGerds\Game\Dialog\DialogOption;
8
use AardsGerds\Game\Dialog\PlayerDialogOption;
9
use AardsGerds\Game\Dialog\PlayerDialogOptionCollection;
10
use AardsGerds\Game\Entity\Entity;
11
use AardsGerds\Game\Event\Decision\Decision;
12
use AardsGerds\Game\Event\Decision\DecisionCollection;
13
use AardsGerds\Game\Infrastructure\Persistence\PlayerState;
14
use AardsGerds\Game\Inventory\Inventory;
15
use AardsGerds\Game\Inventory\InventoryItem;
16
use AardsGerds\Game\Player\Player;
17
use AardsGerds\Game\Player\PlayerAction;
18
use Symfony\Component\Console\Style\SymfonyStyle;
19
use function Lambdish\Phunctional\map;
20
21
final class PlayerIO implements PlayerAction
22
{
23
    public function __construct(
24
        private SymfonyStyle $io,
25
        private PlayerState $playerState,
26
    ) {}
27
28
    public function savePlayerState(Player $player): void
29
    {
30
        $this->playerState->save($player);
31
    }
32
33
    public function askForDecision(string $question, DecisionCollection $decisions): Decision
34
    {
35
        return $this->io->choice($question, $decisions->getItems());
36
    }
37
38
    public function askForResponse(PlayerDialogOptionCollection $dialogOptions): PlayerDialogOption
39
    {
40
        sleep(1);
41
        return $this->io->choice('Your response', $dialogOptions->getItems());
42
    }
43
44
    public function askForChoice(string $question, array $choices): mixed
45
    {
46
        return $this->io->choice($question, $choices);
47
    }
48
49
    public function askForInformation(string $question): string
50
    {
51
        return $this->io->ask($question);
52
    }
53
54
    public function askForConfirmation(string $question): bool
55
    {
56
        return $this->io->confirm($question);
57
    }
58
59
    public function tell(string|array $message): void
60
    {
61
        sleep(1);
62
        $this->io->text($message);
63
    }
64
65
    public function list(array $items): void
66
    {
67
        sleep(1);
68
        $this->io->definitionList(...$items);
69
    }
70
71
    public function listInventory(Inventory $inventory): void
72
    {
73
        $this->io->table(
74
            ['Name', 'Description', 'Sell Value', 'Buy Value'],
75
            [...map(
76
                static fn(InventoryItem $inventoryItem): array => [
77
                    $inventoryItem->getName(),
78
                    $inventoryItem->getDescription(),
79
                    $inventoryItem->getSellValue(),
80
                    $inventoryItem->getBuyValue(),
81
                ],
82
                $inventory,
83
            )],
84
        );
85
    }
86
87
    public function introduce(string $message): void
88
    {
89
        sleep(1);
90
        $this->clearOutput();
91
        $this->io->title($message);
92
        sleep(1);
93
    }
94
95
    public function section(string $message): void
96
    {
97
        sleep(1);
98
        $this->io->section($message);
99
    }
100
101
    public function note(string $message): void
102
    {
103
        $this->io->note($message);
104
    }
105
106
    private function clearOutput(): void
107
    {
108
        $this->io->write("\033\143");
109
    }
110
}
111