-
Notifications
You must be signed in to change notification settings - Fork 3
/
GuiHandler.php
369 lines (310 loc) · 10.1 KB
/
GuiHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
namespace eXpansion\Framework\Core\Plugins;
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpUserGroup;
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyPlayer;
use eXpansion\Framework\Core\Model\Gui\ManialinkFactoryInterface;
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
use eXpansion\Framework\Core\Model\UserGroups\Group;
use eXpansion\Framework\Core\Plugins\Gui\ActionFactory;
use eXpansion\Framework\Core\Services\Console;
use eXpansion\Framework\Core\Storage\Data\Player;
use Maniaplanet\DedicatedServer\Connection;
use oliverde8\AssociativeArraySimplified\AssociativeArray;
use Psr\Log\LoggerInterface;
/**
* Class GuiHandler will send manialinks to player as needed.
*
* @package eXpansion\Framework\Core\Plugins\Gui
* @author Oliver de Cramer
*/
class GuiHandler implements
ListenerInterfaceExpTimer,
ListenerInterfaceExpUserGroup,
ListenerInterfaceMpLegacyPlayer,
GuiHandlerInterface
{
/** @var Connection */
protected $connection;
/** @var LoggerInterface */
protected $logger;
/** @var Console */
protected $console;
/** @var ActionFactory */
protected $actionFactory;
/** @var int */
protected $charLimit;
/** @var ManialinkInterface[][] */
protected $displayQueu = [];
/** @var ManialinkInterface[][] */
protected $individualQueu = [];
/** @var ManialinkInterface[][] */
protected $displayeds = [];
/** @var ManialinkInterface[][] */
protected $hideQueu = [];
/** @var ManialinkInterface[][] */
protected $hideIndividualQueu = [];
/** @var String[] */
protected $disconnectedLogins = [];
/**
* GuiHandler constructor.
*
* @param Connection $connection
*/
public function __construct(
Connection $connection,
LoggerInterface $logger,
Console $console,
ActionFactory $actionFactory,
$charLimit = 262144
) {
$this->connection = $connection;
$this->connection->sendHideManialinkPage(null);
$this->logger = $logger;
$this->console = $console;
$this->actionFactory = $actionFactory;
$this->charLimit = $charLimit;
}
/**
* @inheritdoc
**/
public function addToDisplay(ManialinkInterface $manialink)
{
$userGroup = $manialink->getUserGroup()->getName();
$id = $manialink->getManialinkFactory()->getId();
if (AssociativeArray::getFromKey($this->hideQueu, [$userGroup, $id])) {
unset($this->hideQueu[$userGroup][$id]);
}
$this->displayQueu[$userGroup][$id] = $manialink;
}
/**
* @inheritdoc
*/
public function addToHide(ManialinkInterface $manialink)
{
$userGroup = $manialink->getUserGroup()->getName();
$id = $manialink->getManialinkFactory()->getId();
if (AssociativeArray::getFromKey($this->displayQueu, [$userGroup, $id])) {
unset($this->displayQueu[$userGroup][$id]);
}
if (AssociativeArray::getFromKey($this->displayeds, [$userGroup, $id])) {
unset($this->displayeds[$userGroup][$id]);
}
$this->actionFactory->destroyManialinkActions($manialink);
$this->hideQueu[$userGroup][$id] = $manialink;
}
/**
* @inheritdoc
*/
public function getManialink(Group $group, ManialinkFactoryInterface $manialinkFactory)
{
$varsToCheck = ['displayeds', 'hideQueu', 'displayQueu'];
foreach ($varsToCheck as $var) {
if (isset($this->$var[$group->getName()]) && isset($this->$var[$group->getName()][$manialinkFactory->getId()])) {
return $this->$var[$group->getName()][$manialinkFactory->getId()];
}
}
return null;
}
/**
* Display & hide all manialinks.
*/
protected function displayManialinks()
{
$size = 0;
foreach ($this->getManialinksToDisplay() as $mlData) {
$currentSize = $size;
$size += strlen($mlData['ml']);
if ($currentSize != 0 && $size > $this->charLimit) {
$this->executeMultiCall();
$size = strlen($mlData['ml']);
}
$this->connection->sendDisplayManialinkPage(
$mlData['logins'],
$mlData['ml'],
0,
false,
true
);
}
if ($size > 0) {
$this->executeMultiCall();
}
// Reset the queues.
$this->displayQueu = [];
$this->individualQueu = [];
$this->hideQueu = [];
$this->hideIndividualQueu = [];
$this->disconnectedLogins = [];
}
/**
* Execute multi call & handle error.
*/
protected function executeMultiCall()
{
try {
$this->connection->executeMulticall();
} catch (\Exception $e) {
$this->logger->error("Couldn't deliver all manialinks : ".$e->getMessage(), ['exception' => $e]);
$this->console->writeln('$F00ERROR - Couldn\'t deliver all manialinks : '.$e->getMessage());
}
}
/**
* Get list of all manialinks that needs to be displayed
*
* @return \Generator
*/
protected function getManialinksToDisplay()
{
foreach ($this->displayQueu as $groupName => $manialinks) {
foreach ($manialinks as $factoryId => $manialink) {
$logins = $manialink->getUserGroup()->getLogins();
$this->displayeds[$groupName][$factoryId] = $manialink;
if (!empty($logins)) {
yield ['logins' => $logins, 'ml' => $manialink->getXml()];
}
}
}
foreach ($this->individualQueu as $manialinks) {
// Fetch all logins
$logins = [];
$lastManialink = null;
foreach ($manialinks as $login => $manialink) {
$logins[] = $login;
$lastManialink = $manialink;
}
if ($lastManialink) {
$xml = $manialink->getXml();
yield ['logins' => $logins, 'ml' => $xml];
}
}
foreach ($this->hideQueu as $manialinks) {
foreach ($manialinks as $manialink) {
$id = $manialink->getId();
$manialink->destroy();
$logins = $manialink->getUserGroup()->getLogins();
$logins = array_diff($logins, $this->disconnectedLogins);
if (!empty($logins)) {
yield ['logins' => $logins, 'ml' => '<manialink id="'.$id.'" />'];
}
}
}
foreach ($this->hideIndividualQueu as $id => $manialinks) {
// Fetch all logins.
$logins = [];
$lastManialink = null;
foreach ($manialinks as $login => $manialink) {
if (!in_array($login, $this->disconnectedLogins)) {
$logins[] = $login;
$lastManialink = $manialink;
}
}
if ($lastManialink) {
// Manialink is not destroyed just not shown at a particular user that left the group.
yield ['logins' => $logins, 'ml' => '<manialink id="'.$lastManialink->getId().'" />'];
}
}
}
/**
* @param int $charLimit
*/
public function setCharLimit($charLimit)
{
$this->charLimit = $charLimit;
}
/**
* List of all manialinks that are currently displayed.
*
* @return ManialinkInterface[][]
*/
public function getDisplayeds()
{
return $this->displayeds;
}
/**
* @inheritdoc
*/
public function onPostLoop()
{
$this->displayManialinks();
}
/**
* @inheritdoc
*/
public function onPreLoop()
{
}
/**
* @inheritdoc
*/
public function onEverySecond()
{
}
/**
* @inheritdoc
*/
public function onExpansionGroupAddUser(Group $group, $loginAdded)
{
$group = $group->getName();
// User was added to group, need to display all manialinks of the group to this user
if (isset($this->displayeds[$group])) {
foreach ($this->displayeds[$group] as $mlId => $manialink) {
$this->individualQueu[$mlId][$loginAdded] = $manialink;
if (isset($this->hideIndividualQueu[$mlId]) && isset($this->hideIndividualQueu[$mlId][$loginAdded])) {
unset ($this->hideIndividualQueu[$mlId][$loginAdded]);
}
}
}
}
/**
* @inheritdoc
*/
public function onExpansionGroupRemoveUser(Group $group, $loginRemoved)
{
$group = $group->getName();
// User was removed from group, need to hide all manialinks of the group to this user
if (isset($this->displayeds[$group])) {
foreach ($this->displayeds[$group] as $mlId => $manialink) {
$this->hideIndividualQueu[$mlId][$loginRemoved] = $manialink;
if (isset($this->individualQueu[$mlId]) && isset($this->individualQueu[$mlId][$loginRemoved])) {
unset ($this->individualQueu[$mlId][$loginRemoved]);
}
}
}
}
/**
* @inheritdoc
*/
public function onExpansionGroupDestroy(Group $group, $lastLogin)
{
if (isset($this->displayeds[$group->getName()])) {
unset($this->displayeds[$group->getName()]);
}
}
/**
* @inheritdoc
*/
public function onPlayerConnect(Player $player)
{
}
/**
* @inheritdoc
*/
public function onPlayerDisconnect(Player $player, $disconnectionReason)
{
// To prevent sending manialinks to those players.
$this->disconnectedLogins[] = $player->getLogin();
}
/**
* @inheritdoc
*/
public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
{
}
/**
* @inheritdoc
*/
public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
{
}
}