Completed
Pull Request — master (#168)
by De Cramer
08:32
created

BaseRecords::onEndMapEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 4
crap 2
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 8
     * @param MapStorage           $mapStorage
52
     * @param DispatcherInterface  $dispatcher
53
     * @param GetNumberOfLaps      $getNumberOfLaps
54
     * @param                      $eventPrefix
55
     */
56
    public function __construct(
57
        RecordHandlerFactory $recordsHandlerFactory,
58 8
        Group $allPlayersGroup,
59 8
        MapStorage $mapStorage,
60 8
        DispatcherInterface $dispatcher,
61 8
        GetNumberOfLaps $getNumberOfLaps,
62 8
        $eventPrefix
63 8
    ) {
64
        $this->recordsHandler = $recordsHandlerFactory->create();
65
        $this->allPlayersGroup = $allPlayersGroup;
66
        $this->mapStorage = $mapStorage;
67
        $this->eventPrefix = $eventPrefix;
68
        $this->dispatcher = $dispatcher;
69
        $this->getNumberOfLaps = $getNumberOfLaps;
70 1
    }
71
72 1
    /**
73
     * Get the current record handler.
74
     *
75
     * @return RecordHandler|mixed
76
     */
77
    public function getRecordsHandler()
78
    {
79
        return $this->recordsHandler;
80
    }
81
82 2
    /**
83
     * Set the status of the plugin
84 2
     *
85 1
     * @param boolean $status
86
     *
87
     * @return void
88 1
     */
89
    public function setStatus($status)
90
    {
91 1
        if ($status) {
92
            $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 1
    public function onStartMapStart($count, $time, $restarted, Map $map)
108
    {
109
        $playerGroup = $this->allPlayersGroup;
110 1
        $recordsHandler = $this->recordsHandler;
111
112
        $this->getNumberOfLaps->get(function ($laps) use ($map, $playerGroup, $recordsHandler) {
113 1
            // Load firs X records for this map.
114
            $recordsHandler->loadForMap($map->uId, $laps);
115
116 1
            // Load time information for remaining players.
117 1
            $recordsHandler->loadForPlayers($map->uId, $laps, $playerGroup->getLogins());
118
119
            // Let others know that records information is now available.
120
            $this->dispatchEvent(['event' => 'loaded', 'records' => $recordsHandler->getRecords()]);
121
        });
122 1
    }
123
124 1
    /**
125 1
     * @param Player $player
126
     */
127
    public function onPlayerConnect(Player $player)
128
    {
129
        $this->recordsHandler->loadForPlayers($this->mapStorage->getCurrentMap()->uId, [1], [$player->getLogin()]);
130
    }
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 1
     * @param boolean $restarted true if the map was restarted, false otherwise
138
     * @param Map     $map       Map started with.
139
     *
140 1
     * @return void
141
     */
142
    public function onEndMapEnd($count, $time, $restarted, Map $map)
143
    {
144
        // Nothing to do
145
    }
146
147
    /**
148
     * Callback sent when the "StartMatch" section start.
149
     *
150 1
     * @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 1
     * @return void
154
     */
155
    public function onStartMatchStart($count, $time)
156
    {
157
        // Nothing to do.
158
    }
159
160
    /**
161
     * Callback sent when the "StartMatch" section end.
162
     *
163 1
     * @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 1
     *
166 1
     * @return void
167
     */
168
    public function onStartMatchEnd($count, $time)
169
    {
170
        $this->recordsHandler->save();
171
    }
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 1
     * @param boolean $restarted true if the map was restarted, false otherwise
179
     * @param Map     $map       Map started with.
180
     *
181 1
     * @return void
182
     */
183
    public function onStartMapEnd($count, $time, $restarted, Map $map)
184
    {
185
        // Nothing to do.
186
    }
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 1
     * @param boolean $restarted true if the map was restarted, false otherwise
194
     * @param Map     $map       Map started with.
195
     *
196 1
     * @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
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
214
    {
215
        // Nothing to do.
216
    }
217
218 4
    /**
219
     * Dispatch a record event.
220 4
     *
221 4
     * @param $eventData
222
     */
223 4
    public function dispatchEvent($eventData)
224 4
    {
225
        $event = $this->eventPrefix.'.'.$eventData['event'];
226
        unset($eventData['event']);
227
228
        $this->dispatcher->dispatch($event, [$eventData]);
229
    }
230
231 2
    /**
232
     * Callback sent when the "EndMatch" section start.
233 2
     *
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
    public function onEndMatchStart($count, $time)
240
    {
241
        // Nothing
242
    }
243
244 1
    /**
245
     * Callback sent when the "EndMatch" section end.
246
     *
247 1
     * @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
    public function onEndMatchEnd($count, $time)
253
    {
254
        // Nothing
255
    }
256
257 1
    /**
258
     * Callback sent when the "StartTurn" section start.
259
     *
260 1
     * @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
    public function onStartTurnStart($count, $time)
266
    {
267
        // Nothing
268
    }
269
270 1
    /**
271
     * Callback sent when the "StartTurn" section end.
272
     *
273 1
     * @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
    public function onStartTurnEnd($count, $time)
279
    {
280
        // Nothing
281
    }
282
283 1
    /**
284
     * Callback sent when the "EndMatch" section start.
285
     *
286 1
     * @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
    public function onEndTurnStart($count, $time)
292
    {
293
        // Nothing
294
    }
295
296 1
    /**
297
     * Callback sent when the "EndMatch" section end.
298
     *
299 1
     * @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
    public function onEndTurnEnd($count, $time)
305
    {
306
        // Nothing
307
    }
308
309 1
    /**
310
     * Callback sent when the "StartRound" section start.
311
     *
312 1
     * @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
    public function onStartRoundStart($count, $time)
318
    {
319
        // Nothing
320
    }
321
322 1
    /**
323
     * Callback sent when the "StartRound" section end.
324
     *
325 1
     * @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
    public function onStartRoundEnd($count, $time)
331
    {
332
        // Nothing
333
    }
334
335 1
    /**
336
     * Callback sent when the "EndMatch" section start.
337
     *
338 1
     * @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
    public function onEndRoundStart($count, $time)
344
    {
345
        // Nothing
346
    }
347
348 1
    /**
349
     * Callback sent when the "EndMatch" section end.
350
     *
351 1
     * @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
    public function onEndRoundEnd($count, $time)
357
    {
358
        // Nothing
359
    }
360
}
361