|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Module\Game\Lib\View\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use request; |
|
8
|
|
|
use Stu\Component\Alliance\AllianceDescriptionRendererInterface; |
|
9
|
|
|
use Stu\Component\Alliance\AllianceUserApplicationCheckerInterface; |
|
10
|
|
|
use Stu\Component\Game\ModuleViewEnum; |
|
11
|
|
|
use Stu\Module\Alliance\Lib\AllianceActionManagerInterface; |
|
12
|
|
|
use Stu\Module\Alliance\Lib\AllianceListItem; |
|
13
|
|
|
use Stu\Module\Alliance\Lib\AllianceMemberWrapper; |
|
14
|
|
|
use Stu\Module\Alliance\Lib\AllianceUiFactoryInterface; |
|
15
|
|
|
use Stu\Module\Control\GameControllerInterface; |
|
16
|
|
|
use Stu\Module\Game\Lib\View\Provider\ViewComponentProviderInterface; |
|
17
|
|
|
use Stu\Orm\Entity\AllianceInterface; |
|
18
|
|
|
use Stu\Orm\Entity\UserInterface; |
|
19
|
|
|
use Stu\Orm\Repository\AllianceRelationRepositoryInterface; |
|
20
|
|
|
use Stu\Orm\Repository\AllianceRepositoryInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class AllianceProvider implements ViewComponentProviderInterface |
|
23
|
|
|
{ |
|
24
|
|
|
private AllianceRelationRepositoryInterface $allianceRelationRepository; |
|
25
|
|
|
|
|
26
|
|
|
private AllianceActionManagerInterface $allianceActionManager; |
|
27
|
|
|
|
|
28
|
|
|
private AllianceRepositoryInterface $allianceRepository; |
|
29
|
|
|
|
|
30
|
|
|
private AllianceUserApplicationCheckerInterface $allianceUserApplicationChecker; |
|
31
|
|
|
|
|
32
|
|
|
private AllianceDescriptionRendererInterface $allianceDescriptionRenderer; |
|
33
|
|
|
|
|
34
|
|
|
private AllianceUiFactoryInterface $allianceUiFactory; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
AllianceRelationRepositoryInterface $allianceRelationRepository, |
|
38
|
|
|
AllianceActionManagerInterface $allianceActionManager, |
|
39
|
|
|
AllianceRepositoryInterface $allianceRepository, |
|
40
|
|
|
AllianceUserApplicationCheckerInterface $allianceUserApplicationChecker, |
|
41
|
|
|
AllianceDescriptionRendererInterface $allianceDescriptionRenderer, |
|
42
|
|
|
AllianceUiFactoryInterface $allianceUiFactory |
|
43
|
|
|
) { |
|
44
|
|
|
$this->allianceRelationRepository = $allianceRelationRepository; |
|
45
|
|
|
$this->allianceActionManager = $allianceActionManager; |
|
46
|
|
|
$this->allianceRepository = $allianceRepository; |
|
47
|
|
|
$this->allianceUserApplicationChecker = $allianceUserApplicationChecker; |
|
48
|
|
|
$this->allianceDescriptionRenderer = $allianceDescriptionRenderer; |
|
49
|
|
|
$this->allianceUiFactory = $allianceUiFactory; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function setTemplateVariables(GameControllerInterface $game): void |
|
53
|
|
|
{ |
|
54
|
|
|
$user = $game->getUser(); |
|
55
|
|
|
|
|
56
|
|
|
$alliance = null; |
|
57
|
|
|
if (request::has('id')) { |
|
58
|
|
|
$alliance = $this->allianceRepository->find(request::indInt('id')); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($alliance === null) { |
|
62
|
|
|
$alliance = $user->getAlliance(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$game->setTemplateVar('ALLIANCE', $alliance); |
|
66
|
|
|
|
|
67
|
|
|
if ($alliance === null || request::has('showlist')) { |
|
68
|
|
|
$this->setTemplateVariablesForAllianceList($game); |
|
69
|
|
|
} else { |
|
70
|
|
|
$this->setTemplateVariablesForAlliance($alliance, $game); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function setTemplateVariablesForAlliance(AllianceInterface $alliance, GameControllerInterface $game): void |
|
75
|
|
|
{ |
|
76
|
|
|
$user = $game->getUser(); |
|
77
|
|
|
$allianceId = $alliance->getId(); |
|
78
|
|
|
|
|
79
|
|
|
$result = $this->allianceRelationRepository->getActiveByAlliance($allianceId); |
|
80
|
|
|
$userIsFounder = $alliance->getFounder()->getUser() === $user; |
|
81
|
|
|
$isInAlliance = $alliance === $game->getUser()->getAlliance(); |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$game->appendNavigationPart(sprintf( |
|
85
|
|
|
'%s?id=%d', |
|
86
|
|
|
ModuleViewEnum::ALLIANCE->getPhpPage(), |
|
87
|
|
|
$alliance->getId() |
|
88
|
|
|
), _('Allianz anzeigen')); |
|
89
|
|
|
|
|
90
|
|
|
$relations = []; |
|
91
|
|
|
foreach ($result as $key => $relation) { |
|
92
|
|
|
$relations[$key] = $this->allianceUiFactory->createAllianceRelationWrapper($alliance, $relation); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$game->setTemplateVar('SHOW_ALLIANCE', $alliance); |
|
96
|
|
|
|
|
97
|
|
|
$game->setTemplateVar( |
|
98
|
|
|
'ALLIANCE_RELATIONS', |
|
99
|
|
|
$relations !== [] |
|
100
|
|
|
? $relations |
|
101
|
|
|
: null |
|
102
|
|
|
); |
|
103
|
|
|
$game->setTemplateVar( |
|
104
|
|
|
'DESCRIPTION', |
|
105
|
|
|
$this->allianceDescriptionRenderer->render($alliance) |
|
106
|
|
|
); |
|
107
|
|
|
$game->setTemplateVar('IS_IN_ALLIANCE', $isInAlliance); |
|
108
|
|
|
$game->setTemplateVar('CAN_LEAVE_ALLIANCE', $isInAlliance && !$userIsFounder); |
|
109
|
|
|
$game->setTemplateVar( |
|
110
|
|
|
'CAN_EDIT', |
|
111
|
|
|
$this->allianceActionManager->mayEdit($alliance, $user) |
|
112
|
|
|
); |
|
113
|
|
|
$game->setTemplateVar( |
|
114
|
|
|
'CAN_MANAGE_FOREIGN_RELATIONS', |
|
115
|
|
|
$this->allianceActionManager->mayManageForeignRelations($alliance, $user) |
|
116
|
|
|
); |
|
117
|
|
|
$game->setTemplateVar( |
|
118
|
|
|
'CAN_SIGNUP', |
|
119
|
|
|
$this->allianceUserApplicationChecker->mayApply($user, $alliance) |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
$game->setTemplateVar( |
|
123
|
|
|
'MEMBERS', |
|
124
|
|
|
$alliance->getMembers()->map( |
|
125
|
|
|
fn (UserInterface $user): AllianceMemberWrapper => $this->allianceUiFactory->createAllianceMemberWrapper($user, $alliance) |
|
126
|
|
|
) |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function setTemplateVariablesForAllianceList(GameControllerInterface $game): void |
|
131
|
|
|
{ |
|
132
|
|
|
$game->appendNavigationPart(sprintf( |
|
133
|
|
|
'%s?showlist=1', |
|
134
|
|
|
ModuleViewEnum::ALLIANCE->getPhpPage() |
|
135
|
|
|
), _('Allianzliste')); |
|
136
|
|
|
|
|
137
|
|
|
$game->setTemplateVar('SHOW_ALLIANCE_LIST', true); |
|
138
|
|
|
$game->setTemplateVar( |
|
139
|
|
|
'ALLIANCE_LIST_OPEN', |
|
140
|
|
|
array_map( |
|
141
|
|
|
fn (AllianceInterface $alliance): AllianceListItem => $this->allianceUiFactory->createAllianceListItem($alliance), |
|
142
|
|
|
$this->allianceRepository->findByApplicationState(true) |
|
143
|
|
|
) |
|
144
|
|
|
); |
|
145
|
|
|
$game->setTemplateVar( |
|
146
|
|
|
'ALLIANCE_LIST_CLOSED', |
|
147
|
|
|
array_map( |
|
148
|
|
|
fn (AllianceInterface $alliance): AllianceListItem => $this->allianceUiFactory->createAllianceListItem($alliance), |
|
149
|
|
|
$this->allianceRepository->findByApplicationState(false) |
|
150
|
|
|
) |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|