Completed
Push — master ( d55d81...7c5112 )
by
unknown
12s
created

BaseRecords::onEndMapEnd()   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 4
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
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
    /**
47
     * BaseRecords constructor.
48
     *
49
     * @param RecordHandlerFactory $recordsHandlerFactory
50
     * @param Group                $allPlayersGroup
51
     * @param MapStorage           $mapStorage
52
     * @param DispatcherInterface  $dispatcher
53
     * @param GetNumberOfLaps      $getNumberOfLaps
54
     * @param                      $eventPrefix
55
     */
56 8
    public function __construct(
57
        RecordHandlerFactory $recordsHandlerFactory,
58
        Group $allPlayersGroup,
59
        MapStorage $mapStorage,
60
        DispatcherInterface $dispatcher,
61
        GetNumberOfLaps $getNumberOfLaps,
62
        $eventPrefix
63
    ) {
64 8
        $this->recordsHandler = $recordsHandlerFactory->create();
65 8
        $this->allPlayersGroup = $allPlayersGroup;
66 8
        $this->mapStorage = $mapStorage;
67 8
        $this->eventPrefix = $eventPrefix;
68 8
        $this->dispatcher = $dispatcher;
69 8
        $this->getNumberOfLaps = $getNumberOfLaps;
70 8
    }
71
72
    /**
73
     * Get the current record handler.
74
     *
75
     * @return RecordHandler|mixed
76
     */
77 1
    public function getRecordsHandler()
78
    {
79 1
        return $this->recordsHandler;
80
    }
81
82
    /**
83
     * Set the status of the plugin
84
     *
85
     * @param boolean $status
86
     *
87
     * @return void
88
     */
89 2
    public function setStatus($status)
90
    {
91 2
        if ($status) {
92 1
            $map = $this->mapStorage->getCurrentMap();
93 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...
94
        }
95 2
    }
96
97
    /**
98
     * Callback sent when the "StartMap" section start.
99
     *
100
     * @param int     $count     Each time this section is played, this number is incremented by one
101
     * @param int     $time      Server time when the callback was sent
102
     * @param boolean $restarted true if the map was restarted, false otherwise
103
     * @param Map     $map       Map started with.
104
     *
105
     * @return void
106
     */
107 2
    public function onStartMapStart($count, $time, $restarted, Map $map)
108
    {
109 2
        $playerGroup = $this->allPlayersGroup;
110 2
        $recordsHandler = $this->recordsHandler;
111
112 2
        $this->getNumberOfLaps->get(function ($laps) use ($map, $playerGroup, $recordsHandler) {
113
            // Load firs X records for this map.
114 2
            $recordsHandler->loadForMap($map->uId, $laps);
115
116
            // Load time information for remaining players.
117 2
            $recordsHandler->loadForPlayers($map->uId, $laps, $playerGroup->getLogins());
118
119
            // Let others know that records information is now available.
120 2
            $this->dispatchEvent(['event' => 'loaded', 'records' => $recordsHandler->getRecords()]);
121 2
        });
122 2
    }
123
124
    /**
125
     * @param Player $player
126
     */
127 1
    public function onPlayerConnect(Player $player)
128
    {
129 1
        $this->recordsHandler->loadForPlayers($this->mapStorage->getCurrentMap()->uId, [1], [$player->getLogin()]);
130 1
    }
131
132
    /**
133
     * Callback sent when the "EndMap" section end.
134
     *
135
     * @param int     $count     Each time this section is played, this number is incremented by one
136
     * @param int     $time      Server time when the callback was sent
137
     * @param boolean $restarted true if the map was restarted, false otherwise
138
     * @param Map     $map       Map started with.
139
     *
140
     * @return void
141
     */
142 1
    public function onEndMapEnd($count, $time, $restarted, Map $map)
143
    {
144
        // Nothing to do
145 1
    }
146
147
    /**
148
     * Callback sent when the "StartMatch" section start.
149
     *
150
     * @param int $count Each time this section is played, this number is incremented by one
151
     * @param int $time  Server time when the callback was sent
152
     *
153
     * @return void
154
     */
155 1
    public function onStartMatchStart($count, $time)
156
    {
157
        // Nothing to do.
158 1
    }
159
160
    /**
161
     * Callback sent when the "StartMatch" section end.
162
     *
163
     * @param int $count Each time this section is played, this number is incremented by one
164
     * @param int $time  Server time when the callback was sent
165
     *
166
     * @return void
167
     */
168 1
    public function onStartMatchEnd($count, $time)
169
    {
170 1
        $this->recordsHandler->save();
171 1
    }
172
173
    /**
174
     * Callback sent when the "StartMap" section end.
175
     *
176
     * @param int     $count     Each time this section is played, this number is incremented by one
177
     * @param int     $time      Server time when the callback was sent
178
     * @param boolean $restarted true if the map was restarted, false otherwise
179
     * @param Map     $map       Map started with.
180
     *
181
     * @return void
182
     */
