Completed
Pull Request — master (#191)
by De Cramer
04:00
created

BaseRecords::onPlayerDisconnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace eXpansion\Bundle\LocalRecords\Plugins;
4
5
use eXpansion\Bundle\LocalRecords\Services\RecordHandler;
6
use eXpansion\Bundle\LocalRecords\Services\RecordHandlerFactory;
7
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyPlayer;
8
use eXpansion\Framework\Core\Model\UserGroups\Group;
9
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
10
use eXpansion\Framework\Core\Services\Application\DispatcherInterface;
11
use eXpansion\Framework\Core\Storage\Data\Player;
12
use eXpansion\Framework\Core\Storage\MapStorage;
13
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMap;
14
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMatch;
15
use eXpansion\Framework\GameTrackmania\ScriptMethods\GetNumberOfLaps;
16
use Maniaplanet\DedicatedServer\Structures\Map;
17
use Psr\Log\LoggerInterface;
18
19
/**
20
 * Class RaceRecords
21
 *
22
 * ADD status aware interface and load on activation.
23
 *
24
 * @package eXpansion\Bundle\LocalRecords\Plugins;
25
 * @author  oliver de Cramer <[email protected]>
26
 */
27
class BaseRecords implements ListenerInterfaceMpScriptMap, ListenerInterfaceMpScriptMatch, ListenerInterfaceMpLegacyPlayer, StatusAwarePluginInterface
28
{
29
    /** @var  RecordHandler */
30
    protected $recordsHandler;
31
32
    /** @var Group */
33
    protected $allPlayersGroup;
34
35
    /** @var MapStorage */
36
    protected $mapStorage;
37
38
    /** @var string */
39
    protected $eventPrefix;
40
41
    /** @var GetNumberOfLaps */
42
    protected $getNumberOfLaps;
43
44
    /** @var DispatcherInterface */
45
    protected $dispatcher;
46
47
    /** @var LoggerInterface */
48
    protected $logger;
49
50
    /**
51
     * BaseRecords constructor.
52
     *
53
     * @param RecordHandlerFactory $recordsHandlerFactory
54
     * @param Group                $allPlayersGroup
55
     * @param MapStorage           $mapStorage
56
     * @param DispatcherInterface  $dispatcher
57
     * @param GetNumberOfLaps      $getNumberOfLaps
58
     * @param LoggerInterface      $logger
59
     * @param                      $eventPrefix
60
     */
61 8
    public function __construct(
62
        RecordHandlerFactory $recordsHandlerFactory,
63
        Group $allPlayersGroup,
64
        MapStorage $mapStorage,
65
        DispatcherInterface $dispatcher,
66
        GetNumberOfLaps $getNumberOfLaps,
67
        LoggerInterface $logger,
68
        $eventPrefix
69
    ) {
70 8
        $this->recordsHandler = $recordsHandlerFactory->create();
71 8
        $this->allPlayersGroup = $allPlayersGroup;
72 8
        $this->mapStorage = $mapStorage;
73 8
        $this->eventPrefix = $eventPrefix;
74 8
        $this->dispatcher = $dispatcher;
75 8
        $this->logger = $logger;
76 8
        $this->getNumberOfLaps = $getNumberOfLaps;
77 8
    }
78
79
    /**
80
     * Get the current record handler.
81
     *
82
     * @return RecordHandler|mixed
83
     */
84
    public function getRecordsHandler()
85
    {
86
        return $this->recordsHandler;
87
    }
88
89
90
    /**
91
     * called when init is done and callbacks are enabled
92
     *
93
     * @return void
94
     */
95
    public function setStatus($status)
96
    {
97
        if ($status) {
98
            $map = $this->mapStorage->getCurrentMap();
99
            $this->onStartMapStart(0, 0, 0, $map);
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
        }
101
    }
102
103
    /**
104
     * Callback sent when the "StartMap" section start.
105
     *
106
     * @param int     $count     Each time this section is played, this number is incremented by one
107
     * @param int     $time      Server time when the callback was sent
108
     * @param boolean $restarted true if the map was restarted, false otherwise
109
     * @param Map     $map       Map started with.
110
     *
111
     * @return void
112
     */
113 1
    public function onStartMapStart($count, $time, $restarted, Map $map)
114
    {
115 1
        $plugin = $this;
116
117 1
        $this->getNumberOfLaps->get(function ($laps) use ($map, $plugin) {
118 1
            $plugin->startMap($map, $laps);
119 1
        });
120 1
    }
121
122
    /**
123
     * Start plugin for a certain map.
124
     *
125
     * @param Map $map
126
     * @param int $nbLaps
127
     *
128
     * @throws \Propel\Runtime\Exception\PropelException
129
     */
130 1
    public function startMap($map, $nbLaps)
131
    {
132
        // Load firs X records for this map.
133 1
        $this->recordsHandler->loadForMap($map->uId, $nbLaps);
134
135
        // Load time information for remaining players.
136 1
        $this->recordsHandler->loadForPlayers($map->uId, $nbLaps, $this->allPlayersGroup->getLogins());
137
138
        // Let others know that records information is now available.
139 1
        $this->dispatchEvent(['event' => 'loaded', 'records' => $this->recordsHandler->getRecords()]);
140 1
    }
141
142
    /**
143
     * @inheritdoc
144
     */
145 1
    public function onPlayerConnect(Player $player)
146
    {
147 1
        $this->recordsHandler->loadForPlayers($this->mapStorage->getCurrentMap()->uId, [1], [$player->getLogin()]);
148 1
    }
149
150
    /**
151
     * Callback sent when the "EndMap" section end.
152
     *
153
     * @param int     $count     Each time this section is played, this number is incremented by one
154
     * @param int     $time      Server time when the callback was sent
155
     * @param boolean $restarted true if the map was restarted, false otherwise
156
     * @param Map     $map       Map started with.
157
     *
158
     * @return void
159
     */
160 1
    public function onEndMapEnd($count, $time, $restarted, Map $map)
161
    {
162
        // Nothing to do
163 1
    }
164
165
    /**
166
     * Callback sent when the "StartMatch" section start.
167
     *
168
     * @param int $count Each time this section is played, this number is incremented by one
169
     * @param int $time  Server time when the callback was sent
170
     *
171
     * @return void
172
     */
173 1
    public function onStartMatchStart($count, $time)
174
    {
175
        // Nothing to do.
176 1
    }
177
178
    /**
179
     * Callback sent when the "StartMatch" section end.
180
     *
181
     * @param int $count Each time this section is played, this number is incremented by one
182
     * @param int $time  Server time when the callback was sent
183
     *
184
     * @return void
185
     */
186 1
    public function onStartMatchEnd($count, $time)
187
    {
188 1
        $this->recordsHandler->save();
189 1
    }
190
191
    /**
192
     * Callback sent when the "StartMap" section end.
193
     *
194
     * @param int     $count     Each time this section is played, this number is incremented by one
195
     * @param int     $time      Server time when the callback was sent
196
     * @param boolean $restarted true if the map was restarted, false otherwise
197
     * @param Map     $map       Map started with.
198
     *
199
     * @return void
200
     */
201 1
    public function onStartMapEnd($count, $time, $restarted, Map $map)
202
    {
203
        // Nothing to do.
204 1
    }
205
206
    /**
207
     * Callback sent when the "EndMap" section start.
208
     *
209
     * @param int     $count     Each time this section is played, this number is incremented by one
210
     * @param int     $time      Server time when the callback was sent
211
     * @param boolean $restarted true if the map was restarted, false otherwise
212
     * @param Map     $map       Map started with.
213
     *
214
     * @return void
215
     */
216 1
    public function onEndMapStart($count, $time, $restarted, Map $map)
217
    {
218
        // Nothing to do.
219 1
    }
220
221 1
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
222
    {
223
        // Nothing to do.
224 1
    }
225
226 1
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
227
    {
228
        // Nothing to do.
229 1
    }
230
231 1
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
232
    {
233
        // Nothing to do.
234 1
    }
235
236
    /**
237
     * Dispatch a record event.
238
     *
239
     * @param $eventData
240
     */
241 3
    public function dispatchEvent($eventData)
242
    {
243 3
        $event = $this->eventPrefix.'.'.$eventData['event'];
244 3
        unset($eventData['event']);
245
246 3
        $this->dispatcher->dispatch($event, [$eventData]);
247 3
    }
248
249
    /**
250
     * Callback sent when the "EndMatch" section start.
251
     *
252
     * @param int $count Each time this section is played, this number is incremented by one
253
     * @param int $time Server time when the callback was sent
254
     *
255
     * @return mixed
256
     */
257
    public function onEndMatchStart($count, $time)
258
    {
259
        // Nothing
260
    }
261
262
    /**
263
     * Callback sent when the "EndMatch" section end.
264
     *
265
     * @param int $count Each time this section is played, this number is incremented by one
266
     * @param int $time Server time when the callback was sent
267
     *
268
     * @return mixed
269
     */
270
    public function onEndMatchEnd($count, $time)
271
    {
272
        // Nothing
273
    }
274
275
    /**
276
     * Callback sent when the "StartTurn" section start.
277
     *
278
     * @param int $count Each time this section is played, this number is incremented by one
279
     * @param int $time Server time when the callback was sent
280
     *
281
     * @return mixed
282
     */
283
    public function onStartTurnStart($count, $time)
284
    {
285
        // Nothing
286
    }
287
288
    /**
289
     * Callback sent when the "StartTurn" section end.
290
     *
291
     * @param int $count Each time this section is played, this number is incremented by one
292
     * @param int $time Server time when the callback was sent
293
     *
294
     * @return mixed
295
     */
296
    public function onStartTurnEnd($count, $time)
297
    {
298
        // Nothing
299
    }
300
301
    /**
302
     * Callback sent when the "EndMatch" section start.
303
     *
304
     * @param int $count Each time this section is played, this number is incremented by one
305
     * @param int $time Server time when the callback was sent
306
     *
307
     * @return mixed
308
     */
309
    public function onEndTurnStart($count, $time)
310
    {
311
        // Nothing
312
    }
313
314
    /**
315
     * Callback sent when the "EndMatch" section end.
316
     *
317
     * @param int $count Each time this section is played, this number is incremented by one
318
     * @param int $time Server time when the callback was sent
319
     *
320
     * @return mixed
321
     */
322
    public function onEndTurnEnd($count, $time)
323
    {
324
        // Nothing
325
    }
326
327
    /**
328
     * Callback sent when the "StartRound" section start.
329
     *
330
     * @param int $count Each time this section is played, this number is incremented by one
331
     * @param int $time Server time when the callback was sent
332
     *
333
     * @return mixed
334
     */
335
    public function onStartRoundStart($count, $time)
336
    {
337
        // Nothing
338
    }
339
340
    /**
341
     * Callback sent when the "StartRound" section end.
342
     *
343
     * @param int $count Each time this section is played, this number is incremented by one
344
     * @param int $time Server time when the callback was sent
345
     *
346
     * @return mixed
347
     */
348
    public function onStartRoundEnd($count, $time)
349
    {
350
        // Nothing
351
    }
352
353
    /**
354
     * Callback sent when the "EndMatch" section start.
355
     *
356
     * @param int $count Each time this section is played, this number is incremented by one
357
     * @param int $time Server time when the callback was sent
358
     *
359
     * @return mixed
360
     */
361
    public function onEndRoundStart($count, $time)
362
    {
363
        // Nothing
364
    }
365
366
    /**
367
     * Callback sent when the "EndMatch" section end.
368
     *
369
     * @param int $count Each time this section is played, this number is incremented by one
370
     * @param int $time Server time when the callback was sent
371
     *
372
     * @return mixed
373
     */
374
    public function onEndRoundEnd($count, $time)
375
    {
376
        // Nothing
377
    }
378
}
379