1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Alliance\Lib; |
6
|
|
|
|
7
|
|
|
use Noodlehaus\ConfigInterface; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Stu\Component\Alliance\AllianceEnum; |
10
|
|
|
use Stu\Component\Ship\ShipEnum; |
11
|
|
|
use Stu\Module\Message\Lib\PrivateMessageSenderInterface; |
12
|
|
|
use Stu\Module\PlayerSetting\Lib\UserEnum; |
13
|
|
|
use Stu\Orm\Entity\AllianceInterface; |
14
|
|
|
use Stu\Orm\Entity\AllianceJobInterface; |
15
|
|
|
use Stu\Orm\Entity\UserInterface; |
16
|
|
|
use Stu\Orm\Repository\AllianceJobRepositoryInterface; |
17
|
|
|
use Stu\Orm\Repository\AllianceRepositoryInterface; |
18
|
|
|
use Stu\Orm\Repository\DockingPrivilegeRepositoryInterface; |
19
|
|
|
use Stu\Orm\Repository\UserRepositoryInterface; |
20
|
|
|
|
21
|
|
|
final class AllianceActionManager implements AllianceActionManagerInterface |
22
|
|
|
{ |
23
|
|
|
private AllianceJobRepositoryInterface $allianceJobRepository; |
24
|
|
|
|
25
|
|
|
private AllianceRepositoryInterface $allianceRepository; |
26
|
|
|
|
27
|
|
|
private DockingPrivilegeRepositoryInterface $dockingPrivilegeRepository; |
28
|
|
|
|
29
|
|
|
private PrivateMessageSenderInterface $privateMessageSender; |
30
|
|
|
|
31
|
|
|
private UserRepositoryInterface $userRepository; |
32
|
|
|
|
33
|
|
|
private ConfigInterface $config; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
AllianceJobRepositoryInterface $allianceJobRepository, |
37
|
|
|
AllianceRepositoryInterface $allianceRepository, |
38
|
|
|
DockingPrivilegeRepositoryInterface $dockingPrivilegeRepository, |
39
|
|
|
PrivateMessageSenderInterface $privateMessageSender, |
40
|
|
|
UserRepositoryInterface $userRepository, |
41
|
|
|
ConfigInterface $config |
42
|
|
|
) { |
43
|
|
|
$this->allianceJobRepository = $allianceJobRepository; |
44
|
|
|
$this->allianceRepository = $allianceRepository; |
45
|
|
|
$this->dockingPrivilegeRepository = $dockingPrivilegeRepository; |
46
|
|
|
$this->privateMessageSender = $privateMessageSender; |
47
|
|
|
$this->userRepository = $userRepository; |
48
|
|
|
$this->config = $config; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setJobForUser(int $allianceId, int $userId, int $jobTypeId): void |
52
|
|
|
{ |
53
|
|
|
$obj = $this->allianceJobRepository->getSingleResultByAllianceAndType( |
54
|
|
|
$allianceId, |
55
|
|
|
$jobTypeId |
56
|
|
|
); |
57
|
|
|
if ($obj === null) { |
58
|
|
|
$obj = $this->allianceJobRepository->prototype(); |
59
|
|
|
$obj->setType($jobTypeId); |
60
|
|
|
$obj->setAlliance($this->allianceRepository->find($allianceId)); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
$obj->setUser($this->userRepository->find($userId)); |
|
|
|
|
63
|
|
|
|
64
|
|
|
if (!$obj->getAlliance()->getJobs()->containsKey($jobTypeId)) { |
65
|
|
|
$obj->getAlliance()->getJobs()->set($jobTypeId, $obj); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->allianceJobRepository->save($obj); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function delete(int $allianceId, bool $sendMesage = true): void |
72
|
|
|
{ |
73
|
|
|
$alliance = $this->allianceRepository->find($allianceId); |
74
|
|
|
if ($alliance === null) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->dockingPrivilegeRepository->truncateByTypeAndTarget(ShipEnum::DOCK_PRIVILEGE_ALLIANCE, $allianceId); |
79
|
|
|
|
80
|
|
|
$text = sprintf(_('Die Allianz %s wurde aufgelöst'), $alliance->getName()); |
81
|
|
|
|
82
|
|
|
foreach ($alliance->getMembers() as $user) { |
83
|
|
|
if ($sendMesage === true) { |
84
|
|
|
$this->privateMessageSender->send(UserEnum::USER_NOONE, $user->getId(), $text); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$user->setAlliance(null); |
88
|
|
|
|
89
|
|
|
$this->userRepository->save($user); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($alliance->hasAvatar()) { |
93
|
|
|
$result = @unlink( |
94
|
|
|
sprintf( |
95
|
|
|
'%s/%s/%s.png', |
96
|
|
|
$this->config->get('game.webroot'), |
97
|
|
|
$this->config->get('game.alliance_avatar_path'), |
98
|
|
|
$alliance->getAvatar() |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
if ($result === false) { |
104
|
|
|
throw new RuntimeException('alliance avatar could not be deleted'); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->allianceRepository->delete($alliance); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function mayEdit(AllianceInterface $alliance, UserInterface $user): bool |
112
|
|
|
{ |
113
|
|
|
$successor = $alliance->getSuccessor(); |
114
|
|
|
$founder = $alliance->getFounder(); |
115
|
|
|
|
116
|
|
|
return ($successor !== null && $user === $successor->getUser() |
117
|
|
|
) || $user === $founder->getUser(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function mayManageForeignRelations(AllianceInterface $alliance, UserInterface $user): bool |
121
|
|
|
{ |
122
|
|
|
$diplomatic = $alliance->getDiplomatic(); |
123
|
|
|
|
124
|
|
|
if ($diplomatic === null || $diplomatic->getUser() !== $user) { |
125
|
|
|
return $this->mayEdit($alliance, $user); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function sendMessage(int $allianceId, string $text): void |
132
|
|
|
{ |
133
|
|
|
/** @var AllianceJobInterface[] $jobList */ |
134
|
|
|
$jobList = array_filter( |
135
|
|
|
$this->allianceJobRepository->getByAlliance($allianceId), |
136
|
|
|
static fn (AllianceJobInterface $job): bool => $job->getType() !== AllianceEnum::ALLIANCE_JOBS_PENDING |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
foreach ($jobList as $job) { |
140
|
|
|
$this->privateMessageSender->send(UserEnum::USER_NOONE, $job->getUserId(), $text); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function mayEditFactionMode(AllianceInterface $alliance, int $factionId): bool |
145
|
|
|
{ |
146
|
|
|
if ($alliance->getFaction() === null) { |
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
foreach ($alliance->getMembers() as $obj) { |
151
|
|
|
if ($obj->getFactionId() !== $factionId) { |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|