183 1
    public function onStartMapEnd($count, $time, $restarted, Map $map)
184
    {
185
        // Nothing to do.
186 1
    }
187
188
    /**
189
     * Callback sent when the "EndMap" section start.
190
     *
191
     * @param int     $count     Each time this section is played, this number is incremented by one
192
     * @param int     $time      Server time when the callback was sent
193
     * @param boolean $restarted true if the map was restarted, false otherwise
194
     * @param Map     $map       Map started with.
195
     *
196
     * @return void
197
     */
198 1
    public function onEndMapStart($count, $time, $restarted, Map $map)
199
    {
200
        // Nothing to do.
201 1
    }
202
203 1
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
204
    {
205
        // Nothing to do.
206 1
    }
207
208 1
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
209
    {
210
        // Nothing to do.
211 1
    }
212
213 1
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
214
    {
215
        // Nothing to do.
216 1
    }
217
218
    /**
219
     * Dispatch a record event.
220
     *
221
     * @param $eventData
222
     */
223 4
    public function dispatchEvent($eventData)
224
    {
225 4
        $event = $this->eventPrefix.'.'.$eventData['event'];
226 4
        unset($eventData['event']);
227
228 4
        $this->dispatcher->dispatch($event, [$eventData]);
229 4
    }
230
231
    /**
232
     * Callback sent when the "EndMatch" section start.
233
     *
234
     * @param int $count Each time this section is played, this number is incremented by one
235
     * @param int $time Server time when the callback was sent
236
     *
237
     * @return mixed
238
     */
239 1
    public function onEndMatchStart($count, $time)
240
    {
241
        // Nothing
242 1
    }
243
244
    /**
245
     * Callback sent when the "EndMatch" section end.
246
     *
247
     * @param int $count Each time this section is played, this number is incremented by one
248
     * @param int $time Server time when the callback was sent
249
     *
250
     * @return mixed
251
     */
252 1
    public function onEndMatchEnd($count, $time)
253
    {
254
        // Nothing
255 1
    }
256
257
    /**
258
     * Callback sent when the "StartTurn" section start.
259
     *
260
     * @param int $count Each time this section is played, this number is incremented by one
261
     * @param int $time Server time when the callback was sent
262
     *
263
     * @return mixed
264
     */
265 1
    public function onStartTurnStart($count, $time)
266
    {
267
        // Nothing
268 1
    }
269
270
    /**
271
     * Callback sent when the "StartTurn" section end.
272
     *
273
     * @param int $count Each time this section is played, this number is incremented by one
274
     * @param int $time Server time when the callback was sent
275
     *
276
     * @return mixed
277
     */
278 1
    public function onStartTurnEnd($count, $time)
279
    {
280
        // Nothing
281 1
    }
282
283
    /**
284
     * Callback sent when the "EndMatch" section start.
285
     *
286
     * @param int $count Each time this section is played, this number is incremented by one
287
     * @param int $time Server time when the callback was sent
288
     *
289
     * @return mixed
290
     */
291 1
    public function onEndTurnStart($count, $time)
292
    {
293
        // Nothing
294 1
    }
295
296
    /**
297
     * Callback sent when the "EndMatch" section end.
298
     *
299
     * @param int $count Each time this section is played, this number is incremented by one
300
     * @param int $time Server time when the callback was sent
301
     *
302
     * @return mixed
303
     */
304 1
    public function onEndTurnEnd($count, $time)
305
    {
306
        // Nothing
307 1
    }
308
309
    /**
310
     * Callback sent when the "StartRound" section start.
311
     *
312
     * @param int $count Each time this section is played, this number is incremented by one
313
     * @param int $time Server time when the callback was sent
314
     *
315
     * @return mixed
316
     */
317 1
    public function onStartRoundStart($count, $time)
318
    {
319
        // Nothing
320 1
    }
321
322
    /**
323
     * Callback sent when the "StartRound" section end.
324
     *
325
     * @param int $count Each time this section is played, this number is incremented by one
326
     * @param int $time Server time when the callback was sent
327
     *
328
     * @return mixed
329
     */
330 1
    public function onStartRoundEnd($count, $time)
331
    {
332
        // Nothing
333 1
    }
334
335
    /**
336
     * Callback sent when the "EndMatch" section start.
337
     *
338
     * @param int $count Each time this section is played, this number is incremented by one
339
     * @param int $time Server time when the callback was sent
340
     *
341
     * @return mixed
342
     */
343 1
    public function onEndRoundStart($count, $time)
344
    {
345
        // Nothing
346 1
    }
347
348
    /**
349
     * Callback sent when the "EndMatch" section end.
350
     *
351
     * @param int $count Each time this section is played, this number is incremented by one
352
     * @param int $time Server time when the callback was sent
353
     *
354
     * @return mixed
355
     */
356 1
    public function onEndRoundEnd($count, $time)
357
    {
358
        // Nothing
359 1
    }
360
}
361