Completed
Push — master ( 7c5112...0705e2 )
by
unknown
15s
created

BaseRecords::onStartMatchEnd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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