Completed
Push — master ( e09c01...ac9146 )
by De Cramer
02:18 queued 02:12
created

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