|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Core\Plugins; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\TimerDataListenerInterface; |
|
6
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\UserGroupDataListenerInterface; |
|
7
|
|
|
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface; |
|
8
|
|
|
use eXpansion\Framework\Core\Model\UserGroups\Group; |
|
9
|
|
|
use eXpansion\Framework\Core\Services\Console; |
|
10
|
|
|
use Maniaplanet\DedicatedServer\Connection; |
|
11
|
|
|
use Monolog\Logger; |
|
12
|
|
|
use oliverde8\AssociativeArraySimplified\AssociativeArray; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class GuiHandler will send manialinks to player as needed. |
|
16
|
|
|
* |
|
17
|
|
|
* @package eXpansion\Framework\Core\Plugins\Gui |
|
18
|
|
|
* @author Oliver de Cramer |
|
19
|
|
|
*/ |
|
20
|
|
|
class GuiHandler implements TimerDataListenerInterface, UserGroupDataListenerInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var Connection */ |
|
23
|
|
|
protected $connection; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Logger */ |
|
26
|
|
|
protected $logger; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Console */ |
|
29
|
|
|
protected $console; |
|
30
|
|
|
|
|
31
|
|
|
/** @var int */ |
|
32
|
|
|
protected $charLimit; |
|
33
|
|
|
|
|
34
|
|
|
/** @var ManialinkInterface[][] */ |
|
35
|
|
|
protected $displayQueu = []; |
|
36
|
|
|
|
|
37
|
|
|
/** @var ManialinkInterface[][] */ |
|
38
|
|
|
protected $individualQueu = []; |
|
39
|
|
|
|
|
40
|
|
|
/** @var ManialinkInterface[][] */ |
|
41
|
|
|
protected $displayeds = []; |
|
42
|
|
|
|
|
43
|
|
|
/** @var ManialinkInterface[][] */ |
|
44
|
|
|
protected $hideQueu = []; |
|
45
|
|
|
|
|
46
|
|
|
/** @var String[][] */ |
|
47
|
|
|
protected $hideIndividualQueu = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* GuiHandler constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Connection $connection |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(Connection $connection, Logger $logger, Console $console, $charLimit = 262144) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->connection = $connection; |
|
57
|
|
|
|
|
58
|
|
|
$this->connection->sendHideManialinkPage(null); |
|
59
|
|
|
|
|
60
|
|
|
$this->logger = $logger; |
|
61
|
|
|
$this->console = $console; |
|
62
|
|
|
$this->charLimit = $charLimit; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Add a manialink to the diplay queue. |
|
68
|
|
|
* |
|
69
|
|
|
* @param ManialinkInterface $manialink |
|
70
|
|
|
*/ |
|
71
|
|
|
public function addToDisplay(ManialinkInterface $manialink) |
|
72
|
|
|
{ |
|
73
|
|
|
$userGroup = $manialink->getUserGroup()->getName(); |
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
if (AssociativeArray::getFromKey($this->hideQueu, [$userGroup, $manialink->getId()])) { |
|
|
|
|
|
|
76
|
|
|
unset($this->hideQueu[$userGroup][$manialink->getId()]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->displayQueu[$userGroup][$manialink->getId()] = $manialink; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Add a manialink to the destruction queue. |
|
84
|
|
|
* |
|
85
|
|
|
* @param ManialinkInterface $manialink |
|
86
|
|
|
*/ |
|
87
|
|
|
public function addToHide(ManialinkInterface $manialink) |
|
88
|
|
|
{ |
|
89
|
|
|
$userGroup = $manialink->getUserGroup()->getName(); |
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
if (AssociativeArray::getFromKey($this->displayQueu, [$userGroup, $manialink->getId()])) { |
|
|
|
|
|
|
92
|
|
|
unset($this->displayQueu[$userGroup][$manialink->getId()]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
if (AssociativeArray::getFromKey($this->displayeds, [$userGroup, $manialink->getId()])) { |
|
|
|
|
|
|
96
|
|
|
unset($this->displayeds[$userGroup][$manialink->getId()]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->hideQueu[$userGroup][$manialink->getId()] = $manialink; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Display & hide all manialinks. |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function displayManialinks() |
|
106
|
|
|
{ |
|
107
|
|
|
$size = 0; |
|
108
|
|
|
foreach ($this->getManialinksToDisplay() as $mlData) { |
|
109
|
|
|
$currentSize = $size; |
|
110
|
|
|
$size += strlen($mlData['ml']); |
|
111
|
|
|
|
|
112
|
|
|
if ($currentSize != 0 && $size > $this->charLimit) { |
|
113
|
|
|
$this->executeMultiCall(); |
|
114
|
|
|
$size = strlen($mlData['ml']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->connection->sendDisplayManialinkPage($mlData['logins'], $mlData['ml'], 0, false, true); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($size > 0) { |
|
121
|
|
|
$this->executeMultiCall(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// Reset the queues. |
|
125
|
|
|
$this->displayQueu = []; |
|
126
|
|
|
$this->individualQueu = []; |
|
127
|
|
|
$this->hideQueu = []; |
|
128
|
|
|
$this->hideIndividualQueu = []; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Execute multicall & handle error. |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function executeMultiCall() |
|
135
|
|
|
{ |
|
136
|
|
|
try { |
|
137
|
|
|
$this->connection->executeMulticall(); |
|
138
|
|
|
} catch (\Exception $e) { |
|
139
|
|
|
$this->logger->addError("Couldn't deliver all manialinks : ".$e->getMessage(), ['exception' => $e]); |
|
140
|
|
|
$this->console->writeln('$F00ERROR - Couldn\'t deliver all manialinks : '.$e->getMessage()); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get list of all manialinks that needs to be displayed |
|
146
|
|
|
* |
|
147
|
|
|
* @return \Generator |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function getManialinksToDisplay() |
|
150
|
|
|
{ |
|
151
|
|
|
foreach ($this->displayQueu as $groupName => $manialinks) { |
|
152
|
|
|
foreach ($manialinks as $id => $manialink) { |
|
153
|
|
|
$logins = $manialink->getUserGroup()->getLogins(); |
|
154
|
|
|
|
|
155
|
|
|
$this->displayeds[$groupName][$id] = $manialink; |
|
156
|
|
|
if (!empty($logins)) { |
|
157
|
|
|
yield ['logins' => $logins, 'ml' => $manialink->getXml()]; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
foreach ($this->individualQueu as $login => $manialinks) { |
|
163
|
|
|
foreach ($manialinks as $id => $manialink) { |
|
164
|
|
|
$xml = $manialink->getXml(); |
|
165
|
|
|
yield ['logins' => $login, 'ml' => $xml]; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
foreach ($this->hideQueu as $manialinks) { |
|
170
|
|
|
foreach ($manialinks as $id => $manialink) { |
|
171
|
|
|
$logins = $manialink->getUserGroup()->getLogins(); |
|
172
|
|
|
if (!empty($logins)) { |
|
173
|
|
|
yield ['logins' => $logins, 'ml' => '<manialink id="'.$id.'" />']; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
foreach ($this->hideIndividualQueu as $login => $manialinks) { |
|
179
|
|
|
foreach ($manialinks as $id => $manialink) { |
|
180
|
|
|
yield ['logins' => $login, 'ml' => '<manialink id="'.$id.'" />']; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @inheritdoc |
|
187
|
|
|
*/ |
|
188
|
|
|
public function onPostLoop() |
|
189
|
|
|
{ |
|
190
|
|
|
$this->displayManialinks(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @inheritdoc |
|
195
|
|
|
*/ |
|
196
|
|
|
public function onPreLoop() |
|
197
|
|
|
{ |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @inheritdoc |
|
202
|
|
|
*/ |
|
203
|
|
|
public function onEverySecond() |
|
204
|
|
|
{ |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @inheritdoc |
|
209
|
|
|
*/ |
|
210
|
|
View Code Duplication |
public function onExpansionGroupAddUser(Group $group, $loginAdded) |
|
|
|
|
|
|
211
|
|
|
{ |
|
212
|
|
|
$group = $group->getName(); |
|
213
|
|
|
|
|
214
|
|
|
// User was added to group, need to display all manialinks of the group to this user |
|
215
|
|
|
if (isset($this->displayeds[$group])) { |
|
216
|
|
|
foreach ($this->displayeds[$group] as $mlId => $manialink) { |
|
217
|
|
|
$this->individualQueu[$loginAdded][$mlId] = $manialink; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @inheritdoc |
|
224
|
|
|
*/ |
|
225
|
|
View Code Duplication |
public function onExpansionGroupRemoveUser(Group $group, $loginRemoved) |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
|
|
$group = $group->getName(); |
|
228
|
|
|
|
|
229
|
|
|
// User was added to group, need to hide all manialinks of the group to this user |
|
230
|
|
|
if (isset($this->displayeds[$group])) { |
|
231
|
|
|
foreach ($this->displayeds[$group] as $mlId => $manialink) { |
|
232
|
|
|
$this->hideIndividualQueu[$loginRemoved][$mlId] = $manialink; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @inheritdoc |
|
239
|
|
|
*/ |
|
240
|
|
|
public function onExpansionGroupDestroy(Group $group, $lastLogin) |
|
241
|
|
|
{ |
|
242
|
|
|
if (isset($this->displayeds[$group->getName()])) { |
|
243
|
|
|
unset($this->displayeds[$group->getName()]); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* List of all manialinks that are currentyl displayed. |
|
249
|
|
|
* |
|
250
|
|
|
* @return ManialinkInterface[][] |
|
251
|
|
|
*/ |
|
252
|
|
|
public function getDisplayeds() |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->displayeds; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @param int $charLimit |
|
259
|
|
|
*/ |
|
260
|
|
|
public function setCharLimit($charLimit) |
|
261
|
|
|
{ |
|
262
|
|
|
$this->charLimit = $charLimit; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.