1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Component\Player\Register; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Stu\Component\Map\MapEnum; |
9
|
|
|
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum; |
10
|
|
|
use Stu\Orm\Entity\UserInterface; |
11
|
|
|
use Stu\Orm\Repository\LayerRepositoryInterface; |
12
|
|
|
use Stu\Orm\Repository\PrivateMessageFolderRepositoryInterface; |
13
|
|
|
use Stu\Orm\Repository\ResearchedRepositoryInterface; |
14
|
|
|
use Stu\Orm\Repository\UserLayerRepositoryInterface; |
15
|
|
|
|
16
|
|
|
final class PlayerDefaultsCreator implements PlayerDefaultsCreatorInterface |
17
|
|
|
{ |
18
|
|
|
private PrivateMessageFolderRepositoryInterface $privateMessageFolderRepository; |
19
|
|
|
|
20
|
|
|
private ResearchedRepositoryInterface $researchedRepository; |
21
|
|
|
|
22
|
|
|
private LayerRepositoryInterface $layerRepository; |
23
|
|
|
|
24
|
|
|
private UserLayerRepositoryInterface $userLayerRepository; |
25
|
|
|
|
26
|
1 |
|
public function __construct( |
27
|
|
|
PrivateMessageFolderRepositoryInterface $privateMessageFolderRepository, |
28
|
|
|
ResearchedRepositoryInterface $researchedRepository, |
29
|
|
|
LayerRepositoryInterface $layerRepository, |
30
|
|
|
UserLayerRepositoryInterface $userLayerRepository |
31
|
|
|
) { |
32
|
1 |
|
$this->privateMessageFolderRepository = $privateMessageFolderRepository; |
33
|
1 |
|
$this->researchedRepository = $researchedRepository; |
34
|
1 |
|
$this->layerRepository = $layerRepository; |
35
|
1 |
|
$this->userLayerRepository = $userLayerRepository; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function createDefault(UserInterface $user): void |
39
|
|
|
{ |
40
|
1 |
|
$this->createDefaultPmCategories($user); |
41
|
1 |
|
$this->createDefaultUserLayer($user); |
42
|
1 |
|
$this->createDefaultStartResearch($user); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
private function createDefaultPmCategories(UserInterface $user): void |
46
|
|
|
{ |
47
|
1 |
|
foreach (PrivateMessageFolderSpecialEnum::DEFAULT_CATEGORIES as $categoryId => $label) { |
48
|
1 |
|
$cat = $this->privateMessageFolderRepository->prototype(); |
49
|
1 |
|
$cat->setUser($user); |
50
|
1 |
|
$cat->setDescription(gettext($label)); |
51
|
1 |
|
$cat->setSpecial($categoryId); |
52
|
1 |
|
$cat->setSort($categoryId); |
53
|
|
|
|
54
|
1 |
|
$this->privateMessageFolderRepository->save($cat); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
private function createDefaultUserLayer(UserInterface $user): void |
59
|
|
|
{ |
60
|
1 |
|
$defaultLayer = $this->layerRepository->find(MapEnum::DEFAULT_LAYER); |
61
|
|
|
|
62
|
1 |
|
if ($defaultLayer === null) { |
63
|
|
|
throw new RuntimeException('the default layer should be available'); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$userLayer = $this->userLayerRepository->prototype(); |
67
|
1 |
|
$userLayer->setLayer($defaultLayer); |
68
|
1 |
|
$userLayer->setUser($user); |
69
|
|
|
|
70
|
1 |
|
$this->userLayerRepository->save($userLayer); |
71
|
1 |
|
$user->getUserLayers()->set(MapEnum::DEFAULT_LAYER, $userLayer); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
private function createDefaultStartResearch(UserInterface $user): void |
75
|
|
|
{ |
76
|
1 |
|
$faction = $user->getFaction(); |
77
|
1 |
|
$startResarch = $faction->getStartResearch(); |
78
|
1 |
|
if ($startResarch === null) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$db = $this->researchedRepository->prototype(); |
83
|
|
|
|
84
|
1 |
|
$db->setResearch($startResarch); |
85
|
1 |
|
$db->setUser($user); |
86
|
1 |
|
$db->setFinished(time()); |
87
|
1 |
|
$db->setActive(0); |
88
|
|
|
|
89
|
1 |
|
$this->researchedRepository->save($db); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|