1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Alliance\Action\CreateRelation; |
6
|
|
|
|
7
|
|
|
use Stu\Component\Alliance\AllianceEnum; |
8
|
|
|
use Stu\Component\Alliance\Event\DiplomaticRelationProposedEvent; |
9
|
|
|
use Stu\Component\Alliance\Event\WarDeclaredEvent; |
10
|
|
|
use Stu\Exception\AccessViolation; |
11
|
|
|
use Stu\Module\Alliance\Lib\AllianceActionManagerInterface; |
12
|
|
|
use Stu\Module\Control\ActionControllerInterface; |
13
|
|
|
use Stu\Module\Control\GameControllerInterface; |
14
|
|
|
use Stu\Orm\Repository\AllianceRelationRepositoryInterface; |
15
|
|
|
use Stu\Orm\Repository\AllianceRepositoryInterface; |
16
|
|
|
|
17
|
|
|
final class CreateRelation implements ActionControllerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public const ACTION_IDENTIFIER = 'B_NEW_RELATION'; |
23
|
|
|
|
24
|
|
|
private CreateRelationRequestInterface $createRelationRequest; |
25
|
|
|
|
26
|
|
|
private AllianceRelationRepositoryInterface $allianceRelationRepository; |
27
|
|
|
|
28
|
|
|
private AllianceActionManagerInterface $allianceActionManager; |
29
|
|
|
|
30
|
|
|
private AllianceRepositoryInterface $allianceRepository; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
CreateRelationRequestInterface $createRelationRequest, |
34
|
|
|
AllianceRelationRepositoryInterface $allianceRelationRepository, |
35
|
|
|
AllianceActionManagerInterface $allianceActionManager, |
36
|
|
|
AllianceRepositoryInterface $allianceRepository |
37
|
|
|
) { |
38
|
|
|
$this->createRelationRequest = $createRelationRequest; |
39
|
|
|
$this->allianceRelationRepository = $allianceRelationRepository; |
40
|
|
|
$this->allianceActionManager = $allianceActionManager; |
41
|
|
|
$this->allianceRepository = $allianceRepository; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function handle(GameControllerInterface $game): void |
45
|
|
|
{ |
46
|
|
|
$alliance = $game->getUser()->getAlliance(); |
47
|
|
|
|
48
|
|
|
if ($alliance === null) { |
49
|
|
|
throw new AccessViolation(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$allianceId = $alliance->getId(); |
53
|
|
|
$user = $game->getUser(); |
54
|
|
|
|
55
|
|
|
if (!$this->allianceActionManager->mayManageForeignRelations($alliance, $user)) { |
56
|
|
|
throw new AccessViolation(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$counterpartId = $this->createRelationRequest->getCounterpartId(); |
60
|
|
|
$typeId = $this->createRelationRequest->getRelationType(); |
61
|
|
|
|
62
|
|
|
$counterpart = $this->allianceRepository->find($counterpartId); |
63
|
|
|
if ( |
64
|
|
|
$counterpart === null |
65
|
|
|
|| $alliance === $counterpart |
66
|
|
|
|| !in_array($typeId, AllianceEnum::ALLOWED_RELATION_TYPES, true) |
67
|
|
|
) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$cnt = $this->allianceRelationRepository->getPendingCountByAlliances($allianceId, $counterpartId); |
72
|
|
|
if ($cnt >= 2) { |
73
|
|
|
$game->addInformation('Es gibt bereits ein Angebot für diese Allianz'); |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// check if a relation exists |
78
|
|
|
$existingRelations = $this->allianceRelationRepository->getByAlliancePair($allianceId, $counterpartId); |
79
|
|
|
|
80
|
|
|
// Iteriere durch die gefundenen Einträge |
81
|
|
|
foreach ($existingRelations as $existingRelation) { |
82
|
|
|
$existingRelationType = $existingRelation->getType(); |
83
|
|
|
|
84
|
|
|
if ($existingRelationType === $typeId) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($typeId === AllianceEnum::ALLIANCE_RELATION_WAR) { |
90
|
|
|
$game->triggerEvent(new WarDeclaredEvent( |
91
|
|
|
$alliance, |
92
|
|
|
$counterpart, |
93
|
|
|
$user |
94
|
|
|
)); |
95
|
|
|
|
96
|
|
|
$game->addInformation( |
97
|
|
|
sprintf('Der Allianz %s wurde der Krieg erklärt', $counterpart->getName()) |
98
|
|
|
); |
99
|
|
|
} else { |
100
|
|
|
$game->triggerEvent(new DiplomaticRelationProposedEvent( |
101
|
|
|
$alliance, |
102
|
|
|
$counterpart, |
103
|
|
|
$typeId |
104
|
|
|
)); |
105
|
|
|
|
106
|
|
|
$game->addInformation('Das Abkommen wurde angeboten'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function performSessionCheck(): bool |
111
|
|
|
{ |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
} |