1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stu\Module\Control\Component; |
4
|
|
|
|
5
|
|
|
use Stu\Component\Game\JavascriptExecutionTypeEnum; |
6
|
|
|
use Stu\Module\Control\GameControllerInterface; |
7
|
|
|
use Stu\Module\Control\ViewContext; |
8
|
|
|
use Stu\Orm\Entity\TutorialStep; |
9
|
|
|
use Stu\Orm\Repository\TutorialStepRepositoryInterface; |
10
|
|
|
use Stu\Orm\Repository\UserTutorialRepositoryInterface; |
11
|
|
|
|
12
|
|
|
class TutorialProvider |
13
|
|
|
{ |
14
|
2 |
|
public function __construct( |
15
|
|
|
private UserTutorialRepositoryInterface $userTutorialRepository, |
16
|
|
|
private TutorialStepRepositoryInterface $tutorialStepRepository |
17
|
2 |
|
) {} |
18
|
|
|
|
19
|
|
|
public function setTemplateVariables( |
20
|
|
|
ViewContext $viewContext, |
21
|
|
|
GameControllerInterface $game |
22
|
|
|
): void { |
23
|
|
|
|
24
|
|
|
$user = $game->getUser(); |
25
|
|
|
|
26
|
|
|
$userTutorials = $user->getTutorials(); |
27
|
|
|
if ($userTutorials->isEmpty()) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$userTutorial = $this->userTutorialRepository->findByUserAndViewContext($user, $viewContext); |
32
|
|
|
if ($userTutorial === null) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$tutorialSteps = $this->tutorialStepRepository->findByUserAndViewContext($user, $viewContext); |
37
|
|
|
if ($tutorialSteps == []) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$payloadArray = array_map(fn(TutorialStep $tutorialStep): array => $this->convertTutorialStep($tutorialStep), $tutorialSteps); |
42
|
|
|
|
43
|
|
|
$game->addExecuteJS(sprintf( |
44
|
|
|
"initTutorialSteps('%s', %d);", |
45
|
|
|
json_encode($payloadArray), |
46
|
|
|
$userTutorial->getTutorialStep()->getId() |
47
|
|
|
), JavascriptExecutionTypeEnum::AFTER_RENDER); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @return array<string, mixed> */ |
51
|
|
|
private function convertTutorialStep(TutorialStep $tutorialStep): array |
52
|
|
|
{ |
53
|
|
|
$result = []; |
54
|
|
|
|
55
|
|
|
if ($tutorialStep->getElementIds() !== null) { |
56
|
|
|
$result['elementIds'] = array_map('trim', explode(',', $tutorialStep->getElementIds())); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($tutorialStep->getTitle() !== null) { |
60
|
|
|
$result['title'] = trim((string)json_encode($tutorialStep->getTitle()), '"'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($tutorialStep->getText() !== null) { |
64
|
|
|
$result['text'] = trim((string)json_encode($tutorialStep->getText()), '"'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($tutorialStep->getInnerUpdate() !== null) { |
68
|
|
|
$result['innerUpdate'] = $tutorialStep->getInnerUpdate(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($tutorialStep->getFallbackIndex() !== null) { |
72
|
|
|
$result['fallbackIndex'] = $tutorialStep->getFallbackIndex(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$result['previousid'] = $tutorialStep->getPreviousStepId(); |
76
|
|
|
$result['nextid'] = $tutorialStep->getNextStepId(); |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|