Completed
Push — master ( 638f8a...aff35e )
by
unknown
15s
created

BaseRecords::onStartMatchStart()   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\Core\DataProviders\Listener\ListenerInterfaceExpApplication;
8
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyPlayer;
9
use eXpansion\Framework\Core\Model\UserGroups\Group;
10
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
11
use eXpansion\Framework\Core\Services\Application\DispatcherInterface;
12
use eXpansion\Framework\Core\Storage\Data\Player;
13
use eXpansion\Framework\Core\Storage\MapStorage;
14
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMap;
15
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMatch;
16
use eXpansion\Framework\GameTrackmania\ScriptMethods\GetNumberOfLaps;
17
use Maniaplanet\DedicatedServer\Structures\Map;
18
use Psr\Log\LoggerInterface;
19
20
/**
21
 * Class RaceRecords
22
 *
23
 * ADD status aware interface and load on activation.
24
 *
25
 * @package eXpansion\Bundle\LocalRecords\Plugins;
26
 * @author  oliver de Cramer <[email protected]>
27
 */
28
class BaseRecords implements ListenerInterfaceMpScriptMap, ListenerInterfaceMpScriptMatch, ListenerInterfaceMpLegacyPlayer, ListenerInterfaceExpApplication
29
{
30
    /** @var  RecordHandler */
31
    protected $recordsHandler;
32
33
    /** @var Group */
34
    protected $allPlayersGroup;
35
36
    /** @var MapStorage */
37
    protected $mapStorage;
38
39
    /** @var string */
40
    protected $eventPrefix;
41
42
    /** @var GetNumberOfLaps */
43
    protected $getNumberOfLaps;
44
45
    /** @var DispatcherInterface */
46
    protected $dispatcher;
47
48
    /** @var LoggerInterface */
49
    protected $logger;
50
51
    /** @var bool Is the plugin running forc current map */
52
    protected $status = true;
53
54
    /**
55
     * BaseRecords constructor.
56
     *
57
     * @param RecordHandlerFactory $recordsHandlerFactory
58
     * @param Group                $allPlayersGroup
59
     * @param MapStorage           $mapStorage
60
     * @param DispatcherInterface  $dispatcher
61
     * @param GetNumberOfLaps      $getNumberOfLaps
62
     * @param LoggerInterface      $logger
63
     * @param                      $eventPrefix
64
     */
65 8
    public function __construct(
66
        RecordHandlerFactory $recordsHandlerFactory,
67
        Group $allPlayersGroup,
68
        MapStorage $mapStorage,
69
        DispatcherInterface $dispatcher,
70
        GetNumberOfLaps $getNumberOfLaps,
71
        LoggerInterface $logger,
72
        $eventPrefix
73
    ) {
74 8
        $this->recordsHandler = $recordsHandlerFactory->create();
75 8
        $this->allPlayersGroup = $allPlayersGroup;
76 8
        $this->mapStorage = $mapStorage;
77 8
        $this->eventPrefix = $eventPrefix;
78 8
        $this->dispatcher = $dispatcher;
79 8
        $this->logger = $logger;
80 8
        $this->getNumberOfLaps = $getNumberOfLaps;
81 8
    }
82
83
    /**
84
     * Get the current record handler.
85
     *
86
     * @return RecordHandler|mixed
87
     */
88 1
    public function getRecordsHandler()
89
    {
90 1
        return $this->recordsHandler;
91
    }
92
93
    /**
94
     * called when init is done and callbacks are enabled
95
     *
96
     * @return void
97
     */
98 1
    public function onApplicationReady()
99
    {
100 1
        $map = $this->mapStorage->getCurrentMap();
101 1
        $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...
102 1
    }
103
104
    /**
105
     * Callback sent when the "StartMap" section start.
106
     *
107
     * @param int     $count     Each time this section is played, this number is incremented by one
108
     * @param int     $time      Server time when the callback was sent
109
     * @param boolean $restarted true if the map was restarted, false otherwise
110
     * @param Map     $map       Map started with.
111
     *
112
     * @return void
113
     */
114 2
    public function onStartMapStart($count, $time, $restarted, Map $map)
115
    {
116 2
        $plugin = $this;
117
118 2
        $this->getNumberOfLaps->get(function ($laps) use ($map, $plugin) {
119 2
            $plugin->startMap($map, $laps);
120 2
        });
121 2
    }
122
123
    /**
124
     * Start plugin for a certain map.
125
     *
126
     * @param Map $map
127
     * @param int $nbLaps
128
     *
129
     * @throws \Propel\Runtime\Exception\PropelException
130
     */
131 2
    public function startMap($map, $nbLaps)
132
    {
133 2
        $this->status = true;
134
135
        // Load firs X records for this map.
136 2
        $this->recordsHandler->loadForMap($map->uId, $nbLaps);
137
138
        // Load time information for remaining players.
139 2
        $this->recordsHandler->loadForPlayers($map->uId, $nbLaps, $this->allPlayersGroup->getLogins());
140
141
        // Let others know that records information is now available.
142 2
        $this->dispatchEvent(['event' => 'loaded', 'records' => $this->recordsHandler->getRecords()]);
143 2
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148 1
    public function onPlayerConnect(Player $player)
149
    {
150 1
        if (!$this->status) {
151
            return;
152
        }
153
154 1
        $this->recordsHandler->loadForPlayers($this->mapStorage->getCurrentMap()->uId, [1], [$player->getLogin()]);
155 1
    }
156
157
    /**
158
     * Callback sent when the "EndMap" section end.
159
     *
160
     * @param int     $count     Each time this section is played, this number is incremented by one
161
     * @param int     $time      Server time when the callback was sent
162
     * @param boolean $restarted true if the map was restarted, false otherwise
163
     * @param Map     $map       Map started with.
164
     *
165
     * @return void
166
     */
167 1
    public function onEndMapEnd($count, $time, $restarted, Map $map)
168
    {
169
        // Nothing to do
170 1
    }
171
172
    /**
173
     * Callback sent when the "StartMatch" section start.
174
     *
175
     * @param int $count Each time this section is played, this number is incremented by one
176
     * @param int $time  Server time when the callback was sent
177
     *
178
     * @return void
179
     */
180 1
    public function onStartMatchStart($count, $time)
181
    {
182
        // Nothing to do.
183 1
    }
184
185
    /**
186
     * Callback sent when the "StartMatch" section end.
187
     *
188
     * @param int $count Each time this section is played, this number is incremented by one
189
     * @param int $time  Server time when the callback was sent
190
     *
191
     * @return void
192
     */
193 1
    public function onStartMatchEnd($count, $time)
194
    {
195 1
        if (!$this->status) {
196
            return;
197
        }
198
199 1
        $this->recordsHandler->save();
200 1
    }
201
202
    /**
203
     * Callback sent when the "StartMap" section end.
204
     *
205
     * @param int     $count     Each time this section is played, this number is incremented by one
206
     * @param int     $time      Server time when the callback was sent
207
     * @param boolean $restarted true if the map was restarted, false otherwise
208
     * @param Map     $map       Map started with.
209
     *
210
     * @return void
211
     */
212 1
    public function onStartMapEnd($count, $time, $restarted, Map $map)
213
    {
214
        // Nothing to do.
215 1
    }
216
217
    /**
218
     * Callback sent when the "EndMap" section start.
219
     *
220
     * @param int     $count     Each time this section is played, this number is incremented by one
221
     * @param int     $time      Server time when the callback was sent
222
     * @param boolean $restarted true if the map was restarted, false otherwise
223
     * @param Map     $map       Map started with.
224
     *
225
     * @return void
226
     */
227 1
    public function onEndMapStart($count, $time, $restarted, Map $map)
228
    {
229
        // Nothing to do.
230 1
    }
231
232 1
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
233
    {
234
        // Nothing to do.
235 1
    }
236
237 1
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
238
    {
239
        // Nothing to do.
240 1
    }
241
242 1
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
243
    {
244
        // Nothing to do.
245 1
    }
246
247
    /**
248
     * Dispatch a record event.
249
     *
250
     * @param $eventData
251
     */
252 4
    public function dispatchEvent($eventData)
253
    {
254 4
        $event = $this->eventPrefix.'.'.$eventData['event'];
255 4
        unset($eventData['event']);
256
257 4
        $this->dispatcher->dispatch($event, [$eventData]);
258 4
    }
259
260
    /**
261
     * Callback sent when the "EndMatch" section start.
262
     *
263
     * @param int $count Each time this section is played, this number is incremented by one
264
     * @param int $time Server time when the callback was sent
265
     *
266
     * @return mixed
267
     */
268 1
    public function onEndMatchStart($count, $time)
269
    {
270
        // Nothing
271 1
    }
272
273
    /**
274
     * Callback sent when the "EndMatch" section end.
275
     *
276
     * @param int $count Each time this section is played, this number is incremented by one
277
     * @param int $time Server time when the callback was sent
278
     *
279
     * @return mixed
280
     */
281 1
    public function onEndMatchEnd($count, $time)
282
    {
283
        // Nothing
284 1
    }
285
286
    /**
287
     * Callback sent when the "StartTurn" section start.
288
     *
289
     * @param int $count Each time this section is played, this number is incremented by one
290
     * @param int $time Server time when the callback was sent
291
     *
292
     * @return mixed
293
     */
294 1
    public function onStartTurnStart($count, $time)
295
    {
296
        // Nothing
297 1
    }
298
299
    /**
300
     * Callback sent when the "StartTurn" section end.
301
     *
302
     * @param int $count Each time this section is played, this number is incremented by one
303
     * @param int $time Server time when the callback was sent
304
     *
305
     * @return mixed
306
     */
307 1
    public function onStartTurnEnd($count, $time)
308
    {
309
        // Nothing
310 1
    }
311
312
    /**
313
     * Callback sent when the "EndMatch" section start.
314
     *
315
     * @param int $count Each time this section is played, this number is incremented by one
316
     * @param int $time Server time when the callback was sent
317
     *
318
     * @return mixed
319
     */
320 1
    public function onEndTurnStart($count, $time)
321
    {
322
        // Nothing
323 1
    }
324
325
    /**
326
     * Callback sent when the "EndMatch" section end.
327
     *
328
     * @param int $count Each time this section is played, this number is incremented by one
329
     * @param int $time Server time when the callback was sent
330
     *
331
     * @return mixed
332
     */
333 1
    public function onEndTurnEnd($count, $time)
334
    {
335
        // Nothing
336 1
    }
337
338
    /**
339
     * Callback sent when the "StartRound" section start.
340
     *
341
     * @param int $count Each time this section is played, this number is incremented by one
342
     * @param int $time Server time when the callback was sent
343
     *
344
     * @return mixed
345
     */
346 1
    public function onStartRoundStart($count, $time)
347
    {
348
        // Nothing
349 1
    }
350
351
    /**
352
     * Callback sent when the "StartRound" section end.
353
     *
354
     * @param int $count Each time this section is played, this number is incremented by one
355
     * @param int $time Server time when the callback was sent
356
     *
357
     * @return mixed
358
     */
359 1
    public function onStartRoundEnd($count, $time)
360
    {
361
        // Nothing
362 1
    }
363
364
    /**
365
     * Callback sent when the "EndMatch" section start.
366
     *
367
     * @param int $count Each time this section is played, this number is incremented by one
368
     * @param int $time Server time when the callback was sent
369
     *
370
     * @return mixed
371
     */
372 1
    public function onEndRoundStart($count, $time)
373
    {
374
        // Nothing
375 1
    }
376
377
    /**
378
     * Callback sent when the "EndMatch" section end.
379
     *
380
     * @param int $count Each time this section is played, this number is incremented by one
381
     * @param int $time Server time when the callback was sent
382
     *
383
     * @return mixed
384
     */
385
    public function onEndRoundEnd($count, $time)
386
    {
387
        // Nothing
388
    }
389
390
    /**
391
     * called at eXpansion init
392
     *
393
     * @return void
394
     */
395 1
    public function onApplicationInit()
396
    {
397
        // Nothing
398 1
    }
399
400
    /**
401
     * called when requesting application stop
402
     *
403
     * @return void
404
     */
405 1
    public function onApplicationStop()
406
    {
407
        // Nothing
408 1
    }
409
}
410