Completed
Push — master ( 947184...ad3618 )
by
unknown
9s
created

BaseRecords   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 353
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 97.26%

Importance

Changes 0
Metric Value
wmc 26
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 353
ccs 71
cts 73
cp 0.9726
rs 10

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A onStartMapStart() 0 8 1
A startMap() 0 11 1
A onPlayerConnect() 0 4 1
A onEndMapEnd() 0 4 1
A onStartMatchStart() 0 4 1
A onStartMatchEnd() 0 4 1
A onStartMapEnd() 0 4 1
A onEndMapStart() 0 4 1
A onPlayerDisconnect() 0 4 1
A onPlayerInfoChanged() 0 4 1
A onPlayerAlliesChanged() 0 4 1
A getRecordsHandler() 0 4 1
A setStatus() 0 7 2
A dispatchEvent() 0 8 1
A onEndMatchStart() 0 4 1
A onEndMatchEnd() 0 4 1
A onStartTurnStart() 0 4 1
A onStartTurnEnd() 0 4 1
A onEndTurnStart() 0 4 1
A onEndTurnEnd() 0 4 1
A onStartRoundStart() 0 4 1
A onStartRoundEnd() 0 4 1
A onEndRoundStart() 0 4 1
A onEndRoundEnd() 0 4 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 7
    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 7
        $this->recordsHandler = $recordsHandlerFactory->create();
71 7
        $this->allPlayersGroup = $allPlayersGroup;
72 7
        $this->mapStorage = $mapStorage;
73 7
        $this->eventPrefix = $eventPrefix;
74 7
        $this->dispatcher = $dispatcher;
75 7
        $this->logger = $logger;
76 7
        $this->getNumberOfLaps = $getNumberOfLaps;
77 7
    }
78
79
    /**
80
     * Get the current record handler.
81
     *
82
     * @return RecordHandler
83
     */
84 1
    public function getRecordsHandler()
85
    {
86 1
        return $this->recordsHandler;
87
    }
88
89
90
    /**
91
     * called when init is done and callbacks are enabled
92
     *
93
     * @return void
94
     */
95 1
    public function setStatus($status)
96
    {
97 1
        if ($status) {
98 1
            $map = $this->mapStorage->getCurrentMap();
99 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...
100
        }
101 1
    }
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 2
    public function onStartMapStart($count, $time, $restarted, Map $map)
114
    {
115 2
        $plugin = $this;
116
117 2
        $this->getNumberOfLaps->get(function($laps) use ($map, $plugin) {
118 2
            $plugin->startMap($map, $laps);
119 2
        });
120 2
    }
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 2
    public function startMap($map, $nbLaps)
131
    {
132
        // Load firs X records for this map.
133 2
        $this->recordsHandler->loadForMap($map->uId, $nbLaps);
134
135
        // Load time information for remaining players.
136 2
        $this->recordsHandler->loadForPlayers($map->uId, $nbLaps, $this->allPlayersGroup->getLogins());
137
138
        // Let others know that records information is now available.
139 2
        $this->dispatchEvent(['event' => 'loaded', 'records' => $this->recordsHandler->getRecords()]);
140 2
    }
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 4
    public function dispatchEvent($eventData)
242
    {
243 4
        $event = $this->eventPrefix.'.'.$eventData['event'];
244 4
        unset($eventData['event']);
245
246 4
        $eventData[RecordHandler::COL_PLUGIN] = $this;
247 4
        $this->dispatcher->dispatch($event, [$eventData]);
248 4
    }
249
250
    /**
251
     * Callback sent when the "EndMatch" section start.
252
     *
253
     * @param int $count Each time this section is played, this number is incremented by one
254
     * @param int $time Server time when the callback was sent
255
     *
256
     * @return mixed
257
     */
258 1
    public function onEndMatchStart($count, $time)
259
    {
260
        // Nothing
261 1
    }
262
263
    /**
264
     * Callback sent when the "EndMatch" section end.
265
     *
266
     * @param int $count Each time this section is played, this number is incremented by one
267
     * @param int $time Server time when the callback was sent
268
     *
269
     * @return mixed
270
     */
271 1
    public function onEndMatchEnd($count, $time)
272
    {
273
        // Nothing
274 1
    }
275
276
    /**
277
     * Callback sent when the "StartTurn" section start.
278
     *
279
     * @param int $count Each time this section is played, this number is incremented by one
280
     * @param int $time Server time when the callback was sent
281
     *
282
     * @return mixed
283
     */
284 1
    public function onStartTurnStart($count, $time)
285
    {
286
        // Nothing
287 1
    }
288
289
    /**
290
     * Callback sent when the "StartTurn" section end.
291
     *
292
     * @param int $count Each time this section is played, this number is incremented by one
293
     * @param int $time Server time when the callback was sent
294
     *
295
     * @return mixed
296
     */
297 1
    public function onStartTurnEnd($count, $time)
298
    {
299
        // Nothing
300 1
    }
301
302
    /**
303
     * Callback sent when the "EndMatch" section start.
304
     *
305
     * @param int $count Each time this section is played, this number is incremented by one
306
     * @param int $time Server time when the callback was sent
307
     *
308
     * @return mixed
309
     */
310 1
    public function onEndTurnStart($count, $time)
311
    {
312
        // Nothing
313 1
    }
314
315
    /**
316
     * Callback sent when the "EndMatch" section end.
317
     *
318
     * @param int $count Each time this section is played, this number is incremented by one
319
     * @param int $time Server time when the callback was sent
320
     *
321
     * @return mixed
322
     */
323 1
    public function onEndTurnEnd($count, $time)
324
    {
325
        // Nothing
326 1
    }
327
328
    /**
329
     * Callback sent when the "StartRound" section start.
330
     *
331
     * @param int $count Each time this section is played, this number is incremented by one
332
     * @param int $time Server time when the callback was sent
333
     *
334
     * @return mixed
335
     */
336 1
    public function onStartRoundStart($count, $time)
337
    {
338
        // Nothing
339 1
    }
340
341
    /**
342
     * Callback sent when the "StartRound" section end.
343
     *
344
     * @param int $count Each time this section is played, this number is incremented by one
345
     * @param int $time Server time when the callback was sent
346
     *
347
     * @return mixed
348
     */
349 1
    public function onStartRoundEnd($count, $time)
350
    {
351
        // Nothing
352 1
    }
353
354
    /**
355
     * Callback sent when the "EndMatch" section start.
356
     *
357
     * @param int $count Each time this section is played, this number is incremented by one
358
     * @param int $time Server time when the callback was sent
359
     *
360
     * @return mixed
361
     */
362 1
    public function onEndRoundStart($count, $time)
363
    {
364
        // Nothing
365 1
    }
366
367
    /**
368
     * Callback sent when the "EndMatch" section end.
369
     *
370
     * @param int $count Each time this section is played, this number is incremented by one
371
     * @param int $time Server time when the callback was sent
372
     *
373
     * @return mixed
374
     */
375
    public function onEndRoundEnd($count, $time)
376
    {
377
        // Nothing
378
    }
379
}
